deprecate relative times in torrent_status, replaced by std::chrono::time_point

This commit is contained in:
arvidn 2016-11-10 17:08:32 -05:00 committed by Arvid Norberg
parent bba121d010
commit c9333849e6
14 changed files with 132 additions and 42 deletions

View File

@ -1,3 +1,4 @@
* deprecate relative times in torrent_status, replaced by std::chrono::time_point
* refactor in alert types to use more const fields and more clear API
* changed session_stats_alert counters type to signed (std::int64_t)
* remove torrent eviction/ghost torrent feature

View File

@ -6,6 +6,7 @@
#include "libtorrent/socket.hpp"
#include "libtorrent/address.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/time.hpp"
#include <vector>
using namespace boost::python;

View File

@ -7,6 +7,7 @@
#include "optional.hpp"
#include <boost/version.hpp>
#include "libtorrent/time.hpp"
#include <ctime>
using namespace boost::python;
namespace lt = libtorrent;
@ -27,14 +28,16 @@ object import(str name)
object datetime_timedelta;
object datetime_datetime;
struct chrono_time_duration_to_python
template <typename Duration>
struct chrono_duration_to_python
{
static PyObject* convert(lt::time_duration const& d)
static PyObject* convert(Duration const& d)
{
std::int64_t const us = lt::total_microseconds(d);
object result = datetime_timedelta(
0 // days
, 0 // seconds
, lt::total_microseconds(d)
, us / 1000000 // seconds
, us % 1000000 // microseconds
);
return incref(result.ptr());
@ -55,6 +58,28 @@ struct time_duration_to_python
}
};
struct time_point_to_python
{
static PyObject* convert(lt::time_point pt)
{
using std::chrono::system_clock;
using std::chrono::duration_cast;
time_t const tm = system_clock::to_time_t(system_clock::now()
+ duration_cast<system_clock::duration>(pt - lt::clock_type::now()));
std::tm* date = std::gmtime(&tm);
object result = datetime_datetime(
(int)1900 + date->tm_year
, (int)date->tm_mon
, (int)date->tm_mday
, date->tm_hour
, date->tm_min
, date->tm_sec
);
return incref(result.ptr());
}
};
struct ptime_to_python
{
static PyObject* convert(boost::posix_time::ptime const& pt)
@ -82,20 +107,20 @@ void bind_datetime()
datetime_timedelta = datetime["timedelta"];
datetime_datetime = datetime["datetime"];
to_python_converter<
boost::posix_time::time_duration
, time_duration_to_python
>();
to_python_converter<boost::posix_time::time_duration
, time_duration_to_python>();
to_python_converter<
boost::posix_time::ptime
, ptime_to_python
>();
to_python_converter<boost::posix_time::ptime
, ptime_to_python>();
to_python_converter<
lt::time_duration
, chrono_time_duration_to_python
>();
to_python_converter<lt::time_point
, time_point_to_python>();
to_python_converter<lt::time_duration
, chrono_duration_to_python<lt::time_duration>>();
to_python_converter<std::chrono::seconds
, chrono_duration_to_python<std::chrono::seconds>>();
optional_to_python<boost::posix_time::ptime>();
}

View File

