Merge branch '1.0' of /home/carl/git/dvdomatic into 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 boost::shared_ptr;
31 using boost::scoped_array;
32
33 ServerFinder::ServerFinder ()
34         : _broadcast_thread (0)
35         , _listen_thread (0)
36         , _terminate (false)
37 {
38         _broadcast_thread = new boost::thread (boost::bind (&ServerFinder::broadcast_thread, this));
39         _listen_thread = new boost::thread (boost::bind (&ServerFinder::listen_thread, this));
40 }
41
42 ServerFinder::~ServerFinder ()
43 {
44         {
45                 boost::mutex::scoped_lock lm (_mutex);
46                 _terminate = true;
47         }
48         
49         if (_broadcast_thread && _broadcast_thread->joinable ()) {
50                 _broadcast_thread->join ();
51         }
52         delete _broadcast_thread;
53
54         if (_listen_thread && _listen_thread->joinable ()) {
55                 _listen_thread->join ();
56         }
57         delete _listen_thread;
58 }
59
60 void
61 ServerFinder::broadcast_thread ()
62 {
63         boost::system::error_code error;
64         boost::asio::io_service io_service;
65         boost::asio::ip::udp::socket socket (io_service);
66         socket.open (boost::asio::ip::udp::v4(), error);
67         if (error) {
68                 throw NetworkError ("failed to set up broadcast socket");
69         }
70
71         socket.set_option (boost::asio::ip::udp::socket::reuse_address (true));
72         socket.set_option (boost::asio::socket_base::broadcast (true));
73         
74         boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1);            
75
76         while (1) {
77                 boost::mutex::scoped_lock lm (_mutex);
78                 if (_terminate) {
79                         socket.close (error);
80                         return;
81                 }
82                 
83                 string data = DCPOMATIC_HELLO;
84                 socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point);
85
86                 lm.unlock ();
87                 dcpomatic_sleep (10);
88         }
89 }
90
91 void
92 ServerFinder::listen_thread ()
93 {
94         while (1) {
95                 {
96                         /* See if we need to stop */
97                         boost::mutex::scoped_lock lm (_mutex);
98                         if (_terminate) {
99                                 return;
100                         }
101                 }
102
103                 shared_ptr<Socket> sock (new Socket (10));
104
105                 try {
106                         sock->accept (Config::instance()->server_port_base() + 1);
107                 } catch (std::exception& e) {
108                         continue;
109                 }
110
111                 uint32_t length = sock->read_uint32 ();
112                 scoped_array<char> buffer (new char[length]);
113                 sock->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
114                 
115                 stringstream s (buffer.get());
116                 shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
117                 xml->read_stream (s);
118
119                 ui_signaller->emit (boost::bind (boost::ref (ServerFound), ServerDescription (
120                                                          sock->socket().remote_endpoint().address().to_string (),
121                                                          xml->number_child<int> ("Threads")
122                                                          )));
123         }
124 }