const fix; header guard.
[dcpomatic.git] / src / lib / server_finder.cc
index ecda385eb4782c69f0e09922840f61ecd44707a6..4b532f98129d4f70386040d9dfc182761973312f 100644 (file)
 #include "util.h"
 #include "config.h"
 #include "cross.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"
 
@@ -44,21 +46,30 @@ ServerFinder::ServerFinder ()
        , _search_thread (0)
        , _listen_thread (0)
        , _stop (false)
+{
+       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));
-       Config::instance()->Changed.connect (boost::bind (&ServerFinder::config_changed, this, _1));
 }
 
 ServerFinder::~ServerFinder ()
 {
        _stop = true;
 
-       _search_thread->interrupt ();
-       _search_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
@@ -106,11 +117,8 @@ try
                        }
                }
 
-               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 (...)
@@ -170,7 +178,7 @@ ServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> so
        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 (ServersListChanged)));
@@ -182,7 +190,7 @@ ServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> so
 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;
@@ -196,6 +204,7 @@ ServerFinder::instance ()
 {
        if (!_instance) {
                _instance = new ServerFinder ();
+               _instance->start ();
        }
 
        return _instance;
@@ -211,7 +220,7 @@ ServerFinder::drop ()
 list<ServerDescription>
 ServerFinder::servers () const
 {
-       boost::mutex::scoped_lock lm (_mutex);
+       boost::mutex::scoped_lock lm (_servers_mutex);
        return _servers;
 }
 
@@ -220,9 +229,16 @@ ServerFinder::config_changed (Config::Property what)
 {
        if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
                {
-                       boost::mutex::scoped_lock lm (_mutex);
+                       boost::mutex::scoped_lock lm (_servers_mutex);
                        _servers.clear ();
                }
                ServersListChanged ();
+               search_now ();
        }
 }
+
+void
+ServerFinder::search_now ()
+{
+       _search_condition.notify_all ();
+}