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/bind/placeholders.hpp>
31 #include <boost/lambda/lambda.hpp>
40 using std::shared_ptr;
41 using boost::scoped_array;
43 using boost::optional;
44 #if BOOST_VERSION >= 106100
45 using namespace boost::placeholders;
47 using dcp::raw_convert;
49 EncodeServerFinder* EncodeServerFinder::_instance = 0;
51 EncodeServerFinder::EncodeServerFinder ()
54 Config::instance()->Changed.connect (boost::bind (&EncodeServerFinder::config_changed, this, _1));
58 EncodeServerFinder::start ()
60 _search_thread = boost::thread (boost::bind(&EncodeServerFinder::search_thread, this));
61 _listen_thread = boost::thread (boost::bind(&EncodeServerFinder::listen_thread, this));
62 #ifdef DCPOMATIC_LINUX
63 pthread_setname_np (_search_thread.native_handle(), "encode-server-search");
64 pthread_setname_np (_listen_thread.native_handle(), "encode-server-listen");
69 EncodeServerFinder::~EncodeServerFinder ()
75 EncodeServerFinder::stop ()
77 boost::this_thread::disable_interruption dis;
81 _search_condition.notify_all ();
83 _search_thread.join();
86 _listen_io_service.stop ();
88 _listen_thread.join ();
91 boost::mutex::scoped_lock lm (_servers_mutex);
96 EncodeServerFinder::search_thread ()
99 boost::system::error_code error;
100 boost::asio::io_service io_service;
101 boost::asio::ip::udp::socket socket (io_service);
102 socket.open (boost::asio::ip::udp::v4(), error);
104 throw NetworkError ("failed to set up broadcast socket");
107 socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
108 socket.set_option (boost::asio::socket_base::broadcast (true));
110 string const data = DCPOMATIC_HELLO;
111 int const interval = 10;
114 if (Config::instance()->use_any_servers ()) {
115 /* Broadcast to look for servers */
117 boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), HELLO_PORT);
118 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
124 /* Query our `definite' servers (if there are any) */
125 for (auto const& i: Config::instance()->servers()) {
127 boost::asio::ip::udp::resolver resolver (io_service);
128 boost::asio::ip::udp::resolver::query query (i, raw_convert<string> (HELLO_PORT));
129 boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
130 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
136 /* Discard servers that we haven't seen for a while */
137 bool removed = false;
139 boost::mutex::scoped_lock lm (_servers_mutex);
141 list<EncodeServerDescription>::iterator i = _servers.begin();
142 while (i != _servers.end()) {
143 if (i->last_seen_seconds() > 2 * interval) {
144 list<EncodeServerDescription>::iterator j = i;
156 emit (boost::bind (boost::ref (ServersListChanged)));
159 boost::mutex::scoped_lock lm (_search_condition_mutex);
160 _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds (interval));
169 EncodeServerFinder::listen_thread ()
171 using namespace boost::asio::ip;
174 _listen_acceptor.reset (
175 new tcp::acceptor (_listen_io_service, tcp::endpoint (tcp::v4(), is_batch_converter ? BATCH_SERVER_PRESENCE_PORT : MAIN_SERVER_PRESENCE_PORT))
178 boost::throw_exception (NetworkError (_("Could not listen for remote encode servers. Perhaps another instance of DCP-o-matic is running.")));
182 _listen_io_service.run ();
190 EncodeServerFinder::start_accept ()
192 shared_ptr<Socket> socket (new Socket ());
193 _listen_acceptor->async_accept (
195 boost::bind (&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error, socket)
200 EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> socket)
208 socket->read (reinterpret_cast<uint8_t*> (&length), sizeof (uint32_t));
209 length = ntohl (length);
211 scoped_array<char> buffer (new char[length]);
212 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
214 string s (buffer.get());
215 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
216 xml->read_string (s);
218 string const ip = socket->socket().remote_endpoint().address().to_string ();
219 optional<list<EncodeServerDescription>::iterator> found = server_found (ip);
221 (*found)->set_seen ();
223 EncodeServerDescription sd (ip, xml->number_child<int>("Threads"), xml->optional_number_child<int>("Version").get_value_or(0));
225 boost::mutex::scoped_lock lm (_servers_mutex);
226 _servers.push_back (sd);
228 emit (boost::bind (boost::ref (ServersListChanged)));
234 optional<list<EncodeServerDescription>::iterator>
235 EncodeServerFinder::server_found (string ip)
237 boost::mutex::scoped_lock lm (_servers_mutex);
238 list<EncodeServerDescription>::iterator i = _servers.begin();
239 while (i != _servers.end() && i->host_name() != ip) {
243 if (i != _servers.end()) {
247 return optional<list<EncodeServerDescription>::iterator>();
251 EncodeServerFinder::instance ()
254 _instance = new EncodeServerFinder ();
262 EncodeServerFinder::drop ()
268 list<EncodeServerDescription>
269 EncodeServerFinder::servers () const
271 boost::mutex::scoped_lock lm (_servers_mutex);
276 EncodeServerFinder::config_changed (Config::Property what)
278 if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
280 boost::mutex::scoped_lock lm (_servers_mutex);
283 ServersListChanged ();
284 _search_condition.notify_all ();