/*
- Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
*/
+/** @file src/lib/server_finder.h
+ * @brief ServerFinder class.
+ */
+
+#include "signaller.h"
+#include "server_description.h"
+#include "config.h"
+#include "exception_store.h"
#include <boost/signals2.hpp>
-#include "server.h"
+#include <boost/thread/condition.hpp>
+
+class Socket;
-class ServerFinder
+class ServerFinder : public Signaller, public ExceptionStore
{
public:
+ static ServerFinder* instance ();
+ static void drop ();
+
+ void disable () {
+ _disabled = true;
+ }
+
+ bool disabled () const {
+ return _disabled;
+ }
+
+ std::list<ServerDescription> servers () const;
+
+ /** Emitted whenever the list of servers changes */
+ boost::signals2::signal<void ()> ServersListChanged;
+
+private:
ServerFinder ();
~ServerFinder ();
- boost::signals2::signal<void (ServerDescription)> ServerFound;
+ void start ();
-private:
- void broadcast_thread ();
+ void search_thread ();
void listen_thread ();
-
- /** A thread to periodically issue broadcasts to find encoding servers */
- boost::thread* _broadcast_thread;
+
+ bool server_found (std::string) const;
+ void start_accept ();
+ void handle_accept (boost::system::error_code ec, boost::shared_ptr<Socket> socket);
+
+ void config_changed (Config::Property what);
+ void search_now ();
+
+ bool _disabled;
+
+ /** Thread to periodically issue broadcasts and requests to find encoding servers */
+ boost::thread* _search_thread;
+ /** Thread to listen to the responses from servers */
boost::thread* _listen_thread;
- bool _terminate;
- boost::mutex _mutex;
+ std::list<ServerDescription> _servers;
+ mutable boost::mutex _servers_mutex;
+
+ boost::asio::io_service _listen_io_service;
+ boost::shared_ptr<boost::asio::ip::tcp::acceptor> _listen_acceptor;
+ bool _stop;
+
+ boost::condition _search_condition;
+ boost::mutex _search_condition_mutex;
+
+ static ServerFinder* _instance;
};