Index: code/branches/netp6/src/network/GamestateManager.cc
===================================================================
--- code/branches/netp6/src/network/GamestateManager.cc	(revision 3227)
+++ code/branches/netp6/src/network/GamestateManager.cc	(revision 3240)
@@ -42,6 +42,10 @@
 
 #include <cassert>
+#include <queue>
+#include <boost/thread/mutex.hpp>
 
 #include "util/Debug.h"
+#include "core/Executor.h"
+#include "core/ThreadPool.h"
 #include "ClientInformation.h"
 #include "packet/Acknowledgement.h"
@@ -56,4 +60,6 @@
   {
     trafficControl_ = new TrafficControl();
+    threadMutex_ = new boost::mutex();
+    threadPool_ = new ThreadPool();
   }
 
@@ -71,5 +77,7 @@
         delete (*it2).second;
     }
-    delete trafficControl_;
+    delete this->trafficControl_;
+    delete this->threadMutex_;
+    delete this->threadPool_;
   }
 
@@ -116,30 +124,80 @@
     return true;
   }
-
-
-  packet::Gamestate *GamestateManager::popGameState(unsigned int clientID) {
+  
+  void GamestateManager::sendGamestates()
+  {
+    ClientInformation *temp = ClientInformation::getBegin();
+    std::queue<packet::Gamestate*> clientGamestates;
+    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;
+      unsigned int gID = temp->getGamestateID();
+      if(!reference)
+        return;
+      
+      packet::Gamestate *client=0;
+      if(gID != GAMESTATEID_INITIAL){
+        assert(gamestateMap_.find(cid)!=gamestateMap_.end());
+        std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[cid].find(gID);
+        if(it!=gamestateMap_[cid].end())
+        {
+          client = it->second;
+        }
+      }
+      
+      clientGamestates.push(0);
+//       finishGamestate( cid, clientGamestates.back(), client, reference );
+      //FunctorMember<GamestateManager>* functor = 
+      ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate) );
+      executor->setObject(this);
+      executor->setDefaultValues( cid, &clientGamestates.back(), client, reference );
+//       (*static_cast<Executor*>(executor))();
+      this->threadPool_->passFunction( executor, true );
+//       (*functor)( cid, &(clientGamestates.back()), client, reference );
+      
+      temp = temp->next();
+    }
+    
+    threadPool_->synchronise();
+    
+    while( !clientGamestates.empty() )
+    {
+      if(clientGamestates.front())
+        clientGamestates.front()->send();
+      clientGamestates.pop();
+    }
+  }
+
+
+  void GamestateManager::finishGamestate( unsigned int clientID, packet::Gamestate** destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
     //why are we searching the same client's gamestate id as we searched in
     //Server::sendGameState?
-    packet::Gamestate *gs;
-    unsigned int gID = ClientInformation::findClient(clientID)->getGamestateID();
-    if(!reference)
-      return 0;
-    gs = reference->doSelection(clientID, 10000);
     // save the (undiffed) gamestate in the clients gamestate map
-    gamestateMap_[clientID][gs->getID()]=gs;
     //chose wheather the next gamestate is the first or not
-    packet::Gamestate *client=0;
-    if(gID != GAMESTATEID_INITIAL){
-      assert(gamestateMap_.find(clientID)!=gamestateMap_.end());
-      std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[clientID].find(gID);
-      if(it!=gamestateMap_[clientID].end())
-      {
-        client = it->second;
-      }
-    }
-    if(client){
+    
+    packet::Gamestate *gs = gamestate->doSelection(clientID, 20000);
+//     packet::Gamestate *gs = new packet::Gamestate(*gamestate);
+//     packet::Gamestate *gs = new packet::Gamestate();
+//     gs->collectData( id_, 0x1 );
+    this->threadMutex_->lock();
+    gamestateMap_[clientID][gamestate->getID()]=gs;
+    this->threadMutex_->unlock();
+    
+    if(base)
+    {
+        
 //       COUT(3) << "diffing" << std::endl;
 //       packet::Gamestate* gs1  = gs;
-      packet::Gamestate *diffed = gs->diff(client);
+      packet::Gamestate *diffed = gs->diff(base);
       //packet::Gamestate *gs2 = diffed->undiff(gs);
 //       assert(*gs == *gs2);
@@ -150,15 +208,17 @@
     }
     else{
-//       COUT(3) << "not diffing" << std::endl;
       gs = new packet::Gamestate(*gs);
     }
+    
+    
     bool b = gs->compressData();
     assert(b);
-    COUT(4) << "sending gamestate with id " << gs->getID();
-    if(gs->isDiffed())
-    COUT(4) << " and baseid " << gs->getBaseID() << endl;
-    else
-    COUT(4) << endl;
-    return gs;
+//     COUT(4) << "sending gamestate with id " << gs->getID();
+//     if(gamestate->isDiffed())
+//     COUT(4) << " and baseid " << gs->getBaseID() << endl;
+//     else
+//     COUT(4) << endl;
+    gs->setClientID(clientID);
+    *destgamestate = gs;
   }
 
