Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/Synchronisable.cc @ 1755

Last change on this file since 1755 was 1755, checked in by rgrieder, 16 years ago

merged gui back to trunk.
update the media repository!

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