Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/network/synchronisable/Synchronisable.cc @ 3607

Last change on this file since 3607 was 3302, checked in by scheusso, 15 years ago

changed some debug output
removed some uneccessary "multi"threading
some enhancements in Connection

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