Index: code/trunk/src/libraries/network/Server.cc
===================================================================
--- code/trunk/src/libraries/network/Server.cc	(revision 7284)
+++ code/trunk/src/libraries/network/Server.cc	(revision 7801)
@@ -60,4 +60,5 @@
 #include "FunctionCallManager.h"
 #include "GamestateManager.h"
+#include "WANDiscovery.h"
 
 namespace orxonox
@@ -99,4 +100,12 @@
   }
 
+
+  /** helper that connects to the master server */
+  void Server::helper_ConnectToMasterserver()
+  {
+//     WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " " 
+//       MSPROTO_REGISTER_SERVER );
+  }
+
   /**
   * This function opens the server by creating the listener thread
@@ -107,5 +116,16 @@
     COUT(4) << "opening server" << endl;
     this->openListener();
+    
+    /* make discoverable on LAN */
     LANDiscoverable::setActivity(true);
+
+    /* make discoverable on WAN */
+    WANDiscoverable::setActivity(true);
+    /* TODO this needs to be optional, we need a switch from the UI to
+     * enable/disable this 
+     */
+//     helper_ConnectToMasterserver();
+
+    /* done */
     return;
   }
@@ -120,4 +140,10 @@
     this->disconnectClients();
     this->closeListener();
+
+    /* tell master server we're closing */
+    COUT(2) << "disconnecting." << endl;
+    WANDiscoverable::setActivity(false);    
+    COUT(2) << "disconnecting done" << endl;
+
     LANDiscoverable::setActivity(false);
     return;
@@ -130,6 +156,6 @@
     while(temp){
       chat = new packet::Chat(message, playerID);
-      chat->setClientID(temp->getID());
-      if(!chat->send())
+      chat->setPeerID(temp->getID());
+      if(!chat->send( static_cast<Host*>(this) ))
         COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
       temp = temp->next();
@@ -139,4 +165,28 @@
   }
 
+
+  /* handle incoming data */
+  int rephandler( char *addr, ENetEvent *ev )
+  { 
+    /* reply to pings */
+    if( !strncmp( (char *)ev->packet->data, MSPROTO_PING_GAMESERVER, 
+      MSPROTO_PING_GAMESERVER_LEN ) )
+      //this->msc.sendRequest( MSPROTO_ACK );
+      /* NOTE implement this after pollForReply
+       * reimplementation 
+       */
+      return 0;
+
+    /* done handling, return all ok code 0 */
+    return 0;
+  }
+
+  void Server::helper_HandleMasterServerRequests()
+  { 
+    /* poll the master server for replies and see whether something 
+     * has to be done or changed.
+     */
+    //WANDiscovery::getInstance().msc.pollForReply( rhandler, 10 );
+  }
 
   /**
@@ -149,6 +199,11 @@
     // receive incoming packets
     Connection::processQueue();
+
     // receive and process incoming discovery packets
     LANDiscoverable::update();
+    
+    // receive and process requests from master server
+    /* todo */
+    //helper_HandleMasterServerRequests();
 
     if ( ClientInformation::hasClients() )
@@ -156,7 +211,8 @@
       // process incoming gamestates
       GamestateManager::processGamestates();
+      FunctionCallManager::processBufferedFunctionCalls();
 
       // send function calls to clients
-      FunctionCallManager::sendCalls();
+      FunctionCallManager::sendCalls( static_cast<Host*>(this) );
 
       //this steers our network frequency
@@ -167,11 +223,11 @@
         updateGamestate();
       }
-      sendPackets(); // flush the enet queue
-    }
-  }
-
-  bool Server::queuePacket(ENetPacket *packet, int clientID)
-  {
-    return ServerConnection::addPacket(packet, clientID);
+//       sendPackets(); // flush the enet queue
+    }
+  }
+
+  void Server::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID)
+  {
+    ServerConnection::addPacket(packet, clientID, channelID);
   }
 
@@ -209,57 +265,27 @@
       return;
     GamestateManager::update();
-    COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
+//     COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;
     //std::cout << "updated gamestate, sending it" << std::endl;
     //if(clients->getGamestateID()!=GAMESTATEID_INITIAL)
-    sendGameState();
+    sendGameStates();
     sendObjectDeletes();
-    COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
+//     COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;
     //std::cout << "sent gamestate" << std::endl;
   }
 
