2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "server_finder.h"
21 #include "exceptions.h"
25 #include "server_description.h"
26 #include "dcpomatic_socket.h"
27 #include "raw_convert.h"
28 #include <libcxml/cxml.h>
29 #include <boost/lambda/lambda.hpp>
37 using boost::shared_ptr;
38 using boost::scoped_array;
39 using boost::weak_ptr;
41 ServerFinder* ServerFinder::_instance = 0;
43 ServerFinder::ServerFinder ()
49 Config::instance()->Changed.connect (boost::bind (&ServerFinder::config_changed, this, _1));
53 ServerFinder::start ()
55 _search_thread = new boost::thread (boost::bind (&ServerFinder::search_thread, this));
56 _listen_thread = new boost::thread (boost::bind (&ServerFinder::listen_thread, this));
59 ServerFinder::~ServerFinder ()
63 _search_condition.notify_all ();
65 _search_thread->join ();
68 _listen_io_service.stop ();
70 _listen_thread->join ();
75 ServerFinder::search_thread ()
78 boost::system::error_code error;
79 boost::asio::io_service io_service;
80 boost::asio::ip::udp::socket socket (io_service);
81 socket.open (boost::asio::ip::udp::v4(), error);
83 throw NetworkError ("failed to set up broadcast socket");
86 socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
87 socket.set_option (boost::asio::socket_base::broadcast (true));
89 string const data = DCPOMATIC_HELLO;
92 if (Config::instance()->use_any_servers ()) {
93 /* Broadcast to look for servers */
95 boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1);
96 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
102 /* Query our `definite' servers (if there are any) */
103 vector<string> servers = Config::instance()->servers ();
104 for (vector<string>::const_iterator i = servers.begin(); i != servers.end(); ++i) {
105 if (server_found (*i)) {
106 /* Don't bother asking a server that we already know about */
110 boost::asio::ip::udp::resolver resolver (io_service);
111 boost::asio::ip::udp::resolver::query query (*i, raw_convert<string> (Config::instance()->server_port_base() + 1));
112 boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
113 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
119 boost::mutex::scoped_lock lm (_search_condition_mutex);
120 _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds (10));
129 ServerFinder::listen_thread ()
131 using namespace boost::asio::ip;
134 _listen_acceptor.reset (new tcp::acceptor (_listen_io_service, tcp::endpoint (tcp::v4(), Config::instance()->server_port_base() + 1)));
136 boost::throw_exception (NetworkError (_("Could not listen for remote encode servers. Perhaps another instance of DCP-o-matic is running.")));
140 _listen_io_service.run ();
148 ServerFinder::start_accept ()
150 shared_ptr<Socket> socket (new Socket ());
151 _listen_acceptor->async_accept (
153 boost::bind (&ServerFinder::handle_accept, this, boost::asio::placeholders::error, socket)
158 ServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> socket)
166 socket->read (reinterpret_cast<uint8_t*> (&length), sizeof (uint32_t));
167 length = ntohl (length);
169 scoped_array<char> buffer (new char[length]);
170 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
172 string s (buffer.get());
173 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
174 xml->read_string (s);
176 string const ip = socket->socket().remote_endpoint().address().to_string ();
177 if (!server_found (ip) && xml->optional_number_child<int>("Version").get_value_or (0) == SERVER_LINK_VERSION) {
178 ServerDescription sd (ip, xml->number_child<int> ("Threads"));
180 boost::mutex::scoped_lock lm (_servers_mutex);
181 _servers.push_back (sd);
183 emit (boost::bind (boost::ref (ServersListChanged)));
190 ServerFinder::server_found (string ip) const
192 boost::mutex::scoped_lock lm (_servers_mutex);
193 list<ServerDescription>::const_iterator i = _servers.begin();
194 while (i != _servers.end() && i->host_name() != ip) {
198 return i != _servers.end ();
202 ServerFinder::instance ()
205 _instance = new ServerFinder ();
213 ServerFinder::drop ()
219 list<ServerDescription>
220 ServerFinder::servers () const
222 boost::mutex::scoped_lock lm (_servers_mutex);
227 ServerFinder::config_changed (Config::Property what)
229 if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
231 boost::mutex::scoped_lock lm (_servers_mutex);
234 ServersListChanged ();
240 ServerFinder::search_now ()
242 _search_condition.notify_all ();