Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel/src/libraries/network/GamestateManager.cc @ 8748

Last change on this file since 8748 was 7801, checked in by dafrick, 14 years ago

Merging presentation2 branch back to trunk.

  • Property svn:eol-style set to native
File size: 11.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Oliver Scheuss
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29//
30// C++ Implementation: GameStateManager
31//
32// Description:
33//
34//
35// Author:  Oliver Scheuss, (C) 2007
36//
37// Copyright: See COPYING file that comes with this distribution
38//
39//
40
41#include "GamestateManager.h"
42
43#include <cassert>
44#include <queue>
45// #include <boost/thread/mutex.hpp>
46
47#include "packet/Acknowledgement.h"
48#include "packet/Gamestate.h"
49#include "synchronisable/NetworkCallbackManager.h"
50
51#include "core/ThreadPool.h"
52#include "core/command/Executor.h"
53#include "core/GameMode.h"
54#include "util/Debug.h"
55#include "util/Clock.h"
56// #include "TrafficControl.h"
57
58namespace orxonox
59{
60  GamestateManager::GamestateManager() :
61  currentGamestate_(0), id_(0)
62  {
63//     trafficControl_ = new TrafficControl();
64//     threadMutex_ = new boost::mutex();
65//     threadPool_ = new ThreadPool();
66  }
67
68  GamestateManager::~GamestateManager()
69  {
70    if( this->currentGamestate_ )
71        delete this->currentGamestate_;std::map<unsigned int, packet::Gamestate*>::iterator it;
72    for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it )
73      delete it->second;
74    std::map<uint32_t, peerInfo>::iterator peerIt;
75    std::map<uint32_t, packet::Gamestate*>::iterator gamestateIt;
76    for( peerIt = peerMap_.begin(); peerIt != peerMap_.end(); ++peerIt )
77    {
78      for( gamestateIt = peerIt->second.gamestates.begin(); gamestateIt != peerIt->second.gamestates.end(); ++gamestateIt )
79        delete gamestateIt->second;
80    }
81//     this->trafficControl_->destroy();
82//     delete this->threadMutex_;
83//     delete this->threadPool_;
84  }
85
86  bool GamestateManager::update(){
87//     cleanup();
88    return getSnapshot();
89  }
90
91  bool GamestateManager::addGamestate(packet::Gamestate *gs, unsigned int clientID)
92  {
93    assert(gs);
94    std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID);
95    if(it!=gamestateQueue.end()){
96      // delete obsolete gamestate
97      delete it->second;
98    }
99    gamestateQueue[clientID] = gs;
100    return true;
101  }
102
103  bool GamestateManager::processGamestates()
104  {
105    if( this->gamestateQueue.empty() )
106        return true;
107    std::map<unsigned int, packet::Gamestate*>::iterator it;
108    // now push only the most recent gamestates we received (ignore obsolete ones)
109    for(it = gamestateQueue.begin(); it!=gamestateQueue.end(); it++){
110      bool b = processGamestate(it->second);
111      assert(b);
112      sendAck( it->second->getID(), it->second->getPeerID() );
113      delete it->second;
114    }
115    // now clear the queue
116    gamestateQueue.clear();
117    //and call all queued callbacks
118    NetworkCallbackManager::callCallbacks();
119    return true;
120  }
121 
122  bool GamestateManager::sendAck(unsigned int gamestateID, uint32_t peerID)
123  {
124    packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, peerID);
125    if( !this->sendPacket(ack))
126    {
127      COUT(3) << "could not ack gamestate: " << gamestateID << std::endl;
128      return false;
129    }
130    else
131    {
132      COUT(5) << "acked a gamestate: " << gamestateID << std::endl;
133      return true;
134    }
135  }
136
137
138  bool GamestateManager::getSnapshot(){
139    if ( currentGamestate_ != 0 )
140      delete currentGamestate_;
141    currentGamestate_ = new packet::Gamestate();
142    uint8_t gsMode;
143    if( GameMode::isMaster() )
144      gsMode = packet::GAMESTATE_MODE_SERVER;
145    else
146      gsMode = packet::GAMESTATE_MODE_CLIENT;
147    uint32_t newID;
148    if( GameMode::isMaster() )
149      newID = ++id_;
150    else
151      newID = peerMap_[NETWORK_PEER_ID_SERVER].lastProcessedGamestateID;
152   
153    if(!currentGamestate_->collectData(newID, gsMode)){ //we have no data to send
154      delete currentGamestate_;
155      currentGamestate_=0;
156    }
157    return true;
158  }
159
160  std::vector<packet::Gamestate*> GamestateManager::getGamestates()
161  {
162    if(!currentGamestate_)
163      return std::vector<packet::Gamestate*>();
164    std::vector<packet::Gamestate*> peerGamestates;
165   
166    std::map<uint32_t, peerInfo>::iterator peerIt;
167    for( peerIt=peerMap_.begin(); peerIt!=peerMap_.end(); ++peerIt )
168    {
169      if( !peerIt->second.isSynched )
170      {
171        COUT(5) << "Server: not sending gamestate" << std::endl;
172        continue;
173      }
174      COUT(4) << "client id: " << peerIt->first << std::endl;
175      COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl;
176      int peerID = peerIt->first; //get client id
177
178      unsigned int lastAckedGamestateID = peerIt->second.lastAckedGamestateID;
179
180      packet::Gamestate* baseGamestate=0;
181      if(lastAckedGamestateID != GAMESTATEID_INITIAL)
182      {
183        assert(peerMap_.find(peerID)!=peerMap_.end());
184        std::map<uint32_t, packet::Gamestate*>::iterator it = peerMap_[peerID].gamestates.find(lastAckedGamestateID);
185        assert(it!=peerMap_[peerID].gamestates.end());
186        baseGamestate = it->second;
187      }
188
189      peerGamestates.push_back(0);  // insert an empty gamestate* to change
190      finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ );
191      if( peerGamestates.back()==0 )
192        // nothing to send to remove pointer from vector
193        peerGamestates.pop_back();
194      //FunctorMember<GamestateManager>* functor =
195//       ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) );
196//       executor->setDefaultValues( cid, &clientGamestates.back(), client, currentGamestate_ );
197//       (*static_cast<Executor*>(executor))();
198//       this->threadPool_->passFunction( executor, true );
199//       (*functor)( cid, &(clientGamestates.back()), client, currentGamestate_ );
200    }
201
202//     threadPool_->synchronise();
203
204    return peerGamestates;
205  }
206
207
208  void GamestateManager::finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) {
209    //why are we searching the same client's gamestate id as we searched in
210    //Server::sendGameState?
211    // save the (undiffed) gamestate in the clients gamestate map
212    //chose wheather the next gamestate is the first or not
213
214//     packet::Gamestate *gs = gamestate->doSelection(clientID, 20000);
215//       packet::Gamestate* gs = new packet::Gamestate(*gamestate);
216//     packet::Gamestate* gs = gamestate;
217    packet::Gamestate *gs = new packet::Gamestate(*gamestate); //TODO: is this neccessary ?
218//     packet::Gamestate *gs = new packet::Gamestate();
219//     gs->collectData( id_, 0x1 );
220//     this->threadMutex_->lock();
221    peerMap_[peerID].gamestates[gamestate->getID()]=gs;
222//     this->threadMutex_->unlock();
223    Clock clock;
224    clock.capture();
225
226    if(base)
227    {
228      packet::Gamestate *diffed1 = gs->diffVariables(base);
229      if( diffed1->getDataSize() == 0 )
230      {
231        delete diffed1;
232        destgamestate = 0;
233        return;
234      }
235      gs = diffed1;
236    }
237    else
238    {
239      gs = new packet::Gamestate(*gs);
240    }
241
242
243//     bool b = gs->compressData();
244//     assert(b);
245    clock.capture();
246    COUT(4) << "diff and compress time: " << clock.getDeltaTime() << endl;
247//     COUT(5) << "sending gamestate with id " << gs->getID();
248//     if(gamestate->isDiffed())
249//       COUT(5) << " and baseid " << gs->getBaseID() << endl;
250//     else
251//       COUT(5) << endl;
252    gs->setPeerID(peerID);
253    destgamestate = gs;
254  }
255
256
257  bool GamestateManager::ackGamestate(unsigned int gamestateID, unsigned int peerID)
258  {
259//     ClientInformation *temp = ClientInformation::findClient(peerID);
260//     assert(temp);
261    std::map<uint32_t, peerInfo>::iterator it = this->peerMap_.find(peerID);
262    assert(it!=this->peerMap_.end());
263    unsigned int curid = it->second.lastAckedGamestateID;
264
265    if(gamestateID == ACKID_NACK){
266      it->second.lastAckedGamestateID = GAMESTATEID_INITIAL;
267//       temp->setGamestateID(GAMESTATEID_INITIAL);
268      // now delete all saved gamestates for this client
269      std::map<uint32_t, packet::Gamestate*>::iterator it2;
270      for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){
271        delete it2->second;
272      }
273      it->second.gamestates.clear();
274      return true;
275    }
276
277    assert(curid==GAMESTATEID_INITIAL || curid<=gamestateID);
278    COUT(5) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << std::endl;
279    std::map<uint32_t, packet::Gamestate*>::iterator it2;
280    for( it2=it->second.gamestates.begin(); it2!=it->second.gamestates.end(); )
281    {
282      if( it2->second->getID() < gamestateID )
283      {
284        delete it2->second;
285        it->second.gamestates.erase(it2++);
286      }
287      else
288        ++it2;
289    }
290   
291//     std::map<unsigned int, packet::Gamestate*>::iterator it;
292//     for(it = gamestateMap_[peerID].begin(); it!=gamestateMap_[peerID].end() && it->first<gamestateID; ){
293//       delete it->second;
294//       gamestateMap_[peerID].erase(it++);
295//     }
296    it->second.lastAckedGamestateID = gamestateID;
297//     temp->setGamestateID(gamestateID);
298//     TrafficControl::processAck(peerID, gamestateID);
299    return true;
300  }
301 
302  uint32_t GamestateManager::getLastProcessedGamestateID(unsigned int peerID)
303  {
304    assert( this->peerMap_.find(peerID)!=this->peerMap_.end() );
305    if( this->peerMap_.find(peerID) != this->peerMap_.end() )
306      return this->peerMap_[peerID].lastProcessedGamestateID;
307    else
308      return GAMESTATEID_INITIAL;
309  }
310 
311 
312  void GamestateManager::addPeer(uint32_t peerID)
313  {
314    assert(peerMap_.find(peerID)==peerMap_.end());
315    peerMap_[peerID].peerID = peerID;
316    peerMap_[peerID].lastProcessedGamestateID = GAMESTATEID_INITIAL;
317    peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL;
318    if( GameMode::isMaster() )
319      peerMap_[peerID].isSynched = false;
320    else
321      peerMap_[peerID].isSynched = true;
322  }
323
324  void GamestateManager::removePeer(uint32_t peerID)
325  {
326    assert(peerMap_.find(peerID)!=peerMap_.end());
327    std::map<uint32_t, packet::Gamestate*>::iterator peerIt;
328    for( peerIt = peerMap_[peerID].gamestates.begin(); peerIt!=peerMap_[peerID].gamestates.end(); ++peerIt )
329    {
330      delete peerIt->second;
331    }
332    peerMap_.erase(peerMap_.find(peerID));
333  }
334
335
336//   void GamestateManager::removeClient(ClientInformation* client){
337//     assert(client);
338//     std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID());
339//     // first delete all remained gamestates
340//     std::map<unsigned int, packet::Gamestate*>::iterator it;
341//     for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++)
342//       delete it->second;
343//     // now delete the clients gamestatemap
344//     gamestateMap_.erase(clientMap);
345//   }
346
347  bool GamestateManager::processGamestate(packet::Gamestate *gs)
348  {
349    if(gs->isCompressed())
350    {
351       bool b = gs->decompressData();
352       assert(b);
353    }
354    assert(!gs->isDiffed());
355    uint8_t gsMode;
356    if( GameMode::isMaster() )
357      gsMode = packet::GAMESTATE_MODE_SERVER;
358    else
359      gsMode = packet::GAMESTATE_MODE_CLIENT;
360    if( gs->spreadData(gsMode) )
361    {
362      this->peerMap_[gs->getPeerID()].lastProcessedGamestateID = gs->getID();
363      return true;
364    }
365    else
366      return false;
367  }
368
369}
Note: See TracBrowser for help on using the repository browser.