2 Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file src/lib/encode_server_finder.h
22 * @brief EncodeServerFinder class.
25 #include "signaller.h"
26 #include "encode_server_description.h"
28 #include "exception_store.h"
29 #include <boost/signals2.hpp>
30 #include <boost/thread/condition.hpp>
34 /** @class EncodeServerFinder
35 * @brief Locater of encoding servers.
37 * This class finds active (i.e. responding) encode servers. Depending on
38 * configuration it finds servers by:
40 * 1. broadcasting a request to the local subnet and
41 * 2. checking to see if any of the configured server hosts are up.
43 class EncodeServerFinder : public Signaller, public ExceptionStore
46 static EncodeServerFinder* instance ();
51 std::list<EncodeServerDescription> good_servers () const;
52 std::list<EncodeServerDescription> bad_servers () const;
54 /** Emitted whenever the list of servers changes */
55 boost::signals2::signal<void ()> ServersListChanged;
58 EncodeServerFinder ();
59 ~EncodeServerFinder ();
63 void search_thread ();
64 void listen_thread ();
66 boost::optional<std::list<EncodeServerDescription>::iterator> server_found (std::string);
68 void handle_accept (boost::system::error_code ec, boost::shared_ptr<Socket> socket);
70 void config_changed (Config::Property what);
72 /** Thread to periodically issue broadcasts and requests to find encoding servers */
73 boost::thread* _search_thread;
74 /** Thread to listen to the responses from servers */
75 boost::thread* _listen_thread;
77 /** Available servers with the correct link version */
78 std::list<EncodeServerDescription> _good_servers;
79 /** Available servers with an incorrect link version */
80 std::list<EncodeServerDescription> _bad_servers;
81 /** Mutex for _good_servers and _bad_servers */
82 mutable boost::mutex _servers_mutex;
84 boost::asio::io_service _listen_io_service;
85 boost::shared_ptr<boost::asio::ip::tcp::acceptor> _listen_acceptor;
88 boost::condition _search_condition;
89 boost::mutex _search_condition_mutex;
91 static EncodeServerFinder* _instance;