From 87595812d29c1cf4a9f34d8a9c5180410ec22f6b Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Mon, 6 Nov 2017 21:22:43 +0100 Subject: [PATCH 1/3] Remove BOM --- include/openspace/engine/configurationmanager.h | 2 +- modules/sync/syncs/httpsynchronization.cpp | 2 +- modules/sync/syncs/httpsynchronization.h | 2 +- src/engine/configurationmanager.cpp | 2 +- src/engine/configurationmanager_doc.inl | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/openspace/engine/configurationmanager.h b/include/openspace/engine/configurationmanager.h index 652f1a1332..d1a46f041e 100644 --- a/include/openspace/engine/configurationmanager.h +++ b/include/openspace/engine/configurationmanager.h @@ -1,4 +1,4 @@ -/***************************************************************************************** +/***************************************************************************************** * * * OpenSpace * * * diff --git a/modules/sync/syncs/httpsynchronization.cpp b/modules/sync/syncs/httpsynchronization.cpp index 539ef6c8aa..bb8b9ed52e 100644 --- a/modules/sync/syncs/httpsynchronization.cpp +++ b/modules/sync/syncs/httpsynchronization.cpp @@ -1,4 +1,4 @@ -/***************************************************************************************** +/***************************************************************************************** * * * OpenSpace * * * diff --git a/modules/sync/syncs/httpsynchronization.h b/modules/sync/syncs/httpsynchronization.h index d58c3ae0c9..ed6506c223 100644 --- a/modules/sync/syncs/httpsynchronization.h +++ b/modules/sync/syncs/httpsynchronization.h @@ -1,4 +1,4 @@ -/***************************************************************************************** +/***************************************************************************************** * * * OpenSpace * * * diff --git a/src/engine/configurationmanager.cpp b/src/engine/configurationmanager.cpp index 90f022ea4b..62fcd7c686 100644 --- a/src/engine/configurationmanager.cpp +++ b/src/engine/configurationmanager.cpp @@ -1,4 +1,4 @@ -/***************************************************************************************** +/***************************************************************************************** * * * OpenSpace * * * diff --git a/src/engine/configurationmanager_doc.inl b/src/engine/configurationmanager_doc.inl index bee3aadfc3..908774d734 100644 --- a/src/engine/configurationmanager_doc.inl +++ b/src/engine/configurationmanager_doc.inl @@ -1,4 +1,4 @@ -/***************************************************************************************** +/***************************************************************************************** * * * OpenSpace * * * From 3d8c7ad0a4ae9b30cf190b3a11350f0cc2aac741 Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Mon, 6 Nov 2017 22:11:58 +0100 Subject: [PATCH 2/3] Make screen log thread safe --- include/openspace/util/screenlog.h | 8 ++++++-- src/util/screenlog.cpp | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/openspace/util/screenlog.h b/include/openspace/util/screenlog.h index f92816cf2a..8335c25f78 100644 --- a/include/openspace/util/screenlog.h +++ b/include/openspace/util/screenlog.h @@ -29,6 +29,7 @@ #include #include +#include namespace openspace { @@ -104,7 +105,7 @@ public: * Returns the list of all stored LogEntry%s. * \return The list of all stored LogEntry%s */ - const std::vector& entries() const; + std::vector entries() const; private: /// The list of all LogEntry%s stored by this ScreenLog @@ -113,9 +114,12 @@ private: /// The time-to-live for the LogEntry%s in this ScreenLog. Is used by the /// #removeExpiredEntries method to remove expired entries. std::chrono::seconds _timeToLive; - + /// The minimum LogLevel of messages LogLevel _logLevel; + + /// A mutex to ensure thread-safety + mutable std::mutex _mutex; }; } // namespace openspace diff --git a/src/util/screenlog.cpp b/src/util/screenlog.cpp index aaa24260c5..5f4a6e46a3 100644 --- a/src/util/screenlog.cpp +++ b/src/util/screenlog.cpp @@ -38,6 +38,7 @@ ScreenLog::ScreenLog(std::chrono::seconds timeToLive, LogLevel logLevel) ScreenLog::~ScreenLog() {} void ScreenLog::removeExpiredEntries() { + std::lock_guard guard(_mutex); auto t = std::chrono::steady_clock::now(); auto ttl = _timeToLive; @@ -51,6 +52,7 @@ void ScreenLog::removeExpiredEntries() { } void ScreenLog::log(LogLevel level, const string& category, const string& message) { + std::lock_guard guard(_mutex); if (level >= _logLevel) { _entries.push_back({ level, @@ -62,7 +64,8 @@ void ScreenLog::log(LogLevel level, const string& category, const string& messag } } -const std::vector& ScreenLog::entries() const { +std::vector ScreenLog::entries() const { + std::lock_guard guard(_mutex); return _entries; } From 8da01993cd0cd5f6bc0f60cb1d9d8a0f1bcd149b Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Tue, 7 Nov 2017 00:00:26 +0100 Subject: [PATCH 3/3] Add support for Magnet Links --- assets/missions/newhorizons/spice/spice.asset | 3 +- modules/sync/syncs/torrentsynchronization.cpp | 37 ++++++++++++++---- modules/sync/syncs/torrentsynchronization.h | 4 +- modules/sync/torrentclient.cpp | 38 ++++++++++++++++--- modules/sync/torrentclient.h | 5 ++- 5 files changed, 70 insertions(+), 17 deletions(-) diff --git a/assets/missions/newhorizons/spice/spice.asset b/assets/missions/newhorizons/spice/spice.asset index 60eb057390..e02942a3d6 100644 --- a/assets/missions/newhorizons/spice/spice.asset +++ b/assets/missions/newhorizons/spice/spice.asset @@ -2,7 +2,8 @@ local kernelsPath = asset.syncedResource({ Type = "TorrentSynchronization", - TorrentFile = asset.localResource("new_horizons_kernels.torrent") + Identifier = "new_horizons", + Magnet = "magnet:?xt=urn:btih:4af38bdd42c5b29a0ef1ce4ab274cd91c017b8cc&dn=new_horizons" }) --AssetHelper.registerSpiceKernels(asset, kernels) \ No newline at end of file diff --git a/modules/sync/syncs/torrentsynchronization.cpp b/modules/sync/syncs/torrentsynchronization.cpp index 570f59bef0..790d89c2bc 100644 --- a/modules/sync/syncs/torrentsynchronization.cpp +++ b/modules/sync/syncs/torrentsynchronization.cpp @@ -37,8 +37,8 @@ namespace { const char* _loggerCat = "TorrentSynchronization"; - - const char* KeyTorrentFile = "TorrentFile"; + const char* KeyIdentifier = "Identifier"; + const char* KeyMagnet = "Magnet"; } namespace openspace { @@ -52,7 +52,8 @@ TorrentSynchronization::TorrentSynchronization(const ghoul::Dictionary& dict) "TorrentSynchroniztion" ); - _torrentFilePath = dict.value(KeyTorrentFile); + _identifier = dict.value(KeyIdentifier); + _magnetLink = dict.value(KeyMagnet); // Configure synchronization based on global settings in SyncModule // TODO: For testability and decreaing deps, make it possible to inject this instead. @@ -69,31 +70,51 @@ documentation::Documentation TorrentSynchronization::Documentation() { "torrent_synchronization", { { - KeyTorrentFile, + KeyIdentifier, new StringVerifier, Optional::No, - "An absolute path to a torrent file" + "A unique identifier for this torrent" }, + { + KeyMagnet, + new StringVerifier, + Optional::No, + "A magnet link identifying the torrent" + } } }; } +std::string TorrentSynchronization::uniformResourceName() const { + size_t begin = _magnetLink.find("=urn") + 1; + size_t end = _magnetLink.find('&', begin); + std::string xs = _magnetLink.substr(begin, end == std::string::npos ? end : (end - begin)); + std::transform(xs.begin(), xs.end(), xs.begin(), [](char x) { + if (x == ':') return '.'; + return x; + }); + return xs; +} + std::string TorrentSynchronization::directory() { ghoul::filesystem::Directory d( _synchronizationRoot + ghoul::filesystem::FileSystem::PathSeparator + "torrent" + ghoul::filesystem::FileSystem::PathSeparator + - "test" + _identifier + + ghoul::filesystem::FileSystem::PathSeparator + + uniformResourceName() ); return FileSys.absPath(d); } void TorrentSynchronization::synchronize() { - _torrentClient->addTorrent(_torrentFilePath, directory()); - + _torrentClient->addMagnetLink(_magnetLink, directory()); + bool hasSyncFile(); + void createSyncFile(); resolve(); return; diff --git a/modules/sync/syncs/torrentsynchronization.h b/modules/sync/syncs/torrentsynchronization.h index f37555e6a7..1a5c3fc515 100644 --- a/modules/sync/syncs/torrentsynchronization.h +++ b/modules/sync/syncs/torrentsynchronization.h @@ -48,7 +48,9 @@ public: void synchronize() override; private: - std::string _torrentFilePath; + std::string uniformResourceName() const; + std::string _identifier; + std::string _magnetLink; std::string _synchronizationRoot; TorrentClient* _torrentClient; }; diff --git a/modules/sync/torrentclient.cpp b/modules/sync/torrentclient.cpp index f4e101869b..046cd5b75a 100644 --- a/modules/sync/torrentclient.cpp +++ b/modules/sync/torrentclient.cpp @@ -71,15 +71,32 @@ void TorrentClient::initialize() { libtorrent::error_code ec; _session->listen_on(std::make_pair(20280, 20290), ec); _session->start_upnp(); + + _torrentThread = std::thread([this]() { + while (true) { + std::vector alerts; + _session->pop_alerts(&alerts); + + for (lt::alert const* a : alerts) { + LINFO(a->message()); + // if we receive the finished alert or an error, we're done + if (lt::alert_cast(a)) { + LINFO(a->message()); + } + if (lt::alert_cast(a)) { + LINFO(a->message()); + } + } + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + } + }); } - -int TorrentClient::addTorrent(std::string torrentFile, std::string destination) { +int TorrentClient::addTorrentFile(std::string torrentFile, std::string destination) { if (!_session) { LERROR("Torrent session not initialized when adding torrent"); return -1; } - libtorrent::error_code ec; libtorrent::add_torrent_params p; @@ -87,11 +104,20 @@ int TorrentClient::addTorrent(std::string torrentFile, std::string destination) p.ti = std::make_shared(torrentFile, ec, 0); _session->add_torrent(p, ec); +} - std::vector handles = _session->get_torrents(); - libtorrent::torrent_status s = handles[0].status(); +int TorrentClient::addMagnetLink(std::string magnetLink, std::string destination) { + if (!_session) { + LERROR("Torrent session not initialized when adding torrent"); + return -1; + } + libtorrent::error_code ec; + libtorrent::add_torrent_params p; - LINFO("Torrent: " << s.total_wanted_done << " out of " << s.total_wanted); + p.save_path = destination; + p.url = magnetLink; + + _session->add_torrent(p, ec); } void TorrentClient::removeTorrent(int id) { diff --git a/modules/sync/torrentclient.h b/modules/sync/torrentclient.h index aee9020baf..f448827f7b 100644 --- a/modules/sync/torrentclient.h +++ b/modules/sync/torrentclient.h @@ -27,6 +27,7 @@ #include #include +#include namespace libtorrent { class session; @@ -44,10 +45,12 @@ public: TorrentClient(); ~TorrentClient(); void initialize(); - int addTorrent(std::string torrentFile, std::string destination); + int addTorrentFile(std::string torrentFile, std::string destination); + int addMagnetLink(std::string magnetLink, std::string destination); void removeTorrent(int id); private: std::unique_ptr _session; + std::thread _torrentThread; }; } // namespace openspace