Tidy up to use one list of servers.
authorCarl Hetherington <cth@carlh.net>
Fri, 13 Apr 2018 00:09:10 +0000 (01:09 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 13 Apr 2018 00:09:10 +0000 (01:09 +0100)
src/lib/encode_server_description.h
src/lib/encode_server_finder.cc
src/lib/encode_server_finder.h
src/lib/j2k_encoder.cc
src/tools/dcpomatic_cli.cc
src/wx/servers_list_dialog.cc

index 0b9d72f..f60051b 100644 (file)
@@ -21,6 +21,7 @@
 #ifndef DCPOMATIC_ENCODE_SERVER_DESCRIPTION_H
 #define DCPOMATIC_ENCODE_SERVER_DESCRIPTION_H
 
+#include "types.h"
 #include <boost/date_time/posix_time/posix_time.hpp>
 
 /** @class EncodeServerDescription
@@ -59,8 +60,8 @@ public:
                return _threads;
        }
 
-       int link_version () const {
-               return _link_version;
+       bool current_link_version () const {
+               return _link_version == SERVER_LINK_VERSION;
        }
 
        void set_host_name (std::string n) {
index 06a6a39..1837101 100644 (file)
@@ -99,27 +99,7 @@ EncodeServerFinder::stop ()
        _listen_thread = 0;
 
        boost::mutex::scoped_lock lm (_servers_mutex);
-       _good_servers.clear ();
-       _bad_servers.clear ();
-}
-
-static bool
-remove_missing (list<EncodeServerDescription>& servers, int since)
-{
-       bool removed = false;
-       list<EncodeServerDescription>::iterator i = servers.begin();
-       while (i != servers.end()) {
-               if (i->last_seen_seconds() > since) {
-                       list<EncodeServerDescription>::iterator j = i;
-                       ++j;
-                       servers.erase (i);
-                       i = j;
-                       removed = true;
-               } else {
-                       ++i;
-               }
-       }
-       return removed;
+       _servers.clear ();
 }
 
 void
@@ -171,9 +151,22 @@ try
                /* Discard servers that we haven't seen for a while */
                {
                        boost::mutex::scoped_lock lm (_servers_mutex);
-                       bool g = remove_missing(_good_servers, 2 * interval);
-                       bool b = remove_missing(_bad_servers, 2 * interval);
-                       if (g || b) {
+
+                       bool removed = false;
+                       list<EncodeServerDescription>::iterator i = _servers.begin();
+                       while (i != _servers.end()) {
+                               if (i->last_seen_seconds() > 2 * interval) {
+                                       list<EncodeServerDescription>::iterator j = i;
+                                       ++j;
+                                       _servers.erase (i);
+                                       i = j;
+                                       removed = true;
+                               } else {
+                                       ++i;
+                               }
+                       }
+
+                       if (removed) {
                                emit (boost::bind (boost::ref (ServersListChanged)));
                        }
                }
@@ -243,13 +236,8 @@ EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Sock
                (*found)->set_seen ();
        } else {
                EncodeServerDescription sd (ip, xml->number_child<int>("Threads"), xml->optional_number_child<int>("Version").get_value_or(0));
-               if (sd.link_version() == SERVER_LINK_VERSION) {
-                       boost::mutex::scoped_lock lm (_servers_mutex);
-                       _good_servers.push_back (sd);
-               } else {
-                       boost::mutex::scoped_lock lm (_servers_mutex);
-                       _bad_servers.push_back (sd);
-               }
+               boost::mutex::scoped_lock lm (_servers_mutex);
+               _servers.push_back (sd);
                emit (boost::bind (boost::ref (ServersListChanged)));
        }
 
@@ -260,21 +248,12 @@ optional<list<EncodeServerDescription>::iterator>
 EncodeServerFinder::server_found (string ip)
 {
        boost::mutex::scoped_lock lm (_servers_mutex);
-       list<EncodeServerDescription>::iterator i = _good_servers.begin();
-       while (i != _good_servers.end() && i->host_name() != ip) {
+       list<EncodeServerDescription>::iterator i = _servers.begin();
+       while (i != _servers.end() && i->host_name() != ip) {
                ++i;
        }
 
-       if (i != _good_servers.end()) {
-               return i;
-       }
-
-       i = _bad_servers.begin();
-       while (i != _bad_servers.end() && i->host_name() != ip) {
-               ++i;
-       }
-
-       if (i != _bad_servers.end()) {
+       if (i != _servers.end()) {
                return i;
        }
 
@@ -300,17 +279,10 @@ EncodeServerFinder::drop ()
 }
 
 list<EncodeServerDescription>
-EncodeServerFinder::good_servers () const
-{
-       boost::mutex::scoped_lock lm (_servers_mutex);
-       return _good_servers;
-}
-
-list<EncodeServerDescription>
-EncodeServerFinder::bad_servers () const
+EncodeServerFinder::servers () const
 {
        boost::mutex::scoped_lock lm (_servers_mutex);
-       return _bad_servers;
+       return _servers;
 }
 
 void
@@ -319,8 +291,7 @@ EncodeServerFinder::config_changed (Config::Property what)
        if (what == Config::USE_ANY_SERVERS || what == Config::SERVERS) {
                {
                        boost::mutex::scoped_lock lm (_servers_mutex);
-                       _good_servers.clear ();
-                       _bad_servers.clear ();
+                       _servers.clear ();
                }
                ServersListChanged ();
                _search_condition.notify_all ();
index 58dee7e..abfcc6d 100644 (file)
@@ -48,8 +48,7 @@ public:
 
        void stop ();
 
-       std::list<EncodeServerDescription> good_servers () const;
-       std::list<EncodeServerDescription> bad_servers () const;
+       std::list<EncodeServerDescription> servers () const;
 
        /** Emitted whenever the list of servers changes */
        boost::signals2::signal<void ()> ServersListChanged;
@@ -74,11 +73,9 @@ private:
        /** Thread to listen to the responses from servers */
        boost::thread* _listen_thread;
 
-       /** Available servers with the correct link version */
-       std::list<EncodeServerDescription> _good_servers;
-       /** Available servers with an incorrect link version */
-       std::list<EncodeServerDescription> _bad_servers;
-       /** Mutex for _good_servers and _bad_servers */
+       /** Available servers */
+       std::list<EncodeServerDescription> _servers;
+       /** Mutex for _servers */
        mutable boost::mutex _servers_mutex;
 
        boost::asio::io_service _listen_io_service;
index bbd602d..e62e708 100644 (file)
@@ -412,7 +412,11 @@ J2KEncoder::servers_list_changed ()
                }
        }
 
-       BOOST_FOREACH (EncodeServerDescription i, EncodeServerFinder::instance()->good_servers ()) {
+       BOOST_FOREACH (EncodeServerDescription i, EncodeServerFinder::instance()->servers()) {
+               if (!i.current_link_version()) {
+                       continue;
+               }
+
                LOG_GENERAL (N_("Adding %1 worker threads for remote %2"), i.threads(), i.host_name ());
                for (int j = 0; j < i.threads(); ++j) {
                        _threads.push_back (new boost::thread (boost::bind (&J2KEncoder::encoder_thread, this, i)));
index 52a113c..788594f 100644 (file)
@@ -121,7 +121,7 @@ list_servers ()
 {
        while (true) {
                int N = 0;
-               list<EncodeServerDescription> servers = EncodeServerFinder::instance()->good_servers ();
+               list<EncodeServerDescription> servers = EncodeServerFinder::instance()->servers();
 
                /* This is a bit fiddly because we want to list configured servers that are down as well
                   as all those (configured and found by broadcast) that are up.
@@ -144,7 +144,7 @@ list_servers ()
                                optional<int> threads;
                                list<EncodeServerDescription>::iterator j = servers.begin ();
                                while (j != servers.end ()) {
-                                       if (i == j->host_name()) {
+                                       if (i == j->host_name() && j->current_link_version()) {
                                                threads = j->threads();
                                                list<EncodeServerDescription>::iterator tmp = j;
                                                ++tmp;
@@ -164,14 +164,13 @@ list_servers ()
 
                        /* Now report any left that have been found by broadcast */
                        BOOST_FOREACH (EncodeServerDescription const & i, servers) {
-                               cout << std::left << setw(24) << i.host_name() << " UP     " << i.threads() << "\n";
+                               if (i.current_link_version()) {
+                                       cout << std::left << setw(24) << i.host_name() << " UP     " << i.threads() << "\n";
+                               } else {
+                                       cout << std::left << setw(24) << i.host_name() << " bad version\n";
+                               }
                                ++N;
                        }
-
-                       /* And those that have a bad version */
-                       BOOST_FOREACH (EncodeServerDescription i, EncodeServerFinder::instance()->good_servers()) {
-                               cout << std::left << setw(24) << i.host_name() << " bad version\n";
-                       }
                }
 
                dcpomatic_sleep (1);
index e483991..37a1433 100644 (file)
@@ -77,25 +77,17 @@ ServersListDialog::servers_list_changed ()
 
        int n = 0;
 
-       BOOST_FOREACH (EncodeServerDescription i, EncodeServerFinder::instance()->good_servers()) {
+       BOOST_FOREACH (EncodeServerDescription i, EncodeServerFinder::instance()->servers()) {
                wxListItem list_item;
                list_item.SetId (n);
                _list->InsertItem (list_item);
 
                _list->SetItem (n, 0, std_to_wx (i.host_name ()));
-               _list->SetItem (n, 1, std_to_wx (lexical_cast<string> (i.threads ())));
-
-               ++n;
-       }
-
-       BOOST_FOREACH (EncodeServerDescription i, EncodeServerFinder::instance()->bad_servers()) {
-               wxListItem list_item;
-               list_item.SetId (n);
-               _list->InsertItem (list_item);
-
-               _list->SetItem (n, 0, std_to_wx (i.host_name ()));
-               _list->SetItem (n, 1, _("Incorrect version"));
-
+               if (i.current_link_version()) {
+                       _list->SetItem (n, 1, std_to_wx (lexical_cast<string> (i.threads ())));
+               } else {
+                       _list->SetItem (n, 1, _("Incorrect version"));
+               }
                ++n;
        }
 }