]> git.tdb.fi Git - xinema.git/blobdiff - source/client.cpp
Add a simple networked command interface
[xinema.git] / source / client.cpp
diff --git a/source/client.cpp b/source/client.cpp
new file mode 100644 (file)
index 0000000..dbe6091
--- /dev/null
@@ -0,0 +1,85 @@
+#include <msp/fs/dir.h>
+#include <msp/fs/stat.h>
+#include "client.h"
+#include "xinema.h"
+
+using namespace std;
+using namespace Msp;
+
+Client::Client(Xinema &x, Net::StreamSocket *s):
+       xinema(x),
+       socket(s),
+       stale(false)
+{
+       socket->signal_data_available.connect(sigc::mem_fun(this, &Client::data_available));
+       socket->signal_end_of_file.connect(sigc::mem_fun(this, &Client::end_of_stream));
+}
+
+void Client::data_available()
+{
+       char rbuf[1024];
+       unsigned len = socket->read(rbuf, sizeof(rbuf));
+       buffer.append(rbuf, len);
+
+       string::size_type start = 0;
+       while(1)
+       {
+               string::size_type newline = buffer.find('\n', start);
+               if(newline==string::npos)
+                       break;
+
+               try
+               {
+                       process_command(buffer.substr(start, newline-start));
+               }
+               catch(const exception &e)
+               {
+                       send_reply(string("error ")+e.what());
+                       return;
+               }
+
+               start = newline+1;
+       }
+
+       buffer.erase(0, start);
+}
+
+void Client::end_of_stream()
+{
+       stale = true;
+}
+
+void Client::process_command(const string &cmd)
+{
+       string::size_type space = cmd.find(' ');
+       string keyword = cmd.substr(0, space);
+       string args;
+       if(space!=string::npos)
+               args = cmd.substr(space+1);
+       if(keyword=="list_directory")
+               list_directory(args);
+       else if(keyword=="play_file")
+               xinema.play_file(args);
+       else
+               send_reply("error Invalid command");
+}
+
+void Client::send_reply(const string &reply)
+{
+       socket->write(reply);
+       socket->put('\n');
+}
+
+void Client::list_directory(const FS::Path &dn)
+{
+       list<string> files = FS::list_files(dn);
+
+       send_reply("directory "+dn.str());
+       for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
+       {
+               if(FS::is_dir(dn / *i))
+                       send_reply("subdir "+*i);
+               else
+                       send_reply("file "+*i);
+       }
+}