Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/script_trigger/src/network/Synchronisable.cc @ 1967

Last change on this file since 1967 was 1293, checked in by scheusso, 17 years ago

merged changes from input & camera & network branch into trunk

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