Better updating of servers list when things change.
authorCarl Hetherington <cth@carlh.net>
Mon, 6 Jul 2015 12:37:35 +0000 (13:37 +0100)
committerCarl Hetherington <cth@carlh.net>
Mon, 6 Jul 2015 12:37:35 +0000 (13:37 +0100)
src/lib/config.cc
src/lib/config.h
src/lib/encoder.cc
src/lib/encoder.h
src/lib/server_finder.cc
src/lib/server_finder.h
src/wx/servers_list_dialog.cc
src/wx/servers_list_dialog.h

index ee38e98665994ce4c87380e400032b4dd35adf45..16a3849b51cc93da95af614b923d44bcfec5a8b7 100644 (file)
@@ -407,9 +407,9 @@ Config::drop ()
 }
 
 void
-Config::changed ()
+Config::changed (Property what)
 {
-       Changed ();
+       Changed (what);
 }
 
 void
index 0040591f165543c4b895c9171397aa91b7e95eaa..ad95bc3441fccc3344e82c4bb62c172589c1aae0 100644 (file)
@@ -59,6 +59,12 @@ public:
 
        boost::filesystem::path default_directory_or (boost::filesystem::path a) const;
 
+       enum Property {
+               USE_ANY_SERVERS,
+               SERVERS,
+               OTHER
+       };
+
        /** @return base port number to use for J2K encoding servers */
        int server_port_base () const {
                return _server_port_base;
@@ -66,7 +72,7 @@ public:
 
        void set_use_any_servers (bool u) {
                _use_any_servers = u;
-               changed ();
+               changed (USE_ANY_SERVERS);
        }
 
        bool use_any_servers () const {
@@ -76,7 +82,7 @@ public:
        /** @param s New list of servers */
        void set_servers (std::vector<std::string> s) {
                _servers = s;
-               changed ();
+               changed (SERVERS);
        }
 
        /** @return Host names / IP addresses of J2K encoding servers that should definitely be used */
@@ -395,8 +401,8 @@ public:
 
        void add_to_history (boost::filesystem::path p);
 
-       void changed ();
-       boost::signals2::signal<void ()> Changed;
+       void changed (Property p = OTHER);
+       boost::signals2::signal<void (Property)> Changed;
 
        void write () const;
 
index c3ad084419a071958be2873ac0a8c1c6a373d1d2..07c97c6747fa6a341e5a759ac370139398ae252c 100644 (file)
@@ -36,6 +36,7 @@
 #include "data.h"
 #include <libcxml/cxml.h>
 #include <boost/lambda/lambda.hpp>
+#include <boost/foreach.hpp>
 #include <iostream>
 
 #include "i18n.h"
@@ -66,7 +67,7 @@ Encoder::Encoder (shared_ptr<const Film> film, weak_ptr<Job> j, shared_ptr<Write
        , _terminate (false)
        , _writer (writer)
 {
-
+       servers_list_changed ();
 }
 
 Encoder::~Encoder ()
@@ -99,7 +100,7 @@ Encoder::begin ()
        _writer->set_encoder_threads (_threads.size ());
 
        if (!ServerFinder::instance()->disabled ()) {
-               _server_found_connection = ServerFinder::instance()->connect (boost::bind (&Encoder::server_found, this, _1));
+               _server_found_connection = ServerFinder::instance()->ServersListChanged.connect (boost::bind (&Encoder::servers_list_changed, this));
        }
 }
 
@@ -271,6 +272,7 @@ Encoder::terminate_threads ()
        }
 
        _threads.clear ();
+       _terminate = false;
 }
 
 void
@@ -362,7 +364,10 @@ catch (...)
 }
 
 void
-Encoder::server_found (ServerDescription s)
+Encoder::servers_list_changed ()
 {
-       add_worker_threads (s);
+       terminate_threads ();
+       BOOST_FOREACH (ServerDescription i, ServerFinder::instance()->servers ()) {
+               add_worker_threads (i);
+       }
 }
index 6bbdda4c5e23ea8722395623efd950293d749d13..85bc6ae99d57b823058d6cc95251cc955c416d82 100644 (file)
@@ -83,7 +83,7 @@ private:
        void encoder_thread (boost::optional<ServerDescription>);
        void terminate_threads ();
        void add_worker_threads (ServerDescription);
-       void server_found (ServerDescription);
+       void servers_list_changed ();
 
        /** Film that we are encoding */
        boost::shared_ptr<const Film> _film;
