Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7508 was 7508, checked in by rennerc, 18 years ago

implemented synchronizeable

File size: 7.8 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
23#include "state.h"
24
25#include <cassert>
26
27#include "synchronizeable.h"
28
29
30
31/**
32 *  default constructor
33 */
34Synchronizeable::Synchronizeable()
35{
36  this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable");
37  this->owner = -1;
38  this->hostID = SharedNetworkData::getInstance()->getHostID();
39  this->setIsServer(this->hostID == 0);
40  this->uniqueID = NET_UID_UNASSIGNED;
41  this->networkStream = NULL;
42  this->bSynchronize = false;
43
44  if( State::isOnline())
45  {
46    NetworkStream* nd = SharedNetworkData::getInstance()->getDefaultSyncStream();
47    assert(nd != NULL);
48    nd->connectSynchronizeable(*this);
49    this->setUniqueID(SharedNetworkData::getInstance()->getNewUniqueID());
50  }
51 
52  this->registerVar( new SynchronizeableString( &this->objectName, &this->objectName, "objectName" ) );
53}
54
55
56
57/**
58 *  default destructor deletes all unneded stuff
59 */
60Synchronizeable::~Synchronizeable()
61{
62  if ( this->networkStream )
63    this->networkStream->disconnectSynchronizeable(*this);
64}
65
66/**
67 * Sets the server flag to a given value
68 * @param isServer: the boolean value which the server flag is to set to
69 */
70void Synchronizeable::setIsServer(bool isServer)
71{
72  if( isServer )
73    this->state = this->state | STATE_SERVER;
74  else
75    this->state = this->state & (~STATE_SERVER);
76}
77
78
79/**
80 * Determines if the server flag is set
81 * @return true, if the server flag is true, false else
82 */
83bool Synchronizeable::isServer()
84{
85  return (this->state & STATE_SERVER) >0;
86}
87
88
89/**
90 * get the diff to last acked state of userId
91 * @param userId user to create diff for
92 * @param data buffer to copy diff in
93 * @param maxLength max bytes to copy into data
94 * @param stateId id of current state
95 * @param priorityTH tells getStateDiff to not send element with priority \< priorityTH
96 * @return n bytes copied into data
97 */
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;
197}
198
199/**
200 * sets a new state out of a diff created on another host
201 * @param userId hostId of user who send me that diff
202 * @param data pointer to diff
203 * @param length length of diff
204 * @param stateId id of current state
205 * @return true on success
206 */
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;
280}
281
282 /**
283 * override this function to be notified on change
284 * of your registred variables.
285 * @param id id's which have changed
286 */
287void Synchronizeable::varChangeHandler( std::list<int> & id )
288{
289}
290
291/**
292 * registers a varable to be synchronized over network
293 * @param var see src/lib/network/synchronizeable_var/ for available classes
294 */
295void Synchronizeable::registerVar( SynchronizeableVar * var )
296{
297  syncVarList.push_back( var );
298}
299
300/**
301 * registers a varable to be synchronized over network
302 * return value is passed to varChangeHandler on change
303 * @param var see src/lib/network/synchronizeable_var/ for available classes
304 * @return handle passed to varChangeHandler on changes
305 */
306int Synchronizeable::registerVarId( SynchronizeableVar * var )
307{
308  syncVarList.push_back( var );
309  return syncVarList.size()-1;
310}
311
312
Note: See TracBrowser for help on using the repository browser.