Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/bugger/src/network/synchronisable/Synchronisable.cc @ 2532

Last change on this file since 2532 was 2531, checked in by rgrieder, 17 years ago

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