Do EncodeServerFinder 'disable' in a more sensible way.
[dcpomatic.git] / src / lib / encode_server_finder.h
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/lib/encode_server_finder.h
22  *  @brief EncodeServerFinder class.
23  */
24
25 #include "signaller.h"
26 #include "encode_server_description.h"
27 #include "config.h"
28 #include "exception_store.h"
29 #include <boost/signals2.hpp>
30 #include <boost/thread/condition.hpp>
31
32 class Socket;
33
34 class EncodeServerFinder : public Signaller, public ExceptionStore
35 {
36 public:
37         static EncodeServerFinder* instance ();
38         static void drop ();
39
40         void stop ();
41
42         std::list<EncodeServerDescription> servers () const;
43
44         /** Emitted whenever the list of servers changes */
45         boost::signals2::signal<void ()> ServersListChanged;
46
47 private:
48         EncodeServerFinder ();
49         ~EncodeServerFinder ();
50
51         void start ();
52
53         void search_thread ();
54         void listen_thread ();
55
56         bool server_found (std::string) const;
57         void start_accept ();
58         void handle_accept (boost::system::error_code ec, boost::shared_ptr<Socket> socket);
59
60         void config_changed (Config::Property what);
61
62         /** Thread to periodically issue broadcasts and requests to find encoding servers */
63         boost::thread* _search_thread;
64         /** Thread to listen to the responses from servers */
65         boost::thread* _listen_thread;
66
67         std::list<EncodeServerDescription> _servers;
68         mutable boost::mutex _servers_mutex;
69
70         boost::asio::io_service _listen_io_service;
71         boost::shared_ptr<boost::asio::ip::tcp::acceptor> _listen_acceptor;
72         bool _stop;
73
74         boost::condition _search_condition;
75         boost::mutex _search_condition_mutex;
76
77         static EncodeServerFinder* _instance;
78 };