Skip to content

Commit

Permalink
Added TrackerController to TrackerManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshasa authored Feb 11, 2025
1 parent 0470aaf commit 85e8799
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 437 deletions.
57 changes: 30 additions & 27 deletions src/download/download_main.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#include "config.h"

#include "download/download_main.h"

#include <cassert>
#include <cstring>
#include <limits>

#include "data/chunk_list.h"
#include "download/available_list.h"
#include "download/chunk_selector.h"
#include "download/chunk_statistics.h"
#include "download/download_wrapper.h"
#include "protocol/extensions.h"
#include "protocol/handshake_manager.h"
#include "protocol/initial_seed.h"
Expand All @@ -21,14 +28,9 @@
#include "torrent/peer/peer_info.h"
#include "torrent/tracker_controller.h"
#include "torrent/tracker_list.h"
#include "torrent/tracker/tracker_manager.h"
#include "torrent/utils/log.h"

#include "available_list.h"
#include "chunk_selector.h"
#include "chunk_statistics.h"
#include "download_main.h"
#include "download_wrapper.h"

#define LT_LOG_THIS(log_level, log_fmt, ...) \
lt_log_print_info(LOG_TORRENT_##log_level, m_ptr->info(), "download", log_fmt, __VA_ARGS__);

Expand All @@ -40,13 +42,13 @@ DownloadInfo::DownloadInfo() :
m_upRate(60),
m_downRate(60),
m_skipRate(60),

m_uploadedBaseline(0),
m_completedBaseline(0),
m_sizePex(0),
m_maxSizePex(8),
m_metadataSize(0),

m_creationDate(0),
m_loadDate(rak::timer::current_seconds()),

Expand All @@ -56,6 +58,7 @@ DownloadInfo::DownloadInfo() :

DownloadMain::DownloadMain() :
m_info(new DownloadInfo),
m_tracker_list(new TrackerList),

m_choke_group(NULL),
m_chunkList(new ChunkList),
Expand All @@ -66,14 +69,7 @@ DownloadMain::DownloadMain() :
m_uploadThrottle(NULL),
m_downloadThrottle(NULL) {

m_tracker_list = new TrackerList();
m_tracker_controller = new TrackerController(m_tracker_list);

m_tracker_list->slot_success() = std::bind(&TrackerController::receive_success, m_tracker_controller, std::placeholders::_1, std::placeholders::_2);
m_tracker_list->slot_failure() = std::bind(&TrackerController::receive_failure, m_tracker_controller, std::placeholders::_1, std::placeholders::_2);
m_tracker_list->slot_scrape_success() = std::bind(&TrackerController::receive_scrape, m_tracker_controller, std::placeholders::_1);
m_tracker_list->slot_tracker_enabled() = std::bind(&TrackerController::receive_tracker_enabled, m_tracker_controller, std::placeholders::_1);
m_tracker_list->slot_tracker_disabled() = std::bind(&TrackerController::receive_tracker_disabled, m_tracker_controller, std::placeholders::_1);
// Only set trivial values here, the rest is done in DownloadWrapper.

m_connectionList = new ConnectionList(this);

Expand All @@ -94,17 +90,10 @@ DownloadMain::DownloadMain() :
}

DownloadMain::~DownloadMain() {
if (m_taskTrackerRequest.is_queued())
throw internal_error("DownloadMain::~DownloadMain(): m_taskTrackerRequest is queued.");

// Check if needed.
m_connectionList->clear();
m_tracker_list->clear();
assert(!m_taskTrackerRequest.is_queued() && "DownloadMain::~DownloadMain(): m_taskTrackerRequest is queued.");

if (m_info->size_pex() != 0)
throw internal_error("DownloadMain::~DownloadMain(): m_info->size_pex() != 0.");
assert(m_info->size_pex() == 0 && "DownloadMain::~DownloadMain(): m_info->size_pex() != 0.");

delete m_tracker_controller;
delete m_tracker_list;
delete m_connectionList;

Expand All @@ -117,6 +106,20 @@ DownloadMain::~DownloadMain() {
m_ut_pex_initial.clear();
}

void
DownloadMain::post_initialize() {
auto tracker_controller = new TrackerController(m_tracker_list);

m_tracker_list->slot_success() = std::bind(&TrackerController::receive_success, tracker_controller, std::placeholders::_1, std::placeholders::_2);
m_tracker_list->slot_failure() = std::bind(&TrackerController::receive_failure, tracker_controller, std::placeholders::_1, std::placeholders::_2);
m_tracker_list->slot_scrape_success() = std::bind(&TrackerController::receive_scrape, tracker_controller, std::placeholders::_1);
m_tracker_list->slot_tracker_enabled() = std::bind(&TrackerController::receive_tracker_enabled, tracker_controller, std::placeholders::_1);
m_tracker_list->slot_tracker_disabled() = std::bind(&TrackerController::receive_tracker_disabled, tracker_controller, std::placeholders::_1);

// TODO: Move tracker list to manager, and add the proper barrier for slots.
m_tracker_controller = manager->tracker_manager()->add_controller(info(), tracker_controller);
}

std::pair<ThrottleList*, ThrottleList*>
DownloadMain::throttles(const sockaddr* sa) {
ThrottlePair pair = ThrottlePair(NULL, NULL);
Expand Down Expand Up @@ -321,11 +324,11 @@ DownloadMain::receive_tracker_request() {
if ((info()->is_pex_enabled() && info()->size_pex()) > 0
|| connection_list()->size() + peer_list()->available_list()->size() / 2 >= connection_list()->min_size()) {

m_tracker_controller->stop_requesting();
m_tracker_controller.stop_requesting();
return;
}

m_tracker_controller->start_requesting();
m_tracker_controller.start_requesting();
}

bool
Expand Down
28 changes: 14 additions & 14 deletions src/download/download_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "torrent/download/group_entry.h"
#include "torrent/data/file_list.h"
#include "torrent/peer/peer_list.h"
#include "torrent/tracker/tracker_wrappers.h"

namespace torrent {

Expand All @@ -25,8 +26,6 @@ class choke_group;
class ConnectionList;
class DownloadWrapper;
class HandshakeManager;
class TrackerController;
class TrackerList;
class DownloadInfo;
class ThrottleList;
class InitialSeeding;
Expand All @@ -39,18 +38,23 @@ class DownloadMain {
DownloadMain();
~DownloadMain();

DownloadMain(const DownloadMain&) = delete;
void operator = (const DownloadMain&) = delete;

void post_initialize();

void open(int flags);
void close();

void start();
void stop();

class choke_group* choke_group() { return m_choke_group; }
const class choke_group* c_choke_group() const { return m_choke_group; }
void set_choke_group(class choke_group* grp) { m_choke_group = grp; }
class choke_group* choke_group() { return m_choke_group; }
const class choke_group* c_choke_group() const { return m_choke_group; }
void set_choke_group(class choke_group* grp) { m_choke_group = grp; }

TrackerController* tracker_controller() { return m_tracker_controller; }
TrackerList* tracker_list() { return m_tracker_list; }
TrackerControllerWrapper tracker_controller() { return m_tracker_controller; }
TrackerList* tracker_list() { return m_tracker_list; }

DownloadInfo* info() { return m_info; }

Expand Down Expand Up @@ -84,7 +88,7 @@ class DownloadMain {

DataBuffer get_ut_pex(bool initial) { return (initial ? m_ut_pex_initial : m_ut_pex_delta).clone(); }

bool want_pex_msg() { return m_info->is_pex_active() && m_peerList.available_list()->want_more(); };
bool want_pex_msg() { return m_info->is_pex_active() && m_peerList.available_list()->want_more(); };

void set_metadata_size(size_t s);

Expand Down Expand Up @@ -125,17 +129,13 @@ class DownloadMain {
rak::priority_item& delay_disconnect_peers() { return m_delayDisconnectPeers; }

private:
// Disable copy ctor and assignment.
DownloadMain(const DownloadMain&);
void operator = (const DownloadMain&);

void setup_start();
void setup_stop();

DownloadInfo* m_info;

TrackerController* m_tracker_controller;
TrackerList* m_tracker_list;
TrackerControllerWrapper m_tracker_controller;
TrackerList* m_tracker_list;

class choke_group* m_choke_group;

Expand Down
38 changes: 24 additions & 14 deletions src/download/download_wrapper.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#include "config.h"

#include "download/download_wrapper.h"

#include <iterator>
#include <stdlib.h>
#include <rak/file_stat.h>

#include "data/chunk_list.h"
#include "data/hash_queue.h"
#include "data/hash_torrent.h"
#include "download/available_list.h"
#include "download/chunk_selector.h"
#include "protocol/handshake_manager.h"
#include "protocol/peer_connection_base.h"
#include "torrent/exceptions.h"
Expand All @@ -19,14 +23,10 @@
#include "torrent/peer/connection_list.h"
#include "torrent/tracker_controller.h"
#include "torrent/tracker_list.h"
#include "torrent/tracker/tracker_manager.h"
#include "torrent/utils/log.h"
#include "utils/functional.h"

#include "available_list.h"
#include "chunk_selector.h"

#include "download_wrapper.h"

#define LT_LOG_THIS(log_fmt, ...) \
lt_log_print_info(LOG_TORRENT_INFO, this->info(), "download", log_fmt, __VA_ARGS__);
#define LT_LOG_STORAGE_ERRORS(log_fmt, ...) \
Expand All @@ -47,8 +47,6 @@ DownloadWrapper::DownloadWrapper() :

m_main->peer_list()->set_info(info());
m_main->tracker_list()->set_info(info());
m_main->tracker_controller()->slot_success() = std::bind(&DownloadWrapper::receive_tracker_success, this, std::placeholders::_1);
m_main->tracker_controller()->slot_failure() = std::bind(&DownloadWrapper::receive_tracker_failed, this, std::placeholders::_1);

m_main->chunk_list()->slot_storage_error() = std::bind(&DownloadWrapper::receive_storage_error, this, std::placeholders::_1);
}
Expand All @@ -62,7 +60,15 @@ DownloadWrapper::~DownloadWrapper() {

// If the client wants to do a quick cleanup after calling close, it
// will need to manually cancel the tracker requests.
m_main->tracker_controller()->close();
m_main->tracker_controller().close();

// Check if needed.
m_main->connection_list()->clear();
m_main->tracker_list()->clear();

// TODO: Check first, and return if zero. Need to make the below shared ptrs.
if (info()->hash() != HashString::new_zero())
manager->tracker_manager()->remove_controller(m_main->tracker_controller());

delete m_hashChecker;
delete m_bencode;
Expand Down Expand Up @@ -92,6 +98,11 @@ DownloadWrapper::initialize(const std::string& hash, const std::string& id) {
// Connect various signals and slots.
m_hashChecker->slot_check_chunk() = std::bind(&DownloadWrapper::check_chunk_hash, this, std::placeholders::_1);
m_hashChecker->delay_checked().slot() = std::bind(&DownloadWrapper::receive_initial_hash, this);

m_main->post_initialize();

m_main->tracker_controller().set_slots([this](auto l) { return receive_tracker_success(l); },
[this](auto& m) { return receive_tracker_failed(m); });
}

void
Expand Down Expand Up @@ -119,7 +130,7 @@ DownloadWrapper::close() {

bool
DownloadWrapper::is_stopped() const {
return !m_main->tracker_controller()->is_active() && !m_main->tracker_list()->has_active();
return !m_main->tracker_controller().is_active() && !m_main->tracker_list()->has_active();
}

void
Expand All @@ -144,7 +155,7 @@ DownloadWrapper::receive_initial_hash() {

if (data()->slot_initial_hash())
data()->slot_initial_hash()();
}
}

void
DownloadWrapper::receive_hash_done(ChunkHandle handle, const char* hash) {
Expand All @@ -155,7 +166,6 @@ DownloadWrapper::receive_hash_done(ChunkHandle handle, const char* hash) {
throw internal_error("DownloadWrapper::receive_hash_done(...) called but the download is not open.");

if (m_hashChecker->is_checking()) {

if (hash == NULL) {
m_hashChecker->receive_chunk_cleared(handle.index());

Expand Down Expand Up @@ -195,7 +205,7 @@ DownloadWrapper::receive_hash_done(ChunkHandle handle, const char* hash) {
priority_queue_erase(&taskScheduler, &m_main->delay_partially_restarted());
priority_queue_update(&taskScheduler, &m_main->delay_partially_done(), cachedTime);
}

if (!m_main->have_queue()->empty() && m_main->have_queue()->front().first >= cachedTime)
m_main->have_queue()->emplace_front(m_main->have_queue()->front().first + 1, handle.index());
else
Expand Down Expand Up @@ -225,8 +235,8 @@ DownloadWrapper::receive_storage_error(const std::string& str) {
m_main->stop();
close();

m_main->tracker_controller()->disable();
m_main->tracker_controller()->close();
m_main->tracker_controller().disable();
m_main->tracker_controller().close();

LT_LOG_STORAGE_ERRORS("%s", str.c_str());
}
Expand Down
Loading

0 comments on commit 85e8799

Please sign in to comment.