From ee7e1b9b96b2cda7d238a47bb509a6d395a873cb Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Sat, 24 Feb 2018 00:46:28 -0500 Subject: [PATCH] Fix with asset_helper Initial cleanup of Sync module --- data/assets/util/asset_helper.asset | 2 +- modules/sync/syncmodule.cpp | 46 ++++---- modules/sync/syncmodule.h | 13 ++- modules/sync/syncs/torrentsynchronization.cpp | 11 +- modules/sync/syncs/torrentsynchronization.h | 4 +- modules/sync/torrentclient.cpp | 87 ++++++-------- modules/sync/torrentclient.h | 110 ++++++------------ 7 files changed, 105 insertions(+), 168 deletions(-) diff --git a/data/assets/util/asset_helper.asset b/data/assets/util/asset_helper.asset index 94a1ba0608..cb70c7b739 100644 --- a/data/assets/util/asset_helper.asset +++ b/data/assets/util/asset_helper.asset @@ -1,6 +1,6 @@ local tableLength = function(table) local count = 0 - for _ in pairs(nodes) do count = count + 1 end + for _ in pairs(table) do count = count + 1 end return count end diff --git a/modules/sync/syncmodule.cpp b/modules/sync/syncmodule.cpp index 40d96048ad..c3cdf7e5ef 100644 --- a/modules/sync/syncmodule.cpp +++ b/modules/sync/syncmodule.cpp @@ -58,21 +58,19 @@ void SyncModule::internalInitialize(const ghoul::Dictionary& configuration) { KeyHttpSynchronizationRepositories); for (const std::string& key : dictionary.keys()) { - _httpSynchronizationRepositories.push_back( - dictionary.value(key) - ); + _synchronizationRepositories.push_back(dictionary.value(key)); } } if (configuration.hasKey(KeySynchronizationRoot)) { _synchronizationRoot = configuration.value(KeySynchronizationRoot); } else { - LWARNING( - "No synchronization root specified." - "Resource synchronization will be disabled." + LWARNINGC( + "SyncModule", + "No synchronization root specified. Disabling resource synchronization" ); //_synchronizationEnabled = false; - // TODO: Make it possible to disable synchronization manyally. + // TODO: Make it possible to disable synchronization manually. // Group root and enabled into a sync config object that can be passed to syncs. } @@ -85,7 +83,7 @@ void SyncModule::internalInitialize(const ghoul::Dictionary& configuration) { return new HttpSynchronization( dictionary, _synchronizationRoot, - _httpSynchronizationRepositories + _synchronizationRepositories ); } ); @@ -96,7 +94,7 @@ void SyncModule::internalInitialize(const ghoul::Dictionary& configuration) { return new TorrentSynchronization( dictionary, _synchronizationRoot, - &_torrentClient + _torrentClient ); } ); @@ -105,6 +103,8 @@ void SyncModule::internalInitialize(const ghoul::Dictionary& configuration) { ghoul_assert(fTask, "No task factory existed"); fTask->registerClass("SyncAssetTask"); + + _torrentClient.initialize(); // Deinitialize @@ -113,6 +113,18 @@ void SyncModule::internalInitialize(const ghoul::Dictionary& configuration) { }); } +std::string SyncModule::synchronizationRoot() const { + return _synchronizationRoot; +} + +void SyncModule::addHttpSynchronizationRepository(std::string repository) { + _synchronizationRepositories.push_back(std::move(repository)); +} + +std::vector SyncModule::httpSynchronizationRepositories() const { + return _synchronizationRepositories; +} + std::vector SyncModule::documentations() const { return { HttpSynchronization::Documentation(), @@ -120,20 +132,4 @@ std::vector SyncModule::documentations() const { }; } -std::string SyncModule::synchronizationRoot() const { - return _synchronizationRoot; -} - -void SyncModule::addHttpSynchronizationRepository(const std::string& repository) { - _httpSynchronizationRepositories.push_back(repository); -} - -std::vector SyncModule::httpSynchronizationRepositories() const { - return _httpSynchronizationRepositories; -} - -TorrentClient* SyncModule::torrentClient() { - return &_torrentClient; -} - } // namespace openspace diff --git a/modules/sync/syncmodule.h b/modules/sync/syncmodule.h index dc7de40d2c..e3e51b39a6 100644 --- a/modules/sync/syncmodule.h +++ b/modules/sync/syncmodule.h @@ -36,19 +36,22 @@ public: constexpr static const char* Name = "Sync"; SyncModule(); - virtual ~SyncModule() = default; - std::vector documentations() const override; + std::string synchronizationRoot() const; - void addHttpSynchronizationRepository(const std::string& repository); + + void addHttpSynchronizationRepository(std::string repository); std::vector httpSynchronizationRepositories() const; - TorrentClient* torrentClient(); + + TorrentClient& torrentClient(); + + std::vector documentations() const override; protected: void internalInitialize(const ghoul::Dictionary& configuration) override; private: TorrentClient _torrentClient; - std::vector _httpSynchronizationRepositories; + std::vector _synchronizationRepositories; std::string _synchronizationRoot; }; diff --git a/modules/sync/syncs/torrentsynchronization.cpp b/modules/sync/syncs/torrentsynchronization.cpp index 3127ae20ba..3a4418ba6a 100644 --- a/modules/sync/syncs/torrentsynchronization.cpp +++ b/modules/sync/syncs/torrentsynchronization.cpp @@ -47,7 +47,7 @@ namespace openspace { TorrentSynchronization::TorrentSynchronization(const ghoul::Dictionary& dict, const std::string& synchronizationRoot, - TorrentClient* torrentClient) + TorrentClient& torrentClient) : ResourceSynchronization(dict) , _enabled(false) , _synchronizationRoot(synchronizationRoot) @@ -134,16 +134,15 @@ void TorrentSynchronization::start() { _enabled = true; try { - _torrentId = _torrentClient->addMagnetLink( + _torrentId = _torrentClient.addMagnetLink( _magnetLink, directory(), [this](TorrentClient::TorrentProgress p) { updateTorrentProgress(p); } ); - } catch (const TorrentError&) { - LERROR("Failed to synchronize '" << name() << - "'.\nOpenSpace needs to be linked with libtorrent."); + } catch (const TorrentError& e) { + LERRORC(name(), e.message); if (!isResolved()) { reject(); } @@ -152,7 +151,7 @@ void TorrentSynchronization::start() { void TorrentSynchronization::cancel() { if (_enabled) { - _torrentClient->removeTorrent(_torrentId); + _torrentClient.removeTorrent(_torrentId); _enabled = false; reset(); } diff --git a/modules/sync/syncs/torrentsynchronization.h b/modules/sync/syncs/torrentsynchronization.h index 82470e7851..9f1831c3fd 100644 --- a/modules/sync/syncs/torrentsynchronization.h +++ b/modules/sync/syncs/torrentsynchronization.h @@ -40,7 +40,7 @@ class TorrentSynchronization : public ResourceSynchronization { public: TorrentSynchronization(const ghoul::Dictionary& dict, const std::string& synchronizationRoot, - TorrentClient* client); + TorrentClient& client); virtual ~TorrentSynchronization(); @@ -68,7 +68,7 @@ private: std::string _identifier; std::string _magnetLink; std::string _synchronizationRoot; - TorrentClient* _torrentClient; + TorrentClient& _torrentClient; }; } // namespace openspace diff --git a/modules/sync/torrentclient.cpp b/modules/sync/torrentclient.cpp index a37e2f0663..0416f680cc 100644 --- a/modules/sync/torrentclient.cpp +++ b/modules/sync/torrentclient.cpp @@ -28,6 +28,15 @@ #include +#ifdef SYNC_USE_LIBTORRENT +#include +#include +#include +#include +#include +#include +#endif // SYNC_USE_LIBTORRENT + namespace { constexpr const char* _loggerCat = "TorrentClient"; std::chrono::milliseconds PollInterval(1000); @@ -35,32 +44,18 @@ namespace { namespace openspace { -TorrentError::TorrentError(std::string component) - : RuntimeError("TorrentClient", component) +TorrentError::TorrentError(std::string message) + : RuntimeError(std::move(message), "TorrentClient") {} -} // namespace openspace - -#ifdef SYNC_USE_LIBTORRENT - -#include -#include -#include -#include -#include -#include - -namespace openspace { - -TorrentClient::TorrentClient() - : _active(false) -{} +TorrentClient::TorrentClient() : _active(false) {} TorrentClient::~TorrentClient() { deinitialize(); } void TorrentClient::initialize() { +#ifdef SYNC_USE_LIBTORRENT libtorrent::settings_pack settings; _session = std::make_unique(); @@ -77,7 +72,7 @@ void TorrentClient::initialize() { settings.set_int(libtorrent::settings_pack::active_downloads, -1); settings.set_int(libtorrent::settings_pack::active_seeds, -1); settings.set_int(libtorrent::settings_pack::active_limit, 30); - settings.set_int(libtorrent::settings_pack::dht_announce_interval, 60); + settings.set_int(libtorrent::settings_pack::dht_announce_interval, 15); _session->apply_settings(settings); _session->add_dht_router({ "router.utorrent.com", 6881 }); @@ -98,9 +93,11 @@ void TorrentClient::initialize() { _abortNotifier.wait_for(lock, PollInterval); } }); +#endif // SYNC_USE_LIBTORRENT } void TorrentClient::deinitialize() { +#ifdef SYNC_USE_LIBTORRENT if (!_active) { return; } @@ -119,9 +116,11 @@ void TorrentClient::deinitialize() { _session->abort(); _session = nullptr; +#endif // SYNC_USE_LIBTORRENT } void TorrentClient::pollAlerts() { +#ifdef SYNC_USE_LIBTORRENT // Libtorrent does not seem to reliably generate alerts for all added torrents. // To make sure that the program does not keep waiting for already finished // downsloads, we go through the whole list of torrents when polling. @@ -148,12 +147,14 @@ void TorrentClient::pollAlerts() { for (lt::torrent_handle h : handles) { notify(h.id()); } +#endif // SYNC_USE_LIBTORRENT } TorrentClient::TorrentId TorrentClient::addTorrentFile(std::string torrentFile, std::string destination, TorrentProgressCallback cb) { +#ifdef SYNC_USE_LIBTORRENT std::lock_guard guard(_mutex); if (!_session) { @@ -174,12 +175,16 @@ TorrentClient::TorrentId TorrentClient::addTorrentFile(std::string torrentFile, TorrentId id = h.id(); _torrents.emplace(id, Torrent{id, h, cb}); return id; +#else // SYNC_USE_LIBTORRENT + throw TorrentError("SyncModule is compiled without libtorrent"); +#endif // SYNC_USE_LIBTORRENT } TorrentClient::TorrentId TorrentClient::addMagnetLink(std::string magnetLink, std::string destination, TorrentProgressCallback cb) { +#ifdef SYNC_USE_LIBTORRENT std::lock_guard guard(_mutex); // TODO: register callback! @@ -205,9 +210,13 @@ TorrentClient::TorrentId TorrentClient::addMagnetLink(std::string magnetLink, TorrentId id = h.id(); _torrents.emplace(id, Torrent{id, h, cb}); return id; +#else // SYNC_USE_LIBTORRENT + throw TorrentError("SyncModule is compiled without libtorrent"); +#endif // SYNC_USE_LIBTORRENT } void TorrentClient::removeTorrent(TorrentId id) { +#ifdef SYNC_USE_LIBTORRENT std::lock_guard guard(_mutex); auto it = _torrents.find(id); @@ -219,9 +228,11 @@ void TorrentClient::removeTorrent(TorrentId id) { _session->remove_torrent(h); _torrents.erase(it); +#endif // SYNC_USE_LIBTORRENT } void TorrentClient::notify(TorrentId id) { +#ifdef SYNC_USE_LIBTORRENT TorrentProgressCallback callback; TorrentProgress progress; @@ -245,38 +256,8 @@ void TorrentClient::notify(TorrentId id) { } callback(progress); -} - -} // namespace openspace - -#else // SYNC_USE_LIBTORRENT - -namespace openspace { - -TorrentClient::TorrentClient() {} - -TorrentClient::~TorrentClient() {} - -void TorrentClient::initialize() {} - -void TorrentClient::deinitialize() {} - -void TorrentClient::pollAlerts() {} - -TorrentClient::TorrentId TorrentClient::addTorrentFile(std::string, std::string, - TorrentProgressCallback) -{ - throw TorrentError("SyncModule is not compiled with libtorrent"); -} - -TorrentClient::TorrentId TorrentClient::addMagnetLink(std::string, std::string, - TorrentProgressCallback) -{ - throw TorrentError("SyncModule is not compiled with libtorrent"); -} - -void TorrentClient::removeTorrent(TorrentId id) {} - -} // namespace openspace - #endif // SYNC_USE_LIBTORRENT +} + +} // namespace openspace + diff --git a/modules/sync/torrentclient.h b/modules/sync/torrentclient.h index 7e82485eb5..c81f67f8e5 100644 --- a/modules/sync/torrentclient.h +++ b/modules/sync/torrentclient.h @@ -28,6 +28,29 @@ #include #include +#include +#include +#include +#include +#include +#include +#include + +#ifdef SYNC_USE_LIBTORRENT +#include "libtorrent/torrent_handle.hpp" + +namespace libtorrent { class session; } + +#else // SYNC_USE_LIBTORRENT +// Dummy definition to make TorrentClient compile, these is not actually used if +// SYNC_USE_LIBTORRENT is FALSE +namespace libtorrent { + using torrent_handle = void*; + using session = void*; +} + +#endif // SYNC_USE_LIBTORRENT + namespace openspace { @@ -38,10 +61,11 @@ struct TorrentError : public ghoul::RuntimeError { * \param component The component that initiated the specification test * \pre \p result%'s TestResult::success must be \c false */ - TorrentError(std::string component); + TorrentError(std::string message); }; -class TorrentClientBase { + +class TorrentClient { public: struct TorrentProgress { bool finished = false; @@ -50,61 +74,22 @@ public: size_t nDownloadedBytes = 0; }; - virtual ~TorrentClientBase() = default; - using TorrentProgressCallback = std::function; - using TorrentId = int32_t; - virtual void initialize() = 0; - virtual void deinitialize() = 0; - virtual TorrentId addTorrentFile(std::string torrentFile, - std::string destination, - TorrentProgressCallback cb) = 0; - - virtual TorrentId addMagnetLink(std::string magnetLink, - std::string destination, - TorrentProgressCallback cb) = 0; - - virtual void removeTorrent(TorrentId id) = 0; - virtual void pollAlerts() = 0; -}; - -} // namespace openspace - -#ifdef SYNC_USE_LIBTORRENT - -#include -#include -#include -#include -#include -#include -#include - -#include "libtorrent/torrent_handle.hpp" - -namespace libtorrent { class session; } - -namespace openspace { - -class TorrentClient : public TorrentClientBase { -public: TorrentClient(); virtual ~TorrentClient(); - void initialize() override; - void deinitialize() override; - TorrentId addTorrentFile(std::string torrentFile, - std::string destination, - TorrentProgressCallback cb) override; + void initialize(); + void deinitialize(); + TorrentId addTorrentFile(std::string torrentFile, std::string destination, + TorrentProgressCallback cb); - TorrentId addMagnetLink(std::string magnetLink, - std::string destination, - TorrentProgressCallback cb) override; + TorrentId addMagnetLink(std::string magnetLink, std::string destination, + TorrentProgressCallback cb); - void removeTorrent(TorrentId id) override; - void pollAlerts() override; + void removeTorrent(TorrentId id); + void pollAlerts(); private: struct Torrent { @@ -126,31 +111,4 @@ private: } // namespace openspace -#else // SYNC_USE_LIBTORRENT - -namespace openspace { - -class TorrentClient : public TorrentClientBase { -public: - TorrentClient(); - virtual ~TorrentClient(); - - void initialize() override; - void deinitialize() override; - TorrentId addTorrentFile(std::string torrentFile, - std::string destination, - TorrentProgressCallback cb) override; - - TorrentId addMagnetLink(std::string magnetLink, - std::string destination, - TorrentProgressCallback cb) override; - - void removeTorrent(TorrentId id) override; - void pollAlerts() override; -}; - -} // namespace openspace - -#endif // SYNC_USE_LIBTORRENT - #endif // __OPENSPACE_MODULE_SYNC___TORRENTCLIENT___H__