@ -22,6 +22,8 @@ object bitfield_to_list(bitfield const& bf)
object pieces(torrent_status const& s) { return bitfield_to_list(s.pieces); }
object verified_pieces(torrent_status const& s) { return bitfield_to_list(s.verified_pieces); }
using by_value = return_value_policy<return_by_value>;
void bind_torrent_status()
{
scope status = class_<torrent_status>("torrent_status")
@ -36,19 +38,9 @@ void bind_torrent_status()
.def_readonly("has_metadata", &torrent_status::has_metadata)
.def_readonly("progress", &torrent_status::progress)
.def_readonly("progress_ppm", &torrent_status::progress_ppm)
.add_property(
"next_announce"
, make_getter(
&torrent_status::next_announce, return_value_policy<return_by_value>()
)
)
.add_property("next_announce", make_getter(&torrent_status::next_announce, by_value()))
#ifndef TORRENT_NO_DEPRECATE
.add_property(
"announce_interval"
, make_getter(
&torrent_status::announce_interval, return_value_policy<return_by_value>()
)
)
.add_property("announce_interval", make_getter(&torrent_status::announce_interval, by_value()))
#endif
.def_readonly("current_tracker", &torrent_status::current_tracker)
.def_readonly("total_download", &torrent_status::total_download)
@ -87,19 +79,21 @@ void bind_torrent_status()
.def_readonly("down_bandwidth_queue", &torrent_status::down_bandwidth_queue)
.def_readonly("all_time_upload", &torrent_status::all_time_upload)
.def_readonly("all_time_download", &torrent_status::all_time_download)
.def_readonly("active_time", &torrent_status::active_time)
.def_readonly("finished_time", &torrent_status::finished_time)
.def_readonly("seeding_time", &torrent_status::seeding_time)
.def_readonly("seed_rank", &torrent_status::seed_rank)
.def_readonly("last_scrape", &torrent_status::last_scrape)
.def_readonly("has_incoming", &torrent_status::has_incoming)
.def_readonly("seed_mode", &torrent_status::seed_mode)
.def_readonly("upload_mode", &torrent_status::upload_mode)
.def_readonly("share_mode", &torrent_status::share_mode)
.def_readonly("super_seeding", &torrent_status::super_seeding)
#ifndef TORRENT_NO_DEPRECATE
.def_readonly("active_time", &torrent_status::active_time)
.def_readonly("finished_time", &torrent_status::finished_time)
.def_readonly("seeding_time", &torrent_status::seeding_time)
.def_readonly("last_scrape", &torrent_status::last_scrape)
.def_readonly("error", &torrent_status::error)
.def_readonly("priority", &torrent_status::priority)
.def_readonly("time_since_upload", &torrent_status::time_since_upload)
.def_readonly("time_since_download", &torrent_status::time_since_download)
#endif
.def_readonly("errc", &torrent_status::errc)
.def_readonly("error_file", &torrent_status::error_file)
@ -108,8 +102,6 @@ void bind_torrent_status()
.def_readonly("added_time", &torrent_status::added_time)
.def_readonly("completed_time", &torrent_status::completed_time)
.def_readonly("last_seen_complete", &torrent_status::last_seen_complete)
.def_readonly("time_since_upload", &torrent_status::time_since_upload)
.def_readonly("time_since_download", &torrent_status::time_since_download)
.def_readonly("queue_position", &torrent_status::queue_position)
.def_readonly("need_save_resume", &torrent_status::need_save_resume)
.def_readonly("ip_filter_applies", &torrent_status::ip_filter_applies)
@ -121,6 +113,11 @@ void bind_torrent_status()
.def_readonly("announcing_to_lsd", &torrent_status::announcing_to_lsd)
.def_readonly("announcing_to_dht", &torrent_status::announcing_to_dht)
.def_readonly("info_hash", &torrent_status::info_hash)
.add_property("last_upload", make_getter(&torrent_status::last_upload, by_value()))
.add_property("last_download", make_getter(&torrent_status::last_download, by_value()))
.add_property("active_duration", make_getter(&torrent_status::active_duration, by_value()))
.add_property("finished_duration", make_getter(&torrent_status::finished_duration, by_value()))
.add_property("seeding_duration", make_getter(&torrent_status::seeding_duration, by_value()))
;
enum_<torrent_status::state_t>("states")

View File

@ -139,6 +139,9 @@ class test_alerts(unittest.TestCase):
print(st.distributed_copies)
print(st.paused)
print(st.info_hash)
print(st.seeding_duration)
print(st.last_upload)
print(st.last_download)
self.assertEqual(st.save_path, os.getcwd())
def test_pop_alerts(self):

View File

@ -69,14 +69,18 @@ namespace libtorrent
// this error code specifies what error occurred
error_code last_error;
#ifndef TORRENT_NO_DEPRECATE
// returns the number of seconds to the next announce on this tracker.
// ``min_announce_in()`` returns the number of seconds until we are
// allowed to force another tracker update with this tracker.
//
// If the last time this tracker was contacted failed, ``last_error`` is
// the error code describing what error occurred.
TORRENT_DEPRECATED
int next_announce_in() const;
TORRENT_DEPRECATED
int min_announce_in() const;
#endif
// the time of next tracker announce
time_point next_announce = min_time();

View File

@ -979,6 +979,10 @@ namespace libtorrent
TORRENT_ASSERT(ret <= (std::numeric_limits<std::uint16_t>::max)());
return static_cast<std::uint16_t>(ret);
}
time_point session_start_time() const override
{
return m_created;
}
time_point m_last_tick;
time_point m_last_second_tick;

