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 "encode_server_finder.h"
21 #include "exceptions.h"
25 #include "encode_server_description.h"
26 #include "dcpomatic_socket.h"
27 #include "raw_convert.h"
28 #include <libcxml/cxml.h>
29 #include <boost/lambda/lambda.hpp>
38 using boost::shared_ptr;
39 using boost::scoped_array;
40 using boost::weak_ptr;
42 EncodeServerFinder* EncodeServerFinder::_instance = 0;
44 EncodeServerFinder::EncodeServerFinder ()
50 Config::instance()->Changed.connect (boost::bind (&EncodeServerFinder::config_changed, this, _1));
54 EncodeServerFinder::start ()
56 _search_thread = new boost::thread (boost::bind (&EncodeServerFinder::search_thread, this));
57 _listen_thread = new boost::thread (boost::bind (&EncodeServerFinder::listen_thread, this));
60 EncodeServerFinder::~EncodeServerFinder ()
64 _search_condition.notify_all ();
66 DCPOMATIC_ASSERT (_search_thread->joinable ());
67 _search_thread->join ();
70 _listen_io_service.stop ();
72 DCPOMATIC_ASSERT (_listen_thread->joinable ());
73 _listen_thread->join ();
78 EncodeServerFinder::search_thread ()
81 boost::system::error_code error;
82 boost::asio::io_service io_service;
83 boost::asio::ip::udp::socket socket (io_service);
84 socket.open (boost::asio::ip::udp::v4(), error);
86 throw NetworkError ("failed to set up broadcast socket");
89 socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
90 socket.set_option (boost::asio::socket_base::broadcast (true));
92 string const data = DCPOMATIC_HELLO;
95 if (Config::instance()->use_any_servers ()) {
96 /* Broadcast to look for servers */
98 boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1);
99 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
105 /* Query our `definite' servers (if there are any) */
106 vector<string> servers = Config::instance()->servers ();
107 for (vector<string>::const_iterator i = servers.begin(); i != servers.end(); ++i) {
108 if (server_found (*i)) {
109 /* Don't bother asking a server that we already know about */
113 boost::asio::ip::udp::resolver resolver (io_service);
114 boost::asio::ip::udp::resolver::query query (*i, raw_convert<string> (Config::instance()->server_port_base() + 1));
115 boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
116 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
122 boost::mutex::scoped_lock lm (_search_condition_mutex);
123 _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds (10));
132 EncodeServerFinder::listen_thread ()
134 using namespace boost::asio::ip;
137 _listen_acceptor.reset (new tcp::acceptor (_listen_io_service, tcp::endpoint (tcp::v4(), Config::instance()->server_port_base() + 1)));
139 boost::throw_exception (NetworkError (_("Could not listen for remote encode servers. Perhaps another instance of DCP-o-matic is running.")));
143 _listen_io_service.run ();
151 EncodeServerFinder::start_accept ()
153 shared_ptr<Socket> socket (new Socket ());
154 _listen_acceptor->async_accept (
156 boost::bind (&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error, socket)
161 EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> socket)
169 socket->read (reinterpret_cast<uint8_t*> (&length), sizeof (uint32_t));
170 length = ntohl (length);
172 scoped_array<char> buffer (new char[length]);
173 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
175 string s (buffer.get());
176 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
177 xml->read_string (s);
179 string const ip = socket->socket().remote_endpoint().address().to_string ();
180 if (!server_found (ip) && xml->optional_number_child<int>("Version").get_value_or (0) == SERVER_LINK_VERSION) {
181 EncodeServerDescription sd (ip, xml->number_child<int> ("Threads"));
183 boost::mutex::scoped_lock lm (_servers_mutex);
184 _servers.push_back (sd);
186 emit (boost::bind (boost::ref (ServersListChanged)));
193 EncodeServerFinder::server_found (string ip) const
195 boost::mutex::scoped_lock lm (_servers_mutex);
196 list<EncodeServerDescription>::const_iterator i = _servers.begin();
197 while (i != _servers.end() && i->host_name() != ip) {
201 return i != _servers.end ();
205 EncodeServerFinder::instance ()
208 _instance = new EncodeServerFinder ();
216 EncodeServerFinder::drop ()
222 list<EncodeServerDescription>
223 EncodeServerFinder::servers () const
225 boost::mutex::scoped_lock lm (_servers_mutex);
230 EncodeServerFinder::config_changed (Config::Property what)
232 if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
234 boost::mutex::scoped_lock lm (_servers_mutex);
237 ServersListChanged ();
238 _search_condition.notify_all ();