2 Copyright (C) 2013-2015 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 "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;
43 EncodeServerFinder* EncodeServerFinder::_instance = 0;
45 EncodeServerFinder::EncodeServerFinder ()
51 Config::instance()->Changed.connect (boost::bind (&EncodeServerFinder::config_changed, this, _1));
55 EncodeServerFinder::start ()
57 _search_thread = new boost::thread (boost::bind (&EncodeServerFinder::search_thread, this));
58 _listen_thread = new boost::thread (boost::bind (&EncodeServerFinder::listen_thread, this));
61 EncodeServerFinder::~EncodeServerFinder ()
65 _search_condition.notify_all ();
67 DCPOMATIC_ASSERT (_search_thread->joinable ());
68 _search_thread->join ();
71 _listen_io_service.stop ();
73 DCPOMATIC_ASSERT (_listen_thread->joinable ());
74 _listen_thread->join ();
79 EncodeServerFinder::search_thread ()
82 boost::system::error_code error;
83 boost::asio::io_service io_service;
84 boost::asio::ip::udp::socket socket (io_service);
85 socket.open (boost::asio::ip::udp::v4(), error);
87 throw NetworkError ("failed to set up broadcast socket");
90 socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
91 socket.set_option (boost::asio::socket_base::broadcast (true));
93 string const data = DCPOMATIC_HELLO;
96 if (Config::instance()->use_any_servers ()) {
97 /* Broadcast to look for servers */
99 boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1);
100 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
106 /* Query our `definite' servers (if there are any) */
107 vector<string> servers = Config::instance()->servers ();
108 for (vector<string>::const_iterator i = servers.begin(); i != servers.end(); ++i) {
109 if (server_found (*i)) {
110 /* Don't bother asking a server that we already know about */
114 boost::asio::ip::udp::resolver resolver (io_service);
115 boost::asio::ip::udp::resolver::query query (*i, raw_convert<string> (Config::instance()->server_port_base() + 1));
116 boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
117 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
123 boost::mutex::scoped_lock lm (_search_condition_mutex);
124 _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds (10));
133 EncodeServerFinder::listen_thread ()
135 using namespace boost::asio::ip;
138 _listen_acceptor.reset (new tcp::acceptor (_listen_io_service, tcp::endpoint (tcp::v4(), Config::instance()->server_port_base() + 1)));
140 boost::throw_exception (NetworkError (_("Could not listen for remote encode servers. Perhaps another instance of DCP-o-matic is running.")));
144 _listen_io_service.run ();
152 EncodeServerFinder::start_accept ()
154 shared_ptr<Socket> socket (new Socket ());
155 _listen_acceptor->async_accept (
157 boost::bind (&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error, socket)
162 EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> socket)
170 socket->read (reinterpret_cast<uint8_t*> (&length), sizeof (uint32_t));
171 length = ntohl (length);
173 scoped_array<char> buffer (new char[length]);
174 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
176 string s (buffer.get());
177 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
178 xml->read_string (s);
180 string const ip = socket->socket().remote_endpoint().address().to_string ();
181 if (!server_found (ip) && xml->optional_number_child<int>("Version").get_value_or (0) == SERVER_LINK_VERSION) {
182 EncodeServerDescription sd (ip, xml->number_child<int> ("Threads"));
184 boost::mutex::scoped_lock lm (_servers_mutex);
185 _servers.push_back (sd);
187 emit (boost::bind (boost::ref (ServersListChanged)));
194 EncodeServerFinder::server_found (string ip) const
196 boost::mutex::scoped_lock lm (_servers_mutex);
197 list<EncodeServerDescription>::const_iterator i = _servers.begin();
198 while (i != _servers.end() && i->host_name() != ip) {
202 return i != _servers.end ();
206 EncodeServerFinder::instance ()
209 _instance = new EncodeServerFinder ();
217 EncodeServerFinder::drop ()
223 list<EncodeServerDescription>
224 EncodeServerFinder::servers () const
226 boost::mutex::scoped_lock lm (_servers_mutex);
231 EncodeServerFinder::config_changed (Config::Property what)
233 if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
235 boost::mutex::scoped_lock lm (_servers_mutex);
238 ServersListChanged ();
239 _search_condition.notify_all ();