From 1e8f1be709e8a3fa58f1147db2e58a39396313d8 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Mon, 17 Sep 2012 22:50:47 +0100 Subject: Move server code into library; Server -> ServerDescription. --- src/tools/servomatic_cli.cc | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/tools/servomatic_cli.cc (limited to 'src/tools/servomatic_cli.cc') diff --git a/src/tools/servomatic_cli.cc b/src/tools/servomatic_cli.cc new file mode 100644 index 000000000..1fcd02117 --- /dev/null +++ b/src/tools/servomatic_cli.cc @@ -0,0 +1,50 @@ +/* + Copyright (C) 2012 Carl Hetherington + + 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 + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include "lib/server.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "dcp_video_frame.h" +#include "exceptions.h" +#include "util.h" +#include "config.h" +#include "scaler.h" +#include "image.h" +#include "log.h" + +int +main () +{ + Scaler::setup_scalers (); + Server server; + server.run (); + return 0; +} -- cgit v1.2.3 From 3c1b239453936128d1711ffa063ad4e1617b3e40 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 18 Sep 2012 02:07:59 +0100 Subject: Sort of working log window. --- src/lib/dcp_video_frame.cc | 4 ++-- src/lib/film.cc | 2 +- src/lib/log.cc | 29 +++++++++++++++++------- src/lib/log.h | 27 ++++++++++++++-------- src/lib/server.cc | 6 ++--- src/lib/server.h | 4 ++-- src/tools/servomatic_cli.cc | 3 ++- src/tools/servomatic_gui.cc | 55 +++++++++++++++++++++++++++++++++++++-------- src/wx/config_dialog.cc | 4 ++++ 9 files changed, 99 insertions(+), 35 deletions(-) (limited to 'src/tools/servomatic_cli.cc') diff --git a/src/lib/dcp_video_frame.cc b/src/lib/dcp_video_frame.cc index 91c441543..b128f6fa0 100644 --- a/src/lib/dcp_video_frame.cc +++ b/src/lib/dcp_video_frame.cc @@ -277,7 +277,7 @@ DCPVideoFrame::encode_locally () { stringstream s; - s << "Finished locally-encoded frame " << _frame << " length " << cio_tell (_cio); + s << "Finished locally-encoded frame " << _frame; _log->log (s.str ()); } @@ -342,7 +342,7 @@ DCPVideoFrame::encode_remotely (ServerDescription const * serv) { stringstream s; - s << "Finished remotely-encoded frame " << _frame << " length " << e->size(); + s << "Finished remotely-encoded frame " << _frame; _log->log (s.str ()); } diff --git a/src/lib/film.cc b/src/lib/film.cc index f8a3b192d..3b74f1888 100644 --- a/src/lib/film.cc +++ b/src/lib/film.cc @@ -88,7 +88,7 @@ Film::Film (string d, bool must_exist) read_metadata (); - _log = new Log (_state.file ("log")); + _log = new FileLog (_state.file ("log")); } /** Copy constructor */ diff --git a/src/lib/log.cc b/src/lib/log.cc index accf3694d..7f1eea206 100644 --- a/src/lib/log.cc +++ b/src/lib/log.cc @@ -27,10 +27,8 @@ using namespace std; -/** @param f Filename to write log to */ -Log::Log (string f) - : _file (f) - , _level (VERBOSE) +Log::Log () + : _level (VERBOSE) { } @@ -45,13 +43,13 @@ Log::log (string m, Level l) return; } - ofstream f (_file.c_str(), fstream::app); - time_t t; time (&t); string a = ctime (&t); - - f << a.substr (0, a.length() - 1) << ": " << m << "\n"; + + stringstream s; + s << a.substr (0, a.length() - 1) << ": " << m; + do_log (s.str ()); } void @@ -61,3 +59,18 @@ Log::set_level (Level l) _level = l; } + +/** @param file Filename to write log to */ +FileLog::FileLog (string file) + : _file (file) +{ + +} + +void +FileLog::do_log (string m) +{ + ofstream f (_file.c_str(), fstream::app); + f << m << "\n"; +} + diff --git a/src/lib/log.h b/src/lib/log.h index d32b368f5..2a242e24c 100644 --- a/src/lib/log.h +++ b/src/lib/log.h @@ -29,15 +29,11 @@ /** @class Log * @brief A very simple logging class. - * - * This class simply accepts log messages and writes them to a file. - * Its single nod to complexity is that it has a mutex to prevent - * multi-thread logging from clashing. */ class Log { public: - Log (std::string f); + Log (); enum Level { STANDARD = 0, @@ -48,13 +44,26 @@ public: void set_level (Level l); -private: - /** mutex to prevent simultaneous writes to the file */ +protected: + /** mutex to protect the log */ boost::mutex _mutex; - /** filename to write to */ - std::string _file; + +private: + virtual void do_log (std::string m) = 0; + /** level above which to ignore log messages */ Level _level; }; +class FileLog : public Log +{ +public: + FileLog (std::string file); + +private: + void do_log (std::string m); + /** filename to write to */ + std::string _file; +}; + #endif diff --git a/src/lib/server.cc b/src/lib/server.cc index f4aaa25e1..9e43601c4 100644 --- a/src/lib/server.cc +++ b/src/lib/server.cc @@ -62,8 +62,8 @@ ServerDescription::as_metadata () const return s.str (); } -Server::Server () - : _log ("servomatic.log") +Server::Server (Log* log) + : _log (log) { } @@ -129,7 +129,7 @@ Server::process (shared_ptr socket) image->hash ("Image for encoding (as received by server)"); #endif - DCPVideoFrame dcp_video_frame (image, out_size, padding, scaler, frame, frames_per_second, post_process, colour_lut_index, j2k_bandwidth, &_log); + DCPVideoFrame dcp_video_frame (image, out_size, padding, scaler, frame, frames_per_second, post_process, colour_lut_index, j2k_bandwidth, _log); shared_ptr encoded = dcp_video_frame.encode_locally (); encoded->send (socket); diff --git a/src/lib/server.h b/src/lib/server.h index 8c0f86ebb..58cfe0b3f 100644 --- a/src/lib/server.h +++ b/src/lib/server.h @@ -74,7 +74,7 @@ private: class Server { public: - Server (); + Server (Log* log); void run (); @@ -86,5 +86,5 @@ private: std::list > _queue; boost::mutex _worker_mutex; boost::condition _worker_condition; - Log _log; + Log* _log; }; diff --git a/src/tools/servomatic_cli.cc b/src/tools/servomatic_cli.cc index 1fcd02117..3ad73faf9 100644 --- a/src/tools/servomatic_cli.cc +++ b/src/tools/servomatic_cli.cc @@ -44,7 +44,8 @@ int main () { Scaler::setup_scalers (); - Server server; + FileLog log ("servomatic.log"); + Server server (&log); server.run (); return 0; } diff --git a/src/tools/servomatic_gui.cc b/src/tools/servomatic_gui.cc index d89bd91ad..1d95f498a 100644 --- a/src/tools/servomatic_gui.cc +++ b/src/tools/servomatic_gui.cc @@ -24,28 +24,65 @@ #include "lib/util.h" #include "lib/server.h" +using namespace std; using namespace boost; enum { ID_status = 1, - ID_quit + ID_quit, + ID_timer }; +class MemoryLog : public Log +{ +public: + + string get () const { + boost::mutex::scoped_lock (_mutex); + return _log; + } + +private: + void do_log (string m) + { + _log = m; + } + + string _log; +}; + +static MemoryLog memory_log; + class StatusDialog : public wxDialog { public: StatusDialog () - : wxDialog (0, wxID_ANY, _("DVD-o-matic encode server"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE) + : wxDialog (0, wxID_ANY, _("DVD-o-matic encode server"), wxDefaultPosition, wxSize (600, 40), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) + , _timer (this, ID_timer) { - wxFlexGridSizer* table = new wxFlexGridSizer (2, 6, 6); - table->AddGrowableCol (1, 1); + _sizer = new wxFlexGridSizer (1, 6, 6); + _sizer->AddGrowableCol (0, 1); - add_label_to_sizer (table, this, "Hello"); + _text = new wxTextCtrl (this, wxID_ANY); + _sizer->Add (_text, 1, wxEXPAND); - SetSizer (table); - table->Layout (); - table->SetSizeHints (this); + SetSizer (_sizer); + _sizer->Layout (); + + Connect (ID_timer, wxEVT_TIMER, wxTimerEventHandler (StatusDialog::update)); + _timer.Start (1000); } + +private: + void update (wxTimerEvent &) + { + _text->ChangeValue (std_to_wx (memory_log.get ())); + _sizer->Layout (); + } + + wxFlexGridSizer* _sizer; + wxTextCtrl* _text; + wxTimer _timer; }; class TaskBarIcon : public wxTaskBarIcon @@ -103,7 +140,7 @@ private: void main_thread () { - Server server; + Server server (&memory_log); server.run (); } diff --git a/src/wx/config_dialog.cc b/src/wx/config_dialog.cc index c53eeddf0..c5d9be41f 100644 --- a/src/wx/config_dialog.cc +++ b/src/wx/config_dialog.cc @@ -287,6 +287,10 @@ ConfigDialog::remove_server_clicked (wxCommandEvent &) if (i >= 0) { _servers->DeleteItem (i); } + + vector o = Config::instance()->servers (); + o.erase (o.begin() + i); + Config::instance()->set_servers (o); } void -- cgit v1.2.3 From c2709fbe5438da124b2d493cb714a6c58720cf5b Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sun, 23 Sep 2012 12:39:27 +0100 Subject: Command-line option to specify servomatic_cli threads. --- src/lib/server.cc | 4 ++-- src/lib/server.h | 2 +- src/lib/util.cc | 8 ++++++-- src/tools/servomatic_cli.cc | 47 +++++++++++++++++++++++++++++++++++++++++++-- src/tools/servomatic_gui.cc | 3 ++- 5 files changed, 56 insertions(+), 8 deletions(-) (limited to 'src/tools/servomatic_cli.cc') diff --git a/src/lib/server.cc b/src/lib/server.cc index ff784db5a..a62763447 100644 --- a/src/lib/server.cc +++ b/src/lib/server.cc @@ -181,9 +181,9 @@ Server::worker_thread () } void -Server::run () +Server::run (int num_threads) { - int const num_threads = Config::instance()->num_local_encoding_threads (); + cout << "Server starting with " << num_threads << " threads.\n"; for (int i = 0; i < num_threads; ++i) { _worker_threads.push_back (new thread (bind (&Server::worker_thread, this))); diff --git a/src/lib/server.h b/src/lib/server.h index 58cfe0b3f..fac440a76 100644 --- a/src/lib/server.h +++ b/src/lib/server.h @@ -76,7 +76,7 @@ class Server public: Server (Log* log); - void run (); + void run (int num_threads); private: void worker_thread (); diff --git a/src/lib/util.cc b/src/lib/util.cc index 1478bab2e..e79c7cd1c 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -518,5 +518,9 @@ colour_lut_index_to_name (int index) return ""; } - - +int +read_with_timeout (boost::asio::ip::tcp::socket* socket, uint8_t* data, int size) +{ + + return asio::read (socket, asio::buffer (data, size)); +} diff --git a/src/tools/servomatic_cli.cc b/src/tools/servomatic_cli.cc index 3ad73faf9..f8e713193 100644 --- a/src/tools/servomatic_cli.cc +++ b/src/tools/servomatic_cli.cc @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -39,13 +40,55 @@ #include "scaler.h" #include "image.h" #include "log.h" +#include "version.h" + +using namespace std; + +static void +help (string n) +{ + cerr << "Syntax: " << n << " [OPTION]\n" + << " -v, --version show DVD-o-matic version\n" + << " -h, --help show this help\n" + << " -t, --threads number of parallel encoding threads to use\n"; +} int -main () +main (int argc, char* argv[]) { + int num_threads = Config::instance()->num_local_encoding_threads (); + + int option_index = 0; + while (1) { + static struct option long_options[] = { + { "version", no_argument, 0, 'v'}, + { "help", no_argument, 0, 'h'}, + { "threads", required_argument, 0, 't'}, + { 0, 0, 0, 0 } + }; + + int c = getopt_long (argc, argv, "vht:", long_options, &option_index); + + if (c == -1) { + break; + } + + switch (c) { + case 'v': + cout << "dvdomatic version " << dvdomatic_version << " " << dvdomatic_git_commit << "\n"; + exit (EXIT_SUCCESS); + case 'h': + help (argv[0]); + exit (EXIT_SUCCESS); + case 't': + num_threads = atoi (optarg); + break; + } + } + Scaler::setup_scalers (); FileLog log ("servomatic.log"); Server server (&log); - server.run (); + server.run (num_threads); return 0; } diff --git a/src/tools/servomatic_gui.cc b/src/tools/servomatic_gui.cc index a151658f5..610ba8005 100644 --- a/src/tools/servomatic_gui.cc +++ b/src/tools/servomatic_gui.cc @@ -23,6 +23,7 @@ #include "wx_util.h" #include "lib/util.h" #include "lib/server.h" +#include "lib/config.h" using namespace std; using namespace boost; @@ -141,7 +142,7 @@ private: void main_thread () { Server server (&memory_log); - server.run (); + server.run (Config::instance()->num_local_encoding_threads ()); } boost::thread* _thread; -- cgit v1.2.3