-  bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){
-    packet::Packet *p = packet::Packet::createPacket(packet, peer);
-    return p->process();
-  }
-
-  /**
-  * sends the gamestate
-  */
-  bool Server::sendGameState()
-  {
-//     COUT(5) << "Server: starting function sendGameState" << std::endl;
-//     ClientInformation *temp = ClientInformation::getBegin();
-//     bool added=false;
-//     while(temp != NULL){
-//       if( !(temp->getSynched()) ){
-//         COUT(5) << "Server: not sending gamestate" << std::endl;
-//         temp=temp->next();
-//         if(!temp)
-//           break;
-//         continue;
-//       }
-//       COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl;
-//       COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
-//       int cid = temp->getID(); //get client id
-//       packet::Gamestate *gs = GamestateManager::popGameState(cid);
-//       if(gs==NULL){
-//         COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl;
-//         temp = temp->next();
-//         continue;
-//       }
-//       //std::cout << "adding gamestate" << std::endl;
-//       gs->setClientID(cid);
-//       if ( !gs->send() ){
-//         COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
-//         temp->addFailure();
-//       }else
-//         temp->resetFailures();
-//       added=true;
-//       temp=temp->next();
-//       // gs gets automatically deleted by enet callback
-//     }
-    GamestateManager::sendGamestates();
+  /**
+  * sends the current gamestate to all peers
+  */
+  bool Server::sendGameStates()
+  {
+    std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates();
+    std::vector<packet::Gamestate*>::iterator it;
+    for( it = gamestates.begin(); it != gamestates.end(); ++it )
+    {
+      (*it)->send(static_cast<Host*>(this));
+    }
     return true;
   }
+
 
   bool Server::sendObjectDeletes()
@@ -286,7 +312,7 @@
       packet::DeleteObjects *cd = new packet::DeleteObjects(*del);
       assert(cd);
-      cd->setClientID(cid);
-      if ( !cd->send() )
-        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl;
+      cd->setPeerID(cid);
+      if ( !cd->send( static_cast<Host*>(this) ) )
+        COUT(3) << "Server: packet with client id (cid): " << cid << " not sended" << std::endl;
       temp=temp->next();
       // gs gets automatically deleted by enet callback
@@ -312,4 +338,5 @@
     // inform all the listeners
     ClientConnectionListener::broadcastClientConnected(newid);
+    GamestateManager::addPeer(newid);
 
     ++newid;
@@ -327,4 +354,5 @@
     else
     {
+      GamestateManager::removePeer(client->getID());
       //ServerConnection::disconnectClient( client );
       //ClientConnectionListener::broadcastClientDisconnected( client->getID() ); //this is done in ClientInformation now
@@ -332,4 +360,18 @@
     }
   }
+  
+  void Server::processPacket(packet::Packet* packet)
+  {
+    if( packet->isReliable() )
+    {
+      if( this->getLastProcessedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() )
+        packet->process(static_cast<Host*>(this));
+      else
+        this->packetQueue_.push_back(packet);
+    }
+    else
+      packet->process(static_cast<Host*>(this));
+  }
+
 
   bool Server::createClient(int clientID)
@@ -341,5 +383,5 @@
       return false;
     }
-    COUT(5) << "Con.Man: creating client id: " << temp->getID() << std::endl;
+    COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl;
 
     // synchronise class ids
@@ -348,22 +390,25 @@
     // now synchronise functionIDs
     packet::FunctionIDs *fIDs = new packet::FunctionIDs();
-    fIDs->setClientID(clientID);
-    bool b = fIDs->send();
+    fIDs->setPeerID(clientID);
+    bool b = fIDs->send( static_cast<Host*>(this) );
     assert(b);
 
     temp->setSynched(true);
+    GamestateManager::setSynched(clientID);
+    
     COUT(4) << "sending welcome" << std::endl;
     packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID());
-    w->setClientID(temp->getID());
-    b = w->send();
+    w->setPeerID(temp->getID());
+    b = w->send( static_cast<Host*>(this) );
     assert(b);
     packet::Gamestate *g = new packet::Gamestate();
-    g->setClientID(temp->getID());
-    b = g->collectData(0,0x1);
+    g->setPeerID(temp->getID());
+    b = g->collectData(0,packet::GAMESTATE_MODE_SERVER);
+    assert(b);
     if(!b)
       return false; //no data for the client
-    b = g->compressData();
-    assert(b);
-    b = g->send();
+//     b = g->compressData();
+//     assert(b);
+    b = g->send( static_cast<Host*>(this) );
     assert(b);
     return true;
@@ -373,5 +418,5 @@
   {
     ServerConnection::disconnectClient( client );
-    GamestateManager::removeClient(client);
+    GamestateManager::removePeer(client->getID());
     // inform all the listeners
     // ClientConnectionListener::broadcastClientDisconnected(client->getID()); // this is done in ClientInformation now
@@ -395,6 +440,6 @@
     {
       chat = new packet::Chat(message, clientID);
-      chat->setClientID(temp->getID());
-      if(!chat->send())
+      chat->setPeerID(temp->getID());
+      if(!chat->send( static_cast<Host*>(this) ))
         COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl;
       temp = temp->next();
@@ -411,6 +456,6 @@
     int failures=0;
     packet::ClassID *classid = new packet::ClassID();
-    classid->setClientID(clientID);
-    while(!classid->send() && failures < 10){
+    classid->setPeerID(clientID);
+    while(!classid->send( static_cast<Host*>(this) ) && failures < 10){
       failures++;
     }
