speedcalculator.cpp
1 //------------------------------------------------------------------------------
2 // speedcalculator.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "speedcalculator.h"
24 
25 SpeedCalculator::SpeedCalculator()
26 {
27  dataSizeExpected = 0;
28  lastRegisterAttemptDataSize = 0;
29 }
30 
32 {
33  long double speed = getSpeed();
34  if (speed == 0.0)
35  {
36  return -1.0;
37  }
38 
39  const DataArrivalInfo &endInfo = arrivalData.last();
40  unsigned currentData = endInfo.totalAmountOfArrivedData;
41 
42  // This happens if dataSizeExpected isn't set properly.
43  if (dataSizeExpected < currentData)
44  {
45  return -1.0;
46  }
47 
48  unsigned remainingData = dataSizeExpected - currentData;
49  return (double)remainingData / speed;
50 }
51 
52 long double SpeedCalculator::getSpeed() const
53 {
54  if (arrivalData.size() < 2)
55  {
56  return -1.0;
57  }
58 
59  // Let's calculate weighted mean.
60  long double numerator = 0.0;
61  long double denominator = 0.0;
62 
63  numerator = arrivalData.last().totalAmountOfArrivedData - arrivalData.first().totalAmountOfArrivedData;
64  denominator = arrivalData.last().timeOfArrival - arrivalData.first().timeOfArrival;
65 
66  long double speed = numerator / denominator;
67 
68  // Scale speed to 1 second (multiply by 1000ms).
69  speed *= 1000.0;
70 
71  return speed;
72 }
73 
75 {
76  if (arrivalData.isEmpty())
77  {
78  return 0;
79  }
80 
81  return arrivalData.last().totalAmountOfArrivedData;
82 }
83 
84 void SpeedCalculator::registerDataAmount(qint64 totalAmountOfArrivedData)
85 {
86  DataArrivalInfo dataArrivalInfo(totalAmountOfArrivedData, clock.elapsed());
87  this->lastRegisterAttemptDataSize = totalAmountOfArrivedData;
88 
89  if (arrivalData.isEmpty())
90  {
91  arrivalData << dataArrivalInfo;
92  }
93  else
94  {
95  DataArrivalInfo &lastPacket = arrivalData.last();
96  if (lastPacket.timeOfArrival + 1000 < dataArrivalInfo.timeOfArrival)
97  {
98  // Circulate the DataArrivalInfo objects.
99  arrivalData << dataArrivalInfo;
100  }
101  }
102 
103  if (arrivalData.size() > NUM_ARRIVAL_DATA)
104  {
105  arrivalData.pop_front();
106  }
107 }
108 
110 {
111  dataSizeExpected = size;
112 }
113 
115 {
116  arrivalData.clear();
117  dataSizeExpected = 0;
118  lastRegisterAttemptDataSize = 0;
119  clock.start();
120 }