Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

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
[9357]25
[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*/
33  this->setClassID(CL_CONNECTION_MONITOR, "ConnectionMonitor");
[7954]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}
[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++;
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
[8068]77  //int nZeroBytes = 0;
[7954]78 
[8068]79  //for ( int i = 0; i < length; i++ )
80  //  if ( data[i] == '\0' )
81  //    nZeroBytes++;
[7954]82 
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++;
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 
[5618]129}
130
[7954]131/**
[8623]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/**
[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 );
[7954]151 
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  }
159 
160  if ( packetHistory.size() <= 1 || tick - packetHistory.begin()->first == 0 )
161    res = 0.0f;
162  else
[8068]163    res /= (float)(tick - packetHistory.begin()->first);
[7954]164 
165  res *= 1000.0f;
[8623]166#endif
167
168  for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ )
169  {
170    res += it->second;
171  }
[7954]172 
[8623]173  if ( packetHistory.size() <= 1 )
174    res = 0.0f;
175  else
176    res /= (float)(tick - packetHistory.begin()->first);
177 
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++;
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 );
[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++;
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 
[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 */
232bool ConnectionMonitor::hasTimedOut( )
[5720]233{
[7954]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;
[5720]241}
242
243
244
[7954]245/**
246 * prints bandwith usage, ping and other important things to telnet-console
247 */
248void ConnectionMonitor::printStatis( )
[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.