Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/network/Synchronisable.cc @ 1908

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

merged network branch back to trunk

  • Property svn:eol-style set to native
File size: 10.2 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
46#include "core/CoreIncludes.h"
47// #include "core/Identifier.h"
48
49namespace network
50{
51 
52 
53  int Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
54 
55  /**
56  * Constructor:
57  * calls registarAllVariables, that has to be implemented by the inheriting classID
58  */
59  Synchronisable::Synchronisable(){
60    RegisterRootObject(Synchronisable);
61    static int idCounter=0;
62    datasize=0;
63    objectID=idCounter++;
64    syncList = new std::list<synchronisableVariable *>;
65    //registerAllVariables();
66  }
67
68  Synchronisable::~Synchronisable(){
69    // delete callback function objects
70    if(!orxonox::Identifier::isCreatingHierarchy())
71      for(std::list<synchronisableVariable *>::iterator it = syncList->begin(); it!=syncList->end(); it++)
72        delete (*it)->callback;
73  }
74 
75  bool Synchronisable::create(){
76    this->classID = this->getIdentifier()->getNetworkID();
77    COUT(4) << "creating synchronisable: setting classid from " << this->getIdentifier()->getName() << " to: " << classID << std::endl;
78    return true;
79  }
80 
81  void Synchronisable::setClient(bool b){
82    if(b) // client
83      state_=0x2;
84    else  // server
85      state_=0x1;
86  }
87
88  /**
89  * This function is used to register a variable to be synchronized
90  * also counts the total datasize needed to save the variables
91  * @param var pointer to the variable
92  * @param size size of the datatype the variable consists of
93  */
94  void Synchronisable::registerVar(void *var, int size, variableType t, int mode, NetworkCallbackBase *cb){
95    // create temporary synch.Var struct
96    synchronisableVariable *temp = new synchronisableVariable;
97    temp->size = size;
98    temp->var = var;
99    temp->mode = mode; 
100    temp->type = t;
101    temp->callback = cb;
102    COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl; 
103    // increase datasize
104    datasize+=sizeof(int)+size;
105    //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl;
106    COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl;
107    syncList->push_back(temp);
108  }
109
110  /**
111  * note: only use this function for debug use, because it's inefficient (in order to produce a gamestate, you have to copy the whole data again to another memory location after this process)
112  * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
113  * structure of the bitstream:
114  * (var1_size,var1,var2_size,var2,...)
115  * varx_size: size = sizeof(int)
116  * varx: size = varx_size
117  * @return data containing all variables and their sizes
118  */
119  // syncData Synchronisable::getData(){
120  //   std::list<synchronisableVariable>::iterator i;
121  //   int totalsize=0;
122  //   //figure out size of data to be allocated
123  //   for(i=syncList->begin(); i!=syncList->end(); i++){
124  //     // increase size (size of variable and size of size of variable ;)
125  //     if(i->type == STRING)
126  //       totalsize+=sizeof(int)+((std::string *)i->var)->length()+1;
127  //     else
128  //       totalsize+=sizeof(int)+i->size;
129  //   }
130  //   syncData retVal;
131  //   retVal.objectID=this->objectID;
132  //   retVal.classID=this->classID;
133  //   retVal.length=totalsize;
134  //   // allocate memory
135  //   retVal.data = (unsigned char *)malloc(totalsize);
136  //   // copy to location
137  //   //CHANGED: REMOVED DECLARATION int n=0 FROM LOOP
138  //   int n=0;
139  //   for(i=syncList->begin(); n<totalsize && i!=syncList->end(); i++){
140  //     std::memcpy(retVal.data+n, (const void*)(i->size), sizeof(int));
141  //     n+=sizeof(int);
142  //     switch(i->type){
143  //     case STRING:
144  //       std::memcpy(retVal.data+n, (const void *)(((std::string *)i->var)->c_str()), ((std::string *)i->var)->length()+1);
145  //       n+=((std::string *)i->var)->length()+1;
146  //       break;
147  //     case DATA:
148  //       std::memcpy(retVal.data+n, ((const void*)i->var), i->size);
149  //       n+=i->size;
150  //       break;
151  //     }
152  //   }
153  //   return retVal;
154  // }
155  /**
156  * This function takes all SynchronisableVariables out of the Synchronisable and saves it into a syncData struct
157  * Difference to the above function:
158  * takes a pointer to already allocated memory (must have at least getSize bytes length)
159  * structure of the bitstream:
160  * (var1_size,var1,var2_size,var2,...)
161  * varx_size: size = sizeof(int)
162  * varx: size = varx_size
163  * @return data containing all variables and their sizes
164  */
165  syncData Synchronisable::getData(unsigned char *mem, int mode){
166    //std::cout << "inside getData" << std::endl;
167    if(mode==0x0)
168      mode=state_;
169    if(classID==0)
170      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
171    this->classID=this->getIdentifier()->getNetworkID();
172    std::list<synchronisableVariable *>::iterator i;
173    syncData retVal;
174    retVal.objectID=this->objectID;
175    retVal.classID=this->classID;
176    retVal.length=getSize();
177    COUT(5) << "Synchronisable getting data from objectID: " << retVal.objectID << " classID: " << retVal.classID << " length: " << retVal.length << std::endl;
178    retVal.data=mem;
179    // copy to location
180    int n=0; //offset
181    for(i=syncList->begin(); n<datasize && i!=syncList->end(); ++i){
182      //(std::memcpy(retVal.data+n, (const void*)(&(i->size)), sizeof(int));
183      if( ((*i)->mode & mode) == 0 ){
184        COUT(5) << "not getting data: " << std::endl;
185        continue;  // this variable should only be received
186      }
187      switch((*i)->type){
188      case DATA:
189        std::memcpy( (void *)(retVal.data+n), (void*)((*i)->var), (*i)->size);
190        n+=(*i)->size;
191        break;
192      case STRING:
193        memcpy( (void *)(retVal.data+n), (void *)&((*i)->size), sizeof(int) );
194        n+=sizeof(int);
195        const char *data = ( ( *(std::string *) (*i)->var).c_str());
196        std::memcpy( retVal.data+n, (void*)data, (*i)->size);
197        COUT(5) << "synchronisable: char: " << (const char *)(retVal.data+n) << " data: " << data << " string: " << *(std::string *)((*i)->var) << std::endl;
198        n+=(*i)->size;
199        break;
200      }
201    }
202    return retVal;
203  }
204
205  /**
206  * This function takes a syncData struct and takes it to update the variables
207  * @param vars data of the variables
208  * @return true/false
209  */
210  bool Synchronisable::updateData(syncData vars, int mode){
211    if(mode==0x0)
212      mode=state_;
213    unsigned char *data=vars.data;
214    std::list<synchronisableVariable *>::iterator i;
215    if(syncList->empty()){
216      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
217      return false;
218    }
219    COUT(5) << "Synchronisable: objectID " << vars.objectID << ", classID " << vars.classID << " size: " << vars.length << " synchronising data" << std::endl;
220    for(i=syncList->begin(); i!=syncList->end(); i++){
221      if( ((*i)->mode ^ mode) == 0 ){
222        COUT(5) << "synchronisable: not updating variable " << std::endl;
223        continue;  // this variable should only be set
224      }
225      COUT(5) << "Synchronisable: element size: " << (*i)->size << " type: " << (*i)->type << std::endl;
226      bool callback=false;
227      switch((*i)->type){
228      case DATA:
229        if((*i)->callback) // check whether this variable changed (but only if callback was set)
230          if(strncmp((char *)(*i)->var, (char *)data, (*i)->size)!=0)
231            callback=true;
232        memcpy((void*)(*i)->var, data, (*i)->size);
233        data+=(*i)->size;
234        break;
235      case STRING:
236        (*i)->size = *(int *)data;
237        COUT(5) << "string size: " << (*i)->size << std::endl;
238        data+=sizeof(int);
239        if((*i)->callback) // check whether this string changed
240          if( *(std::string *)((*i)->var) != std::string((char *)data) )
241            callback=true;
242        *((std::string *)((*i)->var)) = std::string((const char*)data);
243        COUT(5) << "synchronisable: char: " << (const char*)data << " string: " << std::string((const char*)data) << std::endl;
244        data += (*i)->size;
245        break;
246      }
247      // call the callback function, if defined
248      if(callback)
249        (*i)->callback->call();
250    }
251    return true;
252  }
253
254  /**
255  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
256  * @return amount of bytes
257  */
258  int Synchronisable::getSize(int mode){
259    int tsize=0;
260    if(mode==0x0)
261      mode=state_;
262    std::list<synchronisableVariable *>::iterator i;
263    for(i=syncList->begin(); i!=syncList->end(); i++){
264      if( ((*i)->mode & mode) == 0 )
265        continue;  // this variable should only be received, so dont add its size to the send-size
266      switch((*i)->type){
267      case DATA:
268        tsize+=(*i)->size;
269        break;
270      case STRING:
271        tsize+=sizeof(int);
272        (*i)->size=((std::string *)(*i)->var)->length()+1;
273        COUT(5) << "String size: " << (*i)->size << std::endl;
274        tsize+=(*i)->size;
275        break;
276      }
277    }
278    return tsize;
279  }
280 
281  void Synchronisable::setBacksync(bool sync){
282    backsync_=sync;
283  }
284
285  bool Synchronisable::getBacksync(){
286    return backsync_;
287  }
288 
289}
Note: See TracBrowser for help on using the repository browser.