]> git.tdb.fi Git - xinema.git/blob - source/client.cpp
Fix a command processing error
[xinema.git] / source / client.cpp
1 #include <msp/fs/dir.h>
2 #include <msp/fs/stat.h>
3 #include <msp/strings/format.h>
4 #include "client.h"
5 #include "xinema.h"
6 #include "xinestream.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 Client::Client(Xinema &x, Net::StreamSocket *s):
12         xinema(x),
13         socket(s),
14         stale(false)
15 {
16         socket->signal_data_available.connect(sigc::mem_fun(this, &Client::data_available));
17         socket->signal_end_of_file.connect(sigc::mem_fun(this, &Client::end_of_stream));
18
19         xinema.signal_stream_created.connect(sigc::mem_fun(this, &Client::stream_created));
20         XineStream *stream = xinema.get_stream();
21         if(stream)
22                 stream_created(*stream);
23 }
24
25 void Client::data_available()
26 {
27         char rbuf[1024];
28         unsigned len = socket->read(rbuf, sizeof(rbuf));
29         buffer.append(rbuf, len);
30
31         string::size_type start = 0;
32         while(1)
33         {
34                 string::size_type newline = buffer.find('\n', start);
35                 if(newline==string::npos)
36                         break;
37
38                 try
39                 {
40                         process_command(buffer.substr(start, newline-start));
41                 }
42                 catch(const exception &e)
43                 {
44                         send_reply(string("error ")+e.what());
45                 }
46
47                 start = newline+1;
48         }
49
50         buffer.erase(0, start);
51 }
52
53 void Client::end_of_stream()
54 {
55         stale = true;
56 }
57
58 void Client::process_command(const string &cmd)
59 {
60         string::size_type space = cmd.find(' ');
61         string keyword = cmd.substr(0, space);
62         string args;
63         if(space!=string::npos)
64                 args = cmd.substr(space+1);
65         if(keyword=="list_directory")
66                 list_directory(args);
67         else if(keyword=="play_file")
68                 xinema.play_file(args);
69         else
70                 send_reply("error Invalid command");
71 }
72
73 void Client::send_reply(const string &reply)
74 {
75         socket->write(reply);
76         socket->put('\n');
77 }
78
79 void Client::list_directory(const FS::Path &dn)
80 {
81         list<string> files = FS::list_files(dn);
82
83         send_reply("directory "+dn.str());
84         for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
85         {
86                 if(FS::is_dir(dn / *i))
87                         send_reply("subdir "+*i);
88                 else
89                         send_reply("file "+*i);
90         }
91 }
92
93 void Client::stream_created(XineStream &stream)
94 {
95         stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
96         stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
97         stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
98         string title = stream.get_title();
99         if(!title.empty())
100                 send_reply("title "+title);
101         if(const Time::TimeDelta &dur = stream.get_duration())
102                 stream_duration_changed(dur);
103 }
104
105 void Client::stream_title_changed(const string &title)
106 {
107         send_reply("title "+title);
108 }
109
110 void Client::stream_duration_changed(const Time::TimeDelta &dur)
111 {
112         send_reply(format("duration %.3f", dur/Time::sec));
113 }
114
115 void Client::stream_position_changed(const Time::TimeDelta &pos)
116 {
117         if(abs(pos-last_position)>=Time::sec)
118         {
119                 send_reply(format("position %.3f", pos/Time::sec));
120                 last_position = pos;
121         }
122 }