Index: branches/network/src/lib/network/connection_monitor.cc
===================================================================
--- branches/network/src/lib/network/connection_monitor.cc	(revision 7766)
+++ branches/network/src/lib/network/connection_monitor.cc	(revision 7767)
@@ -10,9 +10,11 @@
 
 ### File Specific:
-   main-programmer: Silvan Nellen
+   main-programmer: Christoph Renner
    co-programmer: ...
 */
 
 #include "connection_monitor.h"
+#include "network_log.h"
+
 #include <debug.h>
 #include <SDL/SDL.h>
@@ -22,126 +24,110 @@
 using namespace std;
 
-ConnectionMonitor::ConnectionMonitor()
+ConnectionMonitor::ConnectionMonitor( int userId )
 {
-
   /* set the class id for the base object and add ist to class list*/
   this->setClassID(CL_CONNECTION_MONITOR, "ConnectionMonitor");
+  
+  this->userId = userId;
+  this->ping = 0;
+  this->incomingUnzippedBandWidth = 0;
+  this->outgoingUnzippedBandWidth = 0;
+  this->nIncomingPackets = 0;
+  this->nOutgoingPackets = 0;
+  
+  this->lastPacketTick = 0;
+}
 
-  /*initialize variables*/
+ConnectionMonitor::~ConnectionMonitor( )
+{
+}
 
-  /*Data of the lifetime of the ConnectionMonitor Object*/
-  packetToAverage = 100;
-  protocollType = "default(TCP)";
+void ConnectionMonitor::processUnzippedOutgoingPacket( byte * data, int length, int stateId )
+{
+  int tick = SDL_GetTicks();
+  
+  nOutgoingPackets++;
+  
+  // for ping calculation
+  sentStateTicks[stateId] = tick;
+  
+  // calculate bandwidth
+  outgoingUnzippedPacketHistory[tick] = length;
+  outgoingUnzippedBandWidth = calculateBandWidth( outgoingUnzippedPacketHistory, tick );
+  
+  NETPRINTF(n)("UPSTREAM: user: %d bandwidth %f\n", userId, outgoingUnzippedBandWidth );
+  
+  // count zero bytes
+  int nZeroBytes = 0;
+  
+  for ( int i = 0; i < length; i++ )
+    if ( data[i] == '\0' )
+      nZeroBytes++;
+  
+  NETPRINTF(n)( "ZEROBYTES: %d (%f%%)\n", nZeroBytes, ((float)100)*nZeroBytes/length );
+}
 
-  startTime= SDL_GetTicks();
-  totalReceivedPackets=0;
-  averageDatarate=0;
-  totalLostPackets=0;
-  totalPacketloss=0;
+void ConnectionMonitor::processUnzippedIncomingPacket( byte * data, int length, int stateId, int ackedState )
+{
+  int tick = SDL_GetTicks();
+  
+  nIncomingPackets++;
+  
+  lastPacketTick = tick;
+  
+  // calculate ping
+  if ( sentStateTicks.find( ackedState ) != sentStateTicks.end() )
+  {
+    ackDelay.push_back( tick - sentStateTicks[ackedState] );
+  }
+      
+  while ( sentStateTicks.begin()->first <= ackedState )
+    sentStateTicks.erase( sentStateTicks.begin() );
+      
+  while ( ackDelay.size() > N_PACKETS_FOR_PING )
+    ackDelay.erase( ackDelay.begin() );
+      
+  ping = 0;
+      
+  for ( std::list<int>::iterator it = ackDelay.begin(); it != ackDelay.end(); it++ )
+    ping += *it;
+      
+  if ( ackDelay.size() == 0 )
+    ping = -1;
+  else
+    ping /= ackDelay.size();
+      
+  NETPRINTF(n)("PING: user: %d ping: %d\n", userId, ping );
+  
+  // calculate bandwidth
+  incomingUnzippedPacketHistory[tick] = length;
+  incomingUnzippedBandWidth = calculateBandWidth( incomingUnzippedPacketHistory, tick );
+  
+  NETPRINTF(n)("DOWNSTREAM: user: %d bandwidth %f\n", userId, incomingUnzippedBandWidth );
+  
+}
 
