Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/connection_monitor.cc @ 5726

Last change on this file since 5726 was 5726, checked in by snellen, 18 years ago

connection monitor updated

File size: 3.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Silvan Nellen
13   co-programmer: ...
14*/
15
16#include "connection_monitor.h"
17#include <debug.h>
18#include <SDL/SDL.h>
19#include <string.h>
20
21/* using namespace std is default, this needs to be here */
22using namespace std;
23
24ConnectionMonitor::ConnectionMonitor()
25{
26
27  /* set the class id for the base object and add ist to class list*/
28  this->setClassID(CL_CONNECTION_MONITOR, "ConnectionMonitor");
29
30  /*initialize variables*/
31
32  /*Data of the lifetime of the ConnectionMonitor Object*/
33  packetToAverage = 100;
34  protocollType = "default(TCP)";
35
36  startTime= SDL_GetTicks();
37  totalReceivedPackets=0;
38  averageDatarate=0;
39  totalLostPackets=0;
40  totalPacketloss=0;
41
42  /*Data of the current packet*/
43  currentPacketID=0;
44  currentPacketTick=0;
45  lastPacketID=0;
46  lastPacketTick=0;
47  currentDelay=0;
48
49  /*Data of the last n packets (n is specified by paxketsToAverage)*/
50  sizeOfLastFewPackets=0;
51  currentDatarate=0;
52  lastFewDelays = new unsigned int [packetToAverage];
53  lastFewPackets = new byte* [packetToAverage];
54  packetCounter=0;
55
56
57}
58
59
60ConnectionMonitor::~ConnectionMonitor()
61{
62
63
64}
65
66
67
68void ConnectionMonitor::processPacket(byte* currentPacket, unsigned int packetLength)
69{
70  /*Process the current Packet*/
71  currentPacketTick = SDL_GetTicks();
72  currentDelay = currentPacketTick - lastPacketTick;
73
74  /*Do whats needed for Averaging*/
75
76  if(packetCounter = packetToAverage)
77    {
78      computeCurrentDatarate();
79      displayStatistic();
80      packetCounter = 0;
81      sizeOfLastFewPackets = 0;
82    }
83
84  lastFewDelays[packetCounter] = currentDelay;
85  lastFewPackets[packetCounter] = currentPacket;
86  sizeOfLastFewPackets += packetLength;
87
88  /*Update the lifetime Variables*/
89  totalReceivedPackets ++;
90  averageDatarate = totalReceivedPackets/(currentPacketTick - startTime);
91
92  /*Preparefor the next Packet*/
93  lastPacketTick = currentPacketTick;
94  packetCounter++;
95}
96
97
98/* Compute the value of current Datarate*/
99void ConnectionMonitor::computeCurrentDatarate()
100{
101  int timeForLastFewPackets=0;
102  for(int i=0;i < packetToAverage;i++)
103    timeForLastFewPackets += lastFewDelays[i];
104
105  currentDatarate = sizeOfLastFewPackets/timeForLastFewPackets;
106}
107
108void doUDPRelatedStuff()
109{
110  /*  Do protocol related stuff
111
112  Only for Udp:
113  "currentPacketID = getID from package";
114
115  if(currentPacketID - lastPacketID > 1)
116  {
117  totalLostPackets += currentPacketID - lastPacketID;
118}
119
120  totalPacketloss = (totalLostPackets/totalReceivedPackets)*100 ;
121  */
122}
123
124
125
126/* Display connectoin statistic*/
127void ConnectionMonitor::displayStatistic()
128{
129  PRINT(0)("============================================\n");
130  PRINT(0)("Connection Monitor Network Statistics:\n");
131  PRINT(0)("Total received packets:",totalReceivedPackets);
132  PRINT(0)("Average datarate :\n",averageDatarate);
133  PRINT(0)("Total lost packets:",totalLostPackets);
134  PRINT(0)("Packetloss [%] :\n",totalPacketloss);
135
136  PRINT(0)("Current datarate :\n",currentDatarate);
137  PRINT(0)("Delays of the last few packets :\n");
138  for(int i=1 ;i <= packetToAverage-1;i++)
139    PRINT(0)("%i ",lastFewDelays[i]);
140
141  PRINT(0)("============================================\n");
142
143}
144
Note: See TracBrowser for help on using the repository browser.