Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2378 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
RevLine 
[1505]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
[1837]43#include <cstring>
[2087]44#include <string>
[1505]45#include <iostream>
[1735]46#include <assert.h>
[1505]47
48#include "core/CoreIncludes.h"
[1735]49#include "core/BaseObject.h"
[1534]50// #include "core/Identifier.h"
[1505]51
[2211]52#include "network/Host.h"
[2171]53namespace orxonox
[1505]54{
[1639]55
[2309]56  std::map<uint32_t, Synchronisable *> Synchronisable::objectMap_;
57  std::queue<uint32_t> Synchronisable::deletedObjects_;
[1639]58
[2171]59  uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
[1639]60
[1505]61  /**
62  * Constructor:
[1907]63  * Initializes all Variables and sets the right objectID
[1505]64  */
[2171]65  Synchronisable::Synchronisable(BaseObject* creator){
[1505]66    RegisterRootObject(Synchronisable);
[1907]67    static uint32_t idCounter=0;
[1751]68    objectFrequency_=1;
[1907]69    objectMode_=0x1; // by default do not send data to server
[2171]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;
[2309]78    classID = static_cast<uint32_t>(-1);
[2087]79
[2171]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
[2087]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    }
[1505]105  }
106
[1907]107  /**
[2087]108   * Destructor:
[1907]109   * Delete all callback objects and remove objectID from the objectMap_
110   */
[1505]111  Synchronisable::~Synchronisable(){
[1534]112    // delete callback function objects
[2171]113    if(!Identifier::isCreatingHierarchy()){
[2245]114      for(std::list<SynchronisableVariableBase*>::iterator it = syncList.begin(); it!=syncList.end(); it++)
115        delete (*it);
[2171]116      if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer()))
[2087]117        deletedObjects_.push(objectID);
[1907]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    }
[2309]123    std::map<uint32_t, Synchronisable*>::iterator it;
[2171]124    it = objectMap_.find(objectID);
125    if (it != objectMap_.end())
126      objectMap_.erase(it);
[1505]127  }
[1639]128
[2087]129
[1907]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   */
[1505]134  void Synchronisable::setClient(bool b){
135    if(b) // client
136      state_=0x2;
137    else  // server
138      state_=0x1;
139  }
[1856]140
[1907]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   */
[2171]148  Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode)
[1735]149  {
[1907]150    synchronisableHeader *header = (synchronisableHeader *)mem;
[1856]151
[2087]152    if(!header->dataAvailable)
153    {
154      mem += header->size;
155      return 0;
156    }
[2171]157
[2087]158    COUT(4) << "fabricating object with id: " << header->objectID << std::endl;
[1856]159
[2171]160    Identifier* id = ClassByID(header->classID);
[1907]161    assert(id);
[2171]162    BaseObject* creator = 0;
[2087]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
[2171]172        creator = dynamic_cast<BaseObject*>(synchronisable_creator);
[2087]173    }
[2171]174    assert(getSynchronisable(header->objectID)==0);   //make sure no object with this id exists
175    BaseObject *bo = id->fabricate(creator);
[2087]176    assert(bo);
[1735]177    Synchronisable *no = dynamic_cast<Synchronisable *>(bo);
178    assert(no);
[1907]179    no->objectID=header->objectID;
[2087]180    no->creatorID=header->creatorID; //TODO: remove this
[1907]181    no->classID=header->classID;
[2087]182    COUT(4) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl;
[1735]183          // update data and create object/entity...
[2087]184    bool b = no->updateData(mem, mode, true);
[1907]185    assert(b);
[2087]186    if (b)
187    {
[2245]188//        b = no->create();
[2087]189        assert(b);
190    }
[1907]191    return no;
192  }
193
[2087]194
[1907]195  /**
196   * Finds and deletes the Synchronisable with the appropriate objectID
197   * @param objectID objectID of the Synchronisable
198   * @return true/false
199   */
[2309]200  bool Synchronisable::deleteObject(uint32_t objectID){
[1907]201//     assert(getSynchronisable(objectID));
202    if(!getSynchronisable(objectID))
[1735]203      return false;
[1907]204    assert(getSynchronisable(objectID)->objectID==objectID);
205//     delete objectMap_[objectID];
206    Synchronisable *s = getSynchronisable(objectID);
207    if(s)
208      delete s;
209    else
[1735]210      return false;
211    return true;
212  }
[2087]213
[1907]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   */
[2309]219  Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID){
220    std::map<uint32_t, Synchronisable*>::iterator it1;
[2171]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      }
[1907]231    }
232    return NULL;
233  }
234
[2087]235
[1505]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
[2171]241  * @param t the type of the variable (DATA or STRING
[1907]242  * @param mode same as in getData
243  * @param cb callback object that should get called, if the value of the variable changes
[1505]244  */
[2245]245
246/*  void Synchronisable::registerVariable(void *var, int size, variableType t, uint8_t mode, NetworkCallbackBase *cb){
[2087]247    assert( mode==direction::toclient || mode==direction::toserver || mode==direction::serverMaster || mode==direction::clientMaster);
[1505]248    // create temporary synch.Var struct
249    synchronisableVariable *temp = new synchronisableVariable;
250    temp->size = size;
251    temp->var = var;
[1639]252    temp->mode = mode;
[1505]253    temp->type = t;
[1534]254    temp->callback = cb;
[2087]255    if( ( mode & direction::bidirectional ) )
256    {
[2171]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      }
[2087]266      temp->varReference = 0;
267    }
[1639]268    COUT(5) << "Syncronisable::registering var with size: " << temp->size << " and type: " << temp->type << std::endl;
[1505]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);
[1907]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
[2245]279  }*/
280 
[1856]281
[1735]282  /**
[1907]283   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory
[1735]284   * takes a pointer to already allocated memory (must have at least getSize bytes length)
[1751]285   * structure of the bitstream:
[1907]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
[2087]294   * @return true: if !doSync or if everything was successfully saved
[1735]295   */
[2309]296  bool Synchronisable::getData(uint8_t*& mem, int32_t id, uint8_t mode){
[2171]297    if(mode==0x0)
298      mode=state_;
[1907]299    //if this tick is we dont synchronise, then abort now
[2171]300    if(!doSync(id, mode))
[1907]301      return true;
[1735]302    //std::cout << "inside getData" << std::endl;
[2309]303    uint32_t tempsize = 0;
[2316]304    if (this->classID==0)
[1735]305      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
[2087]306
[2309]307    if (this->classID == static_cast<uint32_t>(-1))
[2087]308        this->classID = this->getIdentifier()->getNetworkID();
309
[1907]310    assert(this->classID==this->getIdentifier()->getNetworkID());
311//     this->classID=this->getIdentifier()->getNetworkID(); // TODO: correct this
[2245]312    std::list<SynchronisableVariableBase*>::iterator i;
[2309]313    uint32_t size;
[1907]314    size=getSize(id, mode);
[1856]315
[1735]316    // start copy header
[1907]317    synchronisableHeader *header = (synchronisableHeader *)mem;
318    header->size = size;
319    header->objectID = this->objectID;
[2087]320    header->creatorID = this->creatorID;
[1907]321    header->classID = this->classID;
322    header->dataAvailable = true;
[2171]323    tempsize += sizeof(synchronisableHeader);
324    mem += sizeof(synchronisableHeader);
[1735]325    // end copy header
[1856]326
327
[1735]328    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << " length: " << size << std::endl;
329    // copy to location
[2245]330    for(i=syncList.begin(); i!=syncList.end(); ++i){
331      (*i)->getData( mem, mode );
332      tempsize += (*i)->getSize( mode );
[1735]333    }
334    assert(tempsize==size);
335    return true;
336  }
[1505]337
[1856]338
[1505]339  /**
[1907]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
[1735]343   * @return true/false
344   */
[2171]345  bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback){
[1735]346    if(mode==0x0)
347      mode=state_;
[2245]348    std::list<SynchronisableVariableBase *>::iterator i;
[2171]349    //assert(objectMode_!=0x0);
350    //assert( (mode ^ objectMode_) != 0);
[2245]351    if(syncList.empty()){
[1735]352      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
353      return false;
354    }
[1856]355
[2245]356    uint8_t* data=mem;
[1735]357    // start extract header
[1907]358    synchronisableHeader *syncHeader = (synchronisableHeader *)mem;
359    assert(syncHeader->objectID==this->objectID);
[2171]360    assert(syncHeader->creatorID==this->creatorID);
361    assert(this->classID==syncHeader->classID); //TODO: fix this!!! maybe a problem with the identifier ?
[1907]362    if(syncHeader->dataAvailable==false){
[2171]363      mem += syncHeader->size;
[1751]364      return true;
[1907]365    }
[1856]366
[2171]367    mem += sizeof(synchronisableHeader);
[1907]368    // stop extract header
[2087]369
[1907]370    COUT(5) << "Synchronisable: objectID " << syncHeader->objectID << ", classID " << syncHeader->classID << " size: " << syncHeader->size << " synchronising data" << std::endl;
[2245]371    for(i=syncList.begin(); i!=syncList.end() && mem <= data+syncHeader->size; i++)
372    {
373      (*i)->putData( mem, mode, forceCallback );
[1735]374    }
[2171]375    assert(mem == data+syncHeader->size);
[1735]376    return true;
377  }
[1505]378
379  /**
380  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
[1907]381  * @param id id of the gamestate
382  * @param mode same as getData
[1505]383  * @return amount of bytes
384  */
[2309]385  uint32_t Synchronisable::getSize(int32_t id, uint8_t mode){
[1907]386    int tsize=sizeof(synchronisableHeader);
[1505]387    if(mode==0x0)
388      mode=state_;
[2171]389    if(!doSync(id, mode))
390      return 0;
[2245]391    std::list<SynchronisableVariableBase*>::iterator i;
392    for(i=syncList.begin(); i!=syncList.end(); i++){
393      tsize += (*i)->getSize( mode );
[1505]394    }
395    return tsize;
396  }
[1639]397
[1735]398  /**
[1907]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
[1735]402   */
[2309]403  bool Synchronisable::doSync(int32_t id, uint8_t mode){
[2171]404    if(mode==0x0)
405      mode=state_;
[2245]406    return ( (objectMode_&mode)!=0 && (!syncList.empty() ) );
[1735]407  }
[1856]408
[2309]409  bool Synchronisable::doSelection(int32_t id){
[2171]410    return true; //TODO: change this
411    //return ( id==0 || id%objectFrequency_==objectID%objectFrequency_ ) && ((objectMode_&state_)!=0);
[1751]412  }
[1856]413
[1907]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)
[1735]419  {
[1907]420    synchronisableHeader *header = (synchronisableHeader *)mem;
421    assert(header->objectID==this->objectID);
422    return header->dataAvailable;
[1735]423  }
[1856]424
[1907]425  /**
426   * This function sets the synchronisation mode of the object
[2171]427   * If set to 0x0 variables will not be synchronised at all
[1907]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   */
[2171]433  void Synchronisable::setObjectMode(uint8_t mode){
[2087]434    assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3);
[1751]435    objectMode_=mode;
[1505]436  }
[2245]437 
[1505]438
439}
Note: See TracBrowser for help on using the repository browser.