-  /*Data of the current packet*/
-  currentPacketID=0;
-  currentPacketTick=0;
-  lastPacketID=0;
-  lastPacketTick=0;
-  currentDelay=0;
-
-  /*Data of the last n packets (n is specified by paxketsToAverage)*/
-  sizeOfLastFewPackets=0;
-  currentDatarate=0;
-  lastFewDelays = new unsigned int [packetToAverage];
-  lastFewPackets = new byte* [packetToAverage];
-  packetCounter=0;
-
-
+float ConnectionMonitor::calculateBandWidth( std::map< int, int > packetHistory, int tick )
+{
+  // delete old packets
+  while ( packetHistory.begin()->first < tick - MSECS_TO_CALC_BWIDTH )
+    packetHistory.erase( packetHistory.begin() );
+  
+  float res = 0.0f;
+  
+  for ( std::map<int,int>::iterator it = packetHistory.begin(); it != packetHistory.end(); it++ )
+  {
+    res += it->second;
+  }
+  
+  if ( packetHistory.size() <= 1 || tick - packetHistory.begin()->first == 0 )
+    res = 0;
+  else
+    res /= (float)((tick - packetHistory.begin()->first)*( 1 + 1/((float)(packetHistory.size()-1)) ));
+  
+  res *= 1000;
+  
+  return res;
 }
 
 
-ConnectionMonitor::~ConnectionMonitor()
-{
-
-
-}
-
-
-
-void ConnectionMonitor::processPacket(byte* currentPacket, unsigned int packetLength)
-{
-  /*Process the current Packet*/
-  currentPacketTick = SDL_GetTicks();
-  currentDelay = currentPacketTick - lastPacketTick;
-
-  /*Do whats needed for Averaging*/
-
-  if(packetCounter == packetToAverage)
-    {
-      computeCurrentDatarate();
-      displayStatistic();
-      packetCounter = 0;
-      sizeOfLastFewPackets = 0;
-    }
-
-  lastFewDelays[packetCounter] = currentDelay;
-  lastFewPackets[packetCounter] = currentPacket;
-  sizeOfLastFewPackets += packetLength;
-
-  /*Update the lifetime Variables*/
-  totalReceivedPackets ++;
-  float timeDiff = this->currentPacketTick - this->startTime;
-  if( timeDiff != 0.0f )
-    averageDatarate = totalReceivedPackets/timeDiff;
-
-  /*Preparefor the next Packet*/
-  lastPacketTick = currentPacketTick;
-  packetCounter++;
-}
-
-
-/* Compute the value of current Datarate*/
-void ConnectionMonitor::computeCurrentDatarate()
-{
-  int timeForLastFewPackets=0;
-  for(int i=0;i < packetToAverage;i++)
-    timeForLastFewPackets += lastFewDelays[i];
-
-  if( timeForLastFewPackets != 0)
-    currentDatarate = sizeOfLastFewPackets/timeForLastFewPackets;
-}
-
-void doUDPRelatedStuff()
-{
-  /*  Do protocol related stuff
-
-  Only for Udp:
-  "currentPacketID = getID from package";
-
-  if(currentPacketID - lastPacketID > 1)
-  {
-  totalLostPackets += currentPacketID - lastPacketID;
-}
-
-  totalPacketloss = (totalLostPackets/totalReceivedPackets)*100 ;
-  */
-}
-
-
-
-/* Display connectoin statistic*/
-void ConnectionMonitor::displayStatistic()
-{
-//   PRINT(0)("============================================\n");
-//   PRINT(0)("Connection Monitor Network Statistics:\n");
-//   PRINT(0)("Total received packets:",totalReceivedPackets);
-//   PRINT(0)("Average datarate :\n",averageDatarate);
-//   PRINT(0)("Total lost packets:",totalLostPackets);
-//   PRINT(0)("Packetloss [%] :\n",totalPacketloss);
-//
-//   PRINT(0)("Current datarate :\n",currentDatarate);
-//   PRINT(0)("Delays of the last few packets :\n");
-//   for(int i=1 ;i <= packetToAverage-1;i++)
-//     PRINT(0)("%i ",lastFewDelays[i]);
-//
-//   PRINT(0)("============================================\n");
-
-}
-
Index: branches/network/src/lib/network/connection_monitor.h
===================================================================
--- branches/network/src/lib/network/connection_monitor.h	(revision 7766)
+++ branches/network/src/lib/network/connection_monitor.h	(revision 7767)
@@ -10,44 +10,44 @@
 #include "netdefs.h"
 
