Skip to content

Commit

Permalink
Cleaned up TrackerController and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshasa committed Feb 7, 2025
1 parent f038ec2 commit 4055371
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 135 deletions.
2 changes: 2 additions & 0 deletions src/torrent/tracker/tracker_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "torrent/tracker.h"
#include "torrent/utils/log.h"

// TODO: Remove old logging categories.

#define LT_LOG_TRACKER_EVENTS(log_level, log_fmt, ...) \
lt_log_print(LOG_TRACKER_EVENTS, "tracker_manager", log_fmt, __VA_ARGS__);

Expand Down
50 changes: 25 additions & 25 deletions src/torrent/tracker_controller.cc
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#include "config.h"

#include "torrent/tracker_controller.h"

#include "rak/priority_queue_default.h"
#include "torrent/exceptions.h"
#include "torrent/download_info.h"
#include "torrent/tracker.h"
#include "torrent/tracker_controller.h"
#include "torrent/tracker_list.h"
#include "torrent/utils/log.h"

#include "globals.h"

#define LT_LOG_TRACKER(log_level, log_fmt, ...) \
lt_log_print_info(LOG_TRACKER_##log_level, m_tracker_list->info(), "tracker_controller", log_fmt, __VA_ARGS__);
#define LT_LOG_TRACKER_EVENTS(log_fmt, ...) \
lt_log_print_info(LOG_TRACKER_EVENTS, m_tracker_list->info(), "tracker_controller", log_fmt, __VA_ARGS__);

namespace torrent {

// TODO: Make these member variables.
struct tracker_controller_private {
rak::priority_item task_timeout;
rak::priority_item task_scrape;
Expand Down Expand Up @@ -61,14 +63,14 @@ TrackerController::~TrackerController() {
delete m_private;
}

rak::priority_item*
TrackerController::task_timeout() {
return &m_private->task_timeout;
bool
TrackerController::is_timeout_queued() const {
return m_private->task_timeout.is_queued();
}

rak::priority_item*
TrackerController::task_scrape() {
return &m_private->task_scrape;
bool
TrackerController::is_scrape_queued() const {
return m_private->task_scrape.is_queued();
}

int64_t
Expand Down Expand Up @@ -124,7 +126,7 @@ TrackerController::send_start_event() {
// send it when the tracker controller get's enabled.

// If the controller is already running, we insert this new event.

// Return, or something, if already active and sending?

if (m_flags & flag_send_start) {
Expand All @@ -134,9 +136,9 @@ TrackerController::send_start_event() {

m_flags &= ~mask_send;
m_flags |= flag_send_start;

if (!(m_flags & flag_active) || !m_tracker_list->has_usable()) {
LT_LOG_TRACKER(INFO, "Queueing started event.", 0);
LT_LOG_TRACKER_EVENTS("sending start event : queued", 0);
return;
}

Expand All @@ -146,7 +148,7 @@ TrackerController::send_start_event() {

// Do we use the old 'focus' thing?... Rather react on no reply,
// go into promiscious.
LT_LOG_TRACKER(INFO, "Sending started event.", 0);
LT_LOG_TRACKER_EVENTS("sending start event : requesting", 0);

close();
m_tracker_list->send_state_itr(m_tracker_list->find_usable(m_tracker_list->begin()), Tracker::EVENT_STARTED);
Expand All @@ -167,13 +169,13 @@ TrackerController::send_stop_event() {
m_flags &= ~mask_send;

if (!(m_flags & flag_active) || !m_tracker_list->has_usable()) {
LT_LOG_TRACKER(INFO, "Skipping stopped event as no tracker need it.", 0);
LT_LOG_TRACKER_EVENTS("sending stop event : skipped stopped event as no tracker needs it", 0);
return;
}

m_flags |= flag_send_stop;

LT_LOG_TRACKER(INFO, "Sending stopped event.", 0);
LT_LOG_TRACKER_EVENTS("sending stop event : requesting", 0);

close();

Expand All @@ -198,13 +200,11 @@ TrackerController::send_completed_event() {
m_flags |= flag_send_completed;

if (!(m_flags & flag_active) || !m_tracker_list->has_usable()) {
LT_LOG_TRACKER(INFO, "Queueing completed event.", 0);
LT_LOG_TRACKER_EVENTS("sending completed event : queued", 0);
return;
}

LT_LOG_TRACKER(INFO, "Sending completed event.", 0);

// Send to all trackers that would want to know.
LT_LOG_TRACKER_EVENTS("sending completed event : requesting", 0);

close();

Expand All @@ -230,7 +230,7 @@ TrackerController::send_update_event() {
if (!(m_flags & mask_send))
m_flags |= flag_send_update;

LT_LOG_TRACKER(INFO, "Sending update event.", 0);
LT_LOG_TRACKER_EVENTS("sending update event : requesting", 0);

m_tracker_list->send_state_itr(m_tracker_list->find_usable(m_tracker_list->begin()), Tracker::EVENT_NONE);

Expand Down Expand Up @@ -265,7 +265,7 @@ TrackerController::enable(int enable_flags) {
if (!(enable_flags & enable_dont_reset_stats))
m_tracker_list->clear_stats();

LT_LOG_TRACKER(INFO, "Called enable with %u trackers.", m_tracker_list->size());
LT_LOG_TRACKER_EVENTS("enabled : trackers:%u", m_tracker_list->size());

// Adding of the tracker requests gets done after the caller has had
// a chance to override the default behavior.
Expand All @@ -283,7 +283,7 @@ TrackerController::disable() {
m_tracker_list->close_all_excluding((1 << Tracker::EVENT_STOPPED) | (1 << Tracker::EVENT_COMPLETED));
priority_queue_erase(&taskScheduler, &m_private->task_timeout);

LT_LOG_TRACKER(INFO, "Called disable with %u trackers.", m_tracker_list->size());
LT_LOG_TRACKER_EVENTS("disabled : trackers:%u", m_tracker_list->size());
}

void
Expand All @@ -296,7 +296,7 @@ TrackerController::start_requesting() {
if ((m_flags & flag_active))
update_timeout(0);

LT_LOG_TRACKER(INFO, "Start requesting.", 0);
LT_LOG_TRACKER_EVENTS("started requesting", 0);
}

void
Expand All @@ -306,7 +306,7 @@ TrackerController::stop_requesting() {

m_flags &= ~flag_requesting;

LT_LOG_TRACKER(INFO, "Stop requesting.", 0);
LT_LOG_TRACKER_EVENTS("stopped requesting", 0);
}

uint32_t
Expand Down Expand Up @@ -537,7 +537,7 @@ TrackerController::receive_failure(Tracker* tracker, const std::string& msg) {
}

if (tracker == nullptr) {
LT_LOG_TRACKER(INFO, "Received failure msg:'%s'.", msg.c_str());
LT_LOG_TRACKER_EVENTS("received failure : tracker_id:%s msg:'%s'", tracker->tracker_id().c_str(), msg.c_str());
m_slot_failure(msg);
return;
}
Expand Down
12 changes: 7 additions & 5 deletions src/torrent/tracker_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
#include <torrent/common.h>
#include <torrent/tracker.h>

// TODO: Remove from export, rtorrent doesn't need this much api access.

// TODO: Remove all unused functions and slots, move to src/tracker. Then add a
// TrackerControllerWrapper that download and api uses.

// Refactor:
namespace rak { class priority_item; }

Expand Down Expand Up @@ -51,6 +56,8 @@ class LIBTORRENT_EXPORT TrackerController {
bool is_requesting() const { return m_flags & flag_requesting; }
bool is_failure_mode() const { return m_flags & flag_failure_mode; }
bool is_promiscuous_mode() const { return m_flags & flag_promiscuous_mode; }
bool is_timeout_queued() const;
bool is_scrape_queued() const;

TrackerList* tracker_list() { return m_tracker_list; }
TrackerList* tracker_list() const { return m_tracker_list; }
Expand All @@ -63,7 +70,6 @@ class LIBTORRENT_EXPORT TrackerController {
void manual_request(bool request_now);
void scrape_request(uint32_t seconds_to_request);

//protected:
void send_start_event();
void send_stop_event();
void send_completed_event();
Expand Down Expand Up @@ -91,10 +97,6 @@ class LIBTORRENT_EXPORT TrackerController {
slot_tracker& slot_tracker_enabled() { return m_slot_tracker_enabled; }
slot_tracker& slot_tracker_disabled() { return m_slot_tracker_disabled; }

// TEMP:
rak::priority_item* task_timeout();
rak::priority_item* task_scrape();

private:
void do_timeout();
void do_scrape();
Expand Down
12 changes: 6 additions & 6 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ LibTorrent_Test_Torrent_SOURCES = $(LibTorrent_Test_Common) \
torrent/object_static_map_test.h \
torrent/object_stream_test.cc \
torrent/object_stream_test.h \
torrent/tracker_controller_test.cc \
torrent/tracker_controller_test.h \
torrent/tracker_controller_features.cc \
torrent/tracker_controller_features.h \
torrent/tracker_controller_requesting.cc \
torrent/tracker_controller_requesting.h \
torrent/test_tracker_controller.cc \
torrent/test_tracker_controller.h \
torrent/test_tracker_controller_features.cc \
torrent/test_tracker_controller_features.h \
torrent/test_tracker_controller_requesting.cc \
torrent/test_tracker_controller_requesting.h \
torrent/tracker_list_test.cc \
torrent/tracker_list_test.h \
torrent/tracker_list_features_test.cc \
Expand Down
Loading

0 comments on commit 4055371

Please sign in to comment.