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