From 6692f056551099aa2be9f6066e5dc6599dba3e74 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 1 Feb 2011 03:25:40 +0000 Subject: [PATCH] added alerts for added and removed torrents --- ChangeLog | 1 + bindings/python/src/alert.cpp | 9 +++++ docs/manual.rst | 31 ++++++++++++++++ include/libtorrent/alert_types.hpp | 24 +++++++++++++ src/alert.cpp | 9 +++++ src/session_impl.cpp | 58 ++++++++++++++++-------------- 6 files changed, 105 insertions(+), 27 deletions(-) diff --git a/ChangeLog b/ChangeLog index 797de17ba..37c7f4f41 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * added alerts for added and removed torrents * expanded plugin interface to support session wide states * made the metadata block requesting algorithm more robust against hash check failures * support a separate option to use proxies for peers or not diff --git a/bindings/python/src/alert.cpp b/bindings/python/src/alert.cpp index 126f6c55e..1a98da050 100644 --- a/bindings/python/src/alert.cpp +++ b/bindings/python/src/alert.cpp @@ -90,6 +90,15 @@ void bind_alert() .def_readonly("url", &tracker_alert::url) ; + class_, noncopyable>( + "torrent_added_alert", no_init) + ; + + class_, noncopyable>( + "torrent_removed_alert", no_init) + .def_readonly("info_hash", &torrent_removed_alert::info_hash) + ; + class_, noncopyable>( "read_piece_alert", 0, no_init) .add_property("buffer", get_buffer) diff --git a/docs/manual.rst b/docs/manual.rst index 0775a83b3..c349a78cf 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -5903,6 +5903,37 @@ There's also a base class for all alerts referring to tracker events:: The specific alerts are: +torrent_added_alert +------------------- + +The ``torrent_added_alert`` is posted once every time a torrent is added. +It doesn't contain any members of its own, but inherits the torrent handle +from its base class. +It's posted when the ``status_notification`` bit is set in the alert mask. + +:: + + struct torrent_added_alert: torrent_alert + { + // ... + }; + +torrent_removed_alert +--------------------- + +The ``torrent_removed_alert`` is posted whenever a torrent is removed. Since +the torrent handle in its baseclass will always be invalid (since the torrent +is already removed) it has the info hash as a member, to identify it. +It's posted when the ``status_notification`` bit is set in the alert mask. + +:: + + struct torrent_removed_alert: torrent_alert + { + // ... + sha1_hash info_hash; + }; + read_piece_alert ---------------- diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 48629c5ea..f4cb04cb9 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -107,6 +107,30 @@ namespace libtorrent virtual int category() const { return static_category; } \ virtual char const* what() const { return #name; } + struct TORRENT_EXPORT torrent_added_alert: torrent_alert + { + torrent_added_alert(torrent_handle const& h) + : torrent_alert(h) + {} + + TORRENT_DEFINE_ALERT(torrent_added_alert); + const static int static_category = alert::status_notification; + virtual std::string message() const; + }; + + struct TORRENT_EXPORT torrent_removed_alert: torrent_alert + { + torrent_removed_alert(torrent_handle const& h, sha1_hash const& ih) + : torrent_alert(h) + , info_hash(ih) + {} + + TORRENT_DEFINE_ALERT(torrent_removed_alert); + const static int static_category = alert::status_notification; + virtual std::string message() const; + sha1_hash info_hash; + }; + struct TORRENT_EXPORT read_piece_alert: torrent_alert { read_piece_alert(torrent_handle const& h diff --git a/src/alert.cpp b/src/alert.cpp index bcb2cd9cf..82d41b7f8 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -535,7 +535,16 @@ namespace libtorrent { snprintf(msg, sizeof(msg), " ERROR: %s", error.message().c_str()); return torrent_alert::message() + msg; } + + std::string torrent_added_alert::message() const + { + return torrent_alert::message() + " added"; + } + std::string torrent_removed_alert::message() const + { + return torrent_alert::message() + " removed"; + } } // namespace libtorrent diff --git a/src/session_impl.cpp b/src/session_impl.cpp index a1b734da4..b0f4619cc 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -3703,6 +3703,9 @@ namespace aux { m_uuids.insert(std::make_pair(params.uuid.empty() ? params.url : params.uuid, torrent_ptr)); + if (m_alerts.should_post()) + m_alerts.post_alert(torrent_added_alert(torrent_ptr->get_handle())); + return torrent_handle(torrent_ptr); } @@ -3778,43 +3781,44 @@ namespace aux { i = m_torrents.find(urlhash); } - if (i != m_torrents.end()) - { - torrent& t = *i->second; - if (options & session::delete_files) - t.delete_files(); - t.abort(); + if (i == m_torrents.end()) return; + + torrent& t = *i->second; + if (options & session::delete_files) + t.delete_files(); + t.abort(); + + if (m_alerts.should_post()) + m_alerts.post_alert(torrent_removed_alert(t.get_handle(), t.info_hash())); #ifdef TORRENT_DEBUG - sha1_hash i_hash = t.torrent_file().info_hash(); + sha1_hash i_hash = t.torrent_file().info_hash(); #endif #ifndef TORRENT_DISABLE_DHT - if (i == m_next_dht_torrent) - ++m_next_dht_torrent; + if (i == m_next_dht_torrent) + ++m_next_dht_torrent; #endif - if (i == m_next_lsd_torrent) - ++m_next_lsd_torrent; - if (i == m_next_connect_torrent) - ++m_next_connect_torrent; + if (i == m_next_lsd_torrent) + ++m_next_lsd_torrent; + if (i == m_next_connect_torrent) + ++m_next_connect_torrent; - t.set_queue_position(-1); - m_torrents.erase(i); + t.set_queue_position(-1); + m_torrents.erase(i); #ifndef TORRENT_DISABLE_DHT - if (m_next_dht_torrent == m_torrents.end()) - m_next_dht_torrent = m_torrents.begin(); + if (m_next_dht_torrent == m_torrents.end()) + m_next_dht_torrent = m_torrents.begin(); #endif - if (m_next_lsd_torrent == m_torrents.end()) - m_next_lsd_torrent = m_torrents.begin(); - if (m_next_connect_torrent == m_torrents.end()) - m_next_connect_torrent = m_torrents.begin(); + if (m_next_lsd_torrent == m_torrents.end()) + m_next_lsd_torrent = m_torrents.begin(); + if (m_next_connect_torrent == m_torrents.end()) + m_next_connect_torrent = m_torrents.begin(); - std::list >::iterator k - = std::find(m_queued_for_checking.begin(), m_queued_for_checking.end(), tptr); - if (k != m_queued_for_checking.end()) m_queued_for_checking.erase(k); - TORRENT_ASSERT(m_torrents.find(i_hash) == m_torrents.end()); - return; - } + std::list >::iterator k + = std::find(m_queued_for_checking.begin(), m_queued_for_checking.end(), tptr); + if (k != m_queued_for_checking.end()) m_queued_for_checking.erase(k); + TORRENT_ASSERT(m_torrents.find(i_hash) == m_torrents.end()); } bool session_impl::listen_on(