View File

@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/peer_id.hpp"
#include "libtorrent/address.hpp"
#include "libtorrent/io_service.hpp"
#include "libtorrent/time.hpp"
#include "libtorrent/disk_buffer_holder.hpp"
#include <functional>
@ -169,6 +170,7 @@ namespace libtorrent { namespace aux
virtual void ban_ip(address addr) = 0;
virtual std::uint16_t session_time() const = 0;
virtual time_point session_start_time() const = 0;
virtual bool is_aborted() const = 0;
virtual int num_uploads() const = 0;

View File

@ -704,11 +704,6 @@ namespace libtorrent
void scrape_tracker(int idx, bool user_triggered);
void announce_with_tracker(std::uint8_t e
= tracker_request::none);
int seconds_since_last_scrape() const
{
return m_last_scrape == (std::numeric_limits<std::int16_t>::min)()
? -1 : int(m_ses.session_time() - m_last_scrape);
}
#ifndef TORRENT_DISABLE_DHT
void dht_announce();
@ -1609,10 +1604,12 @@ namespace libtorrent
// is optional and may be 0xffffff
unsigned int m_downloaded:24;
#ifndef TORRENT_NO_DEPRECATE
// the timestamp of the last scrape request to one of the trackers in
// this torrent specified in session_time. This is signed because it must
// be able to represent time before the session started
std::int16_t m_last_scrape = (std::numeric_limits<std::int16_t>::min)();
#endif
// ----

View File

@ -391,6 +391,12 @@ namespace libtorrent
int up_bandwidth_queue = 0;
int down_bandwidth_queue = 0;
#ifndef TORRENT_NO_DEPRECATE
// deprecated in 1.2
// use last_upload, last_download or
// seeding_duration, finished_duration and active_duration
// instead
// the number of seconds since any peer last uploaded from this torrent
// and the last time a downloaded piece passed the hash check,
// respectively. Note, when starting up a torrent that needs its files
@ -410,6 +416,13 @@ namespace libtorrent
int active_time = 0;
int finished_time = 0;
int seeding_time = 0;
#else
int deprecated_time_since_upload = 0;
int deprecated_time_since_download = 0;
int deprecated_active_time = 0;
int deprecated_finished_time = 0;
int deprecated_seeding_time = 0;
#endif
// A rank of how important it is to seed the torrent, it is used to
// determine which torrents to seed and which to queue. It is based on
@ -417,9 +430,15 @@ namespace libtorrent
// see queuing_. Higher value means more important to seed
int seed_rank = 0;
#ifndef TORRENT_NO_DEPRECATE
// deprecated in 1.2
// the number of seconds since this torrent acquired scrape data.
// If it has never done that, this value is -1.
int last_scrape = 0;
#else
int deprecated_last_scrape = 0;
#endif
#ifndef TORRENT_NO_DEPRECATE
// the priority of this torrent
@ -527,6 +546,13 @@ namespace libtorrent
// the info-hash for this torrent
sha1_hash info_hash{nullptr};
time_point last_upload;
time_point last_download;
seconds active_duration;
seconds finished_duration;
seconds seeding_duration;
};
}

View File

