Split EncodeServer into that and Server.
[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 EncodeServer class.
25  */
26
27 #include "server.h"
28 #include "exception_store.h"
29 #include <boost/thread.hpp>
30 #include <boost/asio.hpp>
31 #include <boost/thread/condition.hpp>
32 #include <string>
33
34 class Socket;
35 class Log;
36
37 /** @class EncodeServer
38  *  @brief A class to run a server which can accept requests to perform JPEG2000
39  *  encoding work.
40  */
41 class EncodeServer : public Server, public ExceptionStore
42 {
43 public:
44         EncodeServer (boost::shared_ptr<Log> log, bool verbose, int num_threads);
45         ~EncodeServer ();
46
47         void run ();
48
49 private:
50         void handle (boost::shared_ptr<Socket>);
51         void worker_thread ();
52         int process (boost::shared_ptr<Socket> socket, struct timeval &, struct timeval &);
53         void broadcast_thread ();
54         void broadcast_received ();
55
56         std::vector<boost::thread *> _worker_threads;
57         std::list<boost::shared_ptr<Socket> > _queue;
58         boost::condition _full_condition;
59         boost::condition _empty_condition;
60         boost::shared_ptr<Log> _log;
61         bool _verbose;
62         int _num_threads;
63
64         struct Broadcast {
65
66                 Broadcast ()
67                         : thread (0)
68                         , socket (0)
69                 {}
70
71                 boost::thread* thread;
72                 boost::asio::ip::udp::socket* socket;
73                 char buffer[64];
74                 boost::asio::ip::udp::endpoint send_endpoint;
75                 boost::asio::io_service io_service;
76
77         } _broadcast;
78 };
79
80 #endif