From ecb538b4b26353a8423061a6bf985377954fd972 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 3 Jul 2008 10:05:51 +0000 Subject: [PATCH] added alert for torrent state changes. Fixes #360 --- examples/client_test.cpp | 18 +++++++++++------- include/libtorrent/alert_types.hpp | 15 +++++++++++++++ include/libtorrent/torrent.hpp | 1 + src/torrent.cpp | 25 ++++++++++++++++++------- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index fc78f67f4..0ed8d9ddc 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -584,6 +584,10 @@ libtorrent::torrent_handle get_active_torrent(handles_t const& handles) return i->second; } +static char const* state_str[] = + {"checking (q)", "checking", "connecting", "dl metadata" + , "downloading", "finished", "seeding", "allocating"}; + int main(int ac, char* av[]) { #if BOOST_VERSION < 103400 @@ -1181,6 +1185,12 @@ int main(int ac, char* av[]) if (h.is_paused() && !h.is_auto_managed()) ses.remove_torrent(h); } } + else if (state_changed_alert* p = dynamic_cast(a.get())) + { + std::string name; + if (p->handle.is_valid()) name = p->handle.name(); + event_string << "(" << name << ") " << p->msg() << " " << state_str[p->state]; + } else if (torrent_alert* p = dynamic_cast(a.get())) { std::string name; @@ -1266,13 +1276,7 @@ int main(int ac, char* av[]) if (paused && !auto_managed) out << "paused"; else if (paused && auto_managed) out << "queued"; - else - { - static char const* state_str[] = - {"checking (q)", "checking", "connecting", "dl metadata" - , "downloading", "finished", "seeding", "allocating"}; - out << state_str[s.state]; - } + else out << state_str[s.state]; int seeds = 0; int downloaders = 0; diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 1fc8116ff..5d26a66c2 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -68,6 +68,21 @@ namespace libtorrent std::string name; }; + struct TORRENT_EXPORT state_changed_alert: torrent_alert + { + state_changed_alert(torrent_handle const& h + , torrent_status::state_t const& state_ + , std::string const& msg) + : torrent_alert(h, alert::info, msg) + , state(state_) + {} + + virtual std::auto_ptr clone() const + { return std::auto_ptr(new state_changed_alert(*this)); } + + torrent_status::state_t state; + }; + struct TORRENT_EXPORT tracker_alert: torrent_alert { tracker_alert(torrent_handle const& h diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 61e04f5c1..b1c2cc541 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -167,6 +167,7 @@ namespace libtorrent bool is_aborted() const { return m_abort; } torrent_status::state_t state() const { return m_state; } + void set_state(torrent_status::state_t s); session_settings const& settings() const; diff --git a/src/torrent.cpp b/src/torrent.cpp index 17ac6b3cd..d7e989d37 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -446,7 +446,7 @@ namespace libtorrent std::copy(url_seeds.begin(), url_seeds.end(), std::inserter(m_web_seeds , m_web_seeds.begin())); - m_state = torrent_status::queued_for_checking; + set_state(torrent_status::queued_for_checking); if (m_resume_entry.type() == lazy_entry::dict_t) { @@ -654,7 +654,7 @@ namespace libtorrent , int((m_torrent_file->total_size()+m_block_size-1)/m_block_size)); // assume that we don't have anything m_files_checked = false; - m_state = torrent_status::queued_for_checking; + set_state(torrent_status::queued_for_checking); if (m_auto_managed) set_queue_position((std::numeric_limits::max)()); @@ -691,7 +691,7 @@ namespace libtorrent void torrent::start_checking() { - m_state = torrent_status::checking_files; + set_state(torrent_status::checking_files); m_storage->async_check_files(bind( &torrent::on_piece_checked @@ -3061,7 +3061,7 @@ namespace libtorrent , "torrent has finished downloading")); } - m_state = torrent_status::finished; + set_state(torrent_status::finished); set_queue_position(-1); // we have to call completed() before we start @@ -3102,7 +3102,7 @@ namespace libtorrent INVARIANT_CHECK; TORRENT_ASSERT(!is_finished()); - m_state = torrent_status::downloading; + set_state(torrent_status::downloading); set_queue_position((std::numeric_limits::max)()); } @@ -3116,7 +3116,7 @@ namespace libtorrent // make the next tracker request // be a completed-event m_event = tracker_request::completed; - m_state = torrent_status::seeding; + set_state(torrent_status::seeding); force_tracker_request(); } @@ -3188,7 +3188,7 @@ namespace libtorrent TORRENT_ASSERT(m_torrent_file->is_valid()); INVARIANT_CHECK; - m_state = torrent_status::connecting_to_tracker; + set_state(torrent_status::connecting_to_tracker); if (!is_seed()) { @@ -4039,6 +4039,17 @@ namespace libtorrent } } + void torrent::set_state(torrent_status::state_t s) + { + if (m_state == s) return; + m_state = s; + if (m_ses.m_alerts.should_post(alert::info)) + { + m_ses.m_alerts.post_alert(state_changed_alert(get_handle() + , s, "torrent status changed")); + } + } + torrent_status torrent::status() const { INVARIANT_CHECK;