+#include <map>
+
+#define N_PACKETS_FOR_PING 20
+#define MSECS_TO_CALC_BWIDTH 1000
+#define SECS_TO_TIMEOUT 10
+
 class ConnectionMonitor : virtual public BaseObject
-  {
+{
   public:
-    ConnectionMonitor();
+    ConnectionMonitor( int userId );
     virtual ~ConnectionMonitor();
 
-    void                processPacket(byte* packet, unsigned int packetLength);
+    void processUnzippedOutgoingPacket( byte * data, int length, int stateId );
+    void processUnzippedIncomingPacket( byte * data, int length, int stateId, int ackedState );
+    void processZippedPacket(  );
+    
+    void calculatePing();
+    
+    bool hasTimedOut(){ return ( lastPacketTick + SECS_TO_TIMEOUT*1000 < SDL_GetTicks() ) && nIncomingPackets > 0; }
+    
+  private:
+    float calculateBandWidth( std::map<int,int> packetHistory, int tick );
+    
+    int userId;       //!< user's id 
 
+    std::map<int,int>   sentStateTicks;
+    
+    std::map<int,int>   incomingUnzippedPacketHistory;
+    std::map<int,int>   outgoingUnzippedPacketHistory;
+    
+    std::list<int>      ackDelay;
+    int                 ping;
 
-  private:
-
-
-    void                displayStatistic();
-    void                computeCurrentDatarate();
-    void                doUDPRelatedStuff();
-
-    /*Data of the lifetime of the ConnectionMonitor Object*/
-    unsigned int        packetToAverage;
-    char*               protocollType;
-
-    unsigned int        totalReceivedPackets;
-    float               averageDatarate;
-    unsigned int        totalLostPackets;
-    float               totalPacketloss;
-    unsigned int        startTime;
-
-    /*Data of the current packet*/
-    unsigned int        currentPacketID;
-    unsigned int        currentPacketTick;
-    unsigned int        lastPacketID;
-    unsigned int        lastPacketTick;
-    unsigned int        currentDelay;
-
-    /*Data of the last n packets (n is specified by paxketsToAverage)*/
-    unsigned int        sizeOfLastFewPackets;
-    unsigned int        currentDatarate;
-    byte**              lastFewPackets;
-    unsigned int*       lastFewDelays;
-    unsigned int        packetCounter;
-
+    float               incomingUnzippedBandWidth;
+    float               outgoingUnzippedBandWidth;
+    
+    int                 nIncomingPackets;
+    int                 nOutgoingPackets;
+    
+    int                 lastPacketTick;
   };
 
Index: branches/network/src/lib/network/network_game_manager.cc
===================================================================
--- branches/network/src/lib/network/network_game_manager.cc	(revision 7766)
+++ branches/network/src/lib/network/network_game_manager.cc	(revision 7767)
@@ -478,4 +478,8 @@
 {
   const std::list<BaseObject*>* playableList = ClassList::getList(CL_PLAYABLE);
+  
+  if ( !playableList )
+    return false;
+  
   std::list<BaseObject*>::const_iterator it = playableList->begin();
 
Index: branches/network/src/lib/network/network_stream.cc
===================================================================
--- branches/network/src/lib/network/network_stream.cc	(revision 7766)
+++ branches/network/src/lib/network/network_stream.cc	(revision 7767)
@@ -56,6 +56,4 @@
   /* initialize the references */
   this->type = NET_CLIENT;
-  this->networkProtocol = new NetworkProtocol();
-  this->connectionMonitor = new ConnectionMonitor();
 }
 
@@ -68,6 +66,5 @@
   this->peers[0].userId = 0;
   this->peers[0].isServer = true;
-  this->networkProtocol = new NetworkProtocol();
-  this->connectionMonitor = new ConnectionMonitor();
+  this->peers[0].connectionMonitor = new ConnectionMonitor( 0 );
 }
 
