Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/network/synchronisable/Synchronisable.cc @ 7632

Last change on this file since 7632 was 7297, checked in by landauf, 14 years ago

fixed lots of Doxygen warnings

Note: Doxygen prints a warning if only a part of the parameters of a function are documented.

Added documentation for missing parameters (as good as I could), removed documentation of obsolete parameters and fixed names of renamed parameters.
Some parameters are tagged with "FIXME", please replace this with an appropriate documentation if you know what it does.

  • Property svn:eol-style set to native
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#include "Synchronisable.h"
32
[3214]33#include <cstdlib>
[1505]34#include "core/CoreIncludes.h"
[5742]35#include "core/GameMode.h"
[1735]36#include "core/BaseObject.h"
[3214]37#include "network/Host.h"
[1505]38
[2171]39namespace orxonox
[1505]40{
[1639]41
[2309]42  std::map<uint32_t, Synchronisable *> Synchronisable::objectMap_;
43  std::queue<uint32_t> Synchronisable::deletedObjects_;
[1639]44
[2171]45  uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
[1639]46
[1505]47  /**
48  * Constructor:
[5929]49  * Initializes all Variables and sets the right objectID_
[1505]50  */
[7163]51  Synchronisable::Synchronisable(BaseObject* creator )
52  {
[1505]53    RegisterRootObject(Synchronisable);
[1907]54    static uint32_t idCounter=0;
55    objectMode_=0x1; // by default do not send data to server
[7163]56    if ( GameMode::isMaster()/* || ( Host::running() && Host::isServer() )*/ )
[2171]57    {
[5929]58      this->setObjectID( idCounter++ );
[2171]59    }
60    else
[5743]61    {
[5929]62      objectID_=OBJECTID_UNKNOWN;
[5743]63    }
[5929]64    classID_ = static_cast<uint32_t>(-1);
[6417]65
[3084]66    // set dataSize to 0
67    this->dataSize_ = 0;
[2415]68    // set standard priority
[3280]69    this->setPriority( Priority::Normal );
[2171]70
[2415]71    // get creator id
[5929]72    if( creator )
73      this->creatorID_ = creator->getSceneID();
74    else
75      this->creatorID_ = OBJECTID_UNKNOWN;
[1505]76  }
77
[1907]78  /**
[2087]79   * Destructor:
[5929]80   * Delete all callback objects and remove objectID_ from the objectMap_
[1907]81   */
[7163]82  Synchronisable::~Synchronisable()
83  {
[1534]84    // delete callback function objects
[2171]85    if(!Identifier::isCreatingHierarchy()){
[3198]86      // remove object from the static objectMap
87      if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer()))
[5929]88        deletedObjects_.push(objectID_);
[1907]89    }
[7163]90    // delete all Synchronisable Variables from syncList_ ( which are also in stringList_ )
91    for(std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin(); it!=syncList_.end(); it++)
[3304]92      delete (*it);
[7163]93    syncList_.clear();
94    stringList_.clear();
[7183]95    std::map<uint32_t, Synchronisable*>::iterator it2;
96    it2 = objectMap_.find(objectID_);
97    if (it2 != objectMap_.end())
98      objectMap_.erase(it2);
[2485]99
[1505]100  }
[1639]101
[2087]102
[1907]103  /**
104   * This function sets the internal mode for synchronisation
105   * @param b true if this object is located on a client or on a server
106   */
[7163]107  void Synchronisable::setClient(bool b)
108  {
[1505]109    if(b) // client
110      state_=0x2;
111    else  // server
112      state_=0x1;
113  }
[1856]114
[1907]115  /**
116   * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create
117   * After calling this function the mem pointer will be increased by the size of the needed data
118   * @param mem pointer to where the appropriate data is located
119   * @param mode defines the mode, how the data should be loaded
120   * @return pointer to the newly created synchronisable
121   */
[2171]122  Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode)
[1735]123  {
[2662]124    SynchronisableHeader header(mem);
[7163]125    assert( !header.isDiffed() );
[1856]126
[2662]127    COUT(4) << "fabricating object with id: " << header.getObjectID() << std::endl;
[1856]128
[2662]129    Identifier* id = ClassByID(header.getClassID());
[2485]130    if (!id)
131    {
[3088]132        for(int i = 0; i<160; i++)
[2759]133            COUT(0) << "classid: " << i << " identifier: " << ClassByID(i) << endl;
[2485]134        COUT(0) << "Assertion failed: id" << std::endl;
135        COUT(0) << "Possible reason for this error: Client received a synchronizable object whose class has no factory." << std::endl;
136        abort();
137    }
[1907]138    assert(id);
[2171]139    BaseObject* creator = 0;
[2662]140    if (header.getCreatorID() != OBJECTID_UNKNOWN)
[2087]141    {
[2662]142      Synchronisable* synchronisable_creator = Synchronisable::getSynchronisable(header.getCreatorID());
[2087]143      if (!synchronisable_creator)
144      {
[7163]145        mem += header.getDataSize()+SynchronisableHeader::getSize(); //.TODO: this suckz.... remove size from header
[2662]146        assert(0); // TODO: uncomment this if we have a clean objecthierarchy (with destruction of children of objects) ^^
[2087]147        return 0;
148      }
149      else
[3325]150        creator = orxonox_cast<BaseObject*>(synchronisable_creator);
[2087]151    }
[2662]152    assert(getSynchronisable(header.getObjectID())==0);   //make sure no object with this id exists
[2171]153    BaseObject *bo = id->fabricate(creator);
[2087]154    assert(bo);
[3325]155    Synchronisable *no = orxonox_cast<Synchronisable*>(bo);
[1735]156    assert(no);
[5929]157    assert( Synchronisable::objectMap_.find(header.getObjectID()) == Synchronisable::objectMap_.end() );
158    no->setObjectID(header.getObjectID());
159    //no->creatorID=header.getCreatorID(); //TODO: remove this
160    no->setClassID(header.getClassID());
161    assert(no->creatorID_ == header.getCreatorID());
[7163]162    if( creator )
163      bo->setLevel(creator->getLevel());          // Note: this ensures that the level is known on the client for child objects of the scene (and the scene itself)
[5929]164    //assert(no->classID_ == header.getClassID());
165    COUT(4) << "fabricate objectID_: " << no->objectID_ << " classID_: " << no->classID_ << std::endl;
[1735]166          // update data and create object/entity...
[2087]167    bool b = no->updateData(mem, mode, true);
[1907]168    assert(b);
[2087]169    if (b)
170    {
[2245]171//        b = no->create();
[2087]172        assert(b);
173    }
[1907]174    return no;
175  }
176
[2087]177
[1907]178  /**
[5929]179   * Finds and deletes the Synchronisable with the appropriate objectID_
180   * @param objectID_ objectID_ of the Synchronisable
[1907]181   * @return true/false
182   */
[7163]183  bool Synchronisable::deleteObject(uint32_t objectID_)
184  {
[5929]185    if(!getSynchronisable(objectID_))
[1735]186      return false;
[5929]187    assert(getSynchronisable(objectID_)->objectID_==objectID_);
188    Synchronisable *s = getSynchronisable(objectID_);
[1907]189    if(s)
[5929]190      s->destroy(); // or delete?
[1907]191    else
[1735]192      return false;
193    return true;
194  }
[2087]195
[1907]196  /**
[5929]197   * This function looks up the objectID_ in the objectMap_ and returns a pointer to the right Synchronisable
198   * @param objectID_ objectID_ of the Synchronisable
199   * @return pointer to the Synchronisable with the objectID_
[1907]200   */
[7163]201  Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID_)
202  {
[2309]203    std::map<uint32_t, Synchronisable*>::iterator it1;
[5929]204    it1 = objectMap_.find(objectID_);
[2171]205    if (it1 != objectMap_.end())
206      return it1->second;
[3198]207    // if the objects not in the map it should'nt exist at all anymore
[1907]208    return NULL;
209  }
210
[2087]211
[1505]212  /**
[5929]213   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID_ and classID_ to the given memory
[1735]214   * takes a pointer to already allocated memory (must have at least getSize bytes length)
[1751]215   * structure of the bitstream:
[5929]216   * |totalsize,objectID_,classID_,var1,var2,string1_length,string1,var3,...|
[1907]217   * length of varx: size saved int syncvarlist
218   * @param mem pointer to allocated memory with enough size
[7297]219   * @param sizes FIXME - add doc!
[1907]220   * @param id gamestateid of the gamestate to be saved (important for priorities)
221   * @param mode defines the direction in which the data will be send/received
222   *             0x1: server->client
223   *             0x2: client->server (not recommended)
224   *             0x3: bidirectional
[2087]225   * @return true: if !doSync or if everything was successfully saved
[1735]226   */
[7163]227  uint32_t Synchronisable::getData(uint8_t*& mem, std::vector<uint32_t>& sizes, int32_t id, uint8_t mode)
228  {
229    unsigned int test = 0;
[2171]230    if(mode==0x0)
231      mode=state_;
[1907]232    //if this tick is we dont synchronise, then abort now
[2171]233    if(!doSync(id, mode))
[3084]234      return 0;
[2309]235    uint32_t tempsize = 0;
[3304]236#ifndef NDEBUG
[7163]237    uint8_t* oldmem = mem;
[5929]238    if (this->classID_==0)
[1735]239      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
[3304]240#endif
[2087]241
[5929]242    if (this->classID_ == static_cast<uint32_t>(-1))
243        this->classID_ = this->getIdentifier()->getNetworkID();
[2087]244
[5929]245    assert(ClassByID(this->classID_));
246    assert(this->classID_==this->getIdentifier()->getNetworkID());
247    assert(this->objectID_!=OBJECTID_UNKNOWN);
[3084]248    std::vector<SynchronisableVariableBase*>::iterator i;
[1856]249
[1735]250    // start copy header
[2662]251    SynchronisableHeader header(mem);
252    mem += SynchronisableHeader::getSize();
[1735]253    // end copy header
[1856]254
[7163]255    CCOUT(5) << "getting data from objectID_: " << objectID_ << ", classID_: " << classID_ << std::endl;
256//     COUT(4) << "objectid: " << this->objectID_ << ":";
[1735]257    // copy to location
[7163]258    for(i=syncList_.begin(); i!=syncList_.end(); ++i)
259    {
260      uint32_t varsize = (*i)->getData( mem, mode );
261//       COUT(4) << " " << varsize;
262      tempsize += varsize;
263      sizes.push_back(varsize);
264      ++test;
[3084]265      //tempsize += (*i)->getSize( mode );
[1735]266    }
[7163]267//     COUT(4) << endl;
[6417]268
[5929]269    header.setObjectID( this->objectID_ );
270    header.setCreatorID( this->creatorID_ );
271    header.setClassID( this->classID_ );
[3084]272    header.setDataSize( tempsize );
[7163]273    assert( tempsize == mem-oldmem-SynchronisableHeader::getSize() );
274    assert( test == this->getNrOfVariables() );
275    header.setDiffed(false);
276    tempsize += SynchronisableHeader::getSize();
[6417]277
[3084]278#ifndef NDEBUG
279    uint32_t size;
280    size=getSize(id, mode);
[1735]281    assert(tempsize==size);
[3084]282#endif
283    return tempsize;
[1735]284  }
[1505]285
[1856]286
[1505]287  /**
[1907]288   * This function takes a bytestream and loads the data into the registered variables
289   * @param mem pointer to the bytestream
290   * @param mode same as in getData
[7297]291   * @param forceCallback FIXME - add doc!
[1735]292   * @return true/false
293   */
[7163]294  bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback)
295  {
[1735]296    if(mode==0x0)
297      mode=state_;
[7163]298    if(syncList_.empty())
299    {
[2419]300      assert(0);
[7163]301      COUT(2) << "Synchronisable::updateData syncList_ is empty" << std::endl;
[1735]302      return false;
303    }
[1856]304
[2245]305    uint8_t* data=mem;
[1735]306    // start extract header
[7163]307    SynchronisableHeaderLight syncHeaderLight(mem);
308    assert(syncHeaderLight.getObjectID()==this->getObjectID());
[1856]309
[5929]310    //COUT(5) << "Synchronisable: objectID_ " << syncHeader.getObjectID() << ", classID_ " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << std::endl;
[7163]311    if( !syncHeaderLight.isDiffed() )
[2245]312    {
[7163]313      SynchronisableHeader syncHeader2(mem);
314      assert( this->getClassID() == syncHeader2.getClassID() );
315      assert( this->getCreatorID() == syncHeader2.getCreatorID() );
316      mem += SynchronisableHeader::getSize();
317      std::vector<SynchronisableVariableBase *>::iterator i;
318      for(i=syncList_.begin(); i!=syncList_.end(); i++)
319      {
320        assert( mem <= data+syncHeader2.getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream
321        (*i)->putData( mem, mode, forceCallback );
322      }
323      assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeader::getSize() );
[1735]324    }
[7163]325    else
326    {
327      mem += SynchronisableHeaderLight::getSize();
328//       COUT(0) << "objectID: " << this->objectID_ << endl;
329      while( mem < data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() )
330      {
331        VariableID varID = *(VariableID*)mem;
332//         COUT(0) << "varID: " << varID << endl;
333        assert( varID < syncList_.size() );
334        mem += sizeof(VariableID);
335        syncList_[varID]->putData( mem, mode, forceCallback );
336      }
337      assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() );
338    }
[1735]339    return true;
340  }
[1505]341
342  /**
343  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
[1907]344  * @param id id of the gamestate
345  * @param mode same as getData
[1505]346  * @return amount of bytes
347  */
[7163]348  uint32_t Synchronisable::getSize(int32_t id, uint8_t mode)
349  {
350    uint32_t tsize=SynchronisableHeader::getSize();
[3084]351    if (mode==0x0)
[1505]352      mode=state_;
[3084]353    if (!doSync(id, mode))
[2171]354      return 0;
[3084]355    assert( mode==state_ );
356    tsize += this->dataSize_;
357    std::vector<SynchronisableVariableBase*>::iterator i;
[7163]358    for(i=stringList_.begin(); i!=stringList_.end(); ++i)
359    {
[2245]360      tsize += (*i)->getSize( mode );
[1505]361    }
362    return tsize;
363  }
[1639]364
[1735]365  /**
[1907]366   * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction)
367   * @param id gamestate id
[7297]368   * @param mode FIXME - add doc!
[1907]369   * @return true/false
[1735]370   */
[7163]371  bool Synchronisable::doSync(int32_t id, uint8_t mode)
372  {
[2171]373    if(mode==0x0)
374      mode=state_;
[7163]375    return ( (this->objectMode_ & mode)!=0 && (!syncList_.empty() ) );
[1735]376  }
[1856]377
[1907]378  /**
379   * This function sets the synchronisation mode of the object
[2171]380   * If set to 0x0 variables will not be synchronised at all
[1907]381   * If set to 0x1 variables will only be synchronised to the client
382   * If set to 0x2 variables will only be synchronised to the server
383   * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar)
384   * @param mode same as in registerVar
385   */
[7163]386  void Synchronisable::setSyncMode(uint8_t mode)
387  {
[2087]388    assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3);
[5929]389    this->objectMode_=mode;
[1505]390  }
391
[6417]392  template <> void Synchronisable::registerVariable( std::string& variable, uint8_t mode, NetworkCallbackBase *cb, bool bidirectional)
393  {
394    SynchronisableVariableBase* sv;
395    if (bidirectional)
396      sv = new SynchronisableVariableBidirectional<std::string>(variable, mode, cb);
397    else
398      sv = new SynchronisableVariable<std::string>(variable, mode, cb);
[7163]399    syncList_.push_back(sv);
400    stringList_.push_back(sv);
[6417]401  }
[2485]402
[7163]403template <> void Synchronisable::unregisterVariable( std::string& variable )
404  {
405    bool unregistered_nonexistent_variable = true;
406    std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin();
407    while(it!=syncList_.end())
408    {
409      if( ((*it)->getReference()) == &variable )
410      {
411        delete (*it);
412        syncList_.erase(it);
413        unregistered_nonexistent_variable = false;
414        break;
415      }
416      else
417        ++it;
418    }
419    assert(unregistered_nonexistent_variable == false);
420   
421    it = stringList_.begin();
422    while(it!=stringList_.end())
423    {
424      if( ((*it)->getReference()) == &variable )
425      {
426        delete (*it);
427        stringList_.erase(it);
428        return;
429      }
430      else
431        ++it;
432    }
433    unregistered_nonexistent_variable = true;
434    assert(unregistered_nonexistent_variable == false); //if we reach this point something went wrong:
435    // the variable has not been registered before
436  }
[6417]437
[7163]438
[1505]439}
Note: See TracBrowser for help on using the repository browser.