Browse Source

Improve code readability in speed monitor

adaptive-webui-19844
Nick Tiskov 12 years ago
parent
commit
ec08bfac07
  1. 62
      src/qtlibtorrent/torrentspeedmonitor.cpp

62
src/qtlibtorrent/torrentspeedmonitor.cpp

@ -38,19 +38,25 @@
using namespace libtorrent; using namespace libtorrent;
template<class T> struct Sample {
Sample(const T down = 0, const T up = 0) : download(down), upload(up) {}
T download;
T upload;
};
class SpeedSample { class SpeedSample {
public: public:
SpeedSample() {} SpeedSample() {}
void addSample(qint32 speedDL, qint32 speedUL); void addSample(int speedDL, int speedUL);
QPair<qreal, qreal> average() const; Sample<qreal> average() const;
void clear(); void clear();
private: private:
static const int max_samples = 30; static const int max_samples = 30;
private: private:
QList<QPair<qint32, qint32> > m_speedSamples; QList<Sample<int> > m_speedSamples;
}; };
TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) : TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) :
@ -76,31 +82,28 @@ void TorrentSpeedMonitor::run()
} while(!m_abort); } while(!m_abort);
} }
void SpeedSample::addSample(qint32 speedDL, qint32 speedUL) void SpeedSample::addSample(int speedDL, int speedUL)
{ {
m_speedSamples << qMakePair(speedDL, speedUL); m_speedSamples << Sample<int>(speedDL, speedUL);
if (m_speedSamples.size() > max_samples) if (m_speedSamples.size() > max_samples)
m_speedSamples.removeFirst(); m_speedSamples.removeFirst();
} }
QPair<qreal, qreal> SpeedSample::average() const Sample<qreal> SpeedSample::average() const
{ {
if (m_speedSamples.empty()) if (m_speedSamples.empty())
return qMakePair(0., 0.); return Sample<qreal>();
qlonglong sumDL = 0, sumUL =0; qlonglong sumDL = 0;
qlonglong sumUL = 0;
QPair<qint32, qint32> p; foreach (const Sample<int>& s, m_speedSamples) {
foreach (p, m_speedSamples) { sumDL += s.download;
sumDL += p.first; sumUL += s.upload;
sumUL += p.second;
} }
qreal numSamples = m_speedSamples.size(); const qreal numSamples = m_speedSamples.size();
return qMakePair( return Sample<qreal>(sumDL/numSamples, sumUL/numSamples);
(sumDL/numSamples),
(sumUL/numSamples));
} }
void SpeedSample::clear() void SpeedSample::clear()
@ -126,10 +129,10 @@ qlonglong TorrentSpeedMonitor::getETA(const QString &hash) const
if (h.is_paused() || !m_samples.contains(hash)) if (h.is_paused() || !m_samples.contains(hash))
return MAX_ETA; return MAX_ETA;
const QPair<qreal, qreal> speed_average = m_samples.value(hash).average(); const Sample<qreal> speed_average = m_samples[hash].average();
if(h.is_seed()) { if (h.is_seed()) {
if (speed_average.second == 0) if (!speed_average.upload)
return MAX_ETA; return MAX_ETA;
bool _unused; bool _unused;
@ -141,13 +144,13 @@ qlonglong TorrentSpeedMonitor::getETA(const QString &hash) const
if (realDL <= 0) if (realDL <= 0)
realDL = h.total_wanted(); realDL = h.total_wanted();
return (realDL * max_ratio - h.all_time_upload()) / speed_average.second; return (realDL * max_ratio - h.all_time_upload()) / speed_average.upload;
} else { }
if (speed_average.first == 0)
if (!speed_average.download)
return MAX_ETA; return MAX_ETA;
return (h.total_wanted() - h.total_done()) / speed_average.first; return (h.total_wanted() - h.total_done()) / speed_average.download;
}
} }
void TorrentSpeedMonitor::getSamples() void TorrentSpeedMonitor::getSamples()
@ -160,12 +163,13 @@ void TorrentSpeedMonitor::getSamples()
try { try {
#if LIBTORRENT_VERSION_MINOR > 15 #if LIBTORRENT_VERSION_MINOR > 15
torrent_status st = it->status(0x0); torrent_status st = it->status(0x0);
if (!st.paused) if (!st.paused) {
m_samples[misc::toQString(it->info_hash())].addSample(st.download_payload_rate, st.upload_payload_rate);
#else #else
if (!it->is_paused()) if (!it->is_paused()) {
m_samples[misc::toQString(it->info_hash())].addSample(it->status().download_payload_rate, it->status().upload_payload_rate); torrent_status st = it->status();
#endif #endif
m_samples[misc::toQString(it->info_hash())].addSample(st.download_payload_rate, st.upload_payload_rate);
}
} catch(invalid_handle&) {} } catch(invalid_handle&) {}
} }
} }

Loading…
Cancel
Save