Tinker with verbosity of command-line server.
[dcpomatic.git] / src / lib / server.cc
index 19f27ab6a663d48442e93ec7d7c4f3d21a6da0c2..ff77f8e8feba3e6d70161cf61c4f297f884196ba 100644 (file)
@@ -46,6 +46,7 @@ using std::multimap;
 using std::vector;
 using std::list;
 using std::cout;
+using std::cerr;
 using boost::shared_ptr;
 using boost::algorithm::is_any_of;
 using boost::algorithm::split;
@@ -53,6 +54,7 @@ using boost::thread;
 using boost::bind;
 using boost::scoped_array;
 using boost::optional;
+using boost::lexical_cast;
 using libdcp::Size;
 
 ServerDescription::ServerDescription (shared_ptr<const cxml::Node> node)
@@ -76,7 +78,7 @@ optional<ServerDescription>
 ServerDescription::create_from_metadata (string v)
 {
        vector<string> b;
-       split (b, v, is_any_of (N_(" ")));
+       split (b, v, is_any_of (" "));
 
        if (b.size() != 2) {
                return optional<ServerDescription> ();
@@ -85,8 +87,9 @@ ServerDescription::create_from_metadata (string v)
        return ServerDescription (b[0], atoi (b[1].c_str ()));
 }
 
-Server::Server (shared_ptr<Log> log)
+Server::Server (shared_ptr<Log> log, bool verbose)
        : _log (log)
+       , _verbose (verbose)
 {
 
 }
@@ -102,6 +105,7 @@ Server::process (shared_ptr<Socket> socket)
        shared_ptr<cxml::Document> xml (new cxml::Document ("EncodingRequest"));
        xml->read_stream (s);
        if (xml->number_child<int> ("Version") != SERVER_LINK_VERSION) {
+               cerr << "Mismatched server/client versions\n";
                _log->log ("Mismatched server/client versions");
                return -1;
        }
@@ -152,7 +156,7 @@ Server::worker_thread ()
                try {
                        frame = process (socket);
                } catch (std::exception& e) {
-                       _log->log (String::compose (N_("Error: %1"), e.what()));
+                       _log->log (String::compose ("Error: %1", e.what()));
                }
                
                socket.reset ();
@@ -162,7 +166,16 @@ Server::worker_thread ()
                if (frame >= 0) {
                        struct timeval end;
                        gettimeofday (&end, 0);
-                       _log->log (String::compose (N_("Encoded frame %1 in %2"), frame, seconds (end) - seconds (start)));
+
+                       string const message = String::compose (
+                               "Encoded frame %1 from %2 in %3s", frame, socket->socket().remote_endpoint().address().to_string(), seconds(end) - seconds(start)
+                               );
+                       
+                       if (_verbose) {
+                               cout << message << "\n";
+                       }
+
+                       _log->log (message);
                }
                
                _worker_condition.notify_all ();
@@ -172,7 +185,10 @@ Server::worker_thread ()
 void
 Server::run (int num_threads)
 {
-       _log->log (String::compose (N_("Server starting with %1 threads"), num_threads));
+       _log->log (String::compose ("Server starting with %1 threads", num_threads));
+       if (_verbose) {
+               cout << "DCP-o-matic server started with " << num_threads << " threads.\n";
+       }
        
        for (int i = 0; i < num_threads; ++i) {
                _worker_threads.push_back (new thread (bind (&Server::worker_thread, this)));
@@ -181,7 +197,12 @@ Server::run (int num_threads)
        _broadcast.thread = new thread (bind (&Server::broadcast_thread, this));
        
        boost::asio::io_service io_service;
-       boost::asio::ip::tcp::acceptor acceptor (io_service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), Config::instance()->server_port ()));
+
+       boost::asio::ip::tcp::acceptor acceptor (
+               io_service,
+               boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), Config::instance()->server_port_base ())
+               );
+       
        while (1) {
                shared_ptr<Socket> socket (new Socket);
                acceptor.accept (socket->socket ());
@@ -204,7 +225,7 @@ Server::broadcast_thread ()
        boost::asio::io_service io_service;
 
        boost::asio::ip::address address = boost::asio::ip::address_v4::any ();
-       boost::asio::ip::udp::endpoint listen_endpoint (address, Config::instance()->server_port ());
+       boost::asio::ip::udp::endpoint listen_endpoint (address, Config::instance()->server_port_base() + 1);
 
        _broadcast.socket = new boost::asio::ip::udp::socket (io_service);
        _broadcast.socket->open (listen_endpoint.protocol ());
@@ -224,8 +245,24 @@ Server::broadcast_received ()
 {
        _broadcast.buffer[sizeof(_broadcast.buffer) - 1] = '\0';
 
-       cout << _broadcast.buffer << "\n";
-       
+       if (strcmp (_broadcast.buffer, DCPOMATIC_HELLO) == 0) {
+               /* Reply to the client saying what we can do */
+               xmlpp::Document doc;
+               xmlpp::Element* root = doc.create_root_node ("ServerAvailable");
+               root->add_child("Threads")->add_child_text (lexical_cast<string> (_worker_threads.size ()));
+               stringstream xml;
+               doc.write_to_stream (xml, "UTF-8");
+
+               shared_ptr<Socket> socket (new Socket);
+               try {
+                       socket->connect (boost::asio::ip::tcp::endpoint (_broadcast.send_endpoint.address(), Config::instance()->server_port_base() + 1));
+                       socket->write (xml.str().length() + 1);
+                       socket->write ((uint8_t *) xml.str().c_str(), xml.str().length() + 1);
+               } catch (...) {
+
+               }
+       }
+               
        _broadcast.socket->async_receive_from (
                boost::asio::buffer (_broadcast.buffer, sizeof (_broadcast.buffer)),
                _broadcast.send_endpoint, boost::bind (&Server::broadcast_received, this)