index ac4651657872418b7aa56f9aaedfa91e595599f9..a189ae8027dbcb44762b822d281ecc089335ca8e 100644 (file)
@@ -47,6 +47,7 @@ ServerFinder::ServerFinder ()
 {
        _broadcast_thread = new boost::thread (boost::bind (&ServerFinder::broadcast_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 ()
@@ -172,7 +173,7 @@ ServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Socket> so
                        boost::mutex::scoped_lock lm (_mutex);
                        _servers.push_back (sd);
                }
-               emit (boost::bind (boost::ref (ServerFound), sd));
+               emit (boost::bind (boost::ref (ServersListChanged)));
        }
 
        start_accept ();
@@ -190,19 +191,6 @@ ServerFinder::server_found (string ip) const
        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 ()
 {
@@ -219,3 +207,22 @@ ServerFinder::drop ()
        delete _instance;
        _instance = 0;
 }
+
+list<ServerDescription>
+ServerFinder::servers () const
+{
+       boost::mutex::scoped_lock lm (_mutex);
+       return _servers;
+}
+
+void
+ServerFinder::config_changed (Config::Property what)
+{
+       if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
+               {
+                       boost::mutex::scoped_lock lm (_mutex);
+                       _servers.clear ();
+               }
+               ServersListChanged ();
+       }
+}
index ec855938be4ddfd92e1f324194cbf6d8128b4e41..c43c2a422ee9c5e1dceeaa4d7962d0ad29cb1bf1 100644 (file)
 
 #include "server.h"
 #include "signaller.h"
+#include "config.h"
 #include <boost/signals2.hpp>
 
 class ServerFinder : public Signaller, public ExceptionStore
 {
 public:
-       boost::signals2::connection connect (boost::function<void (ServerDescription)>);
-
        static ServerFinder* instance ();
        static void drop ();
 
@@ -37,6 +36,11 @@ public:
                return _disabled;
        }
 
+       std::list<ServerDescription> servers () const;
+
+       /** Emitted whenever the list of servers changes */
+       boost::signals2::signal<void ()> ServersListChanged;
+
 private:
        ServerFinder ();
        ~ServerFinder ();
@@ -48,7 +52,7 @@ private:
        void start_accept ();
        void handle_accept (boost::system::error_code ec, boost::shared_ptr<Socket> socket);
 
-       boost::signals2::signal<void (ServerDescription)> ServerFound;
+       void config_changed (Config::Property what);
 
        bool _disabled;
 
index 13c6df70729746ba40d7556e14fa2e5d8fdd3c5f..b678da073d498f0e55ebc006fa89dfda85116b28 100644 (file)
 
 */
 
-#include <boost/lexical_cast.hpp>
-#include "lib/server_finder.h"
 #include "servers_list_dialog.h"
 #include "wx_util.h"
+#include "lib/server_finder.h"
+#include <boost/lexical_cast.hpp>
+#include <boost/foreach.hpp>
 
 using std::list;
 using std::string;
@@ -61,19 +62,24 @@ ServersListDialog::ServersListDialog (wxWindow* parent)
        s->Layout ();
        s->SetSizeHints (this);
 
-       _server_finder_connection = ServerFinder::instance()->connect (boost::bind (&ServersListDialog::server_found, this, _1));
+       _server_finder_connection = ServerFinder::instance()->ServersListChanged.connect (boost::bind (&ServersListDialog::servers_list_changed, this));
+       servers_list_changed ();
 }
 
 void
-ServersListDialog::server_found (ServerDescription s)
+ServersListDialog::servers_list_changed ()
 {
-       wxListItem list_item;
-       int const n = _list->GetItemCount ();
-       list_item.SetId (n);
-       _list->InsertItem (list_item);
+       _list->DeleteAllItems ();
+
+       int n = 0;
+       BOOST_FOREACH (ServerDescription i, ServerFinder::instance()->servers ()) {
+               wxListItem list_item;
+               list_item.SetId (n);
+               _list->InsertItem (list_item);
 
-       _list->SetItem (n, 0, std_to_wx (s.host_name ()));
-       _list->SetItem (n, 1, std_to_wx (lexical_cast<string> (s.threads ())));
+               _list->SetItem (n, 0, std_to_wx (i.host_name ()));
+               _list->SetItem (n, 1, std_to_wx (lexical_cast<string> (i.threads ())));
 
-       _servers.push_back (s);
+               ++n;
+       }
 }
index 3804d2a7e17f9f3ddecad977759333c3597e655d..60fcdb236bece157ebd11dcf47e29c23db6ec25f 100644 (file)
@@ -27,9 +27,8 @@ public:
        ServersListDialog (wxWindow *);
 
 private:
-       void server_found (ServerDescription);
+       void servers_list_changed ();
 
-       std::list<ServerDescription> _servers;
        wxListCtrl* _list;
 
        boost::signals2::scoped_connection _server_finder_connection;