ThroughputSensor.cc 1.32 KB
Newer Older
1 2 3 4 5
// ThroughputSensor.cc

#include "lib.h"
#include "ThroughputSensor.h"
#include "PacketSensor.h"
6
#include "StateSensor.h"
7 8 9

using namespace std;

10 11
ThroughputSensor::ThroughputSensor(PacketSensor * newPacketHistory,
                                   StateSensor * newState)
12
  : throughputInKbps(0)
13
  , maxThroughput(0)
14
  , packetHistory(newPacketHistory)
15
  , state(newState)
16 17 18 19 20 21 22 23 24 25 26 27 28 29
{
}

int ThroughputSensor::getThroughputInKbps(void) const
{
  return throughputInKbps;
}

void ThroughputSensor::localSend(PacketInfo *)
{
}

void ThroughputSensor::localAck(PacketInfo * packet)
{
30
  if (state->getState() == StateSensor::ESTABLISHED)
31
  {
32 33 34 35 36 37
    Time currentAckTime = packet->packetTime;
    if (lastAckTime != Time() && currentAckTime != lastAckTime)
    {
      // period is in seconds.
      double period = (currentAckTime - lastAckTime).toMilliseconds() / 1000.0;
      double kilobits = packetHistory->getAckedSize() * (8.0/1000.0);
38 39 40 41 42 43 44 45 46 47 48
      int latest = static_cast<int>(kilobits/period);
      if (state->isSaturated() || latest > maxThroughput)
      {
        throughputInKbps = latest;
        maxThroughput = latest;
        logWrite(SENSOR, "THROUGHPUT: %d kbps", throughputInKbps);
      }
    }
    else
    {
      throughputInKbps = 0;
49 50
    }
    lastAckTime = currentAckTime;
51
  }
52 53 54 55 56
  else
  {
    throughputInKbps = 0;
    lastAckTime = Time();
  }
57
}