Some include tidying.
[dcpomatic.git] / src / lib / server.h
index f7a0abb800163985e22129c413790135b80528bb..1dc3b7b4edf92d1c3545458c30ea1c8185c67f1c 100644 (file)
 
 */
 
+#ifndef DCPOMATIC_SERVER_H
+#define DCPOMATIC_SERVER_H
+
 /** @file src/server.h
  *  @brief Class to describe a server to which we can send
- *  encoding work.
+ *  encoding work, and a class to implement such a server.
  */
 
+#include "log.h"
+#include "exceptions.h"
+#include <libxml++/libxml++.h>
+#include <boost/thread.hpp>
+#include <boost/asio.hpp>
+#include <boost/thread/condition.hpp>
+#include <boost/optional.hpp>
 #include <string>
 
-/** @class Server
+class Socket;
+
+namespace cxml {
+       class Node;
+}
+
+/** @class ServerDescription
  *  @brief Class to describe a server to which we can send encoding work.
  */
-class Server
+class ServerDescription
 {
 public:
+       ServerDescription ()
+               : _host_name ("")
+               , _threads (1)
+       {}
+       
        /** @param h Server host name or IP address in string form.
         *  @param t Number of threads to use on the server.
         */
-       Server (std::string h, int t)
+       ServerDescription (std::string h, int t)
                : _host_name (h)
                , _threads (t)
        {}
 
+       /* Default copy constructor is fine */
+       
        /** @return server's host name or IP address in string form */
        std::string host_name () const {
                return _host_name;
@@ -48,9 +71,13 @@ public:
                return _threads;
        }
 
-       std::string as_metadata () const;
-       
-       static Server * create_from_metadata (std::string v);
+       void set_host_name (std::string n) {
+               _host_name = n;
+       }
+
+       void set_threads (int t) {
+               _threads = t;
+       }
 
 private:
        /** server's host name */
@@ -58,3 +85,50 @@ private:
        /** number of threads to use on the server */
        int _threads;
 };
+
+class Server : public ExceptionStore, public boost::noncopyable
+{
+public:
+       Server (boost::shared_ptr<Log> log, bool verbose);
+       ~Server ();
+
+       void run (int num_threads);
+
+private:
+       void worker_thread ();
+       int process (boost::shared_ptr<Socket> socket, struct timeval &, struct timeval &);
+       void broadcast_thread ();
+       void broadcast_received ();
+       void start_accept ();
+       void handle_accept (boost::shared_ptr<Socket>, boost::system::error_code const &);
+
+       bool _terminate;
+
+       std::vector<boost::thread *> _worker_threads;
+       std::list<boost::shared_ptr<Socket> > _queue;
+       boost::mutex _worker_mutex;
+       boost::condition _full_condition;
+       boost::condition _empty_condition;
+       boost::shared_ptr<Log> _log;
+       bool _verbose;
+
+       boost::asio::io_service _io_service;
+       boost::asio::ip::tcp::acceptor _acceptor;
+
+       struct Broadcast {
+
+               Broadcast ()
+                       : thread (0)
+                       , socket (0)
+               {}
+               
+               boost::thread* thread;
+               boost::asio::ip::udp::socket* socket;
+               char buffer[64];
+               boost::asio::ip::udp::endpoint send_endpoint;
+               boost::asio::io_service io_service;
+               
+       } _broadcast;
+};
+
+#endif