Allow sync module to be compiled without libtorrent

This commit is contained in:
Emil Axelsson
2017-12-18 17:36:52 +01:00
parent 90243cf388
commit ef2633980b
6 changed files with 166 additions and 52 deletions

View File

@@ -59,22 +59,26 @@ create_new_module(
${HEADER_FILES} ${SOURCE_FILES}
)
#####
# Libtorrent
#####
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
SET(LIBTORRENT_encryption OFF CACHE BOOL "Use OpenSSL Encryption" FORCE)
SET(LIBTORRENT_shared OFF CACHE BOOL "Use Libtorrent as shared library" FORCE)
option(OPENSPACE_MODULE_SYNC_USE_LIBTORRENT "Use libtorrent" ON)
include_external_library(
${sync_module}
torrent-rasterbar
${CMAKE_CURRENT_SOURCE_DIR}/ext/libtorrent
)
target_include_directories(
${sync_module}
SYSTEM PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/ext/libtorrent/include
)
if (OPENSPACE_MODULE_SYNC_USE_LIBTORRENT)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
SET(LIBTORRENT_encryption OFF CACHE BOOL "Use OpenSSL Encryption" FORCE)
SET(LIBTORRENT_shared OFF CACHE BOOL "Use Libtorrent as shared library" FORCE)
include_external_library(
${sync_module}
torrent-rasterbar
${CMAKE_CURRENT_SOURCE_DIR}/ext/libtorrent
)
target_include_directories(
${sync_module}
SYSTEM PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/ext/libtorrent/include
)
endif () # OPENSPACE_MODULE_SYNC_USE_LIBTORRENT

View File

@@ -124,13 +124,21 @@ void TorrentSynchronization::start() {
}
_enabled = true;
_torrentId = _torrentClient->addMagnetLink(
_magnetLink,
directory(),
[this](TorrentClient::TorrentProgress p) {
updateTorrentProgress(p);
try {
_torrentId = _torrentClient->addMagnetLink(
_magnetLink,
directory(),
[this](TorrentClient::TorrentProgress p) {
updateTorrentProgress(p);
}
);
} catch (const TorrentError& e) {
LERROR("Failed to synchronize '" << name() <<
"'.\nOpenSpace needs to be linked with libtorrent.");
if (!isResolved()) {
reject();
}
);
}
}
void TorrentSynchronization::cancel() {

View File

@@ -32,11 +32,6 @@
#include <ghoul/misc/dictionary.h>
namespace libtorrent {
class session;
struct torrent_handle;
}
namespace openspace {
class TorrentSynchronizationJob;

View File

@@ -24,13 +24,6 @@
#include "torrentclient.h"
#include <libtorrent/entry.hpp>
#include <libtorrent/bencode.hpp>
#include <libtorrent/session.hpp>
#include <libtorrent/alert_types.hpp>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <openspace/openspace.h>
#include <ghoul/logging/logmanager.h>
@@ -40,6 +33,22 @@ namespace {
std::chrono::milliseconds PollInterval(200);
}
namespace openspace {
TorrentError::TorrentError(std::string component)
: RuntimeError("TorrentClient", component)
{}
}
#ifdef OPENSPACE_MODULE_SYNC_USE_LIBTORRENT
#include <libtorrent/entry.hpp>
#include <libtorrent/bencode.hpp>
#include <libtorrent/session.hpp>
#include <libtorrent/alert_types.hpp>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/magnet_uri.hpp>
namespace openspace {
TorrentClient::TorrentClient() {}
@@ -200,5 +209,37 @@ void TorrentClient::notify(TorrentId id) {
}
}
#else // OPENSPACE_MODULE_SYNC_USE_LIBTORRENT
namespace openspace {
TorrentClient::TorrentClient() {}
TorrentClient::~TorrentClient() {}
void TorrentClient::initialize() {}
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) {}
}
#endif // OPENSPACE_MODULE_SYNC_USE_LIBTORRENT

View File

@@ -25,6 +25,53 @@
#ifndef __OPENSPACE_MODULE_SYNC___TORRENTCLIENT___H__
#define __OPENSPACE_MODULE_SYNC___TORRENTCLIENT___H__
#include <functional>
#include <ghoul/misc/exception.h>
namespace openspace {
struct TorrentError : public ghoul::RuntimeError {
/**
* Creates the SpecificationError exception instance.
* \param result The offending TestResult that is passed on
* \param component The component that initiated the specification test
* \pre \p result%'s TestResult::success must be \c false
*/
TorrentError(std::string component);
};
class TorrentClientBase {
public:
struct TorrentProgress {
bool finished = false;
bool nTotalBytesKnown = false;
size_t nTotalBytes = 0;
size_t nDownloadedBytes = 0;
};
virtual ~TorrentClientBase() = default;
using TorrentProgressCallback = std::function<void(TorrentProgress)>;
using TorrentId = int32_t;
virtual void initialize() = 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 OPENSPACE_MODULE_SYNC_USE_LIBTORRENT
#include <atomic>
#include <string>
#include <memory>
@@ -40,35 +87,29 @@ namespace libtorrent {
namespace openspace {
class TorrentClient {
class TorrentClient : public TorrentClientBase {
public:
struct TorrentProgress {
bool finished = false;
bool nTotalBytesKnown = false;
size_t nTotalBytes = 0;
size_t nDownloadedBytes = 0;
};
TorrentClient();
virtual ~TorrentClient();
using TorrentProgressCallback = std::function<void(TorrentProgress)>;
void initialize() override;
TorrentId addTorrentFile(std::string torrentFile,
std::string destination,
TorrentProgressCallback cb) override;
using TorrentId = int32_t;
TorrentId addMagnetLink(std::string magnetLink,
std::string destination,
TorrentProgressCallback cb) override;
void removeTorrent(TorrentId id) override;
void pollAlerts() override;
private:
struct Torrent {
TorrentId id;
libtorrent::torrent_handle handle;
TorrentProgressCallback callback;
};
TorrentClient();
~TorrentClient();
void initialize();
TorrentId addTorrentFile(std::string torrentFile, std::string destination, TorrentProgressCallback cb);
TorrentId addMagnetLink(std::string magnetLink, std::string destination, TorrentProgressCallback cb);
void removeTorrent(TorrentId id);
void pollAlerts();
private:
void notify(TorrentId id);
std::unordered_map<TorrentId, Torrent> _torrents;
@@ -80,4 +121,30 @@ private:
} // namespace openspace
#else // OPENSPACE_MODULE_SYNC_USE_LIBTORRENT
namespace openspace {
class TorrentClient : public TorrentClientBase {
public:
TorrentClient();
virtual ~TorrentClient();
void initialize() 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 // OPENSPACE_MODULE_SYNC_USE_LIBTORRENT
#endif // __OPENSPACE_MODULE_SYNC___TORRENTCLIENT___H__

View File

@@ -46,7 +46,6 @@
#include <openspace/engine/openspaceengine.h>
#include <openspace/engine/wrapper/windowwrapper.h>
class AssetLoaderTest;
namespace {