Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ceguilua/src/network/Synchronisable.cc @ 2207

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

cant remember ;)

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