Files
OpenSpace/modules/sync/torrentclient.cpp
2017-11-06 19:07:52 +01:00

102 lines
4.5 KiB
C++

/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2017 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#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 <openspace/openspace.h>
#include <ghoul/logging/logmanager.h>
namespace {
const char* _loggerCat = "TorrentClient";
}
namespace openspace {
TorrentClient::TorrentClient() {}
TorrentClient::~TorrentClient() {}
void TorrentClient::initialize() {
libtorrent::settings_pack settings;
_session = std::make_unique<libtorrent::session>();
settings.set_str(libtorrent::settings_pack::user_agent, "OpenSpace/" +
std::to_string(openspace::OPENSPACE_VERSION_MAJOR) + "." +
std::to_string(openspace::OPENSPACE_VERSION_MINOR) + "." +
std::to_string(openspace::OPENSPACE_VERSION_PATCH));
settings.set_bool(libtorrent::settings_pack::allow_multiple_connections_per_ip, true);
settings.set_bool(libtorrent::settings_pack::ignore_limits_on_local_network, true);
settings.set_int(libtorrent::settings_pack::connection_speed, 20);
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);
_session->apply_settings(settings);
_session->add_dht_router({ "router.utorrent.com", 6881 });
_session->add_dht_router({ "dht.transmissionbt.com", 6881 });
_session->add_dht_router({ "router.bittorrent.com", 6881 });
_session->add_dht_router({ "router.bitcomet.com", 6881 });
libtorrent::error_code ec;
_session->listen_on(std::make_pair(20280, 20290), ec);
_session->start_upnp();
}
int TorrentClient::addTorrent(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;
p.save_path = destination;
p.ti = std::make_shared<libtorrent::torrent_info>(torrentFile, ec, 0);
_session->add_torrent(p, ec);
std::vector<libtorrent::torrent_handle> handles = _session->get_torrents();
libtorrent::torrent_status s = handles[0].status();
LINFO("Torrent: " << s.total_wanted_done << " out of " << s.total_wanted);
}
void TorrentClient::removeTorrent(int id) {
}
}