Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/synchronizeable.cc @ 9373

Last change on this file since 9373 was 9371, checked in by bensch, 18 years ago

orxonox/trunk: ORXONOX is now completely std::stringed

File size: 15.5 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11
12### File Specific:
13   main-programmer: Silvan Nellen
14   co-programmer: Benjamin Wuest
15*/
16
17#define DEBUG_MODULE_NETWORK
18
19#include "shared_network_data.h"
20#include "network_stream.h"
21#include "netdefs.h"
22#include "network_log.h"
23#include "network_game_manager.h"
24
25#include "state.h"
26
27#include <cassert>
28
29#include "synchronizeable.h"
30
31
32
33/**
34 *  default constructor
35 */
36Synchronizeable::Synchronizeable()
37{
38  this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable");
39  this->owner = 0;
40//   this->setIsServer(SharedNetworkData::getInstance()->getHostID() == 0);
41  this->uniqueID = NET_UID_UNASSIGNED;
42  this->networkStream = NULL;
43  this->bSynchronize = false;
44
45  if( State::isOnline())
46  {
47    NetworkStream* nd = SharedNetworkData::getInstance()->getDefaultSyncStream();
48    assert(nd != NULL);
49    nd->connectSynchronizeable(*this);
50    this->setUniqueID(SharedNetworkData::getInstance()->getNewUniqueID());
51  }
52
53  /* make sure loadClassId is first synced var because this is read by networkStream */
54  assert( syncVarList.size() == 0 );
55  mLeafClassId = this->registerVarId( new SynchronizeableInt( (int*)&this->getLeafClassID(), (int*)&this->getLeafClassID(), "leafClassId" ) );
56
57  this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner" ) );
58  this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) );
59}
60
61
62
63/**
64 *  default destructor deletes all unneded stuff
65 */
66Synchronizeable::~Synchronizeable()
67{
68  if ( this->networkStream )
69  {
70    this->networkStream->disconnectSynchronizeable(*this);
71
72    if ( SharedNetworkData::getInstance()->isMasterServer() && this->beSynchronized() && this->getUniqueID() > 0 && !this->isA( CL_MESSAGE_MANAGER ) )
73      NetworkGameManager::getInstance()->removeSynchronizeable( this->getUniqueID() );
74  }
75
76  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
77  {
78    delete *it;
79  }
80  syncVarList.clear();
81
82  for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ )
83  {
84    for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ )
85    {
86      if ( (*it2)->data )
87      {
88        delete [] (*it2)->data;
89        (*it2)->data = NULL;
90      }
91      delete *it2;
92    }
93
94  }
95
96  for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ )
97  {
98    for ( StateHistory::iterator it2 = it->begin(); it2 != it->end(); it2++ )
99    {
100      if ( (*it2)->data )
101      {
102        delete [] (*it2)->data;
103        (*it2)->data = NULL;
104      }
105      delete *it2;
106    }
107  }
108}
109
110
111
112int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH )
113{
114  //make sure this user has his history
115  if ( sentStates.size() <= userId )
116    sentStates.resize( userId+1 );
117
118  //calculate needed memory
119  int neededSize = 0;
120
121  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
122  {
123    //PRINTF(0)("SIZE = %d %s\n", (*it)->getSize(), (*it)->getName().c_str());
124    neededSize += (*it)->getSize();
125  }
126
127  if ( !( neededSize <= maxLength ) )
128  {
129    PRINTF(0)( "%d > %d\n", neededSize, maxLength );
130    assert(false);
131  }
132
133  //remove older states from history than fromStateId
134  StateHistory::iterator it = sentStates[userId].begin();
135
136  while ( it != sentStates[userId].end() && (*it)->stateId < fromStateId )
137    it++;
138
139  if ( it != sentStates[userId].begin() )
140  {
141    for ( StateHistory::iterator it2 = sentStates[userId].begin(); it2 != it; it2++ )
142    {
143      if ( (*it2)->data != NULL )
144      {
145        delete [] (*it2)->data;
146        (*it2)->data = NULL;
147      }
148
149      delete *it2;
150    }
151    sentStates[userId].erase( sentStates[userId].begin(), it );
152  }
153
154  //find state to create diff from
155  StateHistoryEntry * stateFrom = NULL;
156
157  it = sentStates[userId].begin();
158  while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId )
159    it++;
160
161  if ( it == sentStates[userId].end() )
162  {
163    StateHistoryEntry * initialEntry = new StateHistoryEntry();
164
165    initialEntry->stateId = fromStateId;
166    initialEntry->dataLength = 0;
167    initialEntry->data = NULL;
168
169    stateFrom = initialEntry;
170
171    sentStates[userId].push_back( stateFrom );
172  }
173  else
174    stateFrom = (*it);
175
176  StateHistoryEntry * stateTo = new StateHistoryEntry;
177
178  sentStates[userId].push_back( stateTo );
179
180  stateTo->stateId = stateId;
181  stateTo->dataLength = neededSize;
182  stateTo->data = new byte[ neededSize ];
183
184  std::list<int>::iterator sizeIter = stateFrom->sizeList.begin();
185
186  int i = 0;
187  int n;
188
189  bool hasPermission;
190  bool sizeChanged = false;
191
192  // now do the actual synchronization: kick all variables to write into a common buffer
193  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
194  {
195    hasPermission = (
196        SharedNetworkData::getInstance()->isMasterServer() && (*it)->checkPermission( PERMISSION_MASTER_SERVER ) ||
197        this->owner == SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ) ||
198        SharedNetworkData::getInstance()->isMasterServer() && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER ) ||
199            (*it)->checkPermission( PERMISSION_ALL )
200                    );
201
202    if ( sizeIter == stateFrom->sizeList.end() || *sizeIter != (*it)->getSize() )
203      sizeChanged = true;
204
205    if ( ( hasPermission && (*it)->getPriority() >= priorityTH ) || sizeChanged )
206    {
207      n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i );
208      //NETPRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), n);
209      stateTo->sizeList.push_back( n );
210      //(*it)->debug();
211      i += n;
212    }
213    else
214    {
215      for ( int j = 0; j<(*sizeIter); j++ )
216      {
217        assert( i < stateFrom->dataLength );
218        stateTo->data[i] = stateFrom->data[i];
219        i++;
220      }
221      //NETPRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), *sizeIter);
222      stateTo->sizeList.push_back( (*sizeIter) );
223    }
224
225    if ( sizeIter != stateFrom->sizeList.end() )
226      sizeIter++;
227  }
228
229  if ( i != neededSize )
230  {
231    PRINTF(0)("strange error: (%s) %d != %d\n", this->getClassCName(), i, neededSize);
232    assert(false);
233  }
234
235  //write diff to data
236  for ( i = 0; i<neededSize; i++ )
237  {
238    if ( i < stateFrom->dataLength )
239      data[i] = stateTo->data[i] - stateFrom->data[i];
240    else
241      data[i] = stateTo->data[i];
242  }
243
244  return neededSize;
245}
246
247/**
248 * sets a new state out of a diff created on another host
249 * @param userId hostId of user who send me that diff
250 * @param data pointer to diff
251 * @param length length of diff
252 * @param stateId id of current state
253 * @param fromStateId id of the base state id
254 * @return number bytes read
255 * @todo check for permissions
256 */
257int Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId )
258{
259  //make sure this user has his history
260  if ( recvStates.size() <= userId )
261    recvStates.resize( userId+1 );
262
263  //create new state
264  StateHistoryEntry * stateTo = new StateHistoryEntry();
265  stateTo->stateId = stateId;
266  stateTo->dataLength = length;
267  stateTo->data = new byte[ length ];
268
269
270  //find state to apply diff to
271  StateHistoryEntry * stateFrom = NULL;
272
273  StateHistory::iterator it = recvStates[userId].begin();
274  while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId )
275    it++;
276
277
278//  if ( getLeafClassID() == CL_SPACE_SHIP )
279//  {
280//    PRINTF(0)("setStateDiff:SpaceShip from: %d stateId: %d\n", (it == recvStates[userId].end())?-1:fromStateId, stateId);
281//  }
282
283  if ( it == recvStates[userId].end() )
284  {
285    StateHistoryEntry * initialEntry = new StateHistoryEntry();
286
287    initialEntry->stateId = fromStateId;
288    initialEntry->dataLength = 0;
289    initialEntry->data = NULL;
290
291    stateFrom = initialEntry;
292
293    recvStates[userId].push_back( stateFrom );
294  }
295  else
296    stateFrom = (*it);
297
298  //apply diff
299  for ( int i = 0; i<length; i++ )
300  {
301    if ( i < stateFrom->dataLength )
302      stateTo->data[i] = stateFrom->data[i] + data[i];
303    else
304      stateTo->data[i] = data[i];
305
306  }
307
308  //add state to state history
309  recvStates[userId].push_back( stateTo );
310
311  int i = 0;
312  int n = 0;
313  std::list<int> changes;
314
315  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
316  {
317    if (
318        (*it)->checkPermission( PERMISSION_MASTER_SERVER ) && networkStream->isUserMasterServer( userId ) ||
319        (*it)->checkPermission( PERMISSION_OWNER ) && this->owner == userId ||
320        networkStream->isUserMasterServer( userId ) && this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ) ||
321        (*it)->checkPermission( PERMISSION_ALL )
322       )
323    {
324      n = (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i );
325      i += n;
326      //NETPRINTF(0)("%s::setvar %s %d\n", getClassName(), (*it)->getName().c_str(), n);
327      //(*it)->debug();
328      if ( (*it)->getHasChanged() )
329      {
330        changes.push_back( (*it)->getVarId() );
331      }
332    }
333    else
334    {
335//      PRINTF(0)("DONT SET VAR BECAUSE OF PERMISSION: %s %d %d %d %d %d %d\n", (*it)->getName().c_str(), (*it)->checkPermission( PERMISSION_MASTER_SERVER ), (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_ALL ), networkStream->isUserServer( userId ), this->owner, userId );
336      n = (*it)->getSizeFromBuf( stateTo->data + i, stateTo->dataLength - i );
337      //NETPRINTF(0)("%s::setvar %s %d\n", getClassName(), (*it)->getName().c_str(), n);
338      //(*it)->debug();
339      i += n;
340    }
341  }
342
343  this->varChangeHandler( changes );
344
345  return i;
346}
347
348 /**
349 * override this function to be notified on change
350 * of your registred variables.
351 * @param id id's which have changed
352 */
353void Synchronizeable::varChangeHandler( std::list<int> & id )
354{
355}
356
357/**
358 * registers a varable to be synchronized over network
359 * @param var see src/lib/network/synchronizeable_var/ for available classes
360 */
361void Synchronizeable::registerVar( SynchronizeableVar * var )
362{
363  //PRINTF(0)("ADDING VAR: %s\n", var->getName().c_str());
364  syncVarList.push_back( var );
365}
366
367/**
368 * registers a varable to be synchronized over network
369 * return value is passed to varChangeHandler on change
370 * @param var see src/lib/network/synchronizeable_var/ for available classes
371 * @return handle passed to varChangeHandler on changes
372 */
373int Synchronizeable::registerVarId( SynchronizeableVar * var )
374{
375  //PRINTF(0)("ADDING VAR: %s\n", var->getName().c_str());
376  syncVarList.push_back( var );
377  var->setWatched( true );
378  var->setVarId( syncVarList.size()-1 );
379  return syncVarList.size()-1;
380}
381
382/**
383 * removed user's states from memory
384 * @param userId user to clean
385 */
386void Synchronizeable::cleanUpUser( int userId )
387{
388  if ( recvStates.size() > userId )
389  {
390    for ( std::list<StateHistoryEntry*>::iterator it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ )
391    {
392      if ( (*it)->data )
393      {
394        delete [] (*it)->data;
395        (*it)->data = NULL;
396      }
397
398      delete *it;
399    }
400    recvStates[userId].clear();
401  }
402
403  if ( sentStates.size() > userId )
404  {
405
406    for ( std::list<StateHistoryEntry*>::iterator it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ )
407    {
408      if ( (*it)->data )
409      {
410        delete [] (*it)->data;
411        (*it)->data = NULL;
412      }
413
414      delete *it;
415    }
416    sentStates[userId].clear();
417  }
418}
419
420/**
421 * this function is called after recieving a state.
422 * @param userId
423 * @param stateId
424 * @param fromStateId
425 */
426void Synchronizeable::handleRecvState( int userId, int stateId, int fromStateId )
427{
428   //make sure this user has his history
429  if ( recvStates.size() <= userId )
430    recvStates.resize( userId+1 );
431
432  //remove old states
433  StateHistory::iterator it = recvStates[userId].begin();
434
435#if 0
436  while ( it != recvStates[userId].end() && (*it)->stateId < fromStateId )
437    it++;
438
439  if ( it != recvStates[userId].begin() )
440  {
441    for ( StateHistory::iterator it2 = recvStates[userId].begin(); it2 != it; it2++ )
442    {
443      if ( (*it2)->data != NULL )
444      {
445        delete [] (*it2)->data;
446        (*it2)->data = NULL;
447      }
448    }
449    recvStates[userId].erase( recvStates[userId].begin(), it );
450  }
451#endif
452
453  for ( it = recvStates[userId].begin(); it != recvStates[userId].end();  )
454  {
455    if ( (*it)->stateId < fromStateId )
456    {
457      StateHistory::iterator delIt = it;
458      it ++;
459
460      if ( (*delIt)->data )
461      {
462        delete [] (*delIt)->data;
463        (*delIt)->data = NULL;
464      }
465      delete *delIt;
466      recvStates[userId].erase( delIt );
467
468      continue;
469    }
470    it++;
471  }
472
473  StateHistory::iterator fromState = recvStates[userId].end();
474  StateHistory::iterator toState = recvStates[userId].end();
475
476  for ( it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ )
477  {
478    if ( (*it)->stateId == stateId )
479      toState = it;
480    if ( (*it)->stateId == fromStateId )
481      fromState = it;
482
483    if ( fromState != recvStates[userId].end() && toState != recvStates[userId].end() )
484      break;
485  }
486
487  // setStateDiff was not called and i know fromStateId
488  if ( fromState != recvStates[userId].end() && toState == recvStates[userId].end() )
489  {
490    StateHistoryEntry * entry = new StateHistoryEntry;
491
492    entry->dataLength = (*fromState)->dataLength;
493    if ( entry->dataLength > 0 )
494    {
495      entry->data = new byte[entry->dataLength];
496
497      assert( (*fromState)->data );
498      memcpy( entry->data, (*fromState)->data, entry->dataLength );
499    }
500    else
501      entry->data = NULL;
502
503    entry->sizeList = (*fromState)->sizeList;
504    entry->stateId = stateId;
505
506    recvStates[userId].push_back(entry);
507  }
508}
509
510/**
511 * this function is called after sending a state
512 * @param userId
513 * @param stateId
514 * @param fromStateId
515 */
516void Synchronizeable::handleSentState( int userId, int stateId, int fromStateId )
517{
518   //make sure this user has his history
519  if ( sentStates.size() <= userId )
520    sentStates.resize( userId+1 );
521
522   //remove old states
523  StateHistory::iterator it = sentStates[userId].begin();
524
525  for ( it = sentStates[userId].begin(); it != sentStates[userId].end();  )
526  {
527    if ( (*it)->stateId < fromStateId )
528    {
529      StateHistory::iterator delIt = it;
530      it ++;
531
532      if ( (*delIt)->data )
533      {
534        delete [] (*delIt)->data;
535        (*delIt)->data = NULL;
536      }
537      delete *delIt;
538      sentStates[userId].erase( delIt );
539
540      continue;
541    }
542    it++;
543  }
544
545
546  StateHistory::iterator fromState = sentStates[userId].end();
547  StateHistory::iterator toState = sentStates[userId].end();
548
549  for ( it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ )
550  {
551    if ( (*it)->stateId == stateId )
552      toState = it;
553    if ( (*it)->stateId == fromStateId )
554      fromState = it;
555
556    if ( fromState != sentStates[userId].end() && toState != sentStates[userId].end() )
557      break;
558  }
559
560
561  // getStateDiff was not called and i know fromStateId
562  if ( fromState != sentStates[userId].end() && toState == sentStates[userId].end() )
563  {
564    StateHistoryEntry * entry = new StateHistoryEntry;
565
566    entry->dataLength = (*fromState)->dataLength;
567    if ( entry->dataLength > 0 )
568    {
569      entry->data = new byte[entry->dataLength];
570
571      assert( (*fromState)->data );
572      memcpy( entry->data, (*fromState)->data, entry->dataLength );
573    }
574    else
575      entry->data = NULL;
576
577    entry->sizeList = (*fromState)->sizeList;
578    entry->stateId = stateId;
579
580    sentStates[userId].push_back(entry);
581  }
582
583}
584
585
586
Note: See TracBrowser for help on using the repository browser.