@ -70,11 +70,13 @@ namespace libtorrent
announce_entry::~announce_entry() = default;
#ifndef TORRENT_NO_DEPRECATE
int announce_entry::next_announce_in() const
{ return total_seconds(next_announce - aux::time_now()); }
int announce_entry::min_announce_in() const
{ return total_seconds(min_announce - aux::time_now()); }
#endif
void announce_entry::reset()
{

View File

@ -2875,7 +2875,9 @@ namespace libtorrent
void torrent::scrape_tracker(int idx, bool user_triggered)
{
TORRENT_ASSERT(is_single_thread());
#ifndef TORRENT_NO_DEPRECATE
m_last_scrape = m_ses.session_time();
#endif
if (m_trackers.empty()) return;
@ -3032,8 +3034,10 @@ namespace libtorrent
}
update_tracker_timer(now);
#ifndef TORRENT_NO_DEPRECATE
if (resp.complete >= 0 && resp.incomplete >= 0)
m_last_scrape = m_ses.session_time();
#endif
#ifndef TORRENT_DISABLE_LOGGING
if (should_log())
@ -8438,7 +8442,9 @@ namespace libtorrent
m_last_upload = clamped_subtract_s16(m_last_upload, seconds);
m_last_download = clamped_subtract_s16(m_last_download, seconds);
#ifndef TORRENT_NO_DEPRECATE
m_last_scrape = clamped_subtract_s16(m_last_scrape, seconds);
#endif
m_last_saved_resume = clamped_subtract(m_last_saved_resume, seconds);
m_upload_mode_time = clamped_subtract(m_upload_mode_time, seconds);
@ -10713,8 +10719,10 @@ namespace libtorrent
st->added_time = m_added_time;
st->completed_time = m_completed_time;
#ifndef TORRENT_NO_DEPRECATE
st->last_scrape = m_last_scrape == (std::numeric_limits<std::int16_t>::min)() ? -1
: clamped_subtract(m_ses.session_time(), m_last_scrape);
#endif
st->share_mode = m_share_mode;
st->upload_mode = m_upload_mode;
@ -10744,13 +10752,23 @@ namespace libtorrent
st->all_time_download = m_total_downloaded;
// activity time
#ifndef TORRENT_NO_DEPRECATE
st->finished_time = finished_time();
st->active_time = active_time();
st->seeding_time = seeding_time();
st->time_since_upload = m_last_upload == (std::numeric_limits<std::int16_t>::min)() ? -1
: clamped_subtract(m_ses.session_time(), m_last_upload);
st->time_since_download = m_last_download == (std::numeric_limits<std::int16_t>::min)() ? -1
: clamped_subtract(m_ses.session_time(), m_last_download);
#endif
st->finished_duration = seconds{finished_time()};
st->active_duration = seconds{active_time()};
st->seeding_duration = seconds{seeding_time()};
st->last_upload = m_ses.session_start_time() + seconds(m_last_upload);
st->last_download = m_ses.session_start_time() + seconds(m_last_download);
st->storage_mode = static_cast<storage_mode_t>(m_storage_mode);

View File

@ -69,7 +69,11 @@ TORRENT_TEST(primitives)
for (int i = 0; i < 10; ++i)
{
ae.failed(tracker_backoff, 5);
int delay = ae.next_announce_in();
#ifndef TORRENT_NO_DEPRECATE
int const delay = ae.next_announce_in();
#else
int const delay = total_seconds(ae.next_announce - clock_type::now());
#endif
TEST_CHECK(delay > last);
last = delay;
std::printf("%d, ", delay);

View File

@ -189,11 +189,17 @@ void default_tests(torrent_status const& s)
// allow some slack in the time stamps since they are reported as
// relative times. If the computer is busy while running the unit test
// or running under valgrind it may take several seconds
#ifndef TORRENT_NO_DEPRECATE
TEST_CHECK(s.active_time >= 1339);
TEST_CHECK(s.active_time < 1339 + 10);
#endif
using lt::seconds;
TEST_CHECK(s.finished_duration< seconds(1352 + 2));
TEST_CHECK(s.seeding_duration < seconds(1340 + 2));
TEST_CHECK(s.active_duration >= seconds(1339));
TEST_CHECK(s.active_duration < seconds(1339 + 10));
TEST_CHECK(s.finished_time < 1352 + 2);
TEST_CHECK(s.seeding_time < 1340 + 2);
TEST_CHECK(s.added_time < 1347 + 2);
TEST_CHECK(s.completed_time < 1348 + 2);
}