Use accept() properly when reading replies to server request broadcasts.
[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::list;
31 using std::vector;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::scoped_array;
35 using libdcp::raw_convert;
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 (true) {
66                 if (Config::instance()->use_any_servers ()) {
67                         /* Broadcast to look for servers */
68                         try {
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);
71                         } catch (...) {
72
73                         }
74                 }
75
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 */
81                                 continue;
82                         }
83                         try {
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);
88                         } catch (...) {
89
90                         }
91                 }
92                 
93                 dcpomatic_sleep (10);
94         }
95 }
96 catch (...)
97 {
98         store_current ();
99 }
100
101 void
102 ServerFinder::listen_thread ()
103 try
104 {
105         using namespace boost::asio::ip;
106
107         boost::asio::io_service io_service;
108         tcp::acceptor acceptor (io_service, tcp::endpoint (tcp::v4(), Config::instance()->server_port_base() + 1));
109
110         while (true) {
111                 tcp::socket socket (io_service);
112                 acceptor.accept (socket);
113
114                 /* XXX: does this deadline work with synchronous reads? */
115
116                 boost::asio::deadline_timer deadline (io_service);
117                 deadline.expires_from_now (boost::posix_time::seconds (10));
118
119                 uint32_t length = 0;
120                 boost::asio::read (socket, boost::asio::buffer (&length, sizeof (uint32_t)));
121                 length = ntohl (length);
122
123                 scoped_array<char> buffer (new char[length]);
124                 boost::asio::read (socket, boost::asio::buffer (reinterpret_cast<uint8_t*> (buffer.get ()), length));
125                 
126                 string s (buffer.get());
127                 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
128                 xml->read_string (s);
129                 
130                 string const ip = socket.remote_endpoint().address().to_string ();
131                 if (!server_found (ip)) {
132                         ServerDescription sd (ip, xml->number_child<int> ("Threads"));
133                         _servers.push_back (sd);
134                         ui_signaller->emit (boost::bind (boost::ref (ServerFound), sd));
135                 }
136         }
137 }
138 catch (...)
139 {
140         store_current ();
141 }
142
143 bool
144 ServerFinder::server_found (string ip) const
145 {
146         boost::mutex::scoped_lock lm (_mutex);
147         list<ServerDescription>::const_iterator i = _servers.begin();
148         while (i != _servers.end() && i->host_name() != ip) {
149                 ++i;
150         }
151
152         return i != _servers.end ();
153 }
154
155 void
156 ServerFinder::connect (boost::function<void (ServerDescription)> fn)
157 {
158         if (_disabled) {
159                 return;
160         }
161         
162         boost::mutex::scoped_lock lm (_mutex);
163
164         /* Emit the current list of servers */
165         for (list<ServerDescription>::iterator i = _servers.begin(); i != _servers.end(); ++i) {
166                 fn (*i);
167         }
168
169         ServerFound.connect (fn);
170 }
171
172 ServerFinder*
173 ServerFinder::instance ()
174 {
175         if (!_instance) {
176                 _instance = new ServerFinder ();
177         }
178
179         return _instance;
180 }
181
182         
183