Rename Server -> EncodeServer, ServerFinder -> EncodeServerFinder, ServerDescription...
[dcpomatic.git] / src / lib / encode_server.h
1 /*
2     Copyright (C) 2012-2015 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_ENCODE_SERVER_H
21 #define DCPOMATIC_ENCODE_SERVER_H
22
23 /** @file src/encode_server.h
24  *  @brief Server class.
25  */
26
27 #include "exception_store.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 /** @class EncodeServer
37  *  @brief A class to run a server which can accept requests to perform JPEG2000
38  *  encoding work.
39  */
40 class EncodeServer : public ExceptionStore, public boost::noncopyable
41 {
42 public:
43         EncodeServer (boost::shared_ptr<Log> log, bool verbose);
44         ~EncodeServer ();
45
46         void run (int num_threads);
47
48 private:
49         void worker_thread ();
50         int process (boost::shared_ptr<Socket> socket, struct timeval &, struct timeval &);
51         void broadcast_thread ();
52         void broadcast_received ();
53         void start_accept ();
54         void handle_accept (boost::shared_ptr<Socket>, boost::system::error_code const &);
55
56         bool _terminate;
57
58         std::vector<boost::thread *> _worker_threads;
59         std::list<boost::shared_ptr<Socket> > _queue;
60         boost::mutex _worker_mutex;
61         boost::condition _full_condition;
62         boost::condition _empty_condition;
63         boost::shared_ptr<Log> _log;
64         bool _verbose;
65
66         boost::asio::io_service _io_service;
67         boost::asio::ip::tcp::acceptor _acceptor;
68
69         struct Broadcast {
70
71                 Broadcast ()
72                         : thread (0)
73                         , socket (0)
74                 {}
75
76                 boost::thread* thread;
77                 boost::asio::ip::udp::socket* socket;
78                 char buffer[64];
79                 boost::asio::ip::udp::endpoint send_endpoint;
80                 boost::asio::io_service io_service;
81
82         } _broadcast;
83 };
84
85 #endif