2 Copyright (C) 2013-2021 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/>.
23 #include "constants.h"
25 #include "dcpomatic_socket.h"
26 #include "encode_server_description.h"
27 #include "encode_server_finder.h"
28 #include "exceptions.h"
30 #include <dcp/raw_convert.h>
31 #include <libcxml/cxml.h>
32 #include <boost/bind/placeholders.hpp>
33 #include <boost/lambda/lambda.hpp>
41 using std::make_shared;
42 using std::shared_ptr;
46 using boost::optional;
47 using boost::scoped_array;
48 #if BOOST_VERSION >= 106100
49 using namespace boost::placeholders;
51 using dcp::raw_convert;
54 EncodeServerFinder* EncodeServerFinder::_instance = 0;
57 EncodeServerFinder::EncodeServerFinder ()
60 Config::instance()->Changed.connect (boost::bind (&EncodeServerFinder::config_changed, this, _1));
65 EncodeServerFinder::start ()
67 _search_thread = boost::thread (boost::bind(&EncodeServerFinder::search_thread, this));
68 _listen_thread = boost::thread (boost::bind(&EncodeServerFinder::listen_thread, this));
69 #ifdef DCPOMATIC_LINUX
70 pthread_setname_np (_search_thread.native_handle(), "encode-server-search");
71 pthread_setname_np (_listen_thread.native_handle(), "encode-server-listen");
76 EncodeServerFinder::~EncodeServerFinder ()
83 EncodeServerFinder::stop ()
85 boost::this_thread::disable_interruption dis;
89 _search_condition.notify_all ();
91 _search_thread.join();
94 _listen_io_service.stop ();
96 _listen_thread.join ();
99 boost::mutex::scoped_lock lm (_servers_mutex);
105 EncodeServerFinder::search_thread ()
108 start_of_thread ("EncodeServerFinder-search");
110 boost::system::error_code error;
111 boost::asio::io_service io_service;
112 boost::asio::ip::udp::socket socket (io_service);
113 socket.open (boost::asio::ip::udp::v4(), error);
115 throw NetworkError ("failed to set up broadcast socket");
118 socket.set_option (boost::asio::ip::udp::socket::reuse_address(true));
119 socket.set_option (boost::asio::socket_base::broadcast(true));
121 string const data = DCPOMATIC_HELLO;
122 int const interval = 10;
125 if (Config::instance()->use_any_servers()) {
126 /* Broadcast to look for servers */
128 boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), HELLO_PORT);
129 socket.send_to (boost::asio::buffer(data.c_str(), data.size() + 1), end_point);
135 /* Query our `definite' servers (if there are any) */
136 for (auto const& i: Config::instance()->servers()) {
138 boost::asio::ip::udp::resolver resolver (io_service);
139 boost::asio::ip::udp::resolver::query query (i, raw_convert<string>(HELLO_PORT));
140 boost::asio::ip::udp::endpoint end_point (*resolver.resolve(query));
141 socket.send_to (boost::asio::buffer(data.c_str(), data.size() + 1), end_point);
147 /* Discard servers that we haven't seen for a while */
148 bool removed = false;
150 boost::mutex::scoped_lock lm (_servers_mutex);
152 auto i = _servers.begin();
153 while (i != _servers.end()) {
154 if (i->last_seen_seconds() > 2 * interval) {
167 emit (boost::bind(boost::ref(ServersListChanged)));
170 boost::mutex::scoped_lock lm (_search_condition_mutex);
171 _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds(interval));
181 EncodeServerFinder::listen_thread ()
183 start_of_thread ("EncodeServerFinder-listen");
185 using namespace boost::asio::ip;
188 _listen_acceptor.reset (
189 new tcp::acceptor (_listen_io_service, tcp::endpoint(tcp::v4(), is_batch_converter ? BATCH_SERVER_PRESENCE_PORT : MAIN_SERVER_PRESENCE_PORT))
192 boost::throw_exception(NetworkError(variant::insert_dcpomatic(_("Could not listen for remote encode servers. Perhaps another instance of %1 is running."))));
196 _listen_io_service.run ();
205 EncodeServerFinder::start_accept ()
207 _accept_socket = make_shared<Socket>();
209 _listen_acceptor->async_accept (
210 _accept_socket->socket(),
211 boost::bind(&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error)
217 EncodeServerFinder::handle_accept (boost::system::error_code ec)
224 string server_available;
228 _accept_socket->read (reinterpret_cast<uint8_t*>(&length), sizeof(uint32_t));
229 length = ntohl (length);
231 if (length > 65536) {
236 scoped_array<char> buffer(new char[length]);
237 _accept_socket->read (reinterpret_cast<uint8_t*>(buffer.get()), length);
238 server_available = buffer.get();
239 } catch (NetworkError&) {
240 /* Maybe the server went away; let's just try again */
245 auto xml = make_shared<cxml::Document>("ServerAvailable");
246 xml->read_string(server_available);
248 auto const ip = _accept_socket->socket().remote_endpoint().address().to_string();
249 bool changed = false;
251 boost::mutex::scoped_lock lm (_servers_mutex);
252 auto i = _servers.begin();
253 while (i != _servers.end() && i->host_name() != ip) {
257 if (i != _servers.end()) {
260 EncodeServerDescription sd (ip, xml->number_child<int>("Threads"), xml->optional_number_child<int>("Version").get_value_or(0));
261 _servers.push_back (sd);
267 emit (boost::bind(boost::ref (ServersListChanged)));
275 EncodeServerFinder::instance ()
278 _instance = new EncodeServerFinder ();
287 EncodeServerFinder::drop ()
294 list<EncodeServerDescription>
295 EncodeServerFinder::servers () const
297 boost::mutex::scoped_lock lm (_servers_mutex);
303 EncodeServerFinder::config_changed (Config::Property what)
305 if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
307 boost::mutex::scoped_lock lm (_servers_mutex);
310 ServersListChanged ();
311 _search_condition.notify_all ();