diff options
| author | Carl Hetherington <cth@carlh.net> | 2018-04-13 00:56:11 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2018-04-13 00:56:11 +0100 |
| commit | 6dd6676700f830547e9e7c38781f09de5f2a1a00 (patch) | |
| tree | 83591ef1c19ff60b7fd99213f372b61c7eb2ddfe /src/lib | |
| parent | ed0b3ee0c5a0ba11d3a1a1dfee8e71238bcab4bd (diff) | |
Update encoding server list when servers disappear (#1176).
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/encode_server_description.h | 13 | ||||
| -rw-r--r-- | src/lib/encode_server_finder.cc | 52 | ||||
| -rw-r--r-- | src/lib/encode_server_finder.h | 2 |
3 files changed, 59 insertions, 8 deletions
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 <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 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<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* 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<std::list<EncodeServerDescription>::iterator> server_found (std::string); void start_accept (); void handle_accept (boost::system::error_code ec, boost::shared_ptr<Socket> socket); |
