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