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()
27 unsigned len = socket->read(rbuf, sizeof(rbuf));
28 buffer.append(rbuf, len);
30 string::size_type start = 0;
33 string::size_type newline = buffer.find('\n', start);
34 if(newline==string::npos)
39 process_command(buffer.substr(start, newline-start));
41 catch(const exception &e)
43 send_reply(string("error ")+e.what());
49 buffer.erase(0, start);
52 void Client::end_of_stream()
57 XineStream &Client::get_stream() const
59 XineStream *stream = xinema.get_stream();
63 throw runtime_error("No stream");
66 void Client::process_command(const string &cmd)
68 string::size_type space = cmd.find(' ');
69 string keyword = cmd.substr(0, space);
71 if(space!=string::npos)
72 args = cmd.substr(space+1);
74 if(keyword=="list_directory")
76 else if(keyword=="play_file")
77 xinema.play_file(args);
78 else if(keyword=="play")
80 else if(keyword=="seek")
81 get_stream().seek(lexical_cast<float>(args)*Time::sec);
82 else if(keyword=="pause")
84 else if(keyword=="stop")
87 throw runtime_error("Invalid command");
90 void Client::send_reply(const string &reply)
92 Msp::MutexLock lock(mutex);
97 void Client::list_directory(const FS::Path &dn)
99 list<string> files = FS::list_files(dn);
101 send_reply("directory "+dn.str());
102 for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
104 if(FS::is_dir(dn / *i))
105 send_reply("subdir "+*i);
107 send_reply("file "+*i);
111 void Client::stream_created(XineStream &stream)
113 stream.signal_state_changed.connect(sigc::mem_fun(this, &Client::stream_state_changed));
114 stream.signal_title_changed.connect(sigc::mem_fun(this, &Client::stream_title_changed));
115 stream.signal_duration_changed.connect(sigc::mem_fun(this, &Client::stream_duration_changed));
116 stream.signal_position_changed.connect(sigc::mem_fun(this, &Client::stream_position_changed));
118 stream_state_changed(stream.get_state());
120 string title = stream.get_title();
122 send_reply("title "+title);
124 if(const Time::TimeDelta &dur = stream.get_duration())
125 stream_duration_changed(dur);
128 void Client::stream_state_changed(XineStream::State state)
130 send_reply(format("state %s", state));
133 void Client::stream_title_changed(const string &title)
135 send_reply("title "+title);
138 void Client::stream_duration_changed(const Time::TimeDelta &dur)
140 send_reply(format("duration %.3f", dur/Time::sec));
143 void Client::stream_position_changed(const Time::TimeDelta &pos)
145 if(abs(pos-last_position)>=Time::sec)
147 send_reply(format("position %.3f", pos/Time::sec));