Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9869 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 6.8 KB
RevLine 
[5523]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:
[7954]12   main-programmer: Christoph Renner
[5523]13   co-programmer: ...
[5550]14*/
[5523]15
[5547]16#include "connection_monitor.h"
[7954]17#include "network_log.h"
18
[5618]19#include <debug.h>
20#include <SDL/SDL.h>
21#include <string.h>
[5526]22
[5564]23/* using namespace std is default, this needs to be here */
24
[9869]25ObjectListDefinition(ConnectionMonitor);
[7954]26/**
27 * constructor
28 * @param userId user's id
29 */
30ConnectionMonitor::ConnectionMonitor( int userId )
[5618]31{
32  /* set the class id for the base object and add ist to class list*/
[9869]33  this->registerObject(this, ConnectionMonitor::_objectList);
[9494]34
[7954]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;
[9494]45
[7954]46  this->lastPacketTick = 0;
47  this->lastPrintTick = 0;
48}
[5523]49
[7954]50/**
51 * deconstructor
52 */
53ConnectionMonitor::~ConnectionMonitor( )
54{
55}
[5581]56
[7954]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 */
[8068]63void ConnectionMonitor::processUnzippedOutgoingPacket( int tick, byte * data, int length, int stateId )
[7954]64{
65  nOutgoingPackets++;
[9494]66
[7954]67  // for ping calculation
68  sentStateTicks[stateId] = tick;
[9494]69
[7954]70  // calculate bandwidth
71  outgoingUnzippedPacketHistory[tick] = length;
72  outgoingUnzippedBandWidth = calculateBandWidth( outgoingUnzippedPacketHistory, tick );
[9494]73
[7954]74  //NETPRINTF(n)("UNZIPPED UPSTREAM: user: %d bandwidth %f\n", userId, outgoingUnzippedBandWidth );
[9494]75
[7954]76  // count zero bytes
[8068]77  //int nZeroBytes = 0;
[9494]78
[8068]79  //for ( int i = 0; i < length; i++ )
80  //  if ( data[i] == '\0' )
81  //    nZeroBytes++;
[9494]82
[7954]83  //NETPRINTF(n)( "ZEROBYTES: %d (%f%%)\n", nZeroBytes, ((float)100)*nZeroBytes/length );
84}
[5523]85
[7954]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 */
[8068]93void ConnectionMonitor::processUnzippedIncomingPacket( int tick, byte * data, int length, int stateId, int ackedState )
[7954]94{
95  nIncomingPackets++;
[9494]96
[7954]97  lastPacketTick = tick;
[9494]98
[7954]99  // calculate ping
100  if ( sentStateTicks.find( ackedState ) != sentStateTicks.end() )
101  {
102    ackDelay.push_back( tick - sentStateTicks[ackedState] );
103  }
[9494]104
[7954]105  while ( sentStateTicks.begin() != sentStateTicks.end() && sentStateTicks.begin()->first <= ackedState )
106    sentStateTicks.erase( sentStateTicks.begin() );
[9494]107
[7954]108  while ( ackDelay.size() > N_PACKETS_FOR_PING )
109    ackDelay.erase( ackDelay.begin() );
[9494]110
[7954]111  ping = 0;
[9494]112
[7954]113  for ( std::list<int>::iterator it = ackDelay.begin(); it != ackDelay.end(); it++ )
114    ping += *it;
[9494]115
[7954]116  if ( ackDelay.size() == 0 )
117    ping = -1;
118  else
119    ping /= ackDelay.size();
[9494]120
[7954]121  //NETPRINTF(n)("PING: user: %d ping: %d\n", userId, ping );
[9494]122
[7954]123  // calculate bandwidth
124  incomingUnzippedPacketHistory[tick] = length;
125  incomingUnzippedBandWidth = calculateBandWidth( incomingUnzippedPacketHistory, tick );
[9494]126
[7954]127  //NETPRINTF(n)("UNZIPPED DOWNSTREAM: user: %d bandwidth %f\n", userId, incomingUnzippedBandWidth );
[9494]128
[5618]129}
130
[7954]131/**
[8623]132 * remove old packets
[9494]133 * @param packetHistory
134 * @param tick
[8623]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/**
[7954]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 */
[8623]148float ConnectionMonitor::calculateBandWidth( std::map< int, int > & packetHistory, int tick )
[5618]149{
[8623]150  removeOldPackets( packetHistory, tick );
[9494]151
[7954]152  float res = 0.0f;
[8623]153#if 0
[7954]154  for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ )
155  {
[8068]156    if ( it != packetHistory.begin() )
157      res += it->second;
[7954]158  }
[9494]159
[7954]160  if ( packetHistory.size() <= 1 || tick - packetHistory.begin()->first == 0 )
161    res = 0.0f;
162  else
[8068]163    res /= (float)(tick - packetHistory.begin()->first);
[9494]164
[7954]165  res *= 1000.0f;
[8623]166#endif
167
[9494]168  for ( std::map<int,int>::const_iterator it = packetHistory.begin(); it != packetHistory.end(); it++ )
[8623]169  {
170    res += it->second;
171  }
[9494]172
[8623]173  if ( packetHistory.size() <= 1 )
174    res = 0.0f;
175  else
176    res /= (float)(tick - packetHistory.begin()->first);
[9494]177
[8623]178  res *= 1000.0f;
179
[7954]180  return res;
[5618]181}
[5523]182
183
[7954]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 */
[8068]190void ConnectionMonitor::processZippedOutgoingPacket( int tick, byte * data, int length, int stateId )
[5618]191{
[7954]192  nZOutgoingPackets++;
[9494]193
[7954]194  // calculate bandwidth
195  outgoingZippedPacketHistory[tick] = length;
196  outgoingZippedBandWidth = calculateBandWidth( outgoingZippedPacketHistory, tick );
[9494]197
[7954]198  //NETPRINTF(n)("UPSTREAM: user: %d bandwidth %f nOutgoingPackets %d\n", userId, outgoingZippedBandWidth, nOutgoingPackets );
[5550]199
[7954]200  if ( lastPrintTick < tick-1000 )
201  {
202    printStatis();
203    lastPrintTick = tick;
204  }
[5618]205}
[5581]206
207
[7954]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 */
[8068]215void ConnectionMonitor::processZippedIncomingPacket( int tick, byte * data, int length )
[5618]216{
[7954]217  nZIncomingPackets++;
[9494]218
[7954]219  // calculate bandwidth
220  incomingZippedPacketHistory[tick] = length;
221  incomingZippedBandWidth = calculateBandWidth( incomingZippedPacketHistory, tick );
[9494]222
[7954]223  //NETPRINTF(n)("DOWNSTREAM: user: %d bandwidth %f nIncomingPackets %d\n", userId, incomingZippedBandWidth, nIncomingPackets );
[9494]224
[5618]225}
[5581]226
[7954]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 */
[9494]232bool ConnectionMonitor::hasTimedOut( ) const
[5720]233{
[7954]234  if ( lastPacketTick + SECS_TO_TIMEOUT*1000 < SDL_GetTicks() && nIncomingPackets > 0 )
235    return true;
[9494]236
[7954]237  if ( nIncomingPackets == 0 && nOutgoingPackets >= NETWORK_FREQUENCY*SECS_TO_TIMEOUT )
238    return true;
[9494]239
[7954]240  return false;
[5720]241}
242
243
244
[7954]245/**
246 * prints bandwith usage, ping and other important things to telnet-console
247 */
[9494]248void ConnectionMonitor::printStatis( ) const
[5618]249{
[8068]250  NETPRINT(n)("============NETWORKSTATS FOR USER %d============\n", userId);
251  NETPRINT(n)("PING = %d\n", ping );
[7954]252  NETPRINT(n)("BANDWIDTH: UP: %f (%f) DOWN %f (%f)\n", outgoingZippedBandWidth, outgoingUnzippedBandWidth, incomingZippedBandWidth, incomingUnzippedBandWidth);
[8068]253  NETPRINT(n)("PACKETS: RECIEVED %d; SENT %d\n", nIncomingPackets, nOutgoingPackets );
254  NETPRINT(n)("================================================\n");
[5618]255}
256
[7954]257
Note: See TracBrowser for help on using the repository browser.