From 360c6a6e16bf1681c333a204604f093618402318 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 17 May 2013 03:19:23 +0000 Subject: [PATCH] introduced a new alert torrent_update_alert, for when a torrent_handle changes info-hash --- bindings/python/src/alert.cpp | 6 ++++++ docs/manual.rst | 26 ++++++++++++++++++++++++++ include/libtorrent/alert_types.hpp | 19 +++++++++++++++++++ src/alert.cpp | 9 +++++++++ src/torrent.cpp | 3 +++ 5 files changed, 63 insertions(+) diff --git a/bindings/python/src/alert.cpp b/bindings/python/src/alert.cpp index 368cee31d..3fab65131 100644 --- a/bindings/python/src/alert.cpp +++ b/bindings/python/src/alert.cpp @@ -521,4 +521,10 @@ void bind_alert() .def_readonly("error", &add_torrent_alert::error) .add_property("params", &get_params) ; + + class_, noncopyable>( + "torrent_update_alert", no_init) + .def_readonly("old_ih", &torrent_update_alert::old_ih) + .def_readonly("new_ih", &torrent_update_alert::new_ih) + ; } diff --git a/docs/manual.rst b/docs/manual.rst index 364a62499..d3dccfd4f 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -463,6 +463,12 @@ the torrent will be stopped and the torrent error state (``torrent_status::error will indicate what went wrong. The ``url`` may refer to a magnet link or a regular http URL. +If it refers to an HTTP URL, the info-hash for the added torrent will not be the +true info-hash of the .torrent. Instead a placeholder, unique, info-hash is used +which is later updated once the .torrent file has been downloaded. + +Once the info-hash change happens, a torrent_update_alert_ is posted. + ``dht_nodes`` is a list of hostname and port pairs, representing DHT nodes to be added to the session (if DHT is enabled). The hostname may be an IP address. @@ -7702,6 +7708,26 @@ this message was posted. Note that you can map a torrent status to a specific to via its ``handle`` member. The receiving end is suggested to have all torrents sorted by the ``torrent_handle`` or hashed by it, for efficient updates. +torrent_update_alert +-------------------- + +When a torrent changes its info-hash, this alert is posted. This only happens in very +specific cases. For instance, when a torrent is downloaded from a URL, the true info +hash is not known immediately. First the .torrent file must be downloaded and parsed. + +Once this download completes, the ``torrent_update_alert`` is posted to notify the client +of the info-hash changing. + +:: + + struct torrent_update_alert: torrent_alert + { + // ... + sha1_hash old_ih; + sha1_hash new_ih; + }; + +``old_ih`` and ``new_ih`` are the previous and new info-hash for the torrent, respectively. alert dispatcher ================ diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 5d224e602..3c722d2de 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -1343,6 +1343,25 @@ namespace libtorrent std::vector status; }; + struct TORRENT_EXPORT torrent_update_alert : torrent_alert + { + torrent_update_alert(torrent_handle h, sha1_hash const& old_hash, sha1_hash const& new_hash) + : torrent_alert(h) + , old_ih(old_hash) + , new_ih(new_hash) + {} + + TORRENT_DEFINE_ALERT(torrent_update_alert); + + const static int static_category = alert::status_notification; + virtual std::string message() const; + virtual bool discardable() const { return false; } + + sha1_hash old_ih; + sha1_hash new_ih; + }; + + #undef TORRENT_DEFINE_ALERT } diff --git a/src/alert.cpp b/src/alert.cpp index 8c22f5047..b68aa39ec 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -479,5 +479,14 @@ namespace libtorrent { return msg; } + std::string torrent_update_alert::message() const + { + char msg[200]; + snprintf(msg, sizeof(msg), " torrent changed info-hash from: %s to %s" + , to_hex(old_ih.to_string()).c_str() + , to_hex(new_ih.to_string()).c_str()); + return torrent_alert::message() + msg; + } + } // namespace libtorrent diff --git a/src/torrent.cpp b/src/torrent.cpp index 0d05b1eec..ad284f992 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -725,6 +725,9 @@ namespace libtorrent m_ses.remove_torrent_impl(me, 0); + if (alerts().should_post()) + alerts().post_alert(torrent_update_alert(get_handle(), info_hash(), tf->info_hash())); + m_torrent_file = tf; // now, we might already have this torrent in the session.