Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/network/synchronisable/Synchronisable.cc @ 2382

Last change on this file since 2382 was 2371, checked in by scheusso, 16 years ago

merged network branch to presentation branch

  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/network/src/network/synchronisable/Synchronisable.ccmergedeligible
    /code/branches/ceguilua/src/network/Synchronisable.cc1802-1808
    /code/branches/core3/src/network/Synchronisable.cc1572-1739
    /code/branches/gcc43/src/network/Synchronisable.cc1580
    /code/branches/gui/src/network/Synchronisable.cc1635-1723
    /code/branches/input/src/network/Synchronisable.cc1629-1636
    /code/branches/objecthierarchy/src/network/Synchronisable.cc1911-2085,​2100,​2110-2169
    /code/branches/pickups/src/network/Synchronisable.cc1926-2086
    /code/branches/questsystem/src/network/Synchronisable.cc1894-2088
    /code/branches/script_trigger/src/network/Synchronisable.cc1295-1953,​1955
    /code/branches/weapon/src/network/Synchronisable.cc1925-2094
File size: 14.7 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 <cstring>
44#include <string>
45#include <iostream>
46#include <assert.h>
47
48#include "core/CoreIncludes.h"
49#include "core/BaseObject.h"
50// #include "core/Identifier.h"
51
52#include "network/Host.h"
53namespace orxonox
54{
55
56  std::map<uint32_t, Synchronisable *> Synchronisable::objectMap_;
57  std::queue<uint32_t> Synchronisable::deletedObjects_;
58
59  uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
60
61  /**
62  * Constructor:
63  * Initializes all Variables and sets the right objectID
64  */
65  Synchronisable::Synchronisable(BaseObject* creator){
66    RegisterRootObject(Synchronisable);
67    static uint32_t idCounter=0;
68    objectFrequency_=1;
69    objectMode_=0x1; // by default do not send data to server
70    if ( !Host::running() || ( Host::running() && Host::isServer() ) )
71    {
72      this->objectID = idCounter++; //this is only needed when running a server
73    //add synchronisable to the objectMap
74      objectMap_[this->objectID] = this;
75    }
76    else
77      objectID=OBJECTID_UNKNOWN;
78    classID = static_cast<uint32_t>(-1);
79
80
81#ifndef NDEBUG
82    ObjectList<Synchronisable>::iterator it;
83    for(it = ObjectList<Synchronisable>::begin(); it!=ObjectList<Synchronisable>::end(); ++it){
84      if( it->getObjectID()==this->objectID )
85        assert(*it==this || (it->objectID==OBJECTID_UNKNOWN && it->objectMode_==0x0));
86    }
87#endif
88
89    this->creatorID = OBJECTID_UNKNOWN;
90
91    searchcreatorID:
92    if (creator)
93    {
94        Synchronisable* synchronisable_creator = dynamic_cast<Synchronisable*>(creator);
95        if (synchronisable_creator && synchronisable_creator->objectMode_)
96        {
97            this->creatorID = synchronisable_creator->getObjectID();
98        }
99        else if (creator != creator->getCreator())
100        {
101            creator = creator->getCreator();
102            goto searchcreatorID;
103        }
104    }
105  }
106
107  /**
108   * Destructor:
109   * Delete all callback objects and remove objectID from the objectMap_
110   */
111  Synchronisable::~Synchronisable(){
112    // delete callback function objects
113    if(!Identifier::isCreatingHierarchy()){
114      for(std::list<SynchronisableVariableBase*>::iterator it = syncList.begin(); it!=syncList.end(); it++)
115        delete (*it);
116      if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer()))
117        deletedObjects_.push(objectID);
118//       COUT(3) << "destruct synchronisable +++" << objectID << " | " << classID << std::endl;
119//       COUT(3) << " bump ---" << objectID << " | " << &objectMap_ << std::endl;
120//       assert(objectMap_[objectID]->objectID==objectID);
121//       objectMap_.erase(objectID);
122    }
123    std::map<uint32_t, Synchronisable*>::iterator it;
124    it = objectMap_.find(objectID);
125    if (it != objectMap_.end())
126      objectMap_.erase(it);
127  }
128
129
130  /**
131   * This function sets the internal mode for synchronisation
132   * @param b true if this object is located on a client or on a server
133   */
134  void Synchronisable::setClient(bool b){
135    if(b) // client
136      state_=0x2;
137    else  // server
138      state_=0x1;
139  }
140
141  /**
142   * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create
143   * After calling this function the mem pointer will be increased by the size of the needed data
144   * @param mem pointer to where the appropriate data is located
145   * @param mode defines the mode, how the data should be loaded
146   * @return pointer to the newly created synchronisable
147   */
148  Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode)
149  {
150    synchronisableHeader *header = (synchronisableHeader *)mem;
151
152    if(!header->dataAvailable)
153    {
154      mem += header->size;
155      return 0;
156    }
157
158    COUT(4) << "fabricating object with id: " << header->objectID << std::endl;
159
160    Identifier* id = ClassByID(header->classID);
161    assert(id);
162    BaseObject* creator = 0;
163    if (header->creatorID != OBJECTID_UNKNOWN)
164    {
165      Synchronisable* synchronisable_creator = Synchronisable::getSynchronisable(header->creatorID);
166      if (!synchronisable_creator)
167      {
168        mem += header->size; //.TODO: this suckz.... remove size from header
169        return 0;
170      }
171      else
172        creator = dynamic_cast<BaseObject*>(synchronisable_creator);
173    }
174    assert(getSynchronisable(header->objectID)==0);   //make sure no object with this id exists
175    BaseObject *bo = id->fabricate(creator);
176    assert(bo);
177    Synchronisable *no = dynamic_cast<Synchronisable *>(bo);
178    assert(no);
179    no->objectID=header->objectID;
180    no->creatorID=header->creatorID; //TODO: remove this
181    no->classID=header->classID;
182    COUT(4) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl;
183          // update data and create object/entity...
184    bool b = no->updateData(mem, mode, true);
185    assert(b);
186    if (b)
187    {
188//        b = no->create();
189        assert(b);
190    }
191    return no;
192  }
193
194
195  /**
196   * Finds and deletes the Synchronisable with the appropriate objectID
197   * @param objectID objectID of the Synchronisable
198   * @return true/false
199   */
200  bool Synchronisable::deleteObject(uint32_t objectID){
201//     assert(getSynchronisable(objectID));
202    if(!getSynchronisable(objectID))
203      return false;
204    assert(getSynchronisable(objectID)->objectID==objectID);
205//     delete objectMap_[objectID];
206    Synchronisable *s = getSynchronisable(objectID);
207    if(s)
208      delete s;
209    else
210      return false;
211    return true;
212  }
213
214  /**
215   * This function looks up the objectID in the objectMap_ and returns a pointer to the right Synchronisable
216   * @param objectID objectID of the Synchronisable
217   * @return pointer to the Synchronisable with the objectID
218   */
219  Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID){
220    std::map<uint32_t, Synchronisable*>::iterator it1;
221    it1 = objectMap_.find(objectID);
222    if (it1 != objectMap_.end())
223      return it1->second;
224
225    ObjectList<Synchronisable>::iterator it;
226    for(it = ObjectList<Synchronisable>::begin(); it; ++it){
227      if( it->getObjectID()==objectID ){
228        objectMap_[objectID] = *it;
229        return *it;
230      }
231    }
232    return NULL;
233  }
234
235
236  /**
237  * This function is used to register a variable to be synchronized
238  * also counts the total datasize needed to save the variables
239  * @param var pointer to the variable
240  * @param size size of the datatype the variable consists of
241  * @param t the type of the variable (DATA or STRING
242  * @param mode same as in getData
243  * @param cb callback object that should get called, if the value of the variable changes
244  */
245
246/*  void Synchronisable::registerVariable(void *var, int size, variableType t, uint8_t mode, NetworkCallbackBase *cb){
247    assert( mode==direction::toclient || mode==direction::toserver || mode==direction::serverMaster || mode==direction::clientMaster);
248    // create temporary synch.Var struct
249    synchronisableVariable *temp = new synchronisableVariable;
250    temp->size = size;
251    temp->var = var;
252    temp->mode = mode;
253    temp->type = t;
254    temp->callback = cb;
255    if( ( mode & direction::bidirectional ) )
256    {
257      if(t!=STRING)
258      {
259        temp->varBuffer = new uint8_t[size];
260        memcpy(temp->varBuffer, temp->var, size); //now fill the buffer for the first time
261      }
262      else
263      {
264        temp->varBuffer=new std::string( *static_cast<std::string*>(var) );
265      }
266      temp->varReference = 0;
267    }
268    COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl;
269    //std::cout << "push temp to syncList (at the bottom) " << datasize << std::endl;
270    COUT(5) << "Syncronisable::objectID: " << objectID << " this: " << this << " name: " << this->getIdentifier()->getName() << " networkID: " << this->getIdentifier()->getNetworkID() << std::endl;
271    syncList->push_back(temp);
272#ifndef NDEBUG
273    std::list<synchronisableVariable *>::iterator it = syncList->begin();
274    while(it!=syncList->end()){
275      assert(*it!=var);
276      it++;
277    }
278#endif
279  }*/
280 
281
282  /**
283   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory
284   * takes a pointer to already allocated memory (must have at least getSize bytes length)
285   * structure of the bitstream:
286   * |totalsize,objectID,classID,var1,var2,string1_length,string1,var3,...|
287   * length of varx: size saved int syncvarlist
288   * @param mem pointer to allocated memory with enough size
289   * @param id gamestateid of the gamestate to be saved (important for priorities)
290   * @param mode defines the direction in which the data will be send/received
291   *             0x1: server->client
292   *             0x2: client->server (not recommended)
293   *             0x3: bidirectional
294   * @return true: if !doSync or if everything was successfully saved
295   */
296  bool Synchronisable::getData(uint8_t*& mem, int32_t id, uint8_t mode){
297    if(mode==0x0)
298      mode=state_;
299    //if this tick is we dont synchronise, then abort now
300    if(!doSync(id, mode))
301      return true;
302    //std::cout << "inside getData" << std::endl;
303    uint32_t tempsize = 0;
304    if (this->classID==0)
305      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
306
307    if (this->classID == static_cast<uint32_t>(-1))
308        this->classID = this->getIdentifier()->getNetworkID();
309
310    assert(this->classID==this->getIdentifier()->getNetworkID());
311//     this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this
312    std::list<SynchronisableVariableBase*>::iterator i;
313    uint32_t size;
314    size=getSize(id, mode);
315
316    // start copy header
317    synchronisableHeader *header = (synchronisableHeader *)mem;
318    header->size = size;
319    header->objectID = this->objectID;
320    header->creatorID = this->creatorID;
321    header->classID = this->classID;
322    header->dataAvailable = true;
323    tempsize += sizeof(synchronisableHeader);
324    mem += sizeof(synchronisableHeader);
325    // end copy header
326
327
328    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl;
329    // copy to location
330    for(i=syncList.begin(); i!=syncList.end(); ++i){
331      (*i)->getData( mem, mode );
332      tempsize += (*i)->getSize( mode );
333    }
334    assert(tempsize==size);
335    return true;
336  }
337
338
339  /**
340   * This function takes a bytestream and loads the data into the registered variables
341   * @param mem pointer to the bytestream
342   * @param mode same as in getData
343   * @return true/false
344   */
345  bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback){
346    if(mode==0x0)
347      mode=state_;
348    std::list<SynchronisableVariableBase *>::iterator i;
349    //assert(objectMode_!=0x0);
350    //assert( (mode ^ objectMode_) != 0);
351    if(syncList.empty()){
352      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
353      return false;
354    }
355
356    uint8_t* data=mem;
357    // start extract header
358    synchronisableHeader *syncHeader = (synchronisableHeader *)mem;
359    assert(syncHeader->objectID==this->objectID);
360    assert(syncHeader->creatorID==this->creatorID);
361    assert(this->classID==syncHeader->classID); //TODO: fix this!!! maybe a problem with the identifier ?
362    if(syncHeader->dataAvailable==false){
363      mem += syncHeader->size;
364      return true;
365    }
366
367    mem += sizeof(synchronisableHeader);
368    // stop extract header
369
370    COUT(5) << "Synchronisable: objectID " << syncHeader->objectID << ", classID " << syncHeader->classID << " size: " << syncHeader->size << " synchronising data" << std::endl;
371    for(i=syncList.begin(); i!=syncList.end() && mem <= data+syncHeader->size; i++)
372    {
373      (*i)->putData( mem, mode, forceCallback );
374    }
375    assert(mem == data+syncHeader->size);
376    return true;
377  }
378
379  /**
380  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
381  * @param id id of the gamestate
382  * @param mode same as getData
383  * @return amount of bytes
384  */
385  uint32_t Synchronisable::getSize(int32_t id, uint8_t mode){
386    int tsize=sizeof(synchronisableHeader);
387    if(mode==0x0)
388      mode=state_;
389    if(!doSync(id, mode))
390      return 0;
391    std::list<SynchronisableVariableBase*>::iterator i;
392    for(i=syncList.begin(); i!=syncList.end(); i++){
393      tsize += (*i)->getSize( mode );
394    }
395    return tsize;
396  }
397
398  /**
399   * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction)
400   * @param id gamestate id
401   * @return true/false
402   */
403  bool Synchronisable::doSync(int32_t id, uint8_t mode){
404    if(mode==0x0)
405      mode=state_;
406    return ( (objectMode_&mode)!=0 && (!syncList.empty() ) );
407  }
408
409  bool Synchronisable::doSelection(int32_t id){
410    return true; //TODO: change this
411    //return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0);
412  }
413
414  /**
415   * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones
416   * @param mem pointer to the bytestream
417   */
418  bool Synchronisable::isMyData(uint8_t* mem)
419  {
420    synchronisableHeader *header = (synchronisableHeader *)mem;
421    assert(header->objectID==this->objectID);
422    return header->dataAvailable;
423  }
424
425  /**
426   * This function sets the synchronisation mode of the object
427   * If set to 0x0 variables will not be synchronised at all
428   * If set to 0x1 variables will only be synchronised to the client
429   * If set to 0x2 variables will only be synchronised to the server
430   * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar)
431   * @param mode same as in registerVar
432   */
433  void Synchronisable::setObjectMode(uint8_t mode){
434    assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3);
435    objectMode_=mode;
436  }
437 
438
439}
Note: See TracBrowser for help on using the repository browser.