No-op; fix GPL address and use the explicit-program-name version.
[dcpomatic.git] / src / lib / encode_server_finder.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "encode_server_finder.h"
22 #include "exceptions.h"
23 #include "util.h"
24 #include "config.h"
25 #include "cross.h"
26 #include "encode_server_description.h"
27 #include "dcpomatic_socket.h"
28 #include "raw_convert.h"
29 #include <libcxml/cxml.h>
30 #include <boost/lambda/lambda.hpp>
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::list;
37 using std::vector;
38 using std::cout;
39 using boost::shared_ptr;
40 using boost::scoped_array;
41 using boost::weak_ptr;
42
43 EncodeServerFinder* EncodeServerFinder::_instance = 0;
44
45 EncodeServerFinder::EncodeServerFinder ()
46         : _disabled (false)
47         , _search_thread (0)
48         , _listen_thread (0)
49         , _stop (false)
50 {
51         Config::instance()->Changed.connect (boost::bind (&EncodeServerFinder::config_changed, this, _1));
52 }
53
54 void
55 EncodeServerFinder::start ()
56 {
57         _search_thread = new boost::thread (boost::bind (&EncodeServerFinder::search_thread, this));
58         _listen_thread = new boost::thread (boost::bind (&EncodeServerFinder::listen_thread, this));
59 }
60
61 EncodeServerFinder::~EncodeServerFinder ()
62 {
63         _stop = true;
64
65         _search_condition.notify_all ();
66         if (_search_thread) {
67                 DCPOMATIC_ASSERT (_search_thread->joinable ());
68                 _search_thread->join ();
69         }
70
71         _listen_io_service.stop ();
72         if (_listen_thread) {
73                 DCPOMATIC_ASSERT (_listen_thread->joinable ());
74                 _listen_thread->join ();
75         }
76 }
77
78 void
79 EncodeServerFinder::search_thread ()
80 try
81 {
82         boost::system::error_code error;
83         boost::asio::io_service io_service;
84         boost::asio::ip::udp::socket socket (io_service);
85         socket.open (boost::asio::ip::udp::v4(), error);
86         if (error) {
87                 throw NetworkError ("failed to set up broadcast socket");
88         }
89
90         socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
91         socket.set_option (boost::asio::socket_base::broadcast (true));
92
93         string const data = DCPOMATIC_HELLO;
94
95         while (!_stop) {
96                 if (Config::instance()->use_any_servers ()) {
97                         /* Broadcast to look for servers */
98                         try {
99                                 boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1);
100                                 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
101                         } catch (...) {
102
103                         }
104                 }
105
106                 /* Query our `definite' servers (if there are any) */
107                 vector<string> servers = Config::instance()->servers ();
108                 for (vector<string>::const_iterator i = servers.begin(); i != servers.end(); ++i) {
109                         if (server_found (*i)) {
110                                 /* Don't bother asking a server that we already know about */
111                                 continue;
112                         }
113                         try {
114                                 boost::asio::ip::udp::resolver resolver (io_service);
115                                 boost::asio::ip::udp::resolver::query query (*i, raw_convert<string> (Config::instance()->server_port_base() + 1));
116                                 boost::asio::ip::udp::endpoint end_point (*resolver.resolve (query));
117                                 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
118                         } catch (...) {
119
120                         }
121                 }
122
123                 boost::mutex::scoped_lock lm (_search_condition_mutex);
124                 _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds (10));
125         }
126 }
127 catch (...)
128 {
129         store_current ();
130 }
131
132 void
133 EncodeServerFinder::listen_thread ()
134 try {
135         using namespace boost::asio::ip;
136
137         try {
138                 _listen_acceptor.reset (new tcp::acceptor (_listen_io_service, tcp::endpoint (tcp::v4(), Config::instance()->server_port_base() + 1)));
139         } catch (...) {
140                 boost::throw_exception (NetworkError (_("Could not listen for remote encode servers.  Perhaps another instance of DCP-o-matic is running.")));
141         }
142
143         start_accept ();
144         _listen_io_service.run ();
145 }
146 catch (...)
147 {
148         store_current ();
149 }
150
151 void
152 EncodeServerFinder::start_accept ()
153 {
154         shared_ptr<Socket> socket (new Socket ());
155         _listen_acceptor->async_accept (
156                 socket->socket(),
157                 boost::bind (&EncodeServerFinder::handle_accept, this, boost::asio::placeholders::error, socket)
158                 );
159 }
160
161 void
162 EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> socket)
163 {
164         if (ec) {
165                 start_accept ();
166                 return;
167         }
168
169         uint32_t length;
170         socket->read (reinterpret_cast<uint8_t*> (&length), sizeof (uint32_t));
171         length = ntohl (length);
172
173         scoped_array<char> buffer (new char[length]);
174         socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
175
176         string s (buffer.get());
177         shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
178         xml->read_string (s);
179
180         string const ip = socket->socket().remote_endpoint().address().to_string ();
181         if (!server_found (ip) && xml->optional_number_child<int>("Version").get_value_or (0) == SERVER_LINK_VERSION) {
182                 EncodeServerDescription sd (ip, xml->number_child<int> ("Threads"));
183                 {
184                         boost::mutex::scoped_lock lm (_servers_mutex);
185                         _servers.push_back (sd);
186                 }
187                 emit (boost::bind (boost::ref (ServersListChanged)));
188         }
189
190         start_accept ();
191 }
192
193 bool
194 EncodeServerFinder::server_found (string ip) const
195 {
196         boost::mutex::scoped_lock lm (_servers_mutex);
197         list<EncodeServerDescription>::const_iterator i = _servers.begin();
198         while (i != _servers.end() && i->host_name() != ip) {
199                 ++i;
200         }
201
202         return i != _servers.end ();
203 }
204
205 EncodeServerFinder*
206 EncodeServerFinder::instance ()
207 {
208         if (!_instance) {
209                 _instance = new EncodeServerFinder ();
210                 _instance->start ();
211         }
212
213         return _instance;
214 }
215
216 void
217 EncodeServerFinder::drop ()
218 {
219         delete _instance;
220         _instance = 0;
221 }
222
223 list<EncodeServerDescription>
224 EncodeServerFinder::servers () const
225 {
226         boost::mutex::scoped_lock lm (_servers_mutex);
227         return _servers;
228 }
229
230 void
231 EncodeServerFinder::config_changed (Config::Property what)
232 {
233         if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
234                 {
235                         boost::mutex::scoped_lock lm (_servers_mutex);
236                         _servers.clear ();
237                 }
238                 ServersListChanged ();
239                 _search_condition.notify_all ();
240         }
241 }