|
|
@ -36,25 +36,82 @@ |
|
|
|
|
|
|
|
|
|
|
|
using namespace libtorrent; |
|
|
|
using namespace libtorrent; |
|
|
|
|
|
|
|
|
|
|
|
template<class T> struct Sample { |
|
|
|
namespace { |
|
|
|
Sample(const T down = 0, const T up = 0) : download(down), upload(up) {} |
|
|
|
|
|
|
|
|
|
|
|
template<class T> struct Sample { |
|
|
|
|
|
|
|
Sample() |
|
|
|
|
|
|
|
: download() |
|
|
|
|
|
|
|
, upload() |
|
|
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sample(T download, T upload) |
|
|
|
|
|
|
|
: download(download) |
|
|
|
|
|
|
|
, upload(upload) |
|
|
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename U> |
|
|
|
|
|
|
|
explicit Sample(Sample<U> other) |
|
|
|
|
|
|
|
: download(static_cast<U>(other.download)) |
|
|
|
|
|
|
|
, upload(static_cast<U>(other.upload)) |
|
|
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
T download; |
|
|
|
T download; |
|
|
|
T upload; |
|
|
|
T upload; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
|
|
|
Sample<T>& operator+=(Sample<T>& lhs, Sample<T> const& rhs) { |
|
|
|
|
|
|
|
lhs.download += rhs.download; |
|
|
|
|
|
|
|
lhs.upload += rhs.upload; |
|
|
|
|
|
|
|
return lhs; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
|
|
|
Sample<T>& operator-=(Sample<T>& lhs, Sample<T> const& rhs) { |
|
|
|
|
|
|
|
lhs.download -= rhs.download; |
|
|
|
|
|
|
|
lhs.upload -= rhs.upload; |
|
|
|
|
|
|
|
return lhs; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
|
|
|
Sample<T> operator+(Sample<T> const& lhs, Sample<T> const& rhs) { |
|
|
|
|
|
|
|
return Sample<T>(lhs.download + rhs.download, lhs.upload + rhs.upload); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
|
|
|
Sample<T> operator-(Sample<T> const& lhs, Sample<T> const& rhs) { |
|
|
|
|
|
|
|
return Sample<T>(lhs.download - rhs.download, lhs.upload - rhs.upload); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
|
|
|
Sample<T> operator*(Sample<T> const& lhs, T rhs) { |
|
|
|
|
|
|
|
return Sample<T>(lhs.download * rhs, lhs.upload * rhs); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
|
|
|
Sample<T> operator*(T lhs,Sample<T> const& rhs) { |
|
|
|
|
|
|
|
return Sample<T>(lhs * rhs.download, lhs * rhs.upload); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
|
|
|
Sample<T> operator/(Sample<T> const& lhs, T rhs) { |
|
|
|
|
|
|
|
return Sample<T>(lhs.download / rhs, lhs.upload / rhs); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class SpeedSample { |
|
|
|
class SpeedSample { |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
public: |
|
|
|
SpeedSample() {} |
|
|
|
SpeedSample() {} |
|
|
|
void addSample(int speedDL, int speedUL); |
|
|
|
void addSample(Sample<int> const& item); |
|
|
|
Sample<qreal> average() const; |
|
|
|
Sample<qreal> average() const; |
|
|
|
void clear(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
static const int max_samples = 30; |
|
|
|
static const int max_samples = 30; |
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
private: |
|
|
|
QList<Sample<int> > m_speedSamples; |
|
|
|
QList<Sample<int> > m_speedSamples; |
|
|
|
|
|
|
|
Sample<long long> m_sum; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) |
|
|
|
TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) |
|
|
@ -68,11 +125,14 @@ TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) |
|
|
|
TorrentSpeedMonitor::~TorrentSpeedMonitor() |
|
|
|
TorrentSpeedMonitor::~TorrentSpeedMonitor() |
|
|
|
{} |
|
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
void SpeedSample::addSample(int speedDL, int speedUL) |
|
|
|
void SpeedSample::addSample(Sample<int> const& item) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_speedSamples << Sample<int>(speedDL, speedUL); |
|
|
|
m_speedSamples.push_back(item); |
|
|
|
if (m_speedSamples.size() > max_samples) |
|
|
|
m_sum += Sample<long long>(item); |
|
|
|
m_speedSamples.removeFirst(); |
|
|
|
if (m_speedSamples.size() > max_samples) { |
|
|
|
|
|
|
|
m_sum -= Sample<long long>(m_speedSamples.front()); |
|
|
|
|
|
|
|
m_speedSamples.pop_front(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Sample<qreal> SpeedSample::average() const |
|
|
|
Sample<qreal> SpeedSample::average() const |
|
|
@ -80,21 +140,7 @@ Sample<qreal> SpeedSample::average() const |
|
|
|
if (m_speedSamples.empty()) |
|
|
|
if (m_speedSamples.empty()) |
|
|
|
return Sample<qreal>(); |
|
|
|
return Sample<qreal>(); |
|
|
|
|
|
|
|
|
|
|
|
qlonglong sumDL = 0; |
|
|
|
return Sample<qreal>(m_sum) * (1. / m_speedSamples.size()); |
|
|
|
qlonglong sumUL = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (const Sample<int>& s, m_speedSamples) { |
|
|
|
|
|
|
|
sumDL += s.download; |
|
|
|
|
|
|
|
sumUL += s.upload; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const qreal numSamples = m_speedSamples.size(); |
|
|
|
|
|
|
|
return Sample<qreal>(sumDL/numSamples, sumUL/numSamples); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SpeedSample::clear() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_speedSamples.clear(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void TorrentSpeedMonitor::removeSamples(const QString &hash) |
|
|
|
void TorrentSpeedMonitor::removeSamples(const QString &hash) |
|
|
@ -110,10 +156,14 @@ void TorrentSpeedMonitor::removeSamples(const QTorrentHandle& h) { |
|
|
|
|
|
|
|
|
|
|
|
qlonglong TorrentSpeedMonitor::getETA(const QString &hash, const libtorrent::torrent_status &status) const |
|
|
|
qlonglong TorrentSpeedMonitor::getETA(const QString &hash, const libtorrent::torrent_status &status) const |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (QTorrentHandle::is_paused(status) || !m_samples.contains(hash)) |
|
|
|
if (QTorrentHandle::is_paused(status)) |
|
|
|
return MAX_ETA; |
|
|
|
return MAX_ETA; |
|
|
|
|
|
|
|
|
|
|
|
const Sample<qreal> speed_average = m_samples[hash].average(); |
|
|
|
QHash<QString, SpeedSample>::const_iterator i = m_samples.find(hash); |
|
|
|
|
|
|
|
if (i == m_samples.end()) |
|
|
|
|
|
|
|
return MAX_ETA; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const Sample<qreal> speed_average = i->average(); |
|
|
|
|
|
|
|
|
|
|
|
if (QTorrentHandle::is_seed(status)) { |
|
|
|
if (QTorrentHandle::is_seed(status)) { |
|
|
|
if (!speed_average.upload) |
|
|
|
if (!speed_average.upload) |
|
|
@ -141,8 +191,10 @@ void TorrentSpeedMonitor::statsReceived(const stats_alert &stats) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Q_ASSERT(stats.interval >= 1000); |
|
|
|
Q_ASSERT(stats.interval >= 1000); |
|
|
|
|
|
|
|
|
|
|
|
int speedDL = static_cast<int>(static_cast<long long>(stats.transferred[stats_alert::download_payload]) * 1000 / stats.interval); |
|
|
|
Sample<int> transferred(stats.transferred[stats_alert::download_payload], |
|
|
|
int speedUL = static_cast<int>(static_cast<long long>(stats.transferred[stats_alert::upload_payload]) * 1000 / stats.interval); |
|
|
|
stats.transferred[stats_alert::upload_payload]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sample<int> normalized = Sample<int>(Sample<long long>(transferred) * 1000LL / static_cast<long long>(stats.interval)); |
|
|
|
|
|
|
|
|
|
|
|
m_samples[misc::toQString(stats.handle.info_hash())].addSample(speedDL, speedUL); |
|
|
|
m_samples[misc::toQString(stats.handle.info_hash())].addSample(normalized); |
|
|
|
} |
|
|
|
} |
|
|
|