]> git.tdb.fi Git - xinema.git/blob - source/client.cpp
Inform clients of stream title changes
[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 }
21
22 void Client::data_available()
23 {
24         char rbuf[1024];
25         unsigned len = socket->read(rbuf, sizeof(rbuf));
26         buffer.append(rbuf, len);
27
28         string::size_type start = 0;
29         while(1)
30         {
31                 string::size_type newline = buffer.find('\n', start);
32                 if(newline==string::npos)
33                         break;
34
35                 try
36                 {
37                         process_command(buffer.substr(start, newline-start));
38                 }
39                 catch(const exception &e)
40                 {
41                         send_reply(string("error ")+e.what());
42                         return;
43                 }
44
45                 start = newline+1;
46         }
47
48         buffer.erase(0, start);
49 }
50
51 void Client::end_of_stream()
52 {
53         stale = true;
54 }
55
56 void Client::process_command(const string &cmd)
57 {
58         string::size_type space = cmd.find(' ');
59         string keyword = cmd.substr(0, space);
60         string args;
61         if(space!=string::npos)
62                 args = cmd.substr(space+1);
63         if(keyword=="list_directory")
64                 list_directory(args);
65         else if(keyword=="play_file")
66                 xinema.play_file(args);
67         else
68                 send_reply("error Invalid command");
69 }
70
71 void Client::send_reply(const string &reply)
72 {
73         socket->write(reply);
74         socket->put('\n');
75 }
76
77 void Client::list_directory(const FS::Path &dn)
78 {
79         list<string> files = FS::list_files(dn);
80
81         send_reply("directory "+dn.str());
82         for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
83         {
84                 if(FS::is_dir(dn / *i))
85                         send_reply("subdir "+*i);
86                 else
87                         send_reply("file "+*i);
88         }
89 }
90
91 void Client::stream_created(XineStream &stream)
92 {
93         stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
94         stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
95         stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
96         string title = stream.get_title();
97         if(!title.empty())
98                 send_reply("title "+title);
99 }
100
101 void Client::stream_title_changed(const string &title)
102 {
103         send_reply("title "+title);
104 }
105
106 void Client::stream_duration_changed(const Time::TimeDelta &dur)
107 {
108         send_reply(format("duration %.3f", dur/Time::sec));
109 }
110
111 void Client::stream_position_changed(const Time::TimeDelta &pos)
112 {
113         if(abs(pos-last_position)>=Time::sec)
114         {
115                 send_reply(format("position %.3f", pos/Time::sec));
116                 last_position = pos;
117         }
118 }