diff options
| author | Carl Hetherington <cth@carlh.net> | 2013-11-06 16:43:01 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2013-11-06 16:43:01 +0000 |
| commit | 59602b67d0637817a156b7bd0fc05f96fe41dee5 (patch) | |
| tree | 3214f9f260796cf673c8bc69b069fd63b8a0889c /src/lib/server_finder.cc | |
| parent | 4c7416beb0efbf74868f756ddf8013f93c5841dc (diff) | |
Various bits of server tidying up.
Diffstat (limited to 'src/lib/server_finder.cc')
| -rw-r--r-- | src/lib/server_finder.cc | 83 |
1 files changed, 43 insertions, 40 deletions
diff --git a/src/lib/server_finder.cc b/src/lib/server_finder.cc index c0b554eee..56f52b7fc 100644 --- a/src/lib/server_finder.cc +++ b/src/lib/server_finder.cc @@ -27,36 +27,20 @@ using std::string; using std::stringstream; +using std::list; using boost::shared_ptr; using boost::scoped_array; +ServerFinder* ServerFinder::_instance = 0; + ServerFinder::ServerFinder () : _broadcast_thread (0) , _listen_thread (0) - , _terminate (false) { _broadcast_thread = new boost::thread (boost::bind (&ServerFinder::broadcast_thread, this)); _listen_thread = new boost::thread (boost::bind (&ServerFinder::listen_thread, this)); } -ServerFinder::~ServerFinder () -{ - { - boost::mutex::scoped_lock lm (_mutex); - _terminate = true; - } - - if (_broadcast_thread && _broadcast_thread->joinable ()) { - _broadcast_thread->join (); - } - delete _broadcast_thread; - - if (_listen_thread && _listen_thread->joinable ()) { - _listen_thread->join (); - } - delete _listen_thread; -} - void ServerFinder::broadcast_thread () { @@ -74,16 +58,8 @@ ServerFinder::broadcast_thread () boost::asio::ip::udp::endpoint end_point (boost::asio::ip::address_v4::broadcast(), Config::instance()->server_port_base() + 1); while (1) { - boost::mutex::scoped_lock lm (_mutex); - if (_terminate) { - socket.close (error); - return; - } - - string data = DCPOMATIC_HELLO; + string const data = DCPOMATIC_HELLO; socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point); - - lm.unlock (); dcpomatic_sleep (10); } } @@ -92,14 +68,6 @@ void ServerFinder::listen_thread () { while (1) { - { - /* See if we need to stop */ - boost::mutex::scoped_lock lm (_mutex); - if (_terminate) { - return; - } - } - shared_ptr<Socket> sock (new Socket (10)); try { @@ -116,9 +84,44 @@ ServerFinder::listen_thread () shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable")); xml->read_stream (s); - ui_signaller->emit (boost::bind (boost::ref (ServerFound), ServerDescription ( - sock->socket().remote_endpoint().address().to_string (), - xml->number_child<int> ("Threads") - ))); + boost::mutex::scoped_lock lm (_mutex); + + string const ip = sock->socket().remote_endpoint().address().to_string (); + list<ServerDescription>::const_iterator i = _servers.begin(); + while (i != _servers.end() && i->host_name() != ip) { + ++i; + } + + if (i == _servers.end ()) { + ServerDescription sd (ip, xml->number_child<int> ("Threads")); + _servers.push_back (sd); + ui_signaller->emit (boost::bind (boost::ref (ServerFound), sd)); + } } } + +void +ServerFinder::connect (boost::function<void (ServerDescription)> fn) +{ + boost::mutex::scoped_lock lm (_mutex); + + /* Emit the current list of servers */ + for (list<ServerDescription>::iterator i = _servers.begin(); i != _servers.end(); ++i) { + fn (*i); + } + + ServerFound.connect (fn); +} + +ServerFinder* +ServerFinder::instance () +{ + if (!_instance) { + _instance = new ServerFinder (); + } + + return _instance; +} + + + |
