Index: code/trunk/src/libraries/network/FunctionCallManager.cc
===================================================================
--- code/trunk/src/libraries/network/FunctionCallManager.cc	(revision 7495)
+++ code/trunk/src/libraries/network/FunctionCallManager.cc	(revision 7801)
@@ -30,65 +30,66 @@
 #include "packet/FunctionCalls.h"
 #include "core/GameMode.h"
+#include "GamestateHandler.h"
 
 namespace orxonox {
 
-std::map<uint32_t, packet::FunctionCalls*> FunctionCallManager::sClientMap_;
-std::vector<FunctionCall> FunctionCallManager::sIncomingFunctionCallBuffer_;
+std::map<uint32_t, packet::FunctionCalls*> FunctionCallManager::sPeerMap_;
+std::vector<std::pair<FunctionCall, std::pair<uint32_t, uint32_t> > > FunctionCallManager::sIncomingFunctionCallBuffer_;
 
 // Static calls
 
-void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID)
+void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager::sClientMap_[clientID]->addCallStatic(functionID);
+  FunctionCallManager::sPeerMap_[peerID]->addCallStatic(functionID);
 }
-void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1)
+void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager:: sClientMap_[clientID]->addCallStatic(functionID, &mt1);
+  FunctionCallManager:: sPeerMap_[peerID]->addCallStatic(functionID, &mt1);
 }
-void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2)
+void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager:: sClientMap_[clientID]->addCallStatic(functionID, &mt1, &mt2);
+  FunctionCallManager:: sPeerMap_[peerID]->addCallStatic(functionID, &mt1, &mt2);
 }
-void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)
+void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager:: sClientMap_[clientID]->addCallStatic(functionID, &mt1, &mt2, &mt3);
+  FunctionCallManager:: sPeerMap_[peerID]->addCallStatic(functionID, &mt1, &mt2, &mt3);
 }
-void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)
+void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager:: sClientMap_[clientID]->addCallStatic(functionID, &mt1, &mt2, &mt3, &mt4);
+  FunctionCallManager:: sPeerMap_[peerID]->addCallStatic(functionID, &mt1, &mt2, &mt3, &mt4);
 }
-void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)
+void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager:: sClientMap_[clientID]->addCallStatic(functionID, &mt1, &mt2, &mt3, &mt4, &mt5);
+  FunctionCallManager:: sPeerMap_[peerID]->addCallStatic(functionID, &mt1, &mt2, &mt3, &mt4, &mt5);
 }
 
@@ -96,87 +97,88 @@
 // MemberCalls
 
-void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID)
+void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager::sClientMap_[clientID]->addCallMember(functionID, objectID);
+  FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID);
 }
-void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1)
+void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager::sClientMap_[clientID]->addCallMember(functionID, objectID, &mt1);
+  FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID, &mt1);
 }
-void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2)
+void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager::sClientMap_[clientID]->addCallMember(functionID, objectID, &mt1, &mt2);
+  FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID, &mt1, &mt2);
 }
-void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)
+void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager::sClientMap_[clientID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3);
+  FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3);
 }
-void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)
+void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager::sClientMap_[clientID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3, &mt4);
+  FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3, &mt4);
 }
-void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)
+void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)
 {
-  if(sClientMap_.find(clientID)==sClientMap_.end())
+  if(sPeerMap_.find(peerID)==sPeerMap_.end())
   {
-    FunctionCallManager::sClientMap_[clientID] = new packet::FunctionCalls;
-    FunctionCallManager::sClientMap_[clientID]->setClientID(clientID);
+    FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls;
+    FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID);
   }
-  FunctionCallManager::sClientMap_[clientID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3, &mt4, &mt5);
+  FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3, &mt4, &mt5);
 }
 
 // Send calls
 
-void FunctionCallManager::sendCalls()
+void FunctionCallManager::sendCalls(orxonox::Host* host)
 {
   std::map<uint32_t, packet::FunctionCalls*>::iterator it;
-  for (it = FunctionCallManager::sClientMap_.begin(); it != FunctionCallManager::sClientMap_.end(); ++it )
+  for (it = FunctionCallManager::sPeerMap_.begin(); it != FunctionCallManager::sPeerMap_.end(); ++it )
   {
-    assert(!FunctionCallManager::sClientMap_.empty());
-    it->second->send();
+    assert(!FunctionCallManager::sPeerMap_.empty());
+    it->second->send(host);
   }
-  FunctionCallManager::sClientMap_.clear();
+  FunctionCallManager::sPeerMap_.clear();
 }
 
-void FunctionCallManager::bufferIncomingFunctionCall(const orxonox::FunctionCall& fctCall)
+void FunctionCallManager::bufferIncomingFunctionCall(const orxonox::FunctionCall& fctCall, uint32_t minGamestateID, uint32_t peerID)
 {
-  if( !GameMode::isMaster() )
-    FunctionCallManager::sIncomingFunctionCallBuffer_.push_back( fctCall );
+  FunctionCallManager::sIncomingFunctionCallBuffer_.push_back( std::make_pair(fctCall, std::make_pair(minGamestateID, peerID)));
 }
 
 void FunctionCallManager::processBufferedFunctionCalls()
 {
-  std::vector<FunctionCall>::iterator it = FunctionCallManager::sIncomingFunctionCallBuffer_.begin();
+  std::vector<std::pair<FunctionCall, std::pair<uint32_t, uint32_t> > >::iterator it = FunctionCallManager::sIncomingFunctionCallBuffer_.begin();
   while( it!=FunctionCallManager::sIncomingFunctionCallBuffer_.end() )
   {
-    if( it->execute() )
+    if( it->first.execute() )
       FunctionCallManager::sIncomingFunctionCallBuffer_.erase(it);
     else
+    {
       ++it;
+    }
   }
 }
