1 #include <msp/fs/dir.h>
2 #include <msp/fs/stat.h>
3 #include <msp/strings/format.h>
10 Client::Client(Xinema &x, Net::StreamSocket *s):
15 socket->signal_data_available.connect(sigc::mem_fun(this, &Client::data_available));
16 socket->signal_end_of_file.connect(sigc::mem_fun(this, &Client::end_of_stream));
18 xinema.signal_stream_created.connect(sigc::mem_fun(this, &Client::stream_created));
19 xinema.signal_stream_destroyed.connect(sigc::mem_fun(this, &Client::stream_destroyed));
20 XineStream *stream = xinema.get_stream();
22 stream_created(*stream);
25 void Client::data_available()
31 len = socket->read(rbuf, sizeof(rbuf));
33 catch(const std::exception &)
39 buffer.append(rbuf, len);
41 string::size_type start = 0;
44 string::size_type newline = buffer.find('\n', start);
45 if(newline==string::npos)
50 process_command(buffer.substr(start, newline-start));
52 catch(const exception &e)
54 send_reply(string("error ")+e.what());
60 buffer.erase(0, start);
63 void Client::end_of_stream()
68 XineStream &Client::get_stream() const
70 XineStream *stream = xinema.get_stream();
74 throw runtime_error("No stream");
77 void Client::process_command(const string &cmd)
79 string::size_type space = cmd.find(' ');
80 string keyword = cmd.substr(0, space);
82 if(space!=string::npos)
83 args = cmd.substr(space+1);
85 if(keyword=="list_directory")
87 else if(keyword=="play_file")
88 xinema.play_file(args);
89 else if(keyword=="play")
91 else if(keyword=="seek")
92 get_stream().seek(lexical_cast<float>(args)*Time::sec);
93 else if(keyword=="pause")
95 else if(keyword=="stop")
98 throw runtime_error("Invalid command");
101 void Client::send_reply(const string &reply)
103 Msp::MutexLock lock(mutex);
106 socket->write(reply);
109 catch(const std::exception &)
115 void Client::list_directory(const FS::Path &dn)
117 list<string> files = FS::list_files(dn);
119 send_reply("directory "+dn.str());
120 for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
122 if(FS::is_dir(dn / *i))
123 send_reply("subdir "+*i);
125 send_reply("file "+*i);
129 void Client::stream_created(XineStream &stream)
131 stream.signal_state_changed.connect(sigc::mem_fun(this, &Client::stream_state_changed));
132 stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
133 stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
134 stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
136 stream_state_changed(stream.get_state());
138 string title = stream.get_title();
140 send_reply("title "+title);
142 if(const Time::TimeDelta &dur = stream.get_duration())
143 stream_duration_changed(dur);
146 void Client::stream_destroyed()
148 send_reply("ejected");
151 void Client::stream_state_changed(XineStream::State state)
153 send_reply(format("state %s", state));
156 void Client::stream_title_changed(const string &title)
158 send_reply("title "+title);
161 void Client::stream_duration_changed(const Time::TimeDelta &dur)
163 send_reply(format("duration %.3f", dur/Time::sec));
166 void Client::stream_position_changed(const Time::TimeDelta &pos)
168 if(abs(pos-last_position)>=Time::sec)
170 send_reply(format("position %.3f", pos/Time::sec));