2 Copyright (C) 2013-2014 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 <libcxml/cxml.h>
21 #include <dcp/raw_convert.h>
22 #include "server_finder.h"
23 #include "exceptions.h"
27 #include "ui_signaller.h"
33 using boost::shared_ptr;
34 using boost::scoped_array;
35 using dcp::raw_convert;
37 ServerFinder* ServerFinder::_instance = 0;
39 ServerFinder::ServerFinder ()
41 , _broadcast_thread (0)
44 _broadcast_thread = new boost::thread (boost::bind (&ServerFinder::broadcast_thread, this));
45 _listen_thread = new boost::thread (boost::bind (&ServerFinder::listen_thread, this));
49 ServerFinder::broadcast_thread ()
52 boost::system::error_code error;
53 boost::asio::io_service io_service;
54 boost::asio::ip::udp::socket socket (io_service);
55 socket.open (boost::asio::ip::udp::v4(), error);
57 throw NetworkError ("failed to set up broadcast socket");
60 socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
61 socket.set_option (boost::asio::socket_base::broadcast (true));
63 string const data = DCPOMATIC_HELLO;
66 if (Config::instance()->use_any_servers ()) {
67 /* Broadcast to look for servers */
69 boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1);
70 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
76 /* Query our `definite' servers (if there are any) */
77 vector<string> servers = Config::instance()->servers ();
78 for (vector<string>::const_iterator i = servers.begin(); i != servers.end(); ++i) {
79 if (server_found (*i)) {
80 /* Don't bother asking a server that we already know about */
84 boost::asio::ip::udp::resolver resolver (io_service);
85 boost::asio::ip::udp::resolver::query query (*i, raw_convert<string> (Config::instance()->server_port_base() + 1));
86 boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
87 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
102 ServerFinder::listen_thread ()
106 shared_ptr<Socket> sock (new Socket (60));
109 sock->accept (Config::instance()->server_port_base() + 1);
110 } catch (std::exception& e) {
114 uint32_t length = sock->read_uint32 ();
115 scoped_array<char> buffer (new char[length]);
116 sock->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
118 string s (buffer.get());
119 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
120 xml->read_string (s);
122 string const ip = sock->socket().remote_endpoint().address().to_string ();
123 if (!server_found (ip)) {
124 ServerDescription sd (ip, xml->number_child<int> ("Threads"));
125 _servers.push_back (sd);
126 ui_signaller->emit (boost::bind (boost::ref (ServerFound), sd));
136 ServerFinder::server_found (string ip) const
138 boost::mutex::scoped_lock lm (_mutex);
139 list<ServerDescription>::const_iterator i = _servers.begin();
140 while (i != _servers.end() && i->host_name() != ip) {
144 return i != _servers.end ();
148 ServerFinder::connect (boost::function<void (ServerDescription)> fn)
154 boost::mutex::scoped_lock lm (_mutex);
156 /* Emit the current list of servers */
157 for (list<ServerDescription>::iterator i = _servers.begin(); i != _servers.end(); ++i) {
161 ServerFound.connect (fn);
165 ServerFinder::instance ()
168 _instance = new ServerFinder ();