File: clientstub.h

package info (click to toggle)
imms 2.0.3-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 736 kB
  • ctags: 901
  • sloc: cpp: 5,532; ansic: 1,096; sh: 186; makefile: 113
file content (112 lines) | stat: -rw-r--r-- 2,512 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef __CLIENTSTUB_H_
#define __CLIENTSTUB_H_

#include "giosocket.h"
#include "utils.h"
#include "clientstubbase.h"

#include <stdlib.h>
#include <errno.h>

#include <sstream>
#include <iostream>

using std::stringstream;
using std::ostringstream;
using std::cerr;
using std::endl;

template <typename Ops>
class IMMSClient : public IMMSClientStub, protected GIOSocket 
{
public:
    IMMSClient() : connected(false) { }
    bool connect()
    {
        int fd = socket_connect(get_imms_root("socket"));
        if (fd > 0)
        {
            init(fd);
            return connected = true;
        }
        cerr << "Connection failed: " << strerror(errno) << endl;
        return false;
    }
    virtual void write_command(const string &line)
        { if (isok()) GIOSocket::write(line + "\n"); }
    virtual void process_line(const string &line)
    {
        stringstream sstr;
        sstr << line;

#if defined(DEBUG) && 1
        std::cout << "< " << line << endl;
#endif

        string command = "";
        sstr >> command;

        if (command == "ResetSelection")
        {
            Ops::reset_selection();
            return;
        }
        if (command == "TryAgain")
        {
            write_command("SelectNext");
            return;
        }
        if (command == "EnqueueNext")
        {
            int next;
            sstr >> next;
            Ops::set_next(next);
            return;
        }
        if (command == "PlaylistChanged")
        {
            IMMSClientStub::playlist_changed(Ops::get_length());
            return;
        }
        if (command == "GetPlaylistItem")
        {
            int i;
            sstr >> i;
            send_item("PlaylistItem", i);
            return;
        }
        if (command == "GetEntirePlaylist")
        {
            for (int i = 0; i < Ops::get_length(); ++i)
                send_item("Playlist", i);
            write_command("PlaylistEnd");
            return;
        }

        cerr << "IMMS: Unknown command: " << command << endl;
    }
    virtual void connection_lost() { connected = false; }

    bool check_connection()
    {
        if (isok())
            return false;

        system("immsd &");

        return connect();
    }
    
    bool isok() { return connected; }
private:
    bool connected;

    void send_item(const char *command, int i)
    {
        ostringstream osstr;
        osstr << command << " " << i << " " << Ops::get_item(i);
        write_command(osstr.str());
    }
};

#endif