Changeset 7767 in orxonox.OLD for branches/network/src/lib/network/connection_monitor.cc
- Timestamp:
- May 23, 2006, 11:40:29 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/connection_monitor.cc
r5822 r7767 10 10 11 11 ### File Specific: 12 main-programmer: Silvan Nellen12 main-programmer: Christoph Renner 13 13 co-programmer: ... 14 14 */ 15 15 16 16 #include "connection_monitor.h" 17 #include "network_log.h" 18 17 19 #include <debug.h> 18 20 #include <SDL/SDL.h> … … 22 24 using namespace std; 23 25 24 ConnectionMonitor::ConnectionMonitor( )26 ConnectionMonitor::ConnectionMonitor( int userId ) 25 27 { 26 27 28 /* set the class id for the base object and add ist to class list*/ 28 29 this->setClassID(CL_CONNECTION_MONITOR, "ConnectionMonitor"); 30 31 this->userId = userId; 32 this->ping = 0; 33 this->incomingUnzippedBandWidth = 0; 34 this->outgoingUnzippedBandWidth = 0; 35 this->nIncomingPackets = 0; 36 this->nOutgoingPackets = 0; 37 38 this->lastPacketTick = 0; 39 } 29 40 30 /*initialize variables*/ 41 ConnectionMonitor::~ConnectionMonitor( ) 42 { 43 } 31 44 32 /*Data of the lifetime of the ConnectionMonitor Object*/ 33 packetToAverage = 100; 34 protocollType = "default(TCP)"; 45 void ConnectionMonitor::processUnzippedOutgoingPacket( byte * data, int length, int stateId ) 46 { 47 int tick = SDL_GetTicks(); 48 49 nOutgoingPackets++; 50 51 // for ping calculation 52 sentStateTicks[stateId] = tick; 53 54 // calculate bandwidth 55 outgoingUnzippedPacketHistory[tick] = length; 56 outgoingUnzippedBandWidth = calculateBandWidth( outgoingUnzippedPacketHistory, tick ); 57 58 NETPRINTF(n)("UPSTREAM: user: %d bandwidth %f\n", userId, outgoingUnzippedBandWidth ); 59 60 // count zero bytes 61 int nZeroBytes = 0; 62 63 for ( int i = 0; i < length; i++ ) 64 if ( data[i] == '\0' ) 65 nZeroBytes++; 66 67 NETPRINTF(n)( "ZEROBYTES: %d (%f%%)\n", nZeroBytes, ((float)100)*nZeroBytes/length ); 68 } 35 69 36 startTime= SDL_GetTicks(); 37 totalReceivedPackets=0; 38 averageDatarate=0; 39 totalLostPackets=0; 40 totalPacketloss=0; 70 void ConnectionMonitor::processUnzippedIncomingPacket( byte * data, int length, int stateId, int ackedState ) 71 { 72 int tick = SDL_GetTicks(); 73 74 nIncomingPackets++; 75 76 lastPacketTick = tick; 77 78 // calculate ping 79 if ( sentStateTicks.find( ackedState ) != sentStateTicks.end() ) 80 { 81 ackDelay.push_back( tick - sentStateTicks[ackedState] ); 82 } 83 84 while ( sentStateTicks.begin()->first <= ackedState ) 85 sentStateTicks.erase( sentStateTicks.begin() ); 86 87 while ( ackDelay.size() > N_PACKETS_FOR_PING ) 88 ackDelay.erase( ackDelay.begin() ); 89 90 ping = 0; 91 92 for ( std::list<int>::iterator it = ackDelay.begin(); it != ackDelay.end(); it++ ) 93 ping += *it; 94 95 if ( ackDelay.size() == 0 ) 96 ping = -1; 97 else 98 ping /= ackDelay.size(); 99 100 NETPRINTF(n)("PING: user: %d ping: %d\n", userId, ping ); 101 102 // calculate bandwidth 103 incomingUnzippedPacketHistory[tick] = length; 104 incomingUnzippedBandWidth = calculateBandWidth( incomingUnzippedPacketHistory, tick ); 105 106 NETPRINTF(n)("DOWNSTREAM: user: %d bandwidth %f\n", userId, incomingUnzippedBandWidth ); 107 108 } 41 109 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 110 float ConnectionMonitor::calculateBandWidth( std::map< int, int > packetHistory, int tick ) 111 { 112 // delete old packets 113 while ( packetHistory.begin()->first < tick - MSECS_TO_CALC_BWIDTH ) 114 packetHistory.erase( packetHistory.begin() ); 115 116 float res = 0.0f; 117 118 for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ ) 119 { 120 res += it->second; 121 } 122 123 if ( packetHistory.size() <= 1 || tick - packetHistory.begin()->first == 0 ) 124 res = 0; 125 else 126 res /= (float)((tick - packetHistory.begin()->first)*( 1 + 1/((float)(packetHistory.size()-1)) )); 127 128 res *= 1000; 129 130 return res; 57 131 } 58 132 59 133 60 ConnectionMonitor::~ConnectionMonitor()61 {62 63 64 }65 66 67 68 void 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 float timeDiff = this->currentPacketTick - this->startTime;91 if( timeDiff != 0.0f )92 averageDatarate = totalReceivedPackets/timeDiff;93 94 /*Preparefor the next Packet*/95 lastPacketTick = currentPacketTick;96 packetCounter++;97 }98 99 100 /* Compute the value of current Datarate*/101 void ConnectionMonitor::computeCurrentDatarate()102 {103 int timeForLastFewPackets=0;104 for(int i=0;i < packetToAverage;i++)105 timeForLastFewPackets += lastFewDelays[i];106 107 if( timeForLastFewPackets != 0)108 currentDatarate = sizeOfLastFewPackets/timeForLastFewPackets;109 }110 111 void doUDPRelatedStuff()112 {113 /* Do protocol related stuff114 115 Only for Udp:116 "currentPacketID = getID from package";117 118 if(currentPacketID - lastPacketID > 1)119 {120 totalLostPackets += currentPacketID - lastPacketID;121 }122 123 totalPacketloss = (totalLostPackets/totalReceivedPackets)*100 ;124 */125 }126 127 128 129 /* Display connectoin statistic*/130 void ConnectionMonitor::displayStatistic()131 {132 // PRINT(0)("============================================\n");133 // PRINT(0)("Connection Monitor Network Statistics:\n");134 // PRINT(0)("Total received packets:",totalReceivedPackets);135 // PRINT(0)("Average datarate :\n",averageDatarate);136 // PRINT(0)("Total lost packets:",totalLostPackets);137 // PRINT(0)("Packetloss [%] :\n",totalPacketloss);138 //139 // PRINT(0)("Current datarate :\n",currentDatarate);140 // PRINT(0)("Delays of the last few packets :\n");141 // for(int i=1 ;i <= packetToAverage-1;i++)142 // PRINT(0)("%i ",lastFewDelays[i]);143 //144 // PRINT(0)("============================================\n");145 146 }147
Note: See TracChangeset
for help on using the changeset viewer.