diff options
| author | Carl Hetherington <cth@carlh.net> | 2013-11-06 15:11:43 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2013-11-06 15:11:43 +0000 |
| commit | d98bdad019ba9be5d800dece0414d7a080609027 (patch) | |
| tree | 003bdcb6a1b0f4c6516d4a11dd84c2c79cdab5db /src/lib/encoder.cc | |
| parent | 4782e1c0beec98560950a616902669f09a98faae (diff) | |
Add some missing test stuff; split server discovery off into ServerFinder.
Diffstat (limited to 'src/lib/encoder.cc')
| -rw-r--r-- | src/lib/encoder.cc | 94 |
1 files changed, 11 insertions, 83 deletions
diff --git a/src/lib/encoder.cc b/src/lib/encoder.cc index a442e64d0..2ec32deb7 100644 --- a/src/lib/encoder.cc +++ b/src/lib/encoder.cc @@ -33,6 +33,7 @@ #include "server.h" #include "cross.h" #include "writer.h" +#include "server_finder.h" #include "i18n.h" @@ -56,8 +57,6 @@ Encoder::Encoder (shared_ptr<const Film> f, shared_ptr<Job> j) , _job (j) , _video_frames_out (0) , _terminate (false) - , _broadcast_thread (0) - , _listen_thread (0) { _have_a_real_frame[EYES_BOTH] = false; _have_a_real_frame[EYES_LEFT] = false; @@ -104,10 +103,9 @@ Encoder::process_begin () add_worker_threads (*i); } - _broadcast_thread = new boost::thread (boost::bind (&Encoder::broadcast_thread, this)); - _listen_thread = new boost::thread (boost::bind (&Encoder::listen_thread, this)); - _writer.reset (new Writer (_film, _job)); + _server_finder.reset (new ServerFinder ()); + _server_finder->ServerFound.connect (boost::bind (&Encoder::server_found, this, _1)); } @@ -267,16 +265,6 @@ Encoder::terminate_threads () } _threads.clear (); - - 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 @@ -364,76 +352,16 @@ Encoder::encoder_thread (optional<ServerDescription> server) } void -Encoder::broadcast_thread () +Encoder::server_found (ServerDescription s) { - boost::system::error_code error; - boost::asio::io_service io_service; - boost::asio::ip::udp::socket socket (io_service); - socket.open (boost::asio::ip::udp::v4(), error); - if (error) { - throw NetworkError ("failed to set up broadcast socket"); + /* 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; } - - socket.set_option (boost::asio::ip::udp::socket::reuse_address (true)); - socket.set_option (boost::asio::socket_base::broadcast (true)); - 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; - socket.send_to (boost::asio::buffer (data.c_str(), data.size() + 1), end_point); - - lm.unlock (); - dcpomatic_sleep (10); - } -} - -void -Encoder::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 { - sock->accept (Config::instance()->server_port_base() + 1); - } catch (std::exception& e) { - continue; - } - - uint32_t length = sock->read_uint32 (); - scoped_array<char> buffer (new char[length]); - sock->read (reinterpret_cast<uint8_t*> (buffer.get()), length); - - stringstream s (buffer.get()); - shared_ptr<cxml::Document> xml (new cxml::Document ("ServerAvailable")); - xml->read_stream (s); - - { - /* See if we already know about this server */ - string const ip = sock->socket().remote_endpoint().address().to_string (); - boost::mutex::scoped_lock lm (_mutex); - ThreadList::iterator i = _threads.begin(); - while (i != _threads.end() && (!i->first || i->first->host_name() != ip)) { - ++i; - } - - if (i == _threads.end ()) { - add_worker_threads (ServerDescription (ip, xml->number_child<int> ("Threads"))); - } - } + if (i == _threads.end ()) { + add_worker_threads (s); } } |
