Update encoding server list when servers disappear (#1176).
authorCarl Hetherington <cth@carlh.net>
Thu, 12 Apr 2018 23:56:11 +0000 (00:56 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 12 Apr 2018 23:56:11 +0000 (00:56 +0100)
ChangeLog
src/lib/encode_server_description.h
src/lib/encode_server_finder.cc
src/lib/encode_server_finder.h

index 5201319..ded4292 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2018-04-13  Carl Hetherington  <cth@carlh.net>
 
+       * Update encoding server list when one goes away (#1176).
+
        * Add servers with bad server-link versions in the list (#982).
 
 2018-04-12  Carl Hetherington  <cth@carlh.net>
index 864b0fd..0b9d72f 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef DCPOMATIC_ENCODE_SERVER_DESCRIPTION_H
 #define DCPOMATIC_ENCODE_SERVER_DESCRIPTION_H
 
+#include <boost/date_time/posix_time/posix_time.hpp>
+
 /** @class EncodeServerDescription
  *  @brief Class to describe a server to which we can send encoding work.
  */
@@ -31,6 +33,7 @@ public:
                : _host_name ("")
                , _threads (1)
                , _link_version (0)
+               , _last_seen (boost::posix_time::second_clock::local_time())
        {}
 
        /** @param h Server host name or IP address in string form.
@@ -41,6 +44,7 @@ public:
                : _host_name (h)
                , _threads (t)
                , _link_version (l)
+               , _last_seen (boost::posix_time::second_clock::local_time())
        {}
 
        /* Default copy constructor is fine */
@@ -67,6 +71,14 @@ public:
                _threads = t;
        }
 
+       void set_seen () {
+               _last_seen = boost::posix_time::second_clock::local_time();
+       }
+
+       int last_seen_seconds () const {
+               return boost::posix_time::time_duration(boost::posix_time::second_clock::local_time() - _last_seen).total_seconds();
+       }
+
 private:
        /** server's host name */
        std::string _host_name;
@@ -74,6 +86,7 @@ private:
        int _threads;
        /** server link (i.e. protocol) version number */
        int _link_version;
+       boost::posix_time::ptime _last_seen;
 };
 
 #endif
index 1234dcd..06a6a39 100644 (file)
@@ -39,6 +39,7 @@ using std::cout;
 using boost::shared_ptr;
 using boost::scoped_array;
 using boost::weak_ptr;
+using boost::optional;
 using dcp::raw_convert;
 
 EncodeServerFinder* EncodeServerFinder::_instance = 0;
@@ -102,6 +103,25 @@ EncodeServerFinder::stop ()
        _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;
+}
+
 void
 EncodeServerFinder::search_thread ()
 try
@@ -118,6 +138,7 @@ try
         socket.set_option (boost::asio::socket_base::broadcast (true));
 
        string const data = DCPOMATIC_HELLO;
+       int const interval = 10;
 
        while (!_stop) {
                if (Config::instance()->use_any_servers ()) {
@@ -147,8 +168,18 @@ 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) {
+                               emit (boost::bind (boost::ref (ServersListChanged)));
+                       }
+               }
+
                boost::mutex::scoped_lock lm (_search_condition_mutex);
-               _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds (10));
+               _search_condition.timed_wait (lm, boost::get_system_time() + boost::posix_time::seconds (interval));
        }
 }
 catch (...)
@@ -207,7 +238,10 @@ EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Sock
        xml->read_string (s);
 
        string const ip = socket->socket().remote_endpoint().address().to_string ();
-       if (!server_found (ip)) {
+       optional<list<EncodeServerDescription>::iterator> found = server_found (ip);
+       if (found) {
+               (*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);
@@ -222,17 +256,17 @@ EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptr<Sock
        start_accept ();
 }
 
-bool
-EncodeServerFinder::server_found (string ip) const
+optional<list<EncodeServerDescription>::iterator>
+EncodeServerFinder::server_found (string ip)
 {
        boost::mutex::scoped_lock lm (_servers_mutex);
-       list<EncodeServerDescription>::const_iterator i = _good_servers.begin();
+       list<EncodeServerDescription>::iterator i = _good_servers.begin();
        while (i != _good_servers.end() && i->host_name() != ip) {
                ++i;
        }
 
        if (i != _good_servers.end()) {
-               return true;
+               return i;
        }
 
        i = _bad_servers.begin();
@@ -240,7 +274,11 @@ EncodeServerFinder::server_found (string ip) const
                ++i;
        }
 
-       return i != _bad_servers.end ();
+       if (i != _bad_servers.end()) {
+               return i;
+       }
+
+       return optional<list<EncodeServerDescription>::iterator>();
 }
 
 EncodeServerFinder*
index 90031d2..58dee7e 100644 (file)
@@ -63,7 +63,7 @@ private:
        void search_thread ();
        void listen_thread ();
 
-       bool server_found (std::string) const;
+       boost::optional<std::list<EncodeServerDescription>::iterator> server_found (std::string);
        void start_accept ();
        void handle_accept (boost::system::error_code ec, boost::shared_ptr<Socket> socket);