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 <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 dcp::raw_convert;
44 EncodeServerFinder* EncodeServerFinder::_instance = 0;
46 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));
62 EncodeServerFinder::~EncodeServerFinder ()
68 EncodeServerFinder::stop ()
72 _search_condition.notify_all ();
74 /* Ideally this would be a DCPOMATIC_ASSERT(_search_thread->joinable()) but we
75 can't throw exceptions from a destructor.
77 if (_search_thread->joinable ()) {
78 _search_thread->join ();
81 delete _search_thread;
84 _listen_io_service.stop ();
86 /* Ideally this would be a DCPOMATIC_ASSERT(_listen_thread->joinable()) but we
87 can't throw exceptions from a destructor.
89 if (_listen_thread->joinable ()) {
90 _listen_thread->join ();
93 delete _listen_thread;
96 boost::mutex::scoped_lock lm (_servers_mutex);
101 EncodeServerFinder::search_thread ()
104 boost::system::error_code error;
105 boost::asio::io_service io_service;
106 boost::asio::ip::udp::socket socket (io_service);
107 socket.open (boost::asio::ip::udp::v4(), error);
109 throw NetworkError ("failed to set up broadcast socket");
112 socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
113 socket.set_option (boost::asio::socket_base::broadcast (true));
115 string const data = DCPOMATIC_HELLO;
118 if (Config::instance()->use_any_servers ()) {
119 /* Broadcast to look for servers */
121 boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1);
122 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
128 /* Query our `definite' servers (if there are any) */
129 vector<string> servers = Config::instance()->servers ();
130 for (vector<string>::const_iterator i = servers.begin(); i != servers.end(); ++i) {
131 if (server_found (*i)) {
132 /* Don't bother asking a server that we already know about */
136 boost::asio::ip::udp::resolver resolver (io_service);
137 boost::asio::ip::udp::resolver::query query (*i, raw_convert<string> (Config::instance()->server_port_base() + 1));
138 boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
139 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
145 boost::mutex::scoped_lock lm (_search_condition_mutex);
146 _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds (10));
155 EncodeServerFinder::listen_thread ()
157 using namespace boost::asio::ip;
160 _listen_acceptor.reset (new tcp::acceptor (_listen_io_service, tcp::endpoint (tcp::v4(), Config::instance()->server_port_base() + 1)));
162 boost::throw_exception (NetworkError (_("Could not listen for remote encode servers. Perhaps another instance of DCP-o-matic is running.")));
166 _listen_io_service.run ();
174 EncodeServerFinder::start_accept ()
176 shared_ptr<Socket> socket (new Socket ());
177 _listen_acceptor->async_accept (
179 boost::bind (&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error, socket)
184 EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> socket)
192 socket->read (reinterpret_cast<uint8_t*> (&length), sizeof (uint32_t));
193 length = ntohl (length);
195 scoped_array<char> buffer (new char[length]);
196 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
198 string s (buffer.get());
199 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
200 xml->read_string (s);
202 string const ip = socket->socket().remote_endpoint().address().to_string ();
203 if (!server_found (ip) && xml->optional_number_child<int>("Version").get_value_or (0) == SERVER_LINK_VERSION) {
204 EncodeServerDescription sd (ip, xml->number_child<int> ("Threads"));
206 boost::mutex::scoped_lock lm (_servers_mutex);
207 _servers.push_back (sd);
209 emit (boost::bind (boost::ref (ServersListChanged)));
216 EncodeServerFinder::server_found (string ip) const
218 boost::mutex::scoped_lock lm (_servers_mutex);
219 list<EncodeServerDescription>::const_iterator i = _servers.begin();
220 while (i != _servers.end() && i->host_name() != ip) {
224 return i != _servers.end ();
228 EncodeServerFinder::instance ()
231 _instance = new EncodeServerFinder ();
239 EncodeServerFinder::drop ()
245 list<EncodeServerDescription>
246 EncodeServerFinder::servers () const
248 boost::mutex::scoped_lock lm (_servers_mutex);
253 EncodeServerFinder::config_changed (Config::Property what)
255 if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
257 boost::mutex::scoped_lock lm (_servers_mutex);
260 ServersListChanged ();
261 _search_condition.notify_all ();