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 #include "encode_server_finder.h"
22 #include "exceptions.h"
26 #include "encode_server_description.h"
27 #include "dcpomatic_socket.h"
28 #include <dcp/raw_convert.h>
29 #include <libcxml/cxml.h>
30 #include <boost/lambda/lambda.hpp>
39 using boost::shared_ptr;
40 using boost::scoped_array;
41 using boost::weak_ptr;
42 using boost::optional;
43 using dcp::raw_convert;
45 EncodeServerFinder* EncodeServerFinder::_instance = 0;
47 EncodeServerFinder::EncodeServerFinder ()
52 Config::instance()->Changed.connect (boost::bind (&EncodeServerFinder::config_changed, this, _1));
56 EncodeServerFinder::start ()
58 _search_thread = new boost::thread (boost::bind (&EncodeServerFinder::search_thread, this));
59 _listen_thread = new boost::thread (boost::bind (&EncodeServerFinder::listen_thread, this));
60 #ifdef DCPOMATIC_LINUX
61 pthread_setname_np (_search_thread->native_handle(), "encode-server-search");
62 pthread_setname_np (_listen_thread->native_handle(), "encode-server-listen");
67 EncodeServerFinder::~EncodeServerFinder ()
73 EncodeServerFinder::stop ()
77 _search_condition.notify_all ();
79 /* Ideally this would be a DCPOMATIC_ASSERT(_search_thread->joinable()) but we
80 can't throw exceptions from a destructor.
82 if (_search_thread->joinable ()) {
83 _search_thread->join ();
86 delete _search_thread;
89 _listen_io_service.stop ();
91 /* Ideally this would be a DCPOMATIC_ASSERT(_listen_thread->joinable()) but we
92 can't throw exceptions from a destructor.
94 if (_listen_thread->joinable ()) {
95 _listen_thread->join ();
98 delete _listen_thread;
101 boost::mutex::scoped_lock lm (_servers_mutex);
106 EncodeServerFinder::search_thread ()
109 boost::system::error_code error;
110 boost::asio::io_service io_service;
111 boost::asio::ip::udp::socket socket (io_service);
112 socket.open (boost::asio::ip::udp::v4(), error);
114 throw NetworkError ("failed to set up broadcast socket");
117 socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
118 socket.set_option (boost::asio::socket_base::broadcast (true));
120 string const data = DCPOMATIC_HELLO;
121 int const interval = 10;
124 if (Config::instance()->use_any_servers ()) {
125 /* Broadcast to look for servers */
127 boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), HELLO_PORT);
128 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
134 /* Query our `definite' servers (if there are any) */
135 vector<string> servers = Config::instance()->servers ();
136 for (vector<string>::const_iterator i = servers.begin(); i != servers.end(); ++i) {
137 if (server_found (*i)) {
138 /* Don't bother asking a server that we already know about */
142 boost::asio::ip::udp::resolver resolver (io_service);
143 boost::asio::ip::udp::resolver::query query (*i, raw_convert<string> (HELLO_PORT));
144 boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
145 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
151 /* Discard servers that we haven't seen for a while */
153 boost::mutex::scoped_lock lm (_servers_mutex);
155 bool removed = false;
156 list<EncodeServerDescription>::iterator i = _servers.begin();
157 while (i != _servers.end()) {
158 if (i->last_seen_seconds() > 2 * interval) {
159 list<EncodeServerDescription>::iterator j = i;
170 emit (boost::bind (boost::ref (ServersListChanged)));
174 boost::mutex::scoped_lock lm (_search_condition_mutex);
175 _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds (interval));
184 EncodeServerFinder::listen_thread ()
186 using namespace boost::asio::ip;
189 _listen_acceptor.reset (
190 new tcp::acceptor (_listen_io_service, tcp::endpoint (tcp::v4(), is_batch_converter ? BATCH_SERVER_PRESENCE_PORT : MAIN_SERVER_PRESENCE_PORT))
193 boost::throw_exception (NetworkError (_("Could not listen for remote encode servers. Perhaps another instance of DCP-o-matic is running.")));
197 _listen_io_service.run ();
205 EncodeServerFinder::start_accept ()
207 shared_ptr<Socket> socket (new Socket ());
208 _listen_acceptor->async_accept (
210 boost::bind (&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error, socket)
215 EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> socket)
223 socket->read (reinterpret_cast<uint8_t*> (&length), sizeof (uint32_t));
224 length = ntohl (length);
226 scoped_array<char> buffer (new char[length]);
227 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
229 string s (buffer.get());
230 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
231 xml->read_string (s);
233 string const ip = socket->socket().remote_endpoint().address().to_string ();
234 optional<list<EncodeServerDescription>::iterator> found = server_found (ip);
236 (*found)->set_seen ();
238 EncodeServerDescription sd (ip, xml->number_child<int>("Threads"), xml->optional_number_child<int>("Version").get_value_or(0));
239 boost::mutex::scoped_lock lm (_servers_mutex);
240 _servers.push_back (sd);
241 emit (boost::bind (boost::ref (ServersListChanged)));
247 optional<list<EncodeServerDescription>::iterator>
248 EncodeServerFinder::server_found (string ip)
250 boost::mutex::scoped_lock lm (_servers_mutex);
251 list<EncodeServerDescription>::iterator i = _servers.begin();
252 while (i != _servers.end() && i->host_name() != ip) {
256 if (i != _servers.end()) {
260 return optional<list<EncodeServerDescription>::iterator>();
264 EncodeServerFinder::instance ()
267 _instance = new EncodeServerFinder ();
275 EncodeServerFinder::drop ()
281 list<EncodeServerDescription>
282 EncodeServerFinder::servers () const
284 boost::mutex::scoped_lock lm (_servers_mutex);
289 EncodeServerFinder::config_changed (Config::Property what)
291 if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
293 boost::mutex::scoped_lock lm (_servers_mutex);
296 ServersListChanged ();
297 _search_condition.notify_all ();