]> git.tdb.fi Git - xinema.git/blob - source/client.cpp
47f8186d7d2899c0457568abc13f590037585bc0
[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                         return;
46                 }
47
48                 start = newline+1;
49         }
50
51         buffer.erase(0, start);
52 }
53
54 void Client::end_of_stream()
55 {
56         stale = true;
57 }
58
59 void Client::process_command(const string &cmd)
60 {
61         string::size_type space = cmd.find(' ');
62         string keyword = cmd.substr(0, space);
63         string args;
64         if(space!=string::npos)
65                 args = cmd.substr(space+1);
66         if(keyword=="list_directory")
67                 list_directory(args);
68         else if(keyword=="play_file")
69                 xinema.play_file(args);
70         else
71                 send_reply("error Invalid command");
72 }
73
74 void Client::send_reply(const string &reply)
75 {
76         socket->write(reply);
77         socket->put('\n');
78 }
79
80 void Client::list_directory(const FS::Path &dn)
81 {
82         list<string> files = FS::list_files(dn);
83
84         send_reply("directory "+dn.str());
85         for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
86         {
87                 if(FS::is_dir(dn / *i))
88                         send_reply("subdir "+*i);
89                 else
90                         send_reply("file "+*i);
91         }
92 }
93
94 void Client::stream_created(XineStream &stream)
95 {
96         stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
97         stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
98         stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
99         string title = stream.get_title();
100         if(!title.empty())
101                 send_reply("title "+title);
102         if(const Time::TimeDelta &dur = stream.get_duration())
103                 stream_duration_changed(dur);
104 }
105
106 void Client::stream_title_changed(const string &title)
107 {
108         send_reply("title "+title);
109 }
110
111 void Client::stream_duration_changed(const Time::TimeDelta &dur)
112 {
113         send_reply(format("duration %.3f", dur/Time::sec));
114 }
115
116 void Client::stream_position_changed(const Time::TimeDelta &pos)
117 {
118         if(abs(pos-last_position)>=Time::sec)
119         {
120                 send_reply(format("position %.3f", pos/Time::sec));
121                 last_position = pos;
122         }
123 }