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 */ |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | ConnectionMonitor::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 = 5; |
---|
34 | |
---|
35 | startTime= SDL_GetTicks(); |
---|
36 | totalReceivedPackets=0; |
---|
37 | averageDatarate=0; |
---|
38 | totalLostPackets=0; |
---|
39 | totalPacketloss=0; |
---|
40 | |
---|
41 | /*Data of the current packet*/ |
---|
42 | currentPacketID=0; |
---|
43 | currentPacketTick=0; |
---|
44 | lastPacketID=0; |
---|
45 | lastPacketTick=0; |
---|
46 | currentDelay=0; |
---|
47 | |
---|
48 | /*Data of the last n packets (n is specified by paxketsToAverage)*/ |
---|
49 | sizeOfLastFewPackets=0; |
---|
50 | currentDatarate=0; |
---|
51 | lastFewDelays = new unsigned int [packetToAverage]; |
---|
52 | lastFewPackets = new byte* [packetToAverage]; |
---|
53 | packetCounter=0; |
---|
54 | |
---|
55 | |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | ConnectionMonitor::~ConnectionMonitor() |
---|
60 | { |
---|
61 | |
---|
62 | |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | void ConnectionMonitor::processPacket(byte* currentPacket, unsigned int packetLength) |
---|
68 | { |
---|
69 | /*Process the current Package*/ |
---|
70 | currentPacketTick = SDL_GetTicks(); |
---|
71 | currentDelay = currentPacketTick - lastPacketTick; |
---|
72 | |
---|
73 | /* Only for Udp |
---|
74 | if(currentPacketID - lastPacketID > 1) |
---|
75 | { |
---|
76 | totalLostPackets += currentPacketID - lastPacketID; |
---|
77 | } |
---|
78 | |
---|
79 | totalPacketloss = (totalLostPackets/totalReceivedPackets)*100 ; |
---|
80 | */ |
---|
81 | |
---|
82 | /*Do whats needed for Averaging*/ |
---|
83 | |
---|
84 | if(packetCounter = packetToAverage) |
---|
85 | { |
---|
86 | computeAverageStatistic(); |
---|
87 | displayStatistic(); |
---|
88 | packetCounter = 0; |
---|
89 | } |
---|
90 | |
---|
91 | lastFewDelays[packetCounter] = currentDelay; |
---|
92 | lastFewPackets[packetCounter] = currentPacket; |
---|
93 | sizeOfLastFewPackets += packetLength; |
---|
94 | |
---|
95 | |
---|
96 | /*Update the lifetime Variables*/ |
---|
97 | totalReceivedPackets ++; |
---|
98 | averageDatarate = totalReceivedPackets/(currentPacketTick - startTime); |
---|
99 | |
---|
100 | /*Preparefor the next Packet*/ |
---|
101 | lastPacketTick = currentPacketTick; |
---|
102 | packetCounter++; |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | /* Compute the value of current Datarate*/ |
---|
107 | void ConnectionMonitor::computeAverageStatistic() |
---|
108 | { |
---|
109 | int timeForLastFewPackets; |
---|
110 | for(int i=0;i <= packetToAverage-1;i++) |
---|
111 | timeForLastFewPackets += lastFewDelays[i]; |
---|
112 | |
---|
113 | currentDatarate = packetToAverage/timeForLastFewPackets; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | void ConnectionMonitor::displayStatistic() |
---|
118 | { |
---|
119 | PRINT(0)("============================================\n"); |
---|
120 | PRINT(0)("Connection Monitor Network Statistics:\n"); |
---|
121 | PRINT(0)("Total received packets:",totalReceivedPackets); |
---|
122 | PRINT(0)("Average datarate :\n",averageDatarate); |
---|
123 | PRINT(0)("Total lost packets:",totalLostPackets); |
---|
124 | PRINT(0)("Packetloss [%] :\n",totalPacketloss); |
---|
125 | |
---|
126 | PRINT(0)("Current datarate :\n",currentDatarate); |
---|
127 | PRINT(0)("Delays of the last few packets :\n",currentDelay); |
---|
128 | for(int i=1 ;i <= packetToAverage-1;i++) |
---|
129 | PRINT(0)("%i ",lastFewDelays[i]); |
---|
130 | |
---|
131 | PRINT(0)("============================================\n"); |
---|
132 | |
---|
133 | } |
---|
134 | |
---|