1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/core/toxfileprogress.cpp
Mick Sayson c7efe320d2 refactor(filetransfer): Move file transfer progress into ToxFile
* Refactor/test ToxFileProgress to ensure that when it's moved it
  behaves well
* Notice problems with speed averaging. We were average speeds without
  keeping track of the times they were over. Adding samples of different
  lengths would result in incorrect speeds. Refactor whole class to correct
* Move ToxFileProgress to be a member of ToxFile
* Remove duplicated members between ToxFile and ToxFileProgress
* Move sample addition into CoreFile
2021-12-11 15:38:35 -08:00

138 lines
3.7 KiB
C++

/*
Copyright © 2021 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#include "toxfileprogress.h"
#include <limits>
ToxFileProgress::ToxFileProgress(uint64_t filesize, int samplePeriodMs)
: filesize(filesize)
, samplePeriodMs(samplePeriodMs)
{
if (samplePeriodMs < 0) {
qWarning("Invalid sample rate, healing to 1000ms");
this->samplePeriodMs = 1000;
}
}
QTime ToxFileProgress::lastSampleTime() const
{
return samples[activeSample].timestamp;
}
bool ToxFileProgress::addSample(uint64_t bytesSent, QTime now)
{
if (bytesSent > filesize) {
qWarning("Bytes sent exceeds file size, ignoring sample");
return false;
}
auto* active = &samples[activeSample];
auto* inactive = &samples[!activeSample];
if (bytesSent < active->bytesSent || bytesSent < inactive->bytesSent) {
qWarning("Bytes sent has decreased since last sample, ignoring sample");
return false;
}
if (now < active->timestamp || now < inactive->timestamp) {
qWarning("Sample time has gone backwards, clearing progress buffer");
resetSpeed();
}
// Ensure both samples are initialized
if (inactive->timestamp == QTime()) {
inactive->bytesSent = bytesSent;
inactive->timestamp = now;
}
if (active->timestamp == QTime()) {
active->bytesSent = bytesSent;
active->timestamp = now;
}
if (active->timestamp.msecsTo(now) >= samplePeriodMs) {
// Swap samples and set the newly active sample
activeSample = !activeSample;
std::swap(active, inactive);
}
active->bytesSent = bytesSent;
active->timestamp = now;
return true;
}
void ToxFileProgress::resetSpeed()
{
for (auto& sample : samples) {
sample.timestamp = QTime();
}
}
uint64_t ToxFileProgress::getBytesSent() const
{
return samples[activeSample].bytesSent;
}
double ToxFileProgress::getProgress() const
{
return double(samples[activeSample].bytesSent) / filesize;
}
double ToxFileProgress::getSpeed() const
{
if (samples.size() > 0
&& samples[activeSample].bytesSent == filesize) {
return 0.0f;
}
const auto sampleTimeInvalid = [](const Sample& sample) {
return sample.timestamp == QTime();
};
if (std::any_of(samples.cbegin(), samples.cend(), sampleTimeInvalid)) {
return 0.0f;
}
if (samples[0].timestamp == samples[1].timestamp) {
return 0.0f;
}
const auto& active = samples[activeSample];
const auto& inactive = samples[!activeSample];
return (active.bytesSent - inactive.bytesSent) / double(inactive.timestamp.msecsTo(active.timestamp)) * 1000.0;
}
double ToxFileProgress::getTimeLeftSeconds() const
{
if (samples.size() > 0
&& samples[activeSample].bytesSent == filesize) {
return 0;
}
const auto speed = getSpeed();
if (speed == 0.0f) {
return std::numeric_limits<float>::infinity();
}
return double(filesize - samples[activeSample].bytesSent) / getSpeed();
}