]> git.tdb.fi Git - xinema.git/blob - source/client.cpp
Avoid crashing due to network exceptions
[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
7 using namespace std;
8 using namespace Msp;
9
10 Client::Client(Xinema &x, Net::StreamSocket *s):
11         xinema(x),
12         socket(s),
13         stale(false)
14 {
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));
17
18         xinema.signal_stream_created.connect(sigc::mem_fun(this, &Client::stream_created));
19         XineStream *stream = xinema.get_stream();
20         if(stream)
21                 stream_created(*stream);
22 }
23
24 void Client::data_available()
25 {
26         char rbuf[1024];
27         unsigned len;
28         try
29         {
30                 len = socket->read(rbuf, sizeof(rbuf));
31         }
32         catch(const std::exception &)
33         {
34                 stale = true;
35                 return;
36         }
37
38         buffer.append(rbuf, len);
39
40         string::size_type start = 0;
41         while(1)
42         {
43                 string::size_type newline = buffer.find('\n', start);
44                 if(newline==string::npos)
45                         break;
46
47                 try
48                 {
49                         process_command(buffer.substr(start, newline-start));
50                 }
51                 catch(const exception &e)
52                 {
53                         send_reply(string("error ")+e.what());
54                 }
55
56                 start = newline+1;
57         }
58
59         buffer.erase(0, start);
60 }
61
62 void Client::end_of_stream()
63 {
64         stale = true;
65 }
66
67 XineStream &Client::get_stream() const
68 {
69         XineStream *stream = xinema.get_stream();
70         if(stream)
71                 return *stream;
72
73         throw runtime_error("No stream");
74 }
75
76 void Client::process_command(const string &cmd)
77 {
78         string::size_type space = cmd.find(' ');
79         string keyword = cmd.substr(0, space);
80         string args;
81         if(space!=string::npos)
82                 args = cmd.substr(space+1);
83
84         if(keyword=="list_directory")
85                 list_directory(args);
86         else if(keyword=="play_file")
87                 xinema.play_file(args);
88         else if(keyword=="play")
89                 get_stream().play();
90         else if(keyword=="seek")
91                 get_stream().seek(lexical_cast<float>(args)*Time::sec);
92         else if(keyword=="pause")
93                 get_stream().pause();
94         else if(keyword=="stop")
95                 get_stream().stop();
96         else
97                 throw runtime_error("Invalid command");
98 }
99
100 void Client::send_reply(const string &reply)
101 {
102         Msp::MutexLock lock(mutex);
103         try
104         {
105                 socket->write(reply);
106                 socket->put('\n');
107         }
108         catch(const std::exception &)
109         {
110                 stale = true;
111         }
112 }
113
114 void Client::list_directory(const FS::Path &dn)
115 {
116         list<string> files = FS::list_files(dn);
117
118         send_reply("directory "+dn.str());
119         for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
120         {
121                 if(FS::is_dir(dn / *i))
122                         send_reply("subdir "+*i);
123                 else
124                         send_reply("file "+*i);
125         }
126 }
127
128 void Client::stream_created(XineStream &stream)
129 {
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));
134
135         stream_state_changed(stream.get_state());
136
137         string title = stream.get_title();
138         if(!title.empty())
139                 send_reply("title "+title);
140
141         if(const Time::TimeDelta &dur = stream.get_duration())
142                 stream_duration_changed(dur);
143 }
144
145 void Client::stream_state_changed(XineStream::State state)
146 {
147         send_reply(format("state %s", state));
148 }
149
150 void Client::stream_title_changed(const string &title)
151 {
152         send_reply("title "+title);
153 }
154
155 void Client::stream_duration_changed(const Time::TimeDelta &dur)
156 {
157         send_reply(format("duration %.3f", dur/Time::sec));
158 }
159
160 void Client::stream_position_changed(const Time::TimeDelta &pos)
161 {
162         if(abs(pos-last_position)>=Time::sec)
163         {
164                 send_reply(format("position %.3f", pos/Time::sec));
165                 last_position = pos;
166         }
167 }