mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-04-22 02:48:25 -05:00
Work on asset synchronization
This commit is contained in:
@@ -182,4 +182,17 @@ void HttpSynchronization::createSyncFile() {
|
||||
syncFile.close();
|
||||
}
|
||||
|
||||
float HttpSynchronization::nSynchronizedBytes() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
float HttpSynchronization::nTotalBytes() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool HttpSynchronization::nTotalBytesIsKnown() {
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
|
||||
@@ -37,10 +37,15 @@ class HttpSynchronizationJob;
|
||||
class HttpSynchronization : public ResourceSynchronization {
|
||||
public:
|
||||
HttpSynchronization(const ghoul::Dictionary& dict);
|
||||
virtual ~HttpSynchronization() = default;
|
||||
|
||||
static documentation::Documentation Documentation();
|
||||
|
||||
std::string directory() override;
|
||||
void synchronize() override;
|
||||
float nSynchronizedBytes() override;
|
||||
float nTotalBytes() override;
|
||||
bool nTotalBytesIsKnown() override;
|
||||
|
||||
private:
|
||||
std::vector<std::string> fileListUrls();
|
||||
|
||||
@@ -120,4 +120,16 @@ void TorrentSynchronization::synchronize() {
|
||||
return;
|
||||
}
|
||||
|
||||
float TorrentSynchronization::nSynchronizedBytes() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
float TorrentSynchronization::nTotalBytes() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool TorrentSynchronization::nTotalBytesIsKnown() {
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
@@ -47,6 +47,10 @@ public:
|
||||
std::string directory() override;
|
||||
void synchronize() override;
|
||||
|
||||
float nSynchronizedBytes() override;
|
||||
float nTotalBytes() override;
|
||||
bool nTotalBytesIsKnown() override;
|
||||
|
||||
private:
|
||||
std::string uniformResourceName() const;
|
||||
std::string _identifier;
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <libtorrent/session.hpp>
|
||||
#include <libtorrent/alert_types.hpp>
|
||||
#include <libtorrent/torrent_info.hpp>
|
||||
#include <libtorrent/magnet_uri.hpp>
|
||||
|
||||
#include <openspace/openspace.h>
|
||||
|
||||
@@ -36,6 +37,7 @@
|
||||
|
||||
namespace {
|
||||
const char* _loggerCat = "TorrentClient";
|
||||
std::chrono::milliseconds PollInterval(200);
|
||||
}
|
||||
|
||||
namespace openspace {
|
||||
@@ -77,25 +79,29 @@ void TorrentClient::initialize() {
|
||||
|
||||
_torrentThread = std::thread([this]() {
|
||||
while (_keepRunning) {
|
||||
std::vector<libtorrent::alert*> 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<lt::torrent_finished_alert>(a)) {
|
||||
LINFO(a->message());
|
||||
}
|
||||
if (lt::alert_cast<lt::torrent_error_alert>(a)) {
|
||||
LINFO(a->message());
|
||||
}
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
pollAlerts();
|
||||
std::this_thread::sleep_for(PollInterval);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
int TorrentClient::addTorrentFile(std::string torrentFile, std::string destination) {
|
||||
void TorrentClient::pollAlerts() {
|
||||
std::vector<libtorrent::alert*> 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<lt::torrent_finished_alert>(a)) {
|
||||
LINFO(a->message());
|
||||
}
|
||||
if (lt::alert_cast<lt::torrent_error_alert>(a)) {
|
||||
LINFO(a->message());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t TorrentClient::addTorrentFile(std::string torrentFile, std::string destination) {
|
||||
if (!_session) {
|
||||
LERROR("Torrent session not initialized when adding torrent");
|
||||
return -1;
|
||||
@@ -106,24 +112,37 @@ int TorrentClient::addTorrentFile(std::string torrentFile, std::string destinati
|
||||
p.save_path = destination;
|
||||
p.ti = std::make_shared<libtorrent::torrent_info>(torrentFile, ec, 0);
|
||||
|
||||
_session->add_torrent(p, ec);
|
||||
libtorrent::torrent_handle h = _session->add_torrent(p, ec);
|
||||
if (ec) {
|
||||
LERROR(torrentFile << ": " << ec.message());
|
||||
}
|
||||
|
||||
size_t id = _nextId++;
|
||||
_torrents.emplace(id, Torrent{id, h});
|
||||
return id;
|
||||
}
|
||||
|
||||
int TorrentClient::addMagnetLink(std::string magnetLink, std::string destination) {
|
||||
size_t 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;
|
||||
|
||||
libtorrent::add_torrent_params p = libtorrent::parse_magnet_uri(magnetLink, ec);
|
||||
p.save_path = destination;
|
||||
p.url = magnetLink;
|
||||
p.storage_mode = libtorrent::storage_mode_allocate;
|
||||
|
||||
_session->add_torrent(p, ec);
|
||||
libtorrent::torrent_handle h = _session->add_torrent(p, ec);
|
||||
if (ec) {
|
||||
LERROR(magnetLink << ": " << ec.message());
|
||||
}
|
||||
|
||||
size_t id = _nextId++;
|
||||
_torrents.emplace(id, Torrent{id, h});
|
||||
return id;
|
||||
}
|
||||
|
||||
void TorrentClient::removeTorrent(int id) {
|
||||
void TorrentClient::removeTorrent(size_t id) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -29,6 +29,9 @@
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "libtorrent/torrent_handle.hpp"
|
||||
|
||||
namespace libtorrent {
|
||||
class session;
|
||||
@@ -36,20 +39,25 @@ namespace libtorrent {
|
||||
|
||||
namespace openspace {
|
||||
|
||||
struct Torrent {
|
||||
std::string _torrentFile;
|
||||
std::string _destination;
|
||||
};
|
||||
|
||||
|
||||
class TorrentClient {
|
||||
public:
|
||||
struct Torrent {
|
||||
size_t id;
|
||||
libtorrent::torrent_handle handle;
|
||||
};
|
||||
|
||||
TorrentClient();
|
||||
~TorrentClient();
|
||||
void initialize();
|
||||
int addTorrentFile(std::string torrentFile, std::string destination);
|
||||
int addMagnetLink(std::string magnetLink, std::string destination);
|
||||
void removeTorrent(int id);
|
||||
size_t addTorrentFile(std::string torrentFile, std::string destination);
|
||||
size_t addMagnetLink(std::string magnetLink, std::string destination);
|
||||
void removeTorrent(size_t id);
|
||||
void pollAlerts();
|
||||
private:
|
||||
size_t _nextId = 0;
|
||||
std::unordered_map<size_t, Torrent> _torrents;
|
||||
std::unique_ptr<libtorrent::session> _session;
|
||||
std::thread _torrentThread;
|
||||
std::atomic_bool _keepRunning = true;
|
||||
|
||||
Reference in New Issue
Block a user