Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network3/src/network/Synchronisable.cc @ 1200

Last change on this file since 1200 was 1199, checked in by scheusso, 17 years ago

diffing work now: wohoo

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