Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/Synchronisable.cc @ 1827

Last change on this file since 1827 was 1827, checked in by scheusso, 16 years ago

another new gamestate concept ;)

  • server does an individual object composition (to be sent) for every client
  • atm objects have sync frequencies and are priorized after their frequency (not clienbased yet)
  • after highlevel diff (object composition) a lowlevel diff is done
  • afterwards compression

→ 65 to 80 percent less data to be transmitted

  • Property svn:eol-style set to native
File size: 14.1 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 *      Dumeni Manatschal, (C) 2007
24 *      Oliver Scheuss, (C) 2007
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30//
31// C++ Implementation: synchronisable
32//
33// Description:
34//
35//
36// Author:  Dumeni, Oliver Scheuss, (C) 2007
37//
38// Copyright: See COPYING file that comes with this distribution
39//
40
41#include "Synchronisable.h"
42
43#include <string>
44#include <iostream>
45#include <assert.h>
46
47#include "core/CoreIncludes.h"
48#include "core/BaseObject.h"
49// #include "core/Identifier.h"
50
51namespace network
52{
53 
54
55  std::map<unsigned int, Synchronisable *> Synchronisable::objectMap_;
56  std::queue<unsigned int> Synchronisable::deletedObjects_;
57
58  int Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
59
60  /**
61  * Constructor:
62  * Initializes all Variables and sets the right objectID
63  */
64  Synchronisable::Synchronisable(){
65    RegisterRootObject(Synchronisable);
66    static unsigned int idCounter=0;
67    datasize=0;
68    objectFrequency_=1;
69    objectMode_=0x1; // by default do not send data to servere
70    objectID=idCounter++;
71    syncList = new std::list<synchronisableVariable *>;
72  }
73
74  /**
75   * Destructor:
76   * Delete all callback objects and remove objectID from the objectMap_
77   */
78  Synchronisable::~Synchronisable(){
79    // delete callback function objects
80    if(!orxonox::Identifier::isCreatingHierarchy()){
81      for(std::list<synchronisableVariable *>::iterator it = syncList->begin(); it!=syncList->end(); it++)
82        delete (*it)->callback;
83      assert(objectMap_[objectID]->objectID==objectID);
84      objectMap_.erase(objectID);
85    }
86  }
87
88  /**
89   * This function gets called after all neccessary data has been passed to the object
90   * Overload this function and recall the create function of the parent class
91   * @return true/false
92   */
93  bool Synchronisable::create(){
94    objectMap_[objectID]=this;
95    assert(objectMap_[objectID]==this);
96    this->classID = this->getIdentifier()->getNetworkID();
97    COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl;
98    return true;
99  }
100
101 
102  /**
103   * This function sets the internal mode for synchronisation
104   * @param b true if this object is located on a client or on a server
105   */
106  void Synchronisable::setClient(bool b){
107    if(b) // client
108      state_=0x2;
109    else  // server
110      state_=0x1;
111  }
112 
113  /**
114   * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create
115   * After calling this function the mem pointer will be increased by the size of the needed data
116   * @param mem pointer to where the appropriate data is located
117   * @param mode defines the mode, how the data should be loaded
118   * @return pointer to the newly created synchronisable
119   */
120  Synchronisable *Synchronisable::fabricate(unsigned char*& mem, int mode)
121  {
122    synchronisableHeader *header = (synchronisableHeader *)mem;
123   
124    orxonox::Identifier* id = GetIdentifier(header->classID);
125    assert(id);
126    orxonox::BaseObject *bo = id->fabricate();
127    Synchronisable *no = dynamic_cast<Synchronisable *>(bo);
128    assert(no);
129    no->objectID=header->objectID;
130    no->classID=header->classID;
131    COUT(3) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl;
132          // update data and create object/entity...
133    assert(no->updateData(mem, mode));
134    assert( no->create() );
135    return no;
136  }
137
138 
139  /**
140   * Finds and deletes the Synchronisable with the appropriate objectID
141   * @param objectID objectID of the Synchronisable
142   * @return true/false
143   */
144  bool Synchronisable::deleteObject(unsigned int objectID){
145    assert(getSynchronisable(objectID));
146    assert(getSynchronisable(objectID)->objectID==objectID);
147    delete objectMap_[objectID];
148    return true;
149  }
150 
151  /**
152   * This function looks up the objectID in the objectMap_ and returns a pointer to the right Synchronisable
153   * @param objectID objectID of the Synchronisable
154   * @return pointer to the Synchronisable with the objectID
155   */
156  Synchronisable* Synchronisable::getSynchronisable(unsigned int objectID){
157    std::map<unsigned int, Synchronisable *>::iterator i = objectMap_.find(objectID);
158    if(i==objectMap_.end())
159      return NULL;
160    assert(i->second->objectID==objectID);
161    return (*i).second;
162  }
163
164 
165  /**
166  * This function is used to register a variable to be synchronized
167  * also counts the total datasize needed to save the variables
168  * @param var pointer to the variable
169  * @param size size of the datatype the variable consists of
170  * @param t the type of the variable (network::DATA or network::STRING
171  * @param mode same as in getData
172  * @param cb callback object that should get called, if the value of the variable changes
173  */
174  void Synchronisable::registerVar(void *var, int size, variableType t, int mode, NetworkCallbackBase *cb){
175    // create temporary synch.Var struct
176    synchronisableVariable *temp = new synchronisableVariable;
177    temp->size = size;
178    temp->var = var;
179    temp->mode = mode;
180    temp->type = t;
181    temp->callback = cb;
182    COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl;
183    // increase datasize
184    datasize+=sizeof(int)+size;
185    //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl;
186    COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl;
187    syncList->push_back(temp);
188#ifndef NDEBUG
189    std::list<synchronisableVariable *>::iterator it = syncList->begin();
190    while(it!=syncList->end()){
191      assert(*it!=var);
192      it++;
193    }
194#endif
195  }
196 
197  /**
198   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory
199   * takes a pointer to already allocated memory (must have at least getSize bytes length)
200   * structure of the bitstream:
201   * |totalsize,objectID,classID,var1,var2,string1_length,string1,var3,...|
202   * length of varx: size saved int syncvarlist
203   * @param mem pointer to allocated memory with enough size
204   * @param id gamestateid of the gamestate to be saved (important for priorities)
205   * @param mode defines the direction in which the data will be send/received
206   *             0x1: server->client
207   *             0x2: client->server (not recommended)
208   *             0x3: bidirectional
209   * @return true: if !isMyTick or if everything was successfully saved
210   */
211  bool Synchronisable::getData(unsigned char*& mem, unsigned int id, int mode){
212    //if this tick is we dont synchronise, then abort now
213    if(!isMyTick(id))
214      return true;
215    //std::cout << "inside getData" << std::endl;
216    unsigned int tempsize = 0;
217    if(mode==0x0)
218      mode=state_;
219    if(classID==0)
220      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
221    this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this
222    std::list<synchronisableVariable *>::iterator i;
223    unsigned int size;
224    size=getSize(id, mode);
225   
226    // start copy header
227    synchronisableHeader *header = (synchronisableHeader *)mem;
228    header->size = size;
229    header->objectID = this->objectID;
230    header->classID = this->classID;
231    header->dataAvailable = true;
232    tempsize+=sizeof(synchronisableHeader);
233    mem+=sizeof(synchronisableHeader);
234    // end copy header
235   
236   
237    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl;
238    // copy to location
239    for(i=syncList->begin(); i!=syncList->end(); ++i){
240      if( ((*i)->mode & mode) == 0 ){
241        COUT(5) << "not getting data: " << std::endl;
242        continue;  // this variable should only be received
243      }
244      switch((*i)->type){
245        case DATA:
246          memcpy( (void *)(mem), (void*)((*i)->var), (*i)->size);
247          mem+=(*i)->size;
248          tempsize+=(*i)->size;
249          break;
250        case STRING:
251          memcpy( (void *)(mem), (void *)&((*i)->size), sizeof(int) );
252          mem+=sizeof(int);
253          const char *data = ( ( *(std::string *) (*i)->var).c_str());
254          memcpy( mem, (void*)data, (*i)->size);
255          COUT(5) << "synchronisable: char: " << (const char *)(mem) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;
256          mem+=(*i)->size;
257          tempsize+=(*i)->size + 4;
258          break;
259      }
260    }
261    assert(tempsize==size);
262    return true;
263  }
264
265 
266  /**
267   * This function takes a bytestream and loads the data into the registered variables
268   * @param mem pointer to the bytestream
269   * @param mode same as in getData
270   * @return true/false
271   */
272  bool Synchronisable::updateData(unsigned char*& mem, int mode){
273    if(mode==0x0)
274      mode=state_;
275    std::list<synchronisableVariable *>::iterator i;
276    if(syncList->empty()){
277      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
278      return false;
279    }
280    unsigned char *data=mem;
281    // start extract header
282    synchronisableHeader *syncHeader = (synchronisableHeader *)mem;
283    assert(syncHeader->objectID==this->objectID);
284    if(syncHeader->dataAvailable==false){
285      mem+=syncHeader->size;
286      return true;
287    }
288   
289    mem+=sizeof(synchronisableHeader);
290    // stop extract header
291    assert(this->objectID==syncHeader->objectID);
292//    assert(this->classID==syncHeader->classID); //TODO: fix this!!! maybe a problem with the identifier ?
293   
294    COUT(5) << "Synchronisable: objectID " << syncHeader->objectID << ", classID " << syncHeader->classID << " size: " << syncHeader->size << " synchronising data" << std::endl;
295    for(i=syncList->begin(); i!=syncList->end() && mem <= data+syncHeader->size; i++){
296      if( ((*i)->mode ^ mode) == 0 ){
297        COUT(5) << "synchronisable: not updating variable " << std::endl;
298        continue;  // this variable should only be set
299      }
300      COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;
301      bool callback=false;
302      switch((*i)->type){
303        case DATA:
304          if((*i)->callback) // check whether this variable changed (but only if callback was set)
305            if(strncmp((char *)(*i)->var, (char *)mem, (*i)->size)!=0)
306              callback=true;
307          memcpy((void*)(*i)->var, mem, (*i)->size);
308          mem+=(*i)->size;
309          break;
310        case STRING:
311          (*i)->size = *(int *)mem;
312          COUT(5) << "string size: " << (*i)->size << std::endl;
313          mem+=sizeof(int);
314          if((*i)->callback) // check whether this string changed
315            if( *(std::string *)((*i)->var) != std::string((char *)mem) )
316              callback=true;
317          *((std::string *)((*i)->var)) = std::string((const char*)mem);
318          COUT(5) << "synchronisable: char: " << (const char*)mem << " string: " << std::string((const char*)mem) << std::endl;
319          mem += (*i)->size;
320          break;
321      }
322      // call the callback function, if defined
323      if(callback && (*i)->callback)
324        (*i)->callback->call();
325    }
326    return true;
327  }
328
329  /**
330  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
331  * @param id id of the gamestate
332  * @param mode same as getData
333  * @return amount of bytes
334  */
335  int Synchronisable::getSize(unsigned int id, int mode){
336    if(!isMyTick(id))
337      return 0;
338    int tsize=sizeof(synchronisableHeader);
339    if(mode==0x0)
340      mode=state_;
341    std::list<synchronisableVariable *>::iterator i;
342    for(i=syncList->begin(); i!=syncList->end(); i++){
343      if( ((*i)->mode & mode) == 0 )
344        continue;  // this variable should only be received, so dont add its size to the send-size
345      switch((*i)->type){
346      case DATA:
347        tsize+=(*i)->size;
348        break;
349      case STRING:
350        tsize+=sizeof(int);
351        (*i)->size=((std::string *)(*i)->var)->length()+1;
352        COUT(5) << "String size: " << (*i)->size << std::endl;
353        tsize+=(*i)->size;
354        break;
355      }
356    }
357    return tsize;
358  }
359
360  /**
361   * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction)
362   * @param id gamestate id
363   * @return true/false
364   */
365  bool Synchronisable::isMyTick(unsigned int id){
366    return ( (objectMode_&state_)!=0 );
367  }
368 
369  bool Synchronisable::doSelection(unsigned int id){
370    return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0);
371  }
372 
373  /**
374   * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones
375   * @param mem pointer to the bytestream
376   */
377  bool Synchronisable::isMyData(unsigned char* mem)
378  {
379    synchronisableHeader *header = (synchronisableHeader *)mem;
380    assert(header->objectID==this->objectID);
381    return header->dataAvailable;
382  }
383 
384  /**
385   * This function sets the synchronisation mode of the object
386   * If set to 0x1 variables will only be synchronised to the client
387   * If set to 0x2 variables will only be synchronised to the server
388   * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar)
389   * @param mode same as in registerVar
390   */
391  void Synchronisable::setObjectMode(int mode){
392    assert(mode==0x1 || mode==0x2 || mode==0x3);
393    objectMode_=mode;
394  }
395
396
397}
Note: See TracBrowser for help on using the repository browser.