Merge branch '2.0' of ssh://main.carlh.net/home/carl/git/dcpomatic into 2.0
[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 <dcp/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 #include "i18n.h"
30
31 using std::string;
32 using std::list;
33 using std::vector;
34 using std::cout;
35 using boost::shared_ptr;
36 using boost::scoped_array;
37 using dcp::raw_convert;
38
39 ServerFinder* ServerFinder::_instance = 0;
40
41 ServerFinder::ServerFinder ()
42         : _disabled (false)
43         , _broadcast_thread (0)
44         , _listen_thread (0)
45 {
46         _broadcast_thread = new boost::thread (boost::bind (&ServerFinder::broadcast_thread, this));
47         _listen_thread = new boost::thread (boost::bind (&ServerFinder::listen_thread, this));
48 }
49
50 void
51 ServerFinder::broadcast_thread ()
52 try
53 {
54         boost::system::error_code error;
55         boost::asio::io_service io_service;
56         boost::asio::ip::udp::socket socket (io_service);
57         socket.open (boost::asio::ip::udp::v4(), error);
58         if (error) {
59                 throw NetworkError ("failed to set up broadcast socket");
60         }
61
62         socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
63         socket.set_option (boost::asio::socket_base::broadcast (true));
64
65         string const data = DCPOMATIC_HELLO;
66         
67         while (true) {
68                 if (Config::instance()->use_any_servers ()) {
69                         /* Broadcast to look for servers */
70                         try {
71                                 boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1);
72                                 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
73                         } catch (...) {
74
75                         }
76                 }
77
78                 /* Query our `definite' servers (if there are any) */
79                 vector<string> servers = Config::instance()->servers ();
80                 for (vector<string>::const_iterator i = servers.begin(); i != servers.end(); ++i) {
81                         if (server_found (*i)) {
82                                 /* Don't bother asking a server that we already know about */
83                                 continue;
84                         }
85                         try {
86                                 boost::asio::ip::udp::resolver resolver (io_service);
87                                 boost::asio::ip::udp::resolver::query query (*i, raw_convert<string> (Config::instance()->server_port_base() + 1));
88                                 boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
89                                 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
90                         } catch (...) {
91
92                         }
93                 }
94                 
95                 dcpomatic_sleep (10);
96         }
97 }
98 catch (...)
99 {
100         store_current ();
101 }
102
103 void
104 ServerFinder::listen_thread ()
105 try
106 {
107         using namespace boost::asio::ip;
108
109         boost::asio::io_service io_service;
110         boost::scoped_ptr<tcp::acceptor> acceptor;
111         try {
112                 acceptor.reset (new tcp::acceptor (io_service, tcp::endpoint (tcp::v4(), Config::instance()->server_port_base() + 1)));
113         } catch (...) {
114                 boost::throw_exception (NetworkError (_("Could not listen for remote encode servers.  Perhaps another instance of DCP-o-matic is running.")));
115         }
116
117         while (true) {
118                 tcp::socket socket (io_service);
119                 acceptor->accept (socket);
120
121                 /* XXX: these reads should have timeouts, otherwise we will stop finding servers
122                    if one dies during this conversation
123                 */
124
125                 uint32_t length = 0;
126                 boost::asio::read (socket, boost::asio::buffer (&length, sizeof (uint32_t)));
127                 length = ntohl (length);
128
129                 scoped_array<char> buffer (new char[length]);
130                 boost::asio::read (socket, boost::asio::buffer (reinterpret_cast<uint8_t*> (buffer.get ()), length));
131                 
132                 string s (buffer.get());
133                 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
134                 xml->read_string (s);
135                 
136                 string const ip = socket.remote_endpoint().address().to_string ();
137                 if (!server_found (ip)) {
138                         ServerDescription sd (ip, xml->number_child<int> ("Threads"));
139                         _servers.push_back (sd);
140                         ui_signaller->emit (boost::bind (boost::ref (ServerFound), sd));
141                 }
142         }
143 }
144 catch (...)
145 {
146         store_current ();
147 }
148
149 bool
150 ServerFinder::server_found (string ip) const
151 {
152         boost::mutex::scoped_lock lm (_mutex);
153         list<ServerDescription>::const_iterator i = _servers.begin();
154         while (i != _servers.end() && i->host_name() != ip) {
155                 ++i;
156         }
157
158         return i != _servers.end ();
159 }
160
161 void
162 ServerFinder::connect (boost::function<void (ServerDescription)> fn)
163 {
164         if (_disabled) {
165                 return;
166         }
167         
168         boost::mutex::scoped_lock lm (_mutex);
169
170         /* Emit the current list of servers */
171         for (list<ServerDescription>::iterator i = _servers.begin(); i != _servers.end(); ++i) {
172                 fn (*i);
173         }
174
175         ServerFound.connect (fn);
176 }
177
178 ServerFinder*
179 ServerFinder::instance ()
180 {
181         if (!_instance) {
182                 _instance = new ServerFinder ();
183         }
184
185         return _instance;
186 }
187
188         
189