Merge remote-tracking branch 'origin/main' into v2.17.x
[dcpomatic.git] / src / lib / encode_server_finder.h
1 /*
2     Copyright (C) 2013-2021 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
22 /** @file  src/lib/encode_server_finder.h
23  *  @brief EncodeServerFinder class.
24  */
25
26
27 #include "config.h"
28 #include "encode_server_description.h"
29 #include "exception_store.h"
30 #include "signaller.h"
31 #include <boost/signals2.hpp>
32 #include <boost/thread/condition.hpp>
33
34
35 class Socket;
36
37
38 /** @class EncodeServerFinder
39  *  @brief Locater of encoding servers.
40  *
41  *  This class finds active (i.e. responding) encode servers.  Depending on
42  *  configuration it finds servers by:
43  *
44  *  1. broadcasting a request to the local subnet and
45  *  2. checking to see if any of the configured server hosts are up.
46  */
47 class EncodeServerFinder : public Signaller, public ExceptionStore
48 {
49 public:
50         static EncodeServerFinder* instance ();
51         static void drop ();
52
53         std::list<EncodeServerDescription> servers () const;
54
55         /** Emitted whenever the list of servers changes */
56         boost::signals2::signal<void ()> ServersListChanged;
57
58 private:
59         EncodeServerFinder ();
60         ~EncodeServerFinder ();
61
62         void start ();
63         void stop ();
64
65         void search_thread ();
66         void listen_thread ();
67
68         void start_accept ();
69         void handle_accept (boost::system::error_code ec);
70
71         void config_changed (Config::Property what);
72
73         /** Thread to periodically issue broadcasts and requests to find encoding servers */
74         boost::thread _search_thread;
75         /** Thread to listen to the responses from servers */
76         boost::thread _listen_thread;
77
78         /** Available servers */
79         std::list<EncodeServerDescription> _servers;
80         /** Mutex for _servers */
81         mutable boost::mutex _servers_mutex;
82
83         boost::asio::io_service _listen_io_service;
84         std::shared_ptr<boost::asio::ip::tcp::acceptor> _listen_acceptor;
85         bool _stop;
86
87         boost::condition _search_condition;
88         boost::mutex _search_condition_mutex;
89
90         std::shared_ptr<Socket> _accept_socket;
91
92         static EncodeServerFinder* _instance;
93 };