#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.
*/
: _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.
: _host_name (h)
, _threads (t)
, _link_version (l)
+ , _last_seen (boost::posix_time::second_clock::local_time())
{}
/* Default copy constructor is fine */
_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;
int _threads;
/** server link (i.e. protocol) version number */
int _link_version;
+ boost::posix_time::ptime _last_seen;
};
#endif
using boost::shared_ptr;
using boost::scoped_array;
using boost::weak_ptr;
+using boost::optional;
using dcp::raw_convert;
EncodeServerFinder* EncodeServerFinder::_instance = 0;
_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
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 ()) {
}
}
+ /* 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 (...)
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);
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();
++i;
}
- return i != _bad_servers.end ();
+ if (i != _bad_servers.end()) {
+ return i;
+ }
+
+ return optional<list<EncodeServerDescription>::iterator>();
}
EncodeServerFinder*