Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/connection_monitor.cc @ 8710

Last change on this file since 8710 was 8623, checked in by bensch, 19 years ago

orxonox/trunk: merged the network branche back here
merged with command:
svn merge -r8230:HEAD https://svn.orxonox.net/orxonox/branches/network .
conflicts resolved in favour of the network branche (conflicts were in network)

File size: 6.9 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: Christoph Renner
13   co-programmer: ...
14*/
15
16#include "connection_monitor.h"
17#include "network_log.h"
18
19#include <debug.h>
20#include <SDL/SDL.h>
21#include <string.h>
22
23/* using namespace std is default, this needs to be here */
24using namespace std;
25
26/**
27 * constructor
28 * @param userId user's id
29 */
30ConnectionMonitor::ConnectionMonitor( int userId )
31{
32  /* set the class id for the base object and add ist to class list*/
33  this->setClassID(CL_CONNECTION_MONITOR, "ConnectionMonitor");
34 
35  this->userId = userId;
36  this->ping = 0;
37  this->incomingUnzippedBandWidth = 0;
38  this->outgoingUnzippedBandWidth = 0;
39  this->incomingZippedBandWidth = 0;
40  this->outgoingZippedBandWidth = 0;
41  this->nIncomingPackets = 0;
42  this->nOutgoingPackets = 0;
43  this->nZIncomingPackets = 0;
44  this->nZOutgoingPackets = 0;
45 
46  this->lastPacketTick = 0;
47  this->lastPrintTick = 0;
48}
49
50/**
51 * deconstructor
52 */
53ConnectionMonitor::~ConnectionMonitor( )
54{
55}
56
57/**
58 * process unzipped outgoing packet
59 * @param data pointer to packet data
60 * @param length length of packet
61 * @param stateId packet's state id
62 */
63void ConnectionMonitor::processUnzippedOutgoingPacket( int tick, byte * data, int length, int stateId )
64{
65  nOutgoingPackets++;
66 
67  // for ping calculation
68  sentStateTicks[stateId] = tick;
69 
70  // calculate bandwidth
71  outgoingUnzippedPacketHistory[tick] = length;
72  outgoingUnzippedBandWidth = calculateBandWidth( outgoingUnzippedPacketHistory, tick );
73 
74  //NETPRINTF(n)("UNZIPPED UPSTREAM: user: %d bandwidth %f\n", userId, outgoingUnzippedBandWidth );
75 
76  // count zero bytes
77  //int nZeroBytes = 0;
78 
79  //for ( int i = 0; i < length; i++ )
80  //  if ( data[i] == '\0' )
81  //    nZeroBytes++;
82 
83  //NETPRINTF(n)( "ZEROBYTES: %d (%f%%)\n", nZeroBytes, ((float)100)*nZeroBytes/length );
84}
85
86/**
87 * process unzipped incoming packet
88 * @param data pointer to packet data
89 * @param length length of packet
90 * @param stateId packet's state id
91 * @param ackedState state which was acked by this packet
92 */
93void ConnectionMonitor::processUnzippedIncomingPacket( int tick, byte * data, int length, int stateId, int ackedState )
94{
95  nIncomingPackets++;
96 
97  lastPacketTick = tick;
98 
99  // calculate ping
100  if ( sentStateTicks.find( ackedState ) != sentStateTicks.end() )
101  {
102    ackDelay.push_back( tick - sentStateTicks[ackedState] );
103  }
104 
105  while ( sentStateTicks.begin() != sentStateTicks.end() && sentStateTicks.begin()->first <= ackedState )
106    sentStateTicks.erase( sentStateTicks.begin() );
107     
108  while ( ackDelay.size() > N_PACKETS_FOR_PING )
109    ackDelay.erase( ackDelay.begin() );
110     
111  ping = 0;
112     
113  for ( std::list<int>::iterator it = ackDelay.begin(); it != ackDelay.end(); it++ )
114    ping += *it;
115     
116  if ( ackDelay.size() == 0 )
117    ping = -1;
118  else
119    ping /= ackDelay.size();
120     
121  //NETPRINTF(n)("PING: user: %d ping: %d\n", userId, ping );
122 
123  // calculate bandwidth
124  incomingUnzippedPacketHistory[tick] = length;
125  incomingUnzippedBandWidth = calculateBandWidth( incomingUnzippedPacketHistory, tick );
126 
127  //NETPRINTF(n)("UNZIPPED DOWNSTREAM: user: %d bandwidth %f\n", userId, incomingUnzippedBandWidth );
128 
129}
130
131/**
132 * remove old packets
133 * @param packetHistory
134 * @param tick
135 */
136void ConnectionMonitor::removeOldPackets( std::map< int, int > & packetHistory, int tick )
137{
138  while ( packetHistory.begin()->first < tick - MSECS_TO_CALC_BWIDTH )
139    packetHistory.erase( packetHistory.begin() );
140}
141
142/**
143 * calculate bandwidth out of packethistory
144 * @param packetHistory packet history
145 * @param tick current tick from SDL_GetTicks
146 * @return bandwidth in bytes/sec
147 */
148float ConnectionMonitor::calculateBandWidth( std::map< int, int > & packetHistory, int tick )
149{
150  removeOldPackets( packetHistory, tick );
151 
152  float res = 0.0f;
153#if 0
154  for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ )
155  {
156    if ( it != packetHistory.begin() )
157      res += it->second;
158  }
159 
160  if ( packetHistory.size() <= 1 || tick - packetHistory.begin()->first == 0 )
161    res = 0.0f;
162  else
163    res /= (float)(tick - packetHistory.begin()->first);
164 
165  res *= 1000.0f;
166#endif
167
168  for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ )
169  {
170    res += it->second;
171  }
172 
173  if ( packetHistory.size() <= 1 )
174    res = 0.0f;
175  else
176    res /= (float)(tick - packetHistory.begin()->first);
177 
178  res *= 1000.0f;
179
180  return res;
181}
182
183
184/**
185 * process zipped outgoing packet
186 * @param data pointer to packet data
187 * @param length length of packet
188 * @param stateId packet's state id
189 */
190void ConnectionMonitor::processZippedOutgoingPacket( int tick, byte * data, int length, int stateId )
191{
192  nZOutgoingPackets++;
193 
194  // calculate bandwidth
195  outgoingZippedPacketHistory[tick] = length;
196  outgoingZippedBandWidth = calculateBandWidth( outgoingZippedPacketHistory, tick );
197 
198  //NETPRINTF(n)("UPSTREAM: user: %d bandwidth %f nOutgoingPackets %d\n", userId, outgoingZippedBandWidth, nOutgoingPackets );
199
200  if ( lastPrintTick < tick-1000 )
201  {
202    printStatis();
203    lastPrintTick = tick;
204  }
205}
206
207
208/**
209 * process zipped incoming packet
210 * @param data pointer to packet data
211 * @param length length of packet
212 * @param stateId packet's state id
213 * @param ackedState state which was acked by this packet
214 */
215void ConnectionMonitor::processZippedIncomingPacket( int tick, byte * data, int length )
216{
217  nZIncomingPackets++;
218 
219  // calculate bandwidth
220  incomingZippedPacketHistory[tick] = length;
221  incomingZippedBandWidth = calculateBandWidth( incomingZippedPacketHistory, tick );
222 
223  //NETPRINTF(n)("DOWNSTREAM: user: %d bandwidth %f nIncomingPackets %d\n", userId, incomingZippedBandWidth, nIncomingPackets );
224 
225}
226
227
228/**
229 * check if client sent no packets for SECS_TO_TIMEOUT
230 * @return true if last packet recieved \< NOW() - SECS_TO_TIMEOUT
231 */
232bool ConnectionMonitor::hasTimedOut( )
233{
234  if ( lastPacketTick + SECS_TO_TIMEOUT*1000 < SDL_GetTicks() && nIncomingPackets > 0 )
235    return true;
236 
237  if ( nIncomingPackets == 0 && nOutgoingPackets >= NETWORK_FREQUENCY*SECS_TO_TIMEOUT )
238    return true;
239 
240  return false;
241}
242
243
244
245/**
246 * prints bandwith usage, ping and other important things to telnet-console
247 */
248void ConnectionMonitor::printStatis( )
249{
250  NETPRINT(n)("============NETWORKSTATS FOR USER %d============\n", userId);
251  NETPRINT(n)("PING = %d\n", ping );
252  NETPRINT(n)("BANDWIDTH: UP: %f (%f) DOWN %f (%f)\n", outgoingZippedBandWidth, outgoingUnzippedBandWidth, incomingZippedBandWidth, incomingUnzippedBandWidth);
253  NETPRINT(n)("PACKETS: RECIEVED %d; SENT %d\n", nIncomingPackets, nOutgoingPackets );
254  NETPRINT(n)("================================================\n");
255}
256
257
Note: See TracBrowser for help on using the repository browser.