Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9406 in orxonox.OLD for trunk/src/lib/network/synchronizeable.cc


Ignore:
Timestamp:
Jul 24, 2006, 11:09:47 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/network/synchronizeable.cc

    r9110 r9406  
    1111
    1212### File Specific:
    13    main-programmer: Silvan Nellen
    14    co-programmer: Benjamin Wuest
     13   main-programmer: Christoph Renner (rennerc@ee.ethz.ch)
     14   co-programmer: Patrick Boenzli (patrick@orxonox.ethz.ch)
    1515*/
    1616
     
    2929#include "synchronizeable.h"
    3030
     31#include "converter.h"
     32
    3133
    3234
     
    3840  this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable");
    3941  this->owner = 0;
    40   this->setIsServer(SharedNetworkData::getInstance()->getHostID() == 0);
     42//   this->setIsServer(SharedNetworkData::getInstance()->getHostID() == 0);
    4143  this->uniqueID = NET_UID_UNASSIGNED;
    4244  this->networkStream = NULL;
    4345  this->bSynchronize = false;
    44  
     46
    4547  if( State::isOnline())
    4648  {
     
    5456  assert( syncVarList.size() == 0 );
    5557  mLeafClassId = this->registerVarId( new SynchronizeableInt( (int*)&this->getLeafClassID(), (int*)&this->getLeafClassID(), "leafClassId" ) );
    56    
     58
    5759  this->registerVar( new SynchronizeableInt( &this->owner, &this->owner, "owner" ) );
    5860  this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) );
     
    6971  {
    7072    this->networkStream->disconnectSynchronizeable(*this);
    71  
    72     if ( this->isServer() && this->beSynchronized() && this->getUniqueID() > 0 && !this->isA( CL_MESSAGE_MANAGER ) )
     73
     74    if ( (SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer() )
     75           && this->beSynchronized() && this->getUniqueID() > 0 && !this->isA( CL_MESSAGE_MANAGER ) )
    7376      NetworkGameManager::getInstance()->removeSynchronizeable( this->getUniqueID() );
    7477  }
    75    
     78
    7679  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
    7780  {
     
    7982  }
    8083  syncVarList.clear();
    81  
     84
    8285  for ( UserStateHistory::iterator it = recvStates.begin(); it != recvStates.end(); it++ )
    8386  {
     
    9396
    9497  }
    95  
     98
    9699  for ( UserStateHistory::iterator it = sentStates.begin(); it != sentStates.end(); it++ )
    97100  {
     
    105108      delete *it2;
    106109    }
    107 
    108   }
    109 }
    110 
    111 /**
    112  * Sets the server flag to a given value
    113  * @param isServer: the boolean value which the server flag is to set to
    114  */
    115 void Synchronizeable::setIsServer(bool isServer)
    116 {
    117   if( isServer )
    118     this->state = this->state | STATE_SERVER;
    119   else
    120     this->state = this->state & (~STATE_SERVER);
    121 }
    122 
    123 
    124 /**
    125  * Determines if the server flag is set
    126  * @return true, if the server flag is true, false else
    127  */
    128 bool Synchronizeable::isServer()
    129 {
    130   return (this->state & STATE_SERVER) >0;
    131 }
    132 
    133 
    134 
     110  }
     111}
     112
     113
     114
     115/**
     116 * creates a diff image from two states
     117 * @param userId: the userid of the user where the image will be sent to
     118 * @param data: the binary data array to write to
     119 * @param maxLength: maximal length of the data written (length of available space in the array)
     120 * @param stateId: the state id that this diff will represent
     121 * @param priorityTH: the priority threshold: all syncs below this threshold won't be synchronized
     122 *
     123 * @todo check for permissions
     124 */
    135125int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH )
    136126{
     
    169159        (*it2)->data = NULL;
    170160      }
    171      
     161
    172162      delete *it2;
    173163    }
     
    181171  while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId )
    182172    it++;
    183  
    184 //  if ( getLeafClassID() == CL_SPACE_SHIP )
    185 //  {
    186 //    PRINTF(0)("getStateDiff:SpaceShip from: %d stateId: %d\n", (it == sentStates[userId].end())?-1:fromStateId, stateId);
    187 //  }
    188173
    189174  if ( it == sentStates[userId].end() )
     
    196181
    197182    stateFrom = initialEntry;
    198    
     183
    199184    sentStates[userId].push_back( stateFrom );
    200185  }
     
    205190
    206191  sentStates[userId].push_back( stateTo );
    207  
     192
    208193  stateTo->stateId = stateId;
    209194  stateTo->dataLength = neededSize;
     
    214199  int i = 0;
    215200  int n;
    216  
    217   bool hasPermission;
     201
     202  bool hasPermission = false;
    218203  bool sizeChanged = false;
    219204
     
    221206  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
    222207  {
    223     hasPermission = (
    224             this->isServer() && (*it)->checkPermission( PERMISSION_SERVER ) ||
    225         this->owner == SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ) ||
    226             this->isServer() && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER ) ||
    227             (*it)->checkPermission( PERMISSION_ALL )
    228                     );
    229    
    230     if ( sizeIter == stateFrom->sizeList.end() || *sizeIter != (*it)->getSize() )
     208    // DATA PERMISSIONS
     209    // check if this synchronizeable has the permissions to write the data
     210
     211    // first check MASTER_SERVER permissions
     212    if( SharedNetworkData::getInstance()->isMasterServer() && (*it)->checkPermission( PERMISSION_MASTER_SERVER ))
     213      hasPermission = true;
     214    // now check PROXY_SERVER permissions
     215    else if( SharedNetworkData::getInstance()->isProxyServer() && (*it)->checkPermission( PERMISSION_MASTER_SERVER ))
     216      hasPermission = true;
     217    // now check OWNER permissions
     218    else if( this->owner == SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ))
     219      hasPermission = true;
     220    // now check ALL permissions
     221    else if( (*it)->checkPermission( PERMISSION_ALL ))
     222      hasPermission = true;
     223    // SPECIAL: get write permissions if i am master server and i am able to overwrite the client stuff
     224    else if( SharedNetworkData::getInstance()->isMasterServer() && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER ))
     225      hasPermission = true;
     226    // SPECIAL: get write permissions if i am proxy server and i am able to overwrite the client stuff
     227    else if( SharedNetworkData::getInstance()->isProxyServer()  && this->owner != userId && (*it)->checkPermission( PERMISSION_OWNER ))
     228      hasPermission = true;
     229    else
     230      hasPermission = false;
     231
     232
     233    if ( sizeIter == stateFrom->sizeList.end() || *sizeIter != (*it)->getSize() )
    231234      sizeChanged = true;
    232    
     235
    233236    if ( ( hasPermission && (*it)->getPriority() >= priorityTH ) || sizeChanged )
    234237    {
    235238      n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i );
    236239      //NETPRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), n);
     240      //PRINTF(0)("getvar %s %d\n", (*it)->getName().c_str(), n);
    237241      stateTo->sizeList.push_back( n );
    238       //(*it)->debug();
     242      // this is only for very hardcore debug sessions
     243      // (*it)->debug();
    239244      i += n;
    240245    }
     
    257262  if ( i != neededSize )
    258263  {
    259     PRINTF(0)("strange error: (%s) %d != %d\n", this->getClassName(), i, neededSize);
     264    PRINTF(0)("strange error: (%s) %d != %d\n", this->getClassCName(), i, neededSize);
    260265    assert(false);
    261266  }
     
    281286 * @param fromStateId id of the base state id
    282287 * @return number bytes read
     288 *
    283289 * @todo check for permissions
    284290 */
     
    299305  StateHistoryEntry * stateFrom = NULL;
    300306
     307  // search the state from wich the diff is made of
    301308  StateHistory::iterator it = recvStates[userId].begin();
    302309  while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId )
    303310    it++;
    304311
    305  
    306 //  if ( getLeafClassID() == CL_SPACE_SHIP )
    307 //  {
    308 //    PRINTF(0)("setStateDiff:SpaceShip from: %d stateId: %d\n", (it == recvStates[userId].end())?-1:fromStateId, stateId);
    309 //  }
    310 
     312  // if this is the first state to receive
    311313  if ( it == recvStates[userId].end() )
    312314  {
     
    318320
    319321    stateFrom = initialEntry;
    320    
     322
    321323    recvStates[userId].push_back( stateFrom );
    322324  }
    323325  else
    324326    stateFrom = (*it);
    325  
    326   //apply diff
     327
     328
     329  // apply diff
    327330  for ( int i = 0; i<length; i++ )
    328331  {
     
    331334    else
    332335      stateTo->data[i] = data[i];
    333    
    334   }
    335  
     336  }
     337
    336338  //add state to state history
    337339  recvStates[userId].push_back( stateTo );
    338  
     340
    339341  int i = 0;
    340342  int n = 0;
    341343  std::list<int> changes;
    342  
     344  bool hasPermission = false;
     345
     346  // extract the new state for every client
    343347  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
    344348  {
    345     if (
    346         (*it)->checkPermission( PERMISSION_SERVER ) && networkStream->isUserServer( userId ) ||
    347         (*it)->checkPermission( PERMISSION_OWNER ) && this->owner == userId ||
    348         networkStream->isUserServer( userId ) && this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ) ||
    349         (*it)->checkPermission( PERMISSION_ALL )
    350        )
     349    // DATA PERMISSIONS
     350    // check if this synchronizeable has the permissions to write the data
     351
     352    // first check MASTER_SERVER permissions
     353    if(  this->networkStream->isUserMasterServer( userId ) && (*it)->checkPermission( PERMISSION_MASTER_SERVER ))
     354      hasPermission = true;
     355    // now check PROXY_SERVER permissions
     356    else if( this->networkStream->isUserProxyServer( userId )  && (*it)->checkPermission( PERMISSION_MASTER_SERVER ))
     357      hasPermission = true;
     358    // now check OWNER permissions
     359    else if( this->owner == userId && (*it)->checkPermission( PERMISSION_OWNER ))
     360      hasPermission = true;
     361    // now check ALL permissions
     362    else if( (*it)->checkPermission( PERMISSION_ALL ))
     363      hasPermission = true;
     364    // SPECIAL: get write permissions if im sending to a master server that does not own this sync
     365    else if( this->networkStream->isUserMasterServer( userId ) && this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ))
     366      hasPermission = true;
     367    // SPECIAL: get write permissions if im sending to a proxy server that does not own this sync
     368    else if( this->networkStream->isUserProxyServer( userId )  && this->owner != SharedNetworkData::getInstance()->getHostID() && (*it)->checkPermission( PERMISSION_OWNER ))
     369      hasPermission = true;
     370    else
     371      hasPermission = false;
     372
     373
     374
     375    // if it has the permission to write do it
     376    if( hasPermission)
    351377    {
    352378      n = (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i );
    353379      i += n;
    354       //NETPRINTF(0)("%s::setvar %s %d\n", getClassName(), (*it)->getName().c_str(), n);
     380      //NETPRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n);
     381      //PRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n);
    355382      //(*it)->debug();
    356383      if ( (*it)->getHasChanged() )
     
    361388    else
    362389    {
    363 //      PRINTF(0)("DONT SET VAR BECAUSE OF PERMISSION: %s %d %d %d %d %d %d\n", (*it)->getName().c_str(), (*it)->checkPermission( PERMISSION_SERVER ), (*it)->checkPermission( PERMISSION_OWNER ), (*it)->checkPermission( PERMISSION_ALL ), networkStream->isUserServer( userId ), this->owner, userId );
     390//      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 );
    364391      n = (*it)->getSizeFromBuf( stateTo->data + i, stateTo->dataLength - i );
    365       //NETPRINTF(0)("%s::setvar %s %d\n", getClassName(), (*it)->getName().c_str(), n);
     392      //NETPRINTF(0)("%s::setvar %s %d\n", getClassCName(), (*it)->getName().c_str(), n);
    366393      //(*it)->debug();
    367394      i += n;
     
    370397
    371398  this->varChangeHandler( changes );
    372  
     399
    373400  return i;
    374401}
     
    423450        (*it)->data = NULL;
    424451      }
    425    
     452
    426453      delete *it;
    427454    }
    428455    recvStates[userId].clear();
    429456  }
    430  
     457
    431458  if ( sentStates.size() > userId )
    432459  {
    433    
     460
    434461    for ( std::list<StateHistoryEntry*>::iterator it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ )
    435462    {
     
    439466        (*it)->data = NULL;
    440467      }
    441    
     468
    442469      delete *it;
    443470    }
     
    448475/**
    449476 * this function is called after recieving a state.
    450  * @param userId 
    451  * @param stateId 
    452  * @param fromStateId 
     477 * @param userId
     478 * @param stateId
     479 * @param fromStateId
    453480 */
    454481void Synchronizeable::handleRecvState( int userId, int stateId, int fromStateId )
     
    457484  if ( recvStates.size() <= userId )
    458485    recvStates.resize( userId+1 );
    459  
     486
    460487  //remove old states
    461488  StateHistory::iterator it = recvStates[userId].begin();
     
    485512      StateHistory::iterator delIt = it;
    486513      it ++;
    487      
     514
    488515      if ( (*delIt)->data )
    489516      {
     
    493520      delete *delIt;
    494521      recvStates[userId].erase( delIt );
    495      
     522
    496523      continue;
    497524    }
    498525    it++;
    499526  }
    500  
     527
    501528  StateHistory::iterator fromState = recvStates[userId].end();
    502529  StateHistory::iterator toState = recvStates[userId].end();
    503  
     530
    504531  for ( it = recvStates[userId].begin(); it != recvStates[userId].end(); it++ )
    505532  {
     
    508535    if ( (*it)->stateId == fromStateId )
    509536      fromState = it;
    510    
     537
    511538    if ( fromState != recvStates[userId].end() && toState != recvStates[userId].end() )
    512539      break;
    513540  }
    514  
     541
    515542  // setStateDiff was not called and i know fromStateId
    516543  if ( fromState != recvStates[userId].end() && toState == recvStates[userId].end() )
    517544  {
    518545    StateHistoryEntry * entry = new StateHistoryEntry;
    519    
     546
    520547    entry->dataLength = (*fromState)->dataLength;
    521548    if ( entry->dataLength > 0 )
    522549    {
    523550      entry->data = new byte[entry->dataLength];
    524          
     551
    525552      assert( (*fromState)->data );
    526553      memcpy( entry->data, (*fromState)->data, entry->dataLength );
     
    528555    else
    529556      entry->data = NULL;
    530    
     557
    531558    entry->sizeList = (*fromState)->sizeList;
    532559    entry->stateId = stateId;
    533    
     560
    534561    recvStates[userId].push_back(entry);
    535562  }
     
    538565/**
    539566 * this function is called after sending a state
    540  * @param userId 
    541  * @param stateId 
    542  * @param fromStateId 
     567 * @param userId
     568 * @param stateId
     569 * @param fromStateId
    543570 */
    544571void Synchronizeable::handleSentState( int userId, int stateId, int fromStateId )
     
    557584      StateHistory::iterator delIt = it;
    558585      it ++;
    559      
     586
    560587      if ( (*delIt)->data )
    561588      {
     
    565592      delete *delIt;
    566593      sentStates[userId].erase( delIt );
    567      
     594
    568595      continue;
    569596    }
     
    571598  }
    572599
    573  
     600
    574601  StateHistory::iterator fromState = sentStates[userId].end();
    575602  StateHistory::iterator toState = sentStates[userId].end();
    576  
     603
    577604  for ( it = sentStates[userId].begin(); it != sentStates[userId].end(); it++ )
    578605  {
     
    581608    if ( (*it)->stateId == fromStateId )
    582609      fromState = it;
    583    
     610
    584611    if ( fromState != sentStates[userId].end() && toState != sentStates[userId].end() )
    585612      break;
    586613  }
    587614
    588  
     615
    589616  // getStateDiff was not called and i know fromStateId
    590617  if ( fromState != sentStates[userId].end() && toState == sentStates[userId].end() )
    591618  {
    592619    StateHistoryEntry * entry = new StateHistoryEntry;
    593    
     620
    594621    entry->dataLength = (*fromState)->dataLength;
    595622    if ( entry->dataLength > 0 )
    596623    {
    597624      entry->data = new byte[entry->dataLength];
    598      
     625
    599626      assert( (*fromState)->data );
    600627      memcpy( entry->data, (*fromState)->data, entry->dataLength );
     
    602629    else
    603630      entry->data = NULL;
    604    
     631
    605632    entry->sizeList = (*fromState)->sizeList;
    606633    entry->stateId = stateId;
    607    
     634
    608635    sentStates[userId].push_back(entry);
    609636  }
    610  
    611 }
    612 
    613 
    614 
     637
     638}
     639
     640
     641
Note: See TracChangeset for help on using the changeset viewer.