Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp5/src/network/synchronisable/Synchronisable.cc @ 3209

Last change on this file since 3209 was 3209, checked in by rgrieder, 15 years ago

Cleanup in network plus a few dependency reductions (no enet-function inlines, using enum PacketFlag instead of the enet version)

  • Property svn:eol-style set to native
File size: 12.8 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
33#include "core/CoreIncludes.h"
[1735]34#include "core/BaseObject.h"
[3209]35#include "network/Host.h"
[1505]36
[2171]37namespace orxonox
[1505]38{
[1639]39
[2309]40  std::map<uint32_t, Synchronisable *> Synchronisable::objectMap_;
41  std::queue<uint32_t> Synchronisable::deletedObjects_;
[1639]42
[2171]43  uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
[1639]44
[1505]45  /**
46  * Constructor:
[1907]47  * Initializes all Variables and sets the right objectID
[1505]48  */
[2171]49  Synchronisable::Synchronisable(BaseObject* creator){
[1505]50    RegisterRootObject(Synchronisable);
[1907]51    static uint32_t idCounter=0;
52    objectMode_=0x1; // by default do not send data to server
[2171]53    if ( !Host::running() || ( Host::running() && Host::isServer() ) )
54    {
55      this->objectID = idCounter++; //this is only needed when running a server
56    //add synchronisable to the objectMap
57      objectMap_[this->objectID] = this;
58    }
59    else
60      objectID=OBJECTID_UNKNOWN;
[2309]61    classID = static_cast<uint32_t>(-1);
[3084]62   
63    // set dataSize to 0
64    this->dataSize_ = 0;
[2415]65    // set standard priority
66    this->setPriority( priority::normal );
[2171]67
[2415]68    // get creator id
[2087]69    this->creatorID = OBJECTID_UNKNOWN;
70
71    searchcreatorID:
72    if (creator)
73    {
74        Synchronisable* synchronisable_creator = dynamic_cast<Synchronisable*>(creator);
75        if (synchronisable_creator && synchronisable_creator->objectMode_)
76        {
77            this->creatorID = synchronisable_creator->getObjectID();
78        }
79        else if (creator != creator->getCreator())
80        {
81            creator = creator->getCreator();
82            goto searchcreatorID;
83        }
84    }
[1505]85  }
86
[1907]87  /**
[2087]88   * Destructor:
[1907]89   * Delete all callback objects and remove objectID from the objectMap_
90   */
[1505]91  Synchronisable::~Synchronisable(){
[1534]92    // delete callback function objects
[2171]93    if(!Identifier::isCreatingHierarchy()){
[3198]94      // remove object from the static objectMap
95      if (this->objectMode_ != 0x0 && (Host::running() && Host::isServer()))
96        deletedObjects_.push(objectID);
97      // delete all Synchronisable Variables from syncList ( which are also in stringList )
[3084]98      for(std::vector<SynchronisableVariableBase*>::iterator it = syncList.begin(); it!=syncList.end(); it++)
[2245]99        delete (*it);
[3198]100      syncList.clear();
101      stringList.clear();
[1907]102    }
[2309]103    std::map<uint32_t, Synchronisable*>::iterator it;
[2171]104    it = objectMap_.find(objectID);
105    if (it != objectMap_.end())
106      objectMap_.erase(it);
[2485]107
[1505]108  }
[1639]109
[2087]110
[1907]111  /**
112   * This function sets the internal mode for synchronisation
113   * @param b true if this object is located on a client or on a server
114   */
[1505]115  void Synchronisable::setClient(bool b){
116    if(b) // client
117      state_=0x2;
118    else  // server
119      state_=0x1;
120  }
[1856]121
[1907]122  /**
123   * This function fabricated a new synchrnisable (and children of it), sets calls updateData and create
124   * After calling this function the mem pointer will be increased by the size of the needed data
125   * @param mem pointer to where the appropriate data is located
126   * @param mode defines the mode, how the data should be loaded
127   * @return pointer to the newly created synchronisable
128   */
[2171]129  Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode)
[1735]130  {
[2662]131    SynchronisableHeader header(mem);
[1856]132
[2662]133    if(!header.isDataAvailable())
[2087]134    {
[2662]135      mem += header.getDataSize();
[2087]136      return 0;
137    }
[2171]138
[2662]139    COUT(4) << "fabricating object with id: " << header.getObjectID() << std::endl;
[1856]140
[2662]141    Identifier* id = ClassByID(header.getClassID());
[2485]142    if (!id)
143    {
[3088]144        for(int i = 0; i<160; i++)
[2759]145            COUT(0) << "classid: " << i << " identifier: " << ClassByID(i) << endl;
[2485]146        COUT(0) << "Assertion failed: id" << std::endl;
147        COUT(0) << "Possible reason for this error: Client received a synchronizable object whose class has no factory." << std::endl;
148        abort();
149    }
[1907]150    assert(id);
[2171]151    BaseObject* creator = 0;
[2662]152    if (header.getCreatorID() != OBJECTID_UNKNOWN)
[2087]153    {
[2662]154      Synchronisable* synchronisable_creator = Synchronisable::getSynchronisable(header.getCreatorID());
[2087]155      if (!synchronisable_creator)
156      {
[2662]157        mem += header.getDataSize(); //.TODO: this suckz.... remove size from header
158        assert(0); // TODO: uncomment this if we have a clean objecthierarchy (with destruction of children of objects) ^^
[2087]159        return 0;
160      }
161      else
[2171]162        creator = dynamic_cast<BaseObject*>(synchronisable_creator);
[2087]163    }
[2662]164    assert(getSynchronisable(header.getObjectID())==0);   //make sure no object with this id exists
[2171]165    BaseObject *bo = id->fabricate(creator);
[2087]166    assert(bo);
[1735]167    Synchronisable *no = dynamic_cast<Synchronisable *>(bo);
168    assert(no);
[2662]169    no->objectID=header.getObjectID();
170    no->creatorID=header.getCreatorID(); //TODO: remove this
171    no->classID=header.getClassID();
[2087]172    COUT(4) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl;
[1735]173          // update data and create object/entity...
[3198]174    assert( Synchronisable::objectMap_.find(header.getObjectID()) == Synchronisable::objectMap_.end() );
175    Synchronisable::objectMap_[header.getObjectID()] = no;
[2087]176    bool b = no->updateData(mem, mode, true);
[1907]177    assert(b);
[2087]178    if (b)
179    {
[2245]180//        b = no->create();
[2087]181        assert(b);
182    }
[1907]183    return no;
184  }
185
[2087]186
[1907]187  /**
188   * Finds and deletes the Synchronisable with the appropriate objectID
189   * @param objectID objectID of the Synchronisable
190   * @return true/false
191   */
[2309]192  bool Synchronisable::deleteObject(uint32_t objectID){
[1907]193    if(!getSynchronisable(objectID))
[1735]194      return false;
[1907]195    assert(getSynchronisable(objectID)->objectID==objectID);
196    Synchronisable *s = getSynchronisable(objectID);
197    if(s)
198      delete s;
199    else
[1735]200      return false;
201    return true;
202  }
[2087]203
[1907]204  /**
205   * This function looks up the objectID in the objectMap_ and returns a pointer to the right Synchronisable
206   * @param objectID objectID of the Synchronisable
207   * @return pointer to the Synchronisable with the objectID
208   */
[2309]209  Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID){
210    std::map<uint32_t, Synchronisable*>::iterator it1;
[2171]211    it1 = objectMap_.find(objectID);
212    if (it1 != objectMap_.end())
213      return it1->second;
214
[3198]215//     ObjectList<Synchronisable>::iterator it;
216//     for(it = ObjectList<Synchronisable>::begin(); it; ++it){
217//       if( it->getObjectID()==objectID ){
218//         objectMap_[objectID] = *it;
219//         return *it;
220//       }
221//     }
222    // if the objects not in the map it should'nt exist at all anymore
[1907]223    return NULL;
224  }
225
[2087]226
[1505]227  /**
[1907]228   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory
[1735]229   * takes a pointer to already allocated memory (must have at least getSize bytes length)
[1751]230   * structure of the bitstream:
[1907]231   * |totalsize,objectID,classID,var1,var2,string1_length,string1,var3,...|
232   * length of varx: size saved int syncvarlist
233   * @param mem pointer to allocated memory with enough size
234   * @param id gamestateid of the gamestate to be saved (important for priorities)
235   * @param mode defines the direction in which the data will be send/received
236   *             0x1: server->client
237   *             0x2: client->server (not recommended)
238   *             0x3: bidirectional
[2087]239   * @return true: if !doSync or if everything was successfully saved
[1735]240   */
[3084]241  uint32_t Synchronisable::getData(uint8_t*& mem, int32_t id, uint8_t mode){
[2171]242    if(mode==0x0)
243      mode=state_;
[1907]244    //if this tick is we dont synchronise, then abort now
[2171]245    if(!doSync(id, mode))
[3084]246      return 0;
[2309]247    uint32_t tempsize = 0;
[2316]248    if (this->classID==0)
[1735]249      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
[2087]250
[2309]251    if (this->classID == static_cast<uint32_t>(-1))
[2087]252        this->classID = this->getIdentifier()->getNetworkID();
253
[3088]254    assert(ClassByID(this->classID));
[1907]255    assert(this->classID==this->getIdentifier()->getNetworkID());
[3084]256    std::vector<SynchronisableVariableBase*>::iterator i;
[1856]257
[1735]258    // start copy header
[2662]259    SynchronisableHeader header(mem);
260    mem += SynchronisableHeader::getSize();
[1735]261    // end copy header
[1856]262
263
[3084]264    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << std::endl;
[1735]265    // copy to location
[2245]266    for(i=syncList.begin(); i!=syncList.end(); ++i){
[3084]267      tempsize += (*i)->getData( mem, mode );
268      //tempsize += (*i)->getSize( mode );
[1735]269    }
[3084]270   
271    tempsize += SynchronisableHeader::getSize();
272    header.setObjectID( this->objectID );
273    header.setCreatorID( this->creatorID );
274    header.setClassID( this->classID );
275    header.setDataAvailable( true );
276    header.setDataSize( tempsize );
277   
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
[1735]291   * @return true/false
292   */
[2171]293  bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback){
[1735]294    if(mode==0x0)
295      mode=state_;
[3084]296    std::vector<SynchronisableVariableBase *>::iterator i;
[2245]297    if(syncList.empty()){
[2419]298      assert(0);
[1735]299      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
300      return false;
301    }
[1856]302
[2245]303    uint8_t* data=mem;
[1735]304    // start extract header
[2662]305    SynchronisableHeader syncHeader(mem);
306    assert(syncHeader.getObjectID()==this->objectID);
307    assert(syncHeader.getCreatorID()==this->creatorID);
308    assert(syncHeader.getClassID()==this->classID);
309    if(syncHeader.isDataAvailable()==false){
310      mem += syncHeader.getDataSize();
[1751]311      return true;
[1907]312    }
[1856]313
[2662]314    mem += SynchronisableHeader::getSize();
[1907]315    // stop extract header
[2087]316
[2662]317    //COUT(5) << "Synchronisable: objectID " << syncHeader.getObjectID() << ", classID " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << std::endl;
318    for(i=syncList.begin(); i!=syncList.end(); i++)
[2245]319    {
[2662]320      assert( mem <= data+syncHeader.getDataSize() ); // always make sure we don't exceed the datasize in our stream
[2245]321      (*i)->putData( mem, mode, forceCallback );
[1735]322    }
[2662]323    assert(mem == data+syncHeader.getDataSize());
[1735]324    return true;
325  }
[1505]326
327  /**
328  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
[1907]329  * @param id id of the gamestate
330  * @param mode same as getData
[1505]331  * @return amount of bytes
332  */
[2309]333  uint32_t Synchronisable::getSize(int32_t id, uint8_t mode){
[2662]334    int tsize=SynchronisableHeader::getSize();
[3084]335    if (mode==0x0)
[1505]336      mode=state_;
[3084]337    if (!doSync(id, mode))
[2171]338      return 0;
[3084]339    assert( mode==state_ );
340    tsize += this->dataSize_;
341    std::vector<SynchronisableVariableBase*>::iterator i;
342    for(i=stringList.begin(); i!=stringList.end(); ++i){
[2245]343      tsize += (*i)->getSize( mode );
[1505]344    }
345    return tsize;
346  }
[1639]347
[1735]348  /**
[1907]349   * This function determines, wheter the object should be saved to the bytestream (according to its syncmode/direction)
350   * @param id gamestate id
351   * @return true/false
[1735]352   */
[2309]353  bool Synchronisable::doSync(int32_t id, uint8_t mode){
[2171]354    if(mode==0x0)
355      mode=state_;
[2245]356    return ( (objectMode_&mode)!=0 && (!syncList.empty() ) );
[1735]357  }
[1856]358
[1907]359  /**
360   * This function looks at the header located in the bytestream and checks wheter objectID and classID match with the Synchronisables ones
361   * @param mem pointer to the bytestream
362   */
363  bool Synchronisable::isMyData(uint8_t* mem)
[1735]364  {
[2662]365    SynchronisableHeader header(mem);
366    assert(header.getObjectID()==this->objectID);
367    return header.isDataAvailable();
[1735]368  }
[1856]369
[1907]370  /**
371   * This function sets the synchronisation mode of the object
[2171]372   * If set to 0x0 variables will not be synchronised at all
[1907]373   * If set to 0x1 variables will only be synchronised to the client
374   * If set to 0x2 variables will only be synchronised to the server
375   * If set to 0x3 variables will be synchronised bidirectionally (only if set so in registerVar)
376   * @param mode same as in registerVar
377   */
[2171]378  void Synchronisable::setObjectMode(uint8_t mode){
[2087]379    assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3);
[1751]380    objectMode_=mode;
[1505]381  }
382
[2485]383
[1505]384}
Note: See TracBrowser for help on using the repository browser.