Encoder::add_worker_threads (ServerDescription d)
{
for (int i = 0; i < d.threads(); ++i) {
- _threads.push_back (
- make_pair (d, new boost::thread (boost::bind (&Encoder::encoder_thread, this, d)))
- );
+ _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, d)));
}
}
Encoder::process_begin ()
{
for (int i = 0; i < Config::instance()->num_local_encoding_threads (); ++i) {
- _threads.push_back (
- make_pair (
- optional<ServerDescription> (),
- new boost::thread (boost::bind (&Encoder::encoder_thread, this, optional<ServerDescription> ()))
- )
- );
+ _threads.push_back (new boost::thread (boost::bind (&Encoder::encoder_thread, this, optional<ServerDescription> ())));
}
vector<ServerDescription> servers = Config::instance()->servers ();
}
_writer.reset (new Writer (_film, _job));
- _server_finder.reset (new ServerFinder ());
- _server_finder->ServerFound.connect (boost::bind (&Encoder::server_found, this, _1));
+ ServerFinder::instance()->connect (boost::bind (&Encoder::server_found, this, _1));
}
_condition.notify_all ();
}
- for (ThreadList::iterator i = _threads.begin(); i != _threads.end(); ++i) {
- if (i->second->joinable ()) {
- i->second->join ();
+ for (list<boost::thread *>::iterator i = _threads.begin(); i != _threads.end(); ++i) {
+ if ((*i)->joinable ()) {
+ (*i)->join ();
}
- delete i->second;
+ delete *i;
}
_threads.clear ();
void
Encoder::server_found (ServerDescription s)
{
- /* See if we already know about this server */
- boost::mutex::scoped_lock lm (_mutex);
- ThreadList::iterator i = _threads.begin();
- while (i != _threads.end() && (!i->first || i->first.get().host_name() != s.host_name())) {
- ++i;
- }
-
- if (i == _threads.end ()) {
- add_worker_threads (s);
- }
+ add_worker_threads (s);
}
bool _have_a_real_frame[EYES_COUNT];
bool _terminate;
std::list<boost::shared_ptr<DCPVideoFrame> > _queue;
- typedef std::list<std::pair<boost::optional<ServerDescription>, boost::thread *> > ThreadList;
- ThreadList _threads;
+ std::list<boost::thread *> _threads;
mutable boost::mutex _mutex;
boost::condition _condition;
boost::shared_ptr<Writer> _writer;
- boost::shared_ptr<ServerFinder> _server_finder;
};
#endif
{
_log->log (String::compose ("Server starting with %1 threads", num_threads));
if (_verbose) {
- cout << "DCP-o-matic server started with " << num_threads << " threads.\n";
+ cout << "DCP-o-matic server starting with " << num_threads << " threads.\n";
}
for (int i = 0; i < num_threads; ++i) {
void
Server::broadcast_thread ()
+try
{
boost::asio::io_service io_service;
io_service.run ();
}
+catch (...)
+{
+ store_current ();
+}
void
Server::broadcast_received ()
#include <boost/optional.hpp>
#include <libxml++/libxml++.h>
#include "log.h"
+#include "exceptions.h"
class Socket;
int _threads;
};
-class Server : public boost::noncopyable
+class Server : public ExceptionStore, public boost::noncopyable
{
public:
Server (boost::shared_ptr<Log> log, bool verbose);
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 ()
{
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);
}
}
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 {
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;
+}
+
+
+
class ServerFinder
{
public:
- ServerFinder ();
- ~ServerFinder ();
+ void connect (boost::function<void (ServerDescription)>);
- boost::signals2::signal<void (ServerDescription)> ServerFound;
+ static ServerFinder* instance ();
private:
+ ServerFinder ();
+
void broadcast_thread ();
void listen_thread ();
+
+ boost::signals2::signal<void (ServerDescription)> ServerFound;
- /** A thread to periodically issue broadcasts to find encoding servers */
+ /** Thread to periodically issue broadcasts to find encoding servers */
boost::thread* _broadcast_thread;
+ /** Thread to listen to the responses from servers */
boost::thread* _listen_thread;
- bool _terminate;
+ std::list<ServerDescription> _servers;
boost::mutex _mutex;
+
+ static ServerFinder* _instance;
};
Scaler::setup_scalers ();
shared_ptr<FileLog> log (new FileLog ("dcpomatic_server_cli.log"));
Server server (log, verbose);
- server.run (num_threads);
+ try {
+ server.run (num_threads);
+ } catch (boost::system::system_error e) {
+ if (e.code() == boost::system::errc::address_in_use) {
+ cerr << argv[0] << ": address already in use. Is another DCP-o-matic server instance already running?\n";
+ exit (EXIT_FAILURE);
+ }
+ cerr << argv[0] << ": " << e.what() << "\n";
+ }
return 0;
}
*/
#include <boost/lexical_cast.hpp>
+#include "lib/server_finder.h"
#include "servers_list_dialog.h"
#include "wx_util.h"
s->Layout ();
s->SetSizeHints (this);
- _server_finder.ServerFound.connect (boost::bind (&ServersListDialog::server_found, this, _1));
+ ServerFinder::instance()->connect (boost::bind (&ServersListDialog::server_found, this, _1));
}
void
ServersListDialog::server_found (ServerDescription s)
{
- list<ServerDescription>::const_iterator i = _servers.begin();
- while (i != _servers.end() && i->host_name() != s.host_name()) {
- ++i;
- }
-
- if (i != _servers.end ()) {
- return;
- }
-
wxListItem list_item;
int const n = _list->GetItemCount ();
list_item.SetId (n);
#include <wx/wx.h>
#include <wx/listctrl.h>
-#include "lib/server_finder.h"
class ServersListDialog : public wxDialog
{
private:
void server_found (ServerDescription);
- ServerFinder _server_finder;
std::list<ServerDescription> _servers;
wxListCtrl* _list;
};