#include "util.h"
#include "config.h"
#include "cross.h"
-#include "ui_signaller.h"
+#include "server_description.h"
#include "dcpomatic_socket.h"
#include "raw_convert.h"
#include <libcxml/cxml.h>
#include <boost/lambda/lambda.hpp>
+#include <iostream>
#include "i18n.h"
ServerFinder::ServerFinder ()
: _disabled (false)
- , _broadcast_thread (0)
+ , _search_thread (0)
, _listen_thread (0)
, _stop (false)
{
- _broadcast_thread = new boost::thread (boost::bind (&ServerFinder::broadcast_thread, this));
+ Config::instance()->Changed.connect (boost::bind (&ServerFinder::config_changed, this, _1));
+}
+
+void
+ServerFinder::start ()
+{
+ _search_thread = new boost::thread (boost::bind (&ServerFinder::search_thread, this));
_listen_thread = new boost::thread (boost::bind (&ServerFinder::listen_thread, this));
}
{
_stop = true;
- _broadcast_thread->interrupt ();
- _broadcast_thread->join ();
+ _search_condition.notify_all ();
+ if (_search_thread) {
+ _search_thread->join ();
+ }
_listen_io_service.stop ();
- _listen_thread->join ();
+ if (_listen_thread) {
+ _listen_thread->join ();
+ }
}
void
-ServerFinder::broadcast_thread ()
+ServerFinder::search_thread ()
try
{
boost::system::error_code error;
socket.set_option (boost::asio::socket_base::broadcast (true));
string const data = DCPOMATIC_HELLO;
-
+
while (!_stop) {
if (Config::instance()->use_any_servers ()) {
/* Broadcast to look for servers */
}
}
- try {
- boost::thread::sleep (boost::get_system_time() + boost::posix_time::seconds (10));
- } catch (boost::thread_interrupted& e) {
- return;
- }
+ boost::mutex::scoped_lock lm (_search_condition_mutex);
+ _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds (10));
}
}
catch (...)
start_accept ();
return;
}
-
+
uint32_t length;
socket->read (reinterpret_cast<uint8_t*> (&length), sizeof (uint32_t));
length = ntohl (length);
-
+
scoped_array<char> buffer (new char[length]);
socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
-
+
string s (buffer.get());
shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable"));
xml->read_string (s);
-
+
string const ip = socket->socket().remote_endpoint().address().to_string ();
- if (!server_found (ip)) {
+ if (!server_found (ip) && xml->optional_number_child<int>("Version").get_value_or (0) == SERVER_LINK_VERSION) {
ServerDescription sd (ip, xml->number_child<int> ("Threads"));
{
- boost::mutex::scoped_lock lm (_mutex);
+ boost::mutex::scoped_lock lm (_servers_mutex);
_servers.push_back (sd);
}
- emit (boost::bind (boost::ref (ServerFound), sd));
+ emit (boost::bind (boost::ref (ServersListChanged)));
}
start_accept ();
bool
ServerFinder::server_found (string ip) const
{
- boost::mutex::scoped_lock lm (_mutex);
+ boost::mutex::scoped_lock lm (_servers_mutex);
list<ServerDescription>::const_iterator i = _servers.begin();
while (i != _servers.end() && i->host_name() != ip) {
++i;
return i != _servers.end ();
}
-boost::signals2::connection
-ServerFinder::connect (boost::function<void (ServerDescription)> fn)
-{
- boost::mutex::scoped_lock lm (_mutex);
-
- /* Emit the current list of servers */
- for (list<ServerDescription>::iterator i = _servers.begin(); i != _servers.end(); ++i) {
- fn (*i);
- }
-
- return ServerFound.connect (fn);
-}
-
ServerFinder*
ServerFinder::instance ()
{
if (!_instance) {
_instance = new ServerFinder ();
+ _instance->start ();
}
return _instance;
delete _instance;
_instance = 0;
}
+
+list<ServerDescription>
+ServerFinder::servers () const
+{
+ boost::mutex::scoped_lock lm (_servers_mutex);
+ return _servers;
+}
+
+void
+ServerFinder::config_changed (Config::Property what)
+{
+ if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
+ {
+ boost::mutex::scoped_lock lm (_servers_mutex);
+ _servers.clear ();
+ }
+ ServersListChanged ();
+ search_now ();
+ }
+}
+
+void
+ServerFinder::search_now ()
+{
+ _search_condition.notify_all ();
+}