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"
29 #include <dcp/raw_convert.h>
30 #include <libcxml/cxml.h>
31 #include <boost/bind/placeholders.hpp>
32 #include <boost/lambda/lambda.hpp>
40 using std::make_shared;
41 using std::shared_ptr;
45 using boost::optional;
46 using boost::scoped_array;
47 #if BOOST_VERSION >= 106100
48 using namespace boost::placeholders;
50 using dcp::raw_convert;
53 EncodeServerFinder* EncodeServerFinder::_instance = 0;
56 EncodeServerFinder::EncodeServerFinder ()
59 Config::instance()->Changed.connect (boost::bind (&EncodeServerFinder::config_changed, this, _1));
64 EncodeServerFinder::start ()
66 _search_thread = boost::thread (boost::bind(&EncodeServerFinder::search_thread, this));
67 _listen_thread = boost::thread (boost::bind(&EncodeServerFinder::listen_thread, this));
68 #ifdef DCPOMATIC_LINUX
69 pthread_setname_np (_search_thread.native_handle(), "encode-server-search");
70 pthread_setname_np (_listen_thread.native_handle(), "encode-server-listen");
75 EncodeServerFinder::~EncodeServerFinder ()
82 EncodeServerFinder::stop ()
84 boost::this_thread::disable_interruption dis;
88 _search_condition.notify_all ();
90 _search_thread.join();
93 _listen_io_service.stop ();
95 _listen_thread.join ();
98 boost::mutex::scoped_lock lm (_servers_mutex);
104 EncodeServerFinder::search_thread ()
107 start_of_thread ("EncodeServerFinder-search");
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 for (auto const& i: Config::instance()->servers()) {
137 boost::asio::ip::udp::resolver resolver (io_service);
138 boost::asio::ip::udp::resolver::query query (i, raw_convert<string>(HELLO_PORT));
139 boost::asio::ip::udp::endpoint end_point (*resolver.resolve(query));
140 socket.send_to (boost::asio::buffer(data.c_str(), data.size() + 1), end_point);
146 /* Discard servers that we haven't seen for a while */
147 bool removed = false;
149 boost::mutex::scoped_lock lm (_servers_mutex);
151 auto i = _servers.begin();
152 while (i != _servers.end()) {
153 if (i->last_seen_seconds() > 2 * interval) {
166 emit (boost::bind(boost::ref(ServersListChanged)));
169 boost::mutex::scoped_lock lm (_search_condition_mutex);
170 _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds(interval));
180 EncodeServerFinder::listen_thread ()
182 start_of_thread ("EncodeServerFinder-listen");
184 using namespace boost::asio::ip;
187 _listen_acceptor.reset (
188 new tcp::acceptor (_listen_io_service, tcp::endpoint(tcp::v4(), is_batch_converter ? BATCH_SERVER_PRESENCE_PORT : MAIN_SERVER_PRESENCE_PORT))
191 boost::throw_exception (NetworkError (_("Could not listen for remote encode servers. Perhaps another instance of DCP-o-matic is running.")));
195 _listen_io_service.run ();
204 EncodeServerFinder::start_accept ()
206 _accept_socket = make_shared<Socket>();
208 _listen_acceptor->async_accept (
209 _accept_socket->socket(),
210 boost::bind(&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error)
216 EncodeServerFinder::handle_accept (boost::system::error_code ec)
223 string server_available;
227 _accept_socket->read (reinterpret_cast<uint8_t*>(&length), sizeof(uint32_t));
228 length = ntohl (length);
230 scoped_array<char> buffer(new char[length]);
231 _accept_socket->read (reinterpret_cast<uint8_t*>(buffer.get()), length);
232 server_available = buffer.get();
233 } catch (NetworkError&) {
234 /* Maybe the server went away; let's just try again */
239 auto xml = make_shared<cxml::Document>("ServerAvailable");
240 xml->read_string(server_available);
242 auto const ip = _accept_socket->socket().remote_endpoint().address().to_string();
243 bool changed = false;
245 boost::mutex::scoped_lock lm (_servers_mutex);
246 auto i = _servers.begin();
247 while (i != _servers.end() && i->host_name() != ip) {
251 if (i != _servers.end()) {
254 EncodeServerDescription sd (ip, xml->number_child<int>("Threads"), xml->optional_number_child<int>("Version").get_value_or(0));
255 _servers.push_back (sd);
261 emit (boost::bind(boost::ref (ServersListChanged)));
269 EncodeServerFinder::instance ()
272 _instance = new EncodeServerFinder ();
281 EncodeServerFinder::drop ()
288 list<EncodeServerDescription>
289 EncodeServerFinder::servers () const
291 boost::mutex::scoped_lock lm (_servers_mutex);
297 EncodeServerFinder::config_changed (Config::Property what)
299 if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
301 boost::mutex::scoped_lock lm (_servers_mutex);
304 ServersListChanged ();
305 _search_condition.notify_all ();