Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7508 in orxonox.OLD for branches


Ignore:
Timestamp:
May 3, 2006, 2:29:17 PM (19 years ago)
Author:
rennerc
Message:

implemented synchronizeable

Location:
branches/network/src/lib/network
Files:
6 edited

Legend:

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

    r7459 r7508  
    9696 * @return n bytes copied into data
    9797 */
    98 int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int priorityTH )
    99 {
    100 #warning implement this
     98int Synchronizeable::getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH )
     99{
     100  //make sure this user has his history
     101  if ( sentStates.size() <= userId )
     102    sentStates.resize( userId+1 );
     103 
     104  //calculate needed memory
     105  int neededSize = 0;
     106 
     107  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
     108    neededSize += (*it)->getSize();
     109 
     110  assert( neededSize <= maxLength );
     111 
     112  //remove older states from history than fromStateId
     113  StateHistory::iterator it = sentStates[userId].begin();
     114 
     115  while ( it != sentStates[userId].end() && (*it)->stateId < fromStateId )
     116    it++;
     117 
     118  if ( it != sentStates[userId].begin() )
     119  {
     120    for ( StateHistory::iterator it2 = sentStates[userId].begin(); it2 != it; it2++ )
     121    {
     122      if ( (*it2)->data != NULL )
     123      {
     124        delete (*it2)->data;
     125        (*it2)->data = NULL;
     126      }
     127    }
     128    sentStates[userId].erase( sentStates[userId].begin(), it );
     129  }
     130 
     131  //find state to create diff from
     132  StateHistoryEntry * stateFrom = NULL;
     133 
     134  it = sentStates[userId].begin();
     135  while ( it != sentStates[userId].end() && (*it)->stateId != fromStateId )
     136    it++;
     137 
     138  if ( it == sentStates[userId].end() )
     139  {
     140    StateHistoryEntry * initialEntry = new StateHistoryEntry();
     141   
     142    initialEntry->stateId = fromStateId;
     143    initialEntry->dataLength = 0;
     144    initialEntry->data = NULL;
     145   
     146    stateFrom = initialEntry;
     147  }
     148  else
     149    stateFrom = (*it);
     150 
     151  StateHistoryEntry * stateTo = new StateHistoryEntry();
     152   
     153  stateTo->stateId = stateId;
     154  stateTo->dataLength = neededSize;
     155  stateTo->data = (byte*)malloc( neededSize );
     156 
     157  std::list<int>::iterator sizeIter = stateFrom->sizeList.begin();
     158 
     159  int i = 0;
     160  int n;
     161 
     162  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
     163  {
     164    if ( (*it)->getPriority() >= priorityTH || sizeIter == stateFrom->sizeList.end() )
     165    {
     166      n = (*it)->writeToBuf( stateTo->data+i, stateTo->dataLength - i );
     167      stateTo->sizeList.push_back( n );
     168      i += n;
     169    }
     170    else
     171    {
     172      for ( int j = 0; j<(*sizeIter); j++ )
     173      {
     174        assert( i < stateFrom->dataLength );
     175        stateTo->data[i] = stateFrom->data[i];
     176        i++;
     177      }
     178      stateTo->sizeList.push_back( (*sizeIter) );
     179    }
     180   
     181    if ( sizeIter != stateFrom->sizeList.end() )
     182      sizeIter++;
     183  }
     184 
     185  sentStates[userId].push_back( stateTo );
     186 
     187  //write diff to data
     188  for ( i = 0; i<neededSize; i++ )
     189  {
     190    if ( i < stateFrom->dataLength )
     191      data[i] = stateTo->data[i] - stateFrom->data[i];
     192    else
     193      data[i] = stateTo->data[i];
     194  }
     195 
     196  return neededSize;
    101197}
    102198
     
    109205 * @return true on success
    110206 */
    111 bool Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId )
    112 {
    113 #warning implement this
     207bool Synchronizeable::setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId )
     208{
     209  //make sure this user has his history
     210  if ( recvStates.size() <= userId )
     211    recvStates.resize( userId+1 );
     212 
     213  //create new state
     214  StateHistoryEntry * stateTo = new StateHistoryEntry();
     215  stateTo->stateId = stateId;
     216  stateTo->dataLength = length;
     217  stateTo->data = (byte*)malloc( length );
     218 
     219  //remove old states
     220  StateHistory::iterator it = recvStates[userId].begin();
     221 
     222  while ( it != recvStates[userId].end() && (*it)->stateId < fromStateId )
     223    it++;
     224 
     225  if ( it != recvStates[userId].begin() )
     226  {
     227    for ( StateHistory::iterator it2 = recvStates[userId].begin(); it2 != it; it2++ )
     228    {
     229      if ( (*it2)->data != NULL )
     230      {
     231        delete (*it2)->data;
     232        (*it2)->data = NULL;
     233      }
     234    }
     235    recvStates[userId].erase( recvStates[userId].begin(), it );
     236  }
     237 
     238  //find state to apply diff to
     239  StateHistoryEntry * stateFrom = NULL;
     240 
     241  it = recvStates[userId].begin();
     242  while ( it != recvStates[userId].end() && (*it)->stateId != fromStateId )
     243    it++;
     244 
     245  if ( it == recvStates[userId].end() )
     246  {
     247    StateHistoryEntry * initialEntry = new StateHistoryEntry();
     248   
     249    initialEntry->stateId = fromStateId;
     250    initialEntry->dataLength = 0;
     251    initialEntry->data = NULL;
     252   
     253    stateFrom = initialEntry;
     254  }
     255  else
     256    stateFrom = (*it);
     257 
     258  //apply diff
     259  for ( int i = 0; i<length; i++ )
     260  {
     261    if ( i < stateFrom->dataLength )
     262      stateTo->data[i] = stateFrom->data[i] + data[i];
     263    else
     264      stateTo->data[i] = data[i];
     265  }
     266 
     267  //add state to state history
     268  recvStates[userId].push_back( stateTo );
     269 
     270  int i = 0;
     271 
     272  for ( SyncVarList::iterator it = syncVarList.begin(); it != syncVarList.end(); it++ )
     273  {
     274    i += (*it)->readFromBuf( stateTo->data + i, stateTo->dataLength - i );
     275  }
     276 
     277  assert( i == length -1 );
     278 
     279  return length;
    114280}
    115281
     
    129295void Synchronizeable::registerVar( SynchronizeableVar * var )
    130296{
    131 #warning implement this
     297  syncVarList.push_back( var );
    132298}
    133299
     
    140306int Synchronizeable::registerVarId( SynchronizeableVar * var )
    141307{
    142 #warning implement this
    143 }
    144 
    145 
     308  syncVarList.push_back( var );
     309  return syncVarList.size()-1;
     310}
     311
     312
  • branches/network/src/lib/network/synchronizeable.h

    r7444 r7508  
    2828#define STATE_SERVER 1
    2929
     30struct StateHistoryEntry
     31{
     32  int             stateId;
     33  byte *          data;
     34  int             dataLength;
     35  std::list<int>  sizeList;
     36};
     37
     38typedef std::list<StateHistoryEntry*> StateHistory;
     39
     40typedef std::vector<StateHistory> UserStateHistory;
     41
    3042enum {
    3143  PERMISSION_OWNER = 1,
     
    3345};
    3446
    35 typedef std::vector<SynchronizeableVar> SyncVarList;
     47typedef std::vector<SynchronizeableVar*> SyncVarList;
    3648
    3749class NetworkStream;
     
    4961    virtual void varChangeHandler( std::list<int> & id );
    5062   
    51     int getStateDiff( int userId, byte* data, int maxLength, int stateId, int priorityTH );
    52     bool setStateDiff( int userId, byte* data, int length, int stateId );
     63    int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH );
     64    bool setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId );
    5365   
    5466    void registerVar( SynchronizeableVar * var );
     
    8092    int               hostID;         //!< my own host id
    8193    bool              bSynchronize;   //!< do we need beeing synchronized?
     94   
     95    SyncVarList       syncVarList;    //!< list containing variables to synchronize
     96   
     97    UserStateHistory  sentStates;     //!< store already sent states to create diffs from
     98    UserStateHistory  recvStates;     //!< store recieved states to apply diffs
    8299
    83100};
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_bool.cc

    r7459 r7508  
    2121 * standard constructor
    2222 */
    23 SynchronizeableBool::SynchronizeableBool( bool * ptrIn, bool * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 0, permission, priority )
     23SynchronizeableBool::SynchronizeableBool( bool * ptrIn, bool * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 1, permission, priority )
    2424{
    2525  this->vPtrIn = ptrIn;
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_int.cc

    r7459 r7508  
    2323 * @todo this constructor is not jet implemented - do it
    2424*/
    25 SynchronizeableInt::SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 0, permission, priority )
     25SynchronizeableInt::SynchronizeableInt( int * ptrIn, int * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, INTSIZE, permission, priority )
    2626{
    2727  this->vPtrIn = ptrIn;
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_string.h

    r7459 r7508  
    55
    66#include "synchronizeable_var/synchronizeable_var.h"
     7#include "converter.h"
    78
    89#ifndef _SYNCHRONIZEABLE_STRING_H
     
    2728    virtual bool hasStaticSize(){ return false; };
    2829   
     30    virtual int getSize(){ return vPtrIn->length()+INTSIZE; }
     31   
    2932  private:
    3033    std::string * vPtrIn;       //!< pointer to data (read)
  • branches/network/src/lib/network/synchronizeable_var/synchronizeable_var.h

    r7459 r7508  
    4545   
    4646    /**
     47     * get size writeToBuf needs
     48     * @return size in bytes
     49     */
     50    virtual int getSize(){ return length; }
     51   
     52    /**
    4753     * check for permission to write
    4854     * @return true if you can write
     
    6167     */
    6268    inline void setName( std::string name ) { this->name = name; }
     69   
     70    /**
     71     * get priority
     72     * @return priority
     73     */
     74    inline int getPriority() { return this->priority; }
     75   
     76    /**
     77     * set priority
     78     * @param p priority
     79     */
     80    inline void setPriority( int p ) { this->priority = p; }
     81   
     82    /**
     83     * reset priority to variable specific default value
     84     */
     85    inline void resetPriority() { this->priority = this->real_priority; }
    6386
    6487
Note: See TracChangeset for help on using the changeset viewer.