a51aecd978dce72c47a1adf9eccdeaea17ac0f99
[dcpomatic.git] / src / lib / server_finder.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <libcxml/cxml.h>
21 #include "server_finder.h"
22 #include "exceptions.h"
23 #include "util.h"
24 #include "config.h"
25 #include "cross.h"
26 #include "ui_signaller.h"
27
28 using std::string;
29 using std::stringstream;
30 using std::list;
31 using std::vector;
32 using boost::shared_ptr;
33 using boost::scoped_array;
34
35 ServerFinder* ServerFinder::_instance = 0;
36
37 ServerFinder::ServerFinder ()
38         : _disabled (false)
39         , _broadcast_thread (0)
40         , _listen_thread (0)
41 {
42         _broadcast_thread = new boost::thread (boost::bind (&ServerFinder::broadcast_thread, this));
43         _listen_thread = new boost::thread (boost::bind (&ServerFinder::listen_thread, this));
44 }
45
46 void
47 ServerFinder::broadcast_thread ()
48 {
49         boost::system::error_code error;
50         boost::asio::io_service io_service;
51         boost::asio::ip::udp::socket socket (io_service);
52         socket.open (boost::asio::ip::udp::v4(), error);
53         if (error) {
54                 throw NetworkError ("failed to set up broadcast socket");
55         }
56
57         socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
58         socket.set_option (boost::asio::socket_base::broadcast (true));
59
60         string const data = DCPOMATIC_HELLO;
61         
62         while (1) {
63                 if (Config::instance()->use_any_servers ()) {
64                         /* Broadcast to look for servers */
65                         boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1);
66                         socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
67                 }
68
69                 /* Query our `definite' servers (if there are any) */
70                 vector<string> servers = Config::instance()->servers ();
71                 for (vector<string>::const_iterator i = servers.begin(); i != servers.end(); ++i) {
72                         try {
73                                 boost::asio::ip::udp::resolver resolver (io_service);
74                                 boost::asio::ip::udp::resolver::query query (*i);
75                                 boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
76                                 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
77                         } catch (...) {
78
79                         }
80                 }
81                 
82                 dcpomatic_sleep (10);
83         }
84 }
85
86 void
87 ServerFinder::listen_thread ()
88 {
89         while (1) {
90                 shared_ptr<Socket> sock (new Socket (10));
91
92                 try {
93                         sock->accept (Config::instance()->server_port_base() + 1);
94                 } catch (std::exception& e) {
95                         continue;
96                 }
97
98                 uint32_t length = sock->read_uint32 ();
99                 scoped_array<char> buffer (new char[length]);
100                 sock->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
101                 
102                 stringstream s (buffer.get());
103                 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
104                 xml->read_stream (s);
105
106                 boost::mutex::scoped_lock lm (_mutex);
107
108                 string const ip = sock->socket().remote_endpoint().address().to_string ();
109                 list<ServerDescription>::const_iterator i = _servers.begin();
110                 while (i != _servers.end() && i->host_name() != ip) {
111                         ++i;
112                 }
113
114                 if (i == _servers.end ()) {
115                         ServerDescription sd (ip, xml->number_child<int> ("Threads"));
116                         _servers.push_back (sd);
117                         ui_signaller->emit (boost::bind (boost::ref (ServerFound), sd));
118                 }
119         }
120 }
121
122 void
123 ServerFinder::connect (boost::function<void (ServerDescription)> fn)
124 {
125         if (_disabled) {
126                 return;
127         }
128         
129         boost::mutex::scoped_lock lm (_mutex);
130
131         /* Emit the current list of servers */
132         for (list<ServerDescription>::iterator i = _servers.begin(); i != _servers.end(); ++i) {
133                 fn (*i);
134         }
135
136         ServerFound.connect (fn);
137 }
138
139 ServerFinder*
140 ServerFinder::instance ()
141 {
142         if (!_instance) {
143                 _instance = new ServerFinder ();
144         }
145
146         return _instance;
147 }
148
149         
150