diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2011-12-14 18:47:16 -0500 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2011-12-14 18:51:01 -0500 |
commit | 158da8b23aafd0a2998c2bf708ca4487ff634334 (patch) | |
tree | 8facc118a390ac73f0de5b8bc8df4b18ad8b846b | |
parent | Also scan hidden files. (diff) | |
download | oldgen-zmusicuploader-158da8b23aafd0a2998c2bf708ca4487ff634334.tar.xz oldgen-zmusicuploader-158da8b23aafd0a2998c2bf708ca4487ff634334.zip |
Add rate calculation.
-rw-r--r-- | RateCalculator.cpp | 41 | ||||
-rw-r--r-- | RateCalculator.h | 24 | ||||
-rw-r--r-- | Uploader.cpp | 5 | ||||
-rw-r--r-- | Uploader.h | 2 | ||||
-rw-r--r-- | musicuploader.pro | 6 |
5 files changed, 75 insertions, 3 deletions
diff --git a/RateCalculator.cpp b/RateCalculator.cpp new file mode 100644 index 0000000..de6fe3b --- /dev/null +++ b/RateCalculator.cpp @@ -0,0 +1,41 @@ +#include "RateCalculator.h" + +void RateCalculator::addBytes(qint64 bytes) +{ + if (!m_timer.isValid()) + start(); + m_segments.prepend(qMakePair(m_timer.restart(), bytes)); + m_byteSum += bytes; +} +void RateCalculator::updateTotalBytes(qint64 bytes) +{ + addBytes(bytes - m_byteSum); +} +void RateCalculator::start() +{ + m_byteSum = 0; + m_segments.clear(); + m_timer.restart(); +} +void RateCalculator::stop() +{ + m_timer.invalidate(); +} +void RateCalculator::reset() +{ + stop(); + start(); +} +double RateCalculator::rate(qint64 interval) const +{ + qint64 time = 0; + qint64 bytes = 0; + QPair<qint64, qint64> i; + foreach (i, m_segments) { + time += i.first; + bytes += i.second; + if (interval > 0 && time >= interval) + break; + } + return static_cast<double>(bytes) / static_cast<double>(time); +} diff --git a/RateCalculator.h b/RateCalculator.h new file mode 100644 index 0000000..a8bec1c --- /dev/null +++ b/RateCalculator.h @@ -0,0 +1,24 @@ +#ifndef RATECALCULATOR_H +#define RATECALCULATOR_H + +#include <QtGlobal> +#include <QElapsedTimer> +#include <QLinkedList> +#include <QPair> + +class RateCalculator +{ +public: + void addBytes(qint64 bytes); + void updateTotalBytes(qint64 bytes); + void start(); + void stop(); + void reset(); + double rate(qint64 interval = -1) const; +private: + QElapsedTimer m_timer; + QLinkedList<QPair<qint64, qint64> > m_segments; + qint64 m_byteSum; +}; + +#endif // RATECALCULATOR_H diff --git a/Uploader.cpp b/Uploader.cpp index d915d04..dc54ed8 100644 --- a/Uploader.cpp +++ b/Uploader.cpp @@ -1,6 +1,7 @@ #include "Uploader.h" #include "XorEncrypter.h" #include <iostream> +#include <cmath> using namespace std; Uploader::Uploader(const QString &ftpServer, const QString &ftpPath, const QString &username, const QString &password, const QByteArray &secretKey, const QHash<const QString, QFileInfo> &fileList, QObject *parent) : @@ -105,10 +106,12 @@ void Uploader::uploadNextFile() m_currentFile = new QFile(m_listPosition.value().absoluteFilePath()); m_currentFile->open(QIODevice::ReadOnly); XorEncrypter *xorEncrypter = new XorEncrypter(m_secretKey, m_currentFile); + m_rateCalculator.reset(); m_latestPut = m_ftp.put(xorEncrypter, m_listPosition.key()); ++m_listPosition; } void Uploader::dataTransferProgress(qint64 done, qint64 total) { - cerr << done << " of " << total << " bytes transferred.\r"; + m_rateCalculator.updateTotalBytes(done); + cerr << "\r\033[2K" << static_cast<qint64>(round(done / 1024.0)) << " of " << static_cast<qint64>(round(total / 1024.0)) << " kilobytes transferred at " << static_cast<qint64>(round(m_rateCalculator.rate(10000) * 1024.0 / 1000.0)) << " kilobytes per second."; } @@ -7,6 +7,7 @@ #include <QFile> #include <QByteArray> #include "DirectoryScanner.h" +#include "RateCalculator.h" class Uploader : public QObject { @@ -29,6 +30,7 @@ private: int m_latestCommand; int m_latestPut; QFile *m_currentFile; + RateCalculator m_rateCalculator; private slots: void stateChanged(int state); diff --git a/musicuploader.pro b/musicuploader.pro index 935b99e..c482fa3 100644 --- a/musicuploader.pro +++ b/musicuploader.pro @@ -7,9 +7,11 @@ TEMPLATE = app SOURCES += main.cpp \ XorEncrypter.cpp \ DirectoryScanner.cpp \ - Uploader.cpp + Uploader.cpp \ + RateCalculator.cpp HEADERS += \ XorEncrypter.h \ DirectoryScanner.h \ - Uploader.h + Uploader.h \ + RateCalculator.h |