From: Carl Hetherington Date: Tue, 1 Sep 2015 19:47:14 +0000 (+0100) Subject: update.{cc,h} -> update_checker.{cc,h}. X-Git-Tag: v2.1.50~6 X-Git-Url: https://main.carlh.net/gitweb/?p=dcpomatic.git;a=commitdiff_plain;h=6904ca547ce503c9ea06b4def9b9a716068e493c update.{cc,h} -> update_checker.{cc,h}. --- diff --git a/src/lib/update.cc b/src/lib/update.cc deleted file mode 100644 index 7a3f0bd5d..000000000 --- a/src/lib/update.cc +++ /dev/null @@ -1,213 +0,0 @@ -/* - Copyright (C) 2014-2015 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 "update.h" -#include "version.h" -#include "safe_stringstream.h" -#include "config.h" -#include "util.h" -#include "raw_convert.h" -#include -#include -#include -#include - -#define BUFFER_SIZE 1024 - -using std::cout; -using std::min; -using std::string; -using std::vector; -using boost::is_any_of; -using boost::ends_with; - -/** Singleton instance */ -UpdateChecker* UpdateChecker::_instance = 0; - -static size_t -write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user) -{ - return reinterpret_cast(user)->write_callback (data, size, nmemb); -} - -/** Construct an UpdateChecker. This sets things up and starts a thread to - * do the work. - */ -UpdateChecker::UpdateChecker () - : _buffer (new char[BUFFER_SIZE]) - , _offset (0) - , _curl (0) - , _state (NOT_RUN) - , _emits (0) - , _to_do (0) -{ - _curl = curl_easy_init (); - - curl_easy_setopt (_curl, CURLOPT_URL, "http://dcpomatic.com/update"); - curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper); - curl_easy_setopt (_curl, CURLOPT_WRITEDATA, this); - curl_easy_setopt (_curl, CURLOPT_TIMEOUT, 20); - - string const agent = "dcpomatic/" + string (dcpomatic_version); - curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ()); - - _thread = new boost::thread (boost::bind (&UpdateChecker::thread, this)); -} - -UpdateChecker::~UpdateChecker () -{ - /* We are not cleaning up our thread, but hey well */ - - curl_easy_cleanup (_curl); - delete[] _buffer; -} - -/** Start running the update check */ -void -UpdateChecker::run () -{ - boost::mutex::scoped_lock lm (_process_mutex); - _to_do++; - _condition.notify_one (); -} - -void -UpdateChecker::thread () -{ - while (true) { - /* Block until there is something to do */ - boost::mutex::scoped_lock lock (_process_mutex); - while (_to_do == 0) { - _condition.wait (lock); - } - --_to_do; - lock.unlock (); - - try { - _offset = 0; - - /* Perform the request */ - - int r = curl_easy_perform (_curl); - if (r != CURLE_OK) { - set_state (FAILED); - return; - } - - /* Parse the reply */ - - _buffer[_offset] = '\0'; - string s (_buffer); - cxml::Document doc ("Update"); - doc.read_string (s); - - /* Read the current stable and test version numbers */ - - string stable; - string test; - - { - boost::mutex::scoped_lock lm (_data_mutex); - stable = doc.string_child ("Stable"); - test = doc.string_child ("Test"); - } - - if (version_less_than (dcpomatic_version, stable)) { - _stable = stable; - } - - if (Config::instance()->check_for_test_updates() && version_less_than (dcpomatic_version, test)) { - _test = test; - } - - if (_stable || _test) { - set_state (YES); - } else { - set_state (NO); - } - } catch (...) { - set_state (FAILED); - } - } -} - -size_t -UpdateChecker::write_callback (void* data, size_t size, size_t nmemb) -{ - size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1)); - memcpy (_buffer + _offset, data, t); - _offset += t; - return t; -} - -void -UpdateChecker::set_state (State s) -{ - { - boost::mutex::scoped_lock lm (_data_mutex); - _state = s; - _emits++; - } - - emit (boost::bind (boost::ref (StateChanged))); -} - -UpdateChecker * -UpdateChecker::instance () -{ - if (!_instance) { - _instance = new UpdateChecker (); - } - - return _instance; -} - -bool -UpdateChecker::version_less_than (string const & a, string const & b) -{ - vector ap; - split (ap, a, is_any_of (".")); - vector bp; - split (bp, b, is_any_of (".")); - - DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3); - - if (ap[0] != bp[0]) { - return raw_convert (ap[0]) < raw_convert (bp[0]); - } - - if (ap[1] != bp[1]) { - return raw_convert (ap[1]) < raw_convert (bp[1]); - } - float am; - if (ends_with (ap[2], "devel")) { - am = raw_convert (ap[2].substr (0, ap[2].length() - 5)) + 0.5; - } else { - am = raw_convert (ap[2]); - } - - float bm; - if (ends_with (bp[2], "devel")) { - bm = raw_convert (bp[2].substr (0, bp[2].length() - 5)) + 0.5; - } else { - bm = raw_convert (bp[2]); - } - - return am < bm; -} diff --git a/src/lib/update.h b/src/lib/update.h deleted file mode 100644 index b82be7808..000000000 --- a/src/lib/update.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - Copyright (C) 2014 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. - -*/ - -/** @file src/lib/update.h - * @brief UpdateChecker class. - */ - -#include "signaller.h" -#include -#include -#include -#include -#include - -struct update_checker_test; - -/** Class to check for the existance of an update for DCP-o-matic on a remote server */ -class UpdateChecker : public Signaller, public boost::noncopyable -{ -public: - UpdateChecker (); - ~UpdateChecker (); - - void run (); - - enum State { - YES, ///< there is an update - FAILED, ///< the check failed, so we don't know - NO, ///< there is no update - NOT_RUN ///< the check has not been run (yet) - }; - - /** @return state of the checker */ - State state () { - boost::mutex::scoped_lock lm (_data_mutex); - return _state; - } - - /** @return new stable version, if there is one */ - boost::optional stable () { - boost::mutex::scoped_lock lm (_data_mutex); - return _stable; - } - - /** @return new test version, if there is one and Config is set to look for it */ - boost::optional test () { - boost::mutex::scoped_lock lm (_data_mutex); - return _test; - } - - /** @return true if the last signal emission was the first */ - bool last_emit_was_first () const { - boost::mutex::scoped_lock lm (_data_mutex); - return _emits == 1; - } - - size_t write_callback (void *, size_t, size_t); - - boost::signals2::signal StateChanged; - - static UpdateChecker* instance (); - -private: - friend struct update_checker_test; - - static UpdateChecker* _instance; - - static bool version_less_than (std::string const & a, std::string const & b); - - void set_state (State); - void thread (); - - char* _buffer; - int _offset; - CURL* _curl; - - /** mutex to protect _state, _stable, _test and _emits */ - mutable boost::mutex _data_mutex; - State _state; - boost::optional _stable; - boost::optional _test; - int _emits; - - boost::thread* _thread; - boost::mutex _process_mutex; - boost::condition _condition; - int _to_do; -}; diff --git a/src/lib/update_checker.cc b/src/lib/update_checker.cc new file mode 100644 index 000000000..634328e6a --- /dev/null +++ b/src/lib/update_checker.cc @@ -0,0 +1,213 @@ +/* + Copyright (C) 2014-2015 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 "update_checker.h" +#include "version.h" +#include "safe_stringstream.h" +#include "config.h" +#include "util.h" +#include "raw_convert.h" +#include +#include +#include +#include + +#define BUFFER_SIZE 1024 + +using std::cout; +using std::min; +using std::string; +using std::vector; +using boost::is_any_of; +using boost::ends_with; + +/** Singleton instance */ +UpdateChecker* UpdateChecker::_instance = 0; + +static size_t +write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user) +{ + return reinterpret_cast(user)->write_callback (data, size, nmemb); +} + +/** Construct an UpdateChecker. This sets things up and starts a thread to + * do the work. + */ +UpdateChecker::UpdateChecker () + : _buffer (new char[BUFFER_SIZE]) + , _offset (0) + , _curl (0) + , _state (NOT_RUN) + , _emits (0) + , _to_do (0) +{ + _curl = curl_easy_init (); + + curl_easy_setopt (_curl, CURLOPT_URL, "http://dcpomatic.com/update"); + curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper); + curl_easy_setopt (_curl, CURLOPT_WRITEDATA, this); + curl_easy_setopt (_curl, CURLOPT_TIMEOUT, 20); + + string const agent = "dcpomatic/" + string (dcpomatic_version); + curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ()); + + _thread = new boost::thread (boost::bind (&UpdateChecker::thread, this)); +} + +UpdateChecker::~UpdateChecker () +{ + /* We are not cleaning up our thread, but hey well */ + + curl_easy_cleanup (_curl); + delete[] _buffer; +} + +/** Start running the update check */ +void +UpdateChecker::run () +{ + boost::mutex::scoped_lock lm (_process_mutex); + _to_do++; + _condition.notify_one (); +} + +void +UpdateChecker::thread () +{ + while (true) { + /* Block until there is something to do */ + boost::mutex::scoped_lock lock (_process_mutex); + while (_to_do == 0) { + _condition.wait (lock); + } + --_to_do; + lock.unlock (); + + try { + _offset = 0; + + /* Perform the request */ + + int r = curl_easy_perform (_curl); + if (r != CURLE_OK) { + set_state (FAILED); + return; + } + + /* Parse the reply */ + + _buffer[_offset] = '\0'; + string s (_buffer); + cxml::Document doc ("Update"); + doc.read_string (s); + + /* Read the current stable and test version numbers */ + + string stable; + string test; + + { + boost::mutex::scoped_lock lm (_data_mutex); + stable = doc.string_child ("Stable"); + test = doc.string_child ("Test"); + } + + if (version_less_than (dcpomatic_version, stable)) { + _stable = stable; + } + + if (Config::instance()->check_for_test_updates() && version_less_than (dcpomatic_version, test)) { + _test = test; + } + + if (_stable || _test) { + set_state (YES); + } else { + set_state (NO); + } + } catch (...) { + set_state (FAILED); + } + } +} + +size_t +UpdateChecker::write_callback (void* data, size_t size, size_t nmemb) +{ + size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1)); + memcpy (_buffer + _offset, data, t); + _offset += t; + return t; +} + +void +UpdateChecker::set_state (State s) +{ + { + boost::mutex::scoped_lock lm (_data_mutex); + _state = s; + _emits++; + } + + emit (boost::bind (boost::ref (StateChanged))); +} + +UpdateChecker * +UpdateChecker::instance () +{ + if (!_instance) { + _instance = new UpdateChecker (); + } + + return _instance; +} + +bool +UpdateChecker::version_less_than (string const & a, string const & b) +{ + vector ap; + split (ap, a, is_any_of (".")); + vector bp; + split (bp, b, is_any_of (".")); + + DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3); + + if (ap[0] != bp[0]) { + return raw_convert (ap[0]) < raw_convert (bp[0]); + } + + if (ap[1] != bp[1]) { + return raw_convert (ap[1]) < raw_convert (bp[1]); + } + float am; + if (ends_with (ap[2], "devel")) { + am = raw_convert (ap[2].substr (0, ap[2].length() - 5)) + 0.5; + } else { + am = raw_convert (ap[2]); + } + + float bm; + if (ends_with (bp[2], "devel")) { + bm = raw_convert (bp[2].substr (0, bp[2].length() - 5)) + 0.5; + } else { + bm = raw_convert (bp[2]); + } + + return am < bm; +} diff --git a/src/lib/update_checker.h b/src/lib/update_checker.h new file mode 100644 index 000000000..b82be7808 --- /dev/null +++ b/src/lib/update_checker.h @@ -0,0 +1,104 @@ +/* + Copyright (C) 2014 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. + +*/ + +/** @file src/lib/update.h + * @brief UpdateChecker class. + */ + +#include "signaller.h" +#include +#include +#include +#include +#include + +struct update_checker_test; + +/** Class to check for the existance of an update for DCP-o-matic on a remote server */ +class UpdateChecker : public Signaller, public boost::noncopyable +{ +public: + UpdateChecker (); + ~UpdateChecker (); + + void run (); + + enum State { + YES, ///< there is an update + FAILED, ///< the check failed, so we don't know + NO, ///< there is no update + NOT_RUN ///< the check has not been run (yet) + }; + + /** @return state of the checker */ + State state () { + boost::mutex::scoped_lock lm (_data_mutex); + return _state; + } + + /** @return new stable version, if there is one */ + boost::optional stable () { + boost::mutex::scoped_lock lm (_data_mutex); + return _stable; + } + + /** @return new test version, if there is one and Config is set to look for it */ + boost::optional test () { + boost::mutex::scoped_lock lm (_data_mutex); + return _test; + } + + /** @return true if the last signal emission was the first */ + bool last_emit_was_first () const { + boost::mutex::scoped_lock lm (_data_mutex); + return _emits == 1; + } + + size_t write_callback (void *, size_t, size_t); + + boost::signals2::signal StateChanged; + + static UpdateChecker* instance (); + +private: + friend struct update_checker_test; + + static UpdateChecker* _instance; + + static bool version_less_than (std::string const & a, std::string const & b); + + void set_state (State); + void thread (); + + char* _buffer; + int _offset; + CURL* _curl; + + /** mutex to protect _state, _stable, _test and _emits */ + mutable boost::mutex _data_mutex; + State _state; + boost::optional _stable; + boost::optional _test; + int _emits; + + boost::thread* _thread; + boost::mutex _process_mutex; + boost::condition _condition; + int _to_do; +}; diff --git a/src/lib/wscript b/src/lib/wscript index ae3059af4..68005f50b 100644 --- a/src/lib/wscript +++ b/src/lib/wscript @@ -118,7 +118,7 @@ sources = """ transcoder.cc types.cc signal_manager.cc - update.cc + update_checker.cc upload_job.cc uploader.cc upmixer_a.cc diff --git a/src/tools/dcpomatic.cc b/src/tools/dcpomatic.cc index e2465ea4a..8692f4c83 100644 --- a/src/tools/dcpomatic.cc +++ b/src/tools/dcpomatic.cc @@ -50,7 +50,7 @@ #include "lib/kdm.h" #include "lib/send_kdm_email_job.h" #include "lib/server_finder.h" -#include "lib/update.h" +#include "lib/update_checker.h" #include "lib/cross.h" #include "lib/content_factory.h" #include "lib/compose.hpp" diff --git a/test/update_checker_test.cc b/test/update_checker_test.cc index c9a44993c..5657e69ba 100644 --- a/test/update_checker_test.cc +++ b/test/update_checker_test.cc @@ -18,7 +18,7 @@ */ #include -#include "lib/update.h" +#include "lib/update_checker.h" BOOST_AUTO_TEST_CASE (update_checker_test) {