Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

we have a new gamestate concept now: dont transmit synchronisable header of objects, that dont get updated that tick. transmit objectids of deleted objects to delete them on the client too

  • Property svn:eol-style set to native
File size: 11.5 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  std::map<unsigned int, Synchronisable *> Synchronisable::objectMap_;
55  std::queue<unsigned int> Synchronisable::deletedObjects_;
56
57  int Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
58
59  /**
60  * Constructor:
61  * calls registarAllVariables, that has to be implemented by the inheriting classID
62  */
63  Synchronisable::Synchronisable(){
64    RegisterRootObject(Synchronisable);
65    static unsigned int idCounter=0;
66    datasize=0;
67    objectFrequency_=1;
68    objectMode_=0x1; // by default do not send data to servere
69    objectID=idCounter++;
70    syncList = new std::list<synchronisableVariable *>;
71  }
72
73  Synchronisable::~Synchronisable(){
74    // delete callback function objects
75    if(!orxonox::Identifier::isCreatingHierarchy()){
76      for(std::list<synchronisableVariable *>::iterator it = syncList->begin(); it!=syncList->end(); it++)
77        delete (*it)->callback;
78      assert(objectMap_[objectID]->objectID==objectID);
79      objectMap_.erase(objectID);
80    }
81  }
82
83  bool Synchronisable::create(){
84    objectMap_[objectID]=this;
85    assert(objectMap_[objectID]==this);
86    this->classID = this->getIdentifier()->getNetworkID();
87    COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl;
88    return true;
89  }
90
91 
92  void Synchronisable::setClient(bool b){
93    if(b) // client
94      state_=0x2;
95    else  // server
96      state_=0x1;
97  }
98 
99  Synchronisable *Synchronisable::fabricate(unsigned char*& mem, int mode)
100  {
101    unsigned int size, objectID, classID;
102    size = *(unsigned int *)mem;
103    objectID = *(unsigned int*)(mem+sizeof(unsigned int));
104    classID = *(unsigned int*)(mem+2*sizeof(unsigned int));
105   
106    orxonox::Identifier* id = GetIdentifier(classID);
107    assert(id);
108    orxonox::BaseObject *bo = id->fabricate();
109    Synchronisable *no = dynamic_cast<Synchronisable *>(bo);
110    assert(no);
111    no->objectID=objectID;
112    no->classID=classID;
113    COUT(3) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl;
114          // update data and create object/entity...
115    assert(no->updateData(mem, mode));
116    assert( no->create() );
117    return no;
118  }
119
120 
121  bool Synchronisable::deleteObject(unsigned int objectID){
122    assert(getSynchronisable(objectID));
123    assert(getSynchronisable(objectID)->objectID==objectID);
124    delete objectMap_[objectID];
125    return true;
126  }
127 
128  Synchronisable* Synchronisable::getSynchronisable(unsigned int objectID){
129    std::map<unsigned int, Synchronisable *>::iterator i = objectMap_.find(objectID);
130    if(i==objectMap_.end())
131      return NULL;
132    assert(i->second->objectID==objectID);
133    return (*i).second;
134  }
135
136 
137  /**
138  * This function is used to register a variable to be synchronized
139  * also counts the total datasize needed to save the variables
140  * @param var pointer to the variable
141  * @param size size of the datatype the variable consists of
142  */
143  void Synchronisable::registerVar(void *var, int size, variableType t, int mode, NetworkCallbackBase *cb){
144    // create temporary synch.Var struct
145    synchronisableVariable *temp = new synchronisableVariable;
146    temp->size = size;
147    temp->var = var;
148    temp->mode = mode;
149    temp->type = t;
150    temp->callback = cb;
151    COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl;
152    // increase datasize
153    datasize+=sizeof(int)+size;
154    //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl;
155    COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl;
156    syncList->push_back(temp);
157  }
158 
159  /**
160   * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
161   * Difference to the above function:
162   * takes a pointer to already allocated memory (must have at least getSize bytes length)
163   * structure of the bitstream:
164   * (var1_size,var1,var2_size,var2,...)
165   * varx_size: size = sizeof(int)
166   * varx: size = varx_size
167   * @return data containing all variables and their sizes
168   */
169  bool Synchronisable::getData(unsigned char*& mem, unsigned int id, int mode){
170    //if this tick is we dont synchronise, then abort now
171    if(!isMyTick(id))
172      return true;
173    //std::cout << "inside getData" << std::endl;
174    unsigned int tempsize = 0;
175    if(mode==0x0)
176      mode=state_;
177    if(classID==0)
178      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
179    this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this
180    std::list<synchronisableVariable *>::iterator i;
181    unsigned int size;
182    size=getSize(id, mode);
183   
184    // start copy header
185    memcpy(mem, &size, sizeof(unsigned int));
186    mem+=sizeof(unsigned int);
187    memcpy(mem, &(this->objectID), sizeof(unsigned int));
188    mem+=sizeof(unsigned int);
189    memcpy(mem, &(this->classID), sizeof(unsigned int));
190    mem+=sizeof(unsigned int);
191    tempsize+=12;
192    // end copy header
193   
194   
195    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl;
196    // copy to location
197    for(i=syncList->begin(); i!=syncList->end(); ++i){
198      //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
199      if( ((*i)->mode & mode) == 0 ){
200        COUT(5) << "not getting data: " << std::endl;
201        continue;  // this variable should only be received
202      }
203      switch((*i)->type){
204        case DATA:
205          memcpy( (void *)(mem), (void*)((*i)->var), (*i)->size);
206          mem+=(*i)->size;
207          tempsize+=(*i)->size;
208          break;
209        case STRING:
210          memcpy( (void *)(mem), (void *)&((*i)->size), sizeof(int) );
211          mem+=sizeof(int);
212          const char *data = ( ( *(std::string *) (*i)->var).c_str());
213          memcpy( mem, (void*)data, (*i)->size);
214          COUT(5) << "synchronisable: char: " << (const char *)(mem) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;
215          mem+=(*i)->size;
216          tempsize+=(*i)->size + 4;
217          break;
218      }
219    }
220    assert(tempsize==size);
221    return true;
222  }
223
224 
225  /**
226   * This function takes a syncData struct and takes it to update the variables
227   * @param vars data of the variables
228   * @return true/false
229   */
230  bool Synchronisable::updateData(unsigned char*& mem, int mode){
231    if(mode==0x0)
232      mode=state_;
233    std::list<synchronisableVariable *>::iterator i;
234    if(syncList->empty()){
235      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
236      return false;
237    }
238    unsigned char *data=mem;
239    // start extract header
240    if(!isMyData(mem))
241      return true;
242    unsigned int objectID, classID, size;
243    size = *(int *)mem;
244    mem+=sizeof(size);
245    objectID = *(int *)mem;
246    mem+=sizeof(objectID);
247    classID = *(int *)mem;
248    mem+=sizeof(classID);
249    // stop extract header
250    assert(this->objectID==objectID);
251    assert(this->classID==classID);
252   
253    COUT(5) << "Synchronisable: objectID " << objectID << ", classID " << classID << " size: " << size << " synchronising data" << std::endl;
254    for(i=syncList->begin(); i!=syncList->end() && mem <= data+size; i++){
255      if( ((*i)->mode ^ mode) == 0 ){
256        COUT(5) << "synchronisable: not updating variable " << std::endl;
257        continue;  // this variable should only be set
258      }
259      COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;
260      bool callback=false;
261      switch((*i)->type){
262        case DATA:
263          if((*i)->callback) // check whether this variable changed (but only if callback was set)
264            if(strncmp((char *)(*i)->var, (char *)mem, (*i)->size)!=0)
265              callback=true;
266          memcpy((void*)(*i)->var, mem, (*i)->size);
267          mem+=(*i)->size;
268          break;
269        case STRING:
270          (*i)->size = *(int *)mem;
271          COUT(5) << "string size: " << (*i)->size << std::endl;
272          mem+=sizeof(int);
273          if((*i)->callback) // check whether this string changed
274            if( *(std::string *)((*i)->var) != std::string((char *)mem) )
275              callback=true;
276          *((std::string *)((*i)->var)) = std::string((const char*)mem);
277          COUT(5) << "synchronisable: char: " << (const char*)mem << " string: " << std::string((const char*)mem) << std::endl;
278          mem += (*i)->size;
279          break;
280      }
281      // call the callback function, if defined
282      if(callback && (*i)->callback)
283        (*i)->callback->call();
284    }
285    return true;
286  }
287
288  /**
289  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
290  * @return amount of bytes
291  */
292  int Synchronisable::getSize(unsigned int id, int mode){
293    if(!isMyTick(id))
294      return 0;
295    int tsize=sizeof(synchronisableHeader);
296    if(mode==0x0)
297      mode=state_;
298    std::list<synchronisableVariable *>::iterator i;
299    for(i=syncList->begin(); i!=syncList->end(); i++){
300      if( ((*i)->mode & mode) == 0 )
301        continue;  // this variable should only be received, so dont add its size to the send-size
302      switch((*i)->type){
303      case DATA:
304        tsize+=(*i)->size;
305        break;
306      case STRING:
307        tsize+=sizeof(int);
308        (*i)->size=((std::string *)(*i)->var)->length()+1;
309        COUT(5) << "String size: " << (*i)->size << std::endl;
310        tsize+=(*i)->size;
311        break;
312      }
313    }
314    return tsize;
315  }
316
317  /**
318   *
319   * @param id
320   * @return
321   */
322  bool Synchronisable::isMyTick(unsigned int id){
323//     return true;
324    return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0);
325  }
326 
327  bool Synchronisable::isMyData(unsigned char* mem)
328  {
329    unsigned int objectID, classID, size;
330    size = *(int *)mem;
331    mem+=sizeof(size);
332    objectID = *(int *)mem;
333    mem+=sizeof(objectID);
334    classID = *(int *)mem;
335    mem+=sizeof(classID);
336   
337    assert(classID == this->classID);
338    return (objectID == this->objectID);
339  }
340 
341  void Synchronisable::setObjectMode(int mode){
342    assert(mode==0x1 || mode==0x2 || mode==0x3);
343    objectMode_=mode;
344  }
345
346
347}
Note: See TracBrowser for help on using the repository browser.