1 #include <msp/fs/dir.h>
2 #include <msp/fs/stat.h>
3 #include <msp/strings/format.h>
6 #include "xinestream.h"
11 Client::Client(Xinema &x, Net::StreamSocket *s):
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));
19 xinema.signal_stream_created.connect(sigc::mem_fun(this, &Client::stream_created));
22 void Client::data_available()
25 unsigned len = socket->read(rbuf, sizeof(rbuf));
26 buffer.append(rbuf, len);
28 string::size_type start = 0;
31 string::size_type newline = buffer.find('\n', start);
32 if(newline==string::npos)
37 process_command(buffer.substr(start, newline-start));
39 catch(const exception &e)
41 send_reply(string("error ")+e.what());
48 buffer.erase(0, start);
51 void Client::end_of_stream()
56 void Client::process_command(const string &cmd)
58 string::size_type space = cmd.find(' ');
59 string keyword = cmd.substr(0, space);
61 if(space!=string::npos)
62 args = cmd.substr(space+1);
63 if(keyword=="list_directory")
65 else if(keyword=="play_file")
66 xinema.play_file(args);
68 send_reply("error Invalid command");
71 void Client::send_reply(const string &reply)
77 void Client::list_directory(const FS::Path &dn)
79 list<string> files = FS::list_files(dn);
81 send_reply("directory "+dn.str());
82 for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
84 if(FS::is_dir(dn / *i))
85 send_reply("subdir "+*i);
87 send_reply("file "+*i);
91 void Client::stream_created(XineStream &stream)
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();
98 send_reply("title "+title);
101 void Client::stream_title_changed(const string &title)
103 send_reply("title "+title);
106 void Client::stream_duration_changed(const Time::TimeDelta &dur)
108 send_reply(format("duration %.3f", dur/Time::sec));
111 void Client::stream_position_changed(const Time::TimeDelta &pos)
113 if(abs(pos-last_position)>=Time::sec)
115 send_reply(format("position %.3f", pos/Time::sec));