Move ServerDescription into its own header.
[dcpomatic.git] / src / lib / server.h
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef DCPOMATIC_SERVER_H
21 #define DCPOMATIC_SERVER_H
22
23 /** @file src/server.h
24  *  @brief Server class.
25  */
26
27 #include "exceptions.h"
28 #include <boost/thread.hpp>
29 #include <boost/asio.hpp>
30 #include <boost/thread/condition.hpp>
31 #include <string>
32
33 class Socket;
34 class Log;
35
36 namespace cxml {
37         class Node;
38 }
39
40 /** @class Server
41  *  @brief A class to run a server which can accept requests to perform JPEG2000
42  *  encoding work.
43  */
44 class Server : public ExceptionStore, public boost::noncopyable
45 {
46 public:
47         Server (boost::shared_ptr<Log> log, bool verbose);
48         ~Server ();
49
50         void run (int num_threads);
51
52 private:
53         void worker_thread ();
54         int process (boost::shared_ptr<Socket> socket, struct timeval &, struct timeval &);
55         void broadcast_thread ();
56         void broadcast_received ();
57         void start_accept ();
58         void handle_accept (boost::shared_ptr<Socket>, boost::system::error_code const &);
59
60         bool _terminate;
61
62         std::vector<boost::thread *> _worker_threads;
63         std::list<boost::shared_ptr<Socket> > _queue;
64         boost::mutex _worker_mutex;
65         boost::condition _full_condition;
66         boost::condition _empty_condition;
67         boost::shared_ptr<Log> _log;
68         bool _verbose;
69
70         boost::asio::io_service _io_service;
71         boost::asio::ip::tcp::acceptor _acceptor;
72
73         struct Broadcast {
74
75                 Broadcast ()
76                         : thread (0)
77                         , socket (0)
78                 {}
79
80                 boost::thread* thread;
81                 boost::asio::ip::udp::socket* socket;
82                 char buffer[64];
83                 boost::asio::ip::udp::endpoint send_endpoint;
84                 boost::asio::io_service io_service;
85
86         } _broadcast;
87 };
88
89 #endif