From: Carl Hetherington Date: Thu, 12 Apr 2018 23:56:11 +0000 (+0100) Subject: Update encoding server list when servers disappear (#1176). X-Git-Tag: v2.13.14~2 X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=commitdiff_plain;h=6dd6676700f830547e9e7c38781f09de5f2a1a00 Update encoding server list when servers disappear (#1176). --- diff --git a/ChangeLog b/ChangeLog index 5201319d6..ded429291 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ 2018-04-13 Carl Hetherington + * 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 diff --git a/src/lib/encode_server_description.h b/src/lib/encode_server_description.h index 864b0fdc1..0b9d72f44 100644 --- a/src/lib/encode_server_description.h +++ b/src/lib/encode_server_description.h @@ -21,6 +21,8 @@ #ifndef DCPOMATIC_ENCODE_SERVER_DESCRIPTION_H #define DCPOMATIC_ENCODE_SERVER_DESCRIPTION_H +#include + /** @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 diff --git a/src/lib/encode_server_finder.cc b/src/lib/encode_server_finder.cc index 1234dcd52..06a6a396b 100644 --- a/src/lib/encode_server_finder.cc +++ b/src/lib/encode_server_finder.cc @@ -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& servers, int since) +{ + bool removed = false; + list::iterator i = servers.begin(); + while (i != servers.end()) { + if (i->last_seen_seconds() > since) { + list::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_ptrread_string (s); string const ip = socket->socket().remote_endpoint().address().to_string (); - if (!server_found (ip)) { + optional::iterator> found = server_found (ip); + if (found) { + (*found)->set_seen (); + } else { EncodeServerDescription sd (ip, xml->number_child("Threads"), xml->optional_number_child("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::iterator> +EncodeServerFinder::server_found (string ip) { boost::mutex::scoped_lock lm (_servers_mutex); - list::const_iterator i = _good_servers.begin(); + list::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::iterator>(); } EncodeServerFinder* diff --git a/src/lib/encode_server_finder.h b/src/lib/encode_server_finder.h index 90031d27b..58dee7e64 100644 --- a/src/lib/encode_server_finder.h +++ b/src/lib/encode_server_finder.h @@ -63,7 +63,7 @@ private: void search_thread (); void listen_thread (); - bool server_found (std::string) const; + boost::optional::iterator> server_found (std::string); void start_accept (); void handle_accept (boost::system::error_code ec, boost::shared_ptr socket);