[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: |
---|
| 12 | main-programmer: Silvan Nellen |
---|
| 13 | co-programmer: ... |
---|
[5550] | 14 | */ |
---|
[5523] | 15 | |
---|
[5547] | 16 | #include "connection_monitor.h" |
---|
[5618] | 17 | #include <debug.h> |
---|
| 18 | #include <SDL/SDL.h> |
---|
| 19 | #include <string.h> |
---|
[5526] | 20 | |
---|
[5564] | 21 | /* using namespace std is default, this needs to be here */ |
---|
| 22 | using namespace std; |
---|
| 23 | |
---|
[5523] | 24 | ConnectionMonitor::ConnectionMonitor() |
---|
[5618] | 25 | { |
---|
[5523] | 26 | |
---|
[5618] | 27 | /* set the class id for the base object and add ist to class list*/ |
---|
| 28 | this->setClassID(CL_CONNECTION_MONITOR, "ConnectionMonitor"); |
---|
[5523] | 29 | |
---|
[5618] | 30 | /*initialize variables*/ |
---|
[5581] | 31 | |
---|
[5618] | 32 | /*Data of the lifetime of the ConnectionMonitor Object*/ |
---|
[5726] | 33 | packetToAverage = 100; |
---|
| 34 | protocollType = "default(TCP)"; |
---|
[5523] | 35 | |
---|
[5618] | 36 | startTime= SDL_GetTicks(); |
---|
| 37 | totalReceivedPackets=0; |
---|
| 38 | averageDatarate=0; |
---|
| 39 | totalLostPackets=0; |
---|
| 40 | totalPacketloss=0; |
---|
[5523] | 41 | |
---|
[5618] | 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 | |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
[5547] | 60 | ConnectionMonitor::~ConnectionMonitor() |
---|
[5618] | 61 | { |
---|
[5523] | 62 | |
---|
| 63 | |
---|
[5618] | 64 | } |
---|
[5523] | 65 | |
---|
| 66 | |
---|
| 67 | |
---|
[5618] | 68 | void ConnectionMonitor::processPacket(byte* currentPacket, unsigned int packetLength) |
---|
| 69 | { |
---|
[5726] | 70 | /*Process the current Packet*/ |
---|
[5618] | 71 | currentPacketTick = SDL_GetTicks(); |
---|
| 72 | currentDelay = currentPacketTick - lastPacketTick; |
---|
[5550] | 73 | |
---|
[5618] | 74 | /*Do whats needed for Averaging*/ |
---|
[5581] | 75 | |
---|
[5808] | 76 | if(packetCounter == packetToAverage) |
---|
[5618] | 77 | { |
---|
[5720] | 78 | computeCurrentDatarate(); |
---|
[5618] | 79 | displayStatistic(); |
---|
| 80 | packetCounter = 0; |
---|
[5720] | 81 | sizeOfLastFewPackets = 0; |
---|
[5618] | 82 | } |
---|
[5581] | 83 | |
---|
[5618] | 84 | lastFewDelays[packetCounter] = currentDelay; |
---|
| 85 | lastFewPackets[packetCounter] = currentPacket; |
---|
| 86 | sizeOfLastFewPackets += packetLength; |
---|
[5581] | 87 | |
---|
[5618] | 88 | /*Update the lifetime Variables*/ |
---|
| 89 | totalReceivedPackets ++; |
---|
[5800] | 90 | float timeDiff = this->currentPacketTick - this->startTime; |
---|
| 91 | if( timeDiff != 0.0f ) |
---|
| 92 | averageDatarate = totalReceivedPackets/timeDiff; |
---|
[5581] | 93 | |
---|
[5618] | 94 | /*Preparefor the next Packet*/ |
---|
| 95 | lastPacketTick = currentPacketTick; |
---|
| 96 | packetCounter++; |
---|
| 97 | } |
---|
[5581] | 98 | |
---|
| 99 | |
---|
[5618] | 100 | /* Compute the value of current Datarate*/ |
---|
[5720] | 101 | void ConnectionMonitor::computeCurrentDatarate() |
---|
[5618] | 102 | { |
---|
[5726] | 103 | int timeForLastFewPackets=0; |
---|
[5720] | 104 | for(int i=0;i < packetToAverage;i++) |
---|
[5618] | 105 | timeForLastFewPackets += lastFewDelays[i]; |
---|
[5581] | 106 | |
---|
[5749] | 107 | if( timeForLastFewPackets != 0) |
---|
| 108 | currentDatarate = sizeOfLastFewPackets/timeForLastFewPackets; |
---|
[5618] | 109 | } |
---|
[5581] | 110 | |
---|
[5720] | 111 | void doUDPRelatedStuff() |
---|
| 112 | { |
---|
[5726] | 113 | /* Do protocol related stuff |
---|
[5581] | 114 | |
---|
[5726] | 115 | Only for Udp: |
---|
| 116 | "currentPacketID = getID from package"; |
---|
| 117 | |
---|
| 118 | if(currentPacketID - lastPacketID > 1) |
---|
| 119 | { |
---|
| 120 | totalLostPackets += currentPacketID - lastPacketID; |
---|
[5720] | 121 | } |
---|
| 122 | |
---|
[5726] | 123 | totalPacketloss = (totalLostPackets/totalReceivedPackets)*100 ; |
---|
| 124 | */ |
---|
| 125 | } |
---|
[5720] | 126 | |
---|
| 127 | |
---|
[5726] | 128 | |
---|
[5720] | 129 | /* Display connectoin statistic*/ |
---|
[5618] | 130 | void ConnectionMonitor::displayStatistic() |
---|
| 131 | { |
---|
[5798] | 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); |
---|
[5800] | 138 | // |
---|
[5798] | 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]); |
---|
[5800] | 143 | // |
---|
[5798] | 144 | // PRINT(0)("============================================\n"); |
---|
[5618] | 145 | |
---|
| 146 | } |
---|
| 147 | |
---|