@@ -78,6 +75,4 @@
   this->init();
   this->serverSocket = new UdpServerSocket(port);
-  this->networkProtocol = new NetworkProtocol();
-  this->connectionMonitor = new ConnectionMonitor();
   this->bActive = true;
 }
@@ -126,6 +121,4 @@
   }
 
-  delete connectionMonitor;
-  delete networkProtocol;
 }
 
@@ -170,6 +163,5 @@
     this->synchronizeables.erase(disconnectSynchro);
   
-  //TODO set timestamp
-  oldSynchronizeables[sync.getUniqueID()] = 0;
+  oldSynchronizeables[sync.getUniqueID()] = SDL_GetTicks();
 }
 
@@ -202,4 +194,5 @@
   }
 
+  cleanUpOldSyncList();
   handleHandshakes();
   handleUpstream();
@@ -223,4 +216,5 @@
       peers[clientId].socket = tempNetworkSocket;
       peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID() );
+      peers[clientId].connectionMonitor = new ConnectionMonitor( clientId );
       peers[clientId].handshake->setUniqueID(clientId);
       peers[clientId].userId = clientId;
@@ -237,4 +231,5 @@
       peers[clientId].handshake = new Handshake(true, clientId, this->networkGameManager->getUniqueID(), MessageManager::getInstance()->getUniqueID());
       peers[clientId].handshake->setUniqueID(clientId);
+      peers[clientId].connectionMonitor = new ConnectionMonitor( clientId );
       peers[clientId].userId = clientId;
       peers[clientId].isServer = false;
@@ -259,5 +254,9 @@
   for ( PeerList::iterator it = peers.begin(); it != peers.end(); it++ )
   {
-    if ( it->second.socket && !it->second.socket->isOk() )
+    //TODO check for timeout
+    if (  
+          (it->second.socket && !it->second.socket->isOk() ) ||
+          it->second.connectionMonitor->hasTimedOut()
+       )
     {
       PRINTF(0)("Client is gone: %d\n", it->second.userId);
@@ -424,5 +423,7 @@
     
     assert( peer->second.socket->writePacket( buf, offset ) );
-    peer->second.sentStateTicks[currentState] = SDL_GetTicks();
+    
+    peer->second.connectionMonitor->processUnzippedOutgoingPacket( buf, offset, currentState );
+    
     NETPRINTF(n)("send packet: %d userId = %d\n", offset, peer->second.userId);
   }
@@ -509,5 +510,10 @@
           }
 
-        //TODO dont accept new object from all peers (probably only servers)
+          if ( !peers[peer->second.userId].isServer )
+          {
+            offset += syncDataLength;
+            continue;
+          }
+          
           int leafClassId;
           if ( INTSIZE > length - offset )
@@ -564,26 +570,5 @@
       }
       
-      if ( peer->second.sentStateTicks.find( ackedState ) != peer->second.sentStateTicks.end() )
-      {
-        peer->second.ackDelay.push_back( SDL_GetTicks() - peer->second.sentStateTicks[ackedState] );
-      }
-      
-      while ( peer->second.sentStateTicks.begin()->first <= ackedState )
-        peer->second.sentStateTicks.erase( peer->second.sentStateTicks.begin() );
-      
-      while ( peer->second.ackDelay.size() > 20 )
-        peer->second.ackDelay.erase( peer->second.ackDelay.begin() );
-      
-      peer->second.ping = 0;
-      
-      for ( std::list<int>::iterator it = peer->second.ackDelay.begin(); it != peer->second.ackDelay.end(); it++ )
-        peer->second.ping += *it;
-      
-      if ( peer->second.ackDelay.size() == 0 )
-        peer->second.ping = -1;
-      else
-        peer->second.ping /= peer->second.ackDelay.size();
-      
-      NETPRINTF(n)("PING: user: %d ping: %d\n", peer->second.userId, peer->second.ping );
+      peer->second.connectionMonitor->processUnzippedIncomingPacket( buf, offset, currentState, ackedState );
     
       peer->second.lastAckedState = ackedState;
@@ -600,5 +585,4 @@
  * @todo create playable for new user
  */
-#include "world_entities/space_ships/space_ship.h"
 void NetworkStream::handleNewClient( int userId )
 {
@@ -608,7 +592,27 @@
 }
 
-
-
-
-
-
+/**
+ * removes old items from oldSynchronizeables
+ */
+void NetworkStream::cleanUpOldSyncList( )
+{
+  int now = SDL_GetTicks();
+  
+  for ( std::map<int,int>::iterator it = oldSynchronizeables.begin(); it != oldSynchronizeables.end();  )
+  {
+    if ( it->second < now - 10*1000 )
+    {
+      std::map<int,int>::iterator delIt = it;
+      it++;
+      oldSynchronizeables.erase( delIt );
+      continue;
+    }
+    it++;
+  }
+}
+
+
+
+
+
+
Index: branches/network/src/lib/network/network_stream.h
===================================================================
--- branches/network/src/lib/network/network_stream.h	(revision 7766)
+++ branches/network/src/lib/network/network_stream.h	(revision 7767)
@@ -12,13 +12,11 @@
 
 #include "data_stream.h"
-#include "network_protocol.h"
 #include "server_socket.h"
 #include "handshake.h"
+#include "connection_monitor.h"
 
 class Synchronizeable;
 class NetworkSocket;
 class ServerSocket;
-class ConnectionMonitor;
-class NetworkProtocol;
 class NetworkGameManager;
 
@@ -27,14 +25,12 @@
   public:
     PeerInfo() { clear(); }
-    void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; }
-    int               userId;
-    bool              isServer;
-    NetworkSocket *   socket;
-    Handshake *       handshake;
-    int               lastAckedState;
-    int               lastRecvedState;
-    std::map<int,int> sentStateTicks;
-    std::list<int>    ackDelay;
-    int               ping;
+    void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; connectionMonitor = NULL; }
+    int                 userId;
+    bool                isServer;
+    NetworkSocket *     socket;
+    Handshake *         handshake;
+    ConnectionMonitor * connectionMonitor;
+    int                 lastAckedState;
+    int                 lastRecvedState;
 };
 
@@ -85,14 +81,12 @@
     void handleDownstream(); 
     void handleNewClient( int userId );
+    void cleanUpOldSyncList();
 
 
   private:
-    NetworkProtocol*           networkProtocol;
-    ConnectionMonitor*         connectionMonitor;
     SynchronizeableList        synchronizeables;
     PeerList                   peers;
     ServerSocket*              serverSocket;
     int                        type;
-    Header                     packetHeader;
     bool                       bActive;
     std::list<int>             freeSocketSlots;
Index: branches/network/src/lib/network/synchronizeable.cc
===================================================================
--- branches/network/src/lib/network/synchronizeable.cc	(revision 7766)
+++ branches/network/src/lib/network/synchronizeable.cc	(revision 7767)
@@ -351,5 +351,5 @@
   for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ )
   {
-    for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it++ )
+    for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ )
     {
       if ( (*it2)->data )
@@ -357,5 +357,5 @@
       (*it2)->data = NULL;
       
-      delete [] *it2;
+      delete *it2;
     }
   }
@@ -365,5 +365,5 @@
   for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ )
   {
-    for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it++ )
+    for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ )
     {
       if ( (*it2)->data )
@@ -371,5 +371,5 @@
       (*it2)->data = NULL;
       
-      delete [] *it2;
+      delete *it2;
     }
   }
Index: branches/network/src/world_entities/space_ships/hover.cc
===================================================================
--- branches/network/src/world_entities/space_ships/hover.cc	(revision 7766)
+++ branches/network/src/world_entities/space_ships/hover.cc	(revision 7767)
@@ -176,4 +176,15 @@
   this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
   this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
+  
+  // NETWORK THINGS
+  
+  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
+  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
+  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
+  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
+  registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) );
+  registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) );
+  
+  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
 }
 
