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 XineStream *stream = xinema.get_stream();
21 stream_created(*stream);
24 void Client::data_available()
30 len = socket->read(rbuf, sizeof(rbuf));
32 catch(const std::exception &)
38 buffer.append(rbuf, len);
40 string::size_type start = 0;
43 string::size_type newline = buffer.find('\n', start);
44 if(newline==string::npos)
49 process_command(buffer.substr(start, newline-start));
51 catch(const exception &e)
53 send_reply(string("error ")+e.what());
59 buffer.erase(0, start);
62 void Client::end_of_stream()
67 XineStream &Client::get_stream() const
69 XineStream *stream = xinema.get_stream();
73 throw runtime_error("No stream");
76 void Client::process_command(const string &cmd)
78 string::size_type space = cmd.find(' ');
79 string keyword = cmd.substr(0, space);
81 if(space!=string::npos)
82 args = cmd.substr(space+1);
84 if(keyword=="list_directory")
86 else if(keyword=="play_file")
87 xinema.play_file(args);
88 else if(keyword=="play")
90 else if(keyword=="seek")
91 get_stream().seek(lexical_cast<float>(args)*Time::sec);
92 else if(keyword=="pause")
94 else if(keyword=="stop")
97 throw runtime_error("Invalid command");
100 void Client::send_reply(const string &reply)
102 Msp::MutexLock lock(mutex);
105 socket->write(reply);
108 catch(const std::exception &)
114 void Client::list_directory(const FS::Path &dn)
116 list<string> files = FS::list_files(dn);
118 send_reply("directory "+dn.str());
119 for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
121 if(FS::is_dir(dn / *i))
122 send_reply("subdir "+*i);
124 send_reply("file "+*i);
128 void Client::stream_created(XineStream &stream)
130 stream.signal_state_changed.connect(sigc::mem_fun(this, &Client::stream_state_changed));
131 stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
132 stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
133 stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
135 stream_state_changed(stream.get_state());
137 string title = stream.get_title();
139 send_reply("title "+title);
141 if(const Time::TimeDelta &dur = stream.get_duration())
142 stream_duration_changed(dur);
145 void Client::stream_state_changed(XineStream::State state)
147 send_reply(format("state %s", state));
150 void Client::stream_title_changed(const string &title)
152 send_reply("title "+title);
155 void Client::stream_duration_changed(const Time::TimeDelta &dur)
157 send_reply(format("duration %.3f", dur/Time::sec));
160 void Client::stream_position_changed(const Time::TimeDelta &pos)
162 if(abs(pos-last_position)>=Time::sec)
164 send_reply(format("position %.3f", pos/Time::sec));