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