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
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#include "Synchronisable.h"
32
33#include "core/CoreIncludes.h"
34#include "core/BaseObject.h"
35#include "network/Host.h"
36
37namespace orxonox
38{
39
40  std::map<uint32_t, Synchronisable *> Synchronisable::objectMap_;
41  std::queue<uint32_t> Synchronisable::deletedObjects_;
42
43  uint8_t Synchronisable::state_=0x1; // detemines wheter we are server (default) or client
44
45  /**
46  * Constructor:
47  * Initializes all Variables and sets the right objectID
48  */
49  Synchronisable::Synchronisable(BaseObject* creator){
50    RegisterRootObject(Synchronisable);
51    static uint32_t idCounter=0;
52    objectMode_=0x1; // by default do not send data to server
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;
61    classID = static_cast<uint32_t>(-1);
62   
63    // set dataSize to 0
64    this->dataSize_ = 0;
65    // set standard priority
66    this->setPriority( priority::normal );
67
68    // get creator id
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    }
85  }
86
87  /**
88   * Destructor:
89   * Delete all callback objects and remove objectID from the objectMap_
90   */
91  Synchronisable::~Synchronisable(){
92    // delete callback function objects
93    if(!Identifier::isCreatingHierarchy()){
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 )
98      for(std::vector<SynchronisableVariableBase*>::iterator it = syncList.begin(); it!=syncList.end(); it++)
99        delete (*it);
100      syncList.clear();
101      stringList.clear();
102    }
103    std::map<uint32_t, Synchronisable*>::iterator it;
104    it = objectMap_.find(objectID);
105    if (it != objectMap_.end())
106      objectMap_.erase(it);
107
108  }
109
110
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   */
115  void Synchronisable::setClient(bool b){
116    if(b) // client
117      state_=0x2;
118    else  // server
119      state_=0x1;
120  }
121
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   */
129  Synchronisable *Synchronisable::fabricate(uint8_t*& mem, uint8_t mode)
130  {
131    SynchronisableHeader header(mem);
132
133    if(!header.isDataAvailable())
134    {
135      mem += header.getDataSize();
136      return 0;
137    }
138
139    COUT(4) << "fabricating object with id: " << header.getObjectID() << std::endl;
140
141    Identifier* id = ClassByID(header.getClassID());
142    if (!id)
143    {
144        for(int i = 0; i<160; i++)
145            COUT(0) << "classid: " << i << " identifier: " << ClassByID(i) << endl;
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    }
150    assert(id);
151    BaseObject* creator = 0;
152    if (header.getCreatorID() != OBJECTID_UNKNOWN)
153    {
154      Synchronisable* synchronisable_creator = Synchronisable::getSynchronisable(header.getCreatorID());
155      if (!synchronisable_creator)
156      {
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) ^^
159        return 0;
160      }
161      else
162        creator = dynamic_cast<BaseObject*>(synchronisable_creator);
163    }
164    assert(getSynchronisable(header.getObjectID())==0);   //make sure no object with this id exists
165    BaseObject *bo = id->fabricate(creator);
166    assert(bo);
167    Synchronisable *no = dynamic_cast<Synchronisable *>(bo);
168    assert(no);
169    no->objectID=header.getObjectID();
170    no->creatorID=header.getCreatorID(); //TODO: remove this
171    no->classID=header.getClassID();
172    COUT(4) << "fabricate objectID: " << no->objectID << " classID: " << no->classID << std::endl;
173          // update data and create object/entity...
174    assert( Synchronisable::objectMap_.find(header.getObjectID()) == Synchronisable::objectMap_.end() );
175    Synchronisable::objectMap_[header.getObjectID()] = no;
176    bool b = no->updateData(mem, mode, true);
177    assert(b);
178    if (b)
179    {
180//        b = no->create();
181        assert(b);
182    }
183    return no;
184  }
185
186
187  /**
188   * Finds and deletes the Synchronisable with the appropriate objectID
189   * @param objectID objectID of the Synchronisable
190   * @return true/false
191   */
192  bool Synchronisable::deleteObject(uint32_t objectID){
193    if(!getSynchronisable(objectID))
194      return false;
195    assert(getSynchronisable(objectID)->objectID==objectID);
196    Synchronisable *s = getSynchronisable(objectID);
197    if(s)
198      delete s;
199    else
200      return false;
201    return true;
202  }
203
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   */
209  Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID){
210    std::map<uint32_t, Synchronisable*>::iterator it1;
211    it1 = objectMap_.find(objectID);
212    if (it1 != objectMap_.end())
213      return it1->second;
214
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
223    return NULL;
224  }
225
226
227  /**
228   * This function takes all SynchronisableVariables out of the Synchronisable and saves them together with the size, objectID and classID to the given memory
229   * takes a pointer to already allocated memory (must have at least getSize bytes length)
230   * structure of the bitstream:
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
239   * @return true: if !doSync or if everything was successfully saved
240   */
241  uint32_t Synchronisable::getData(uint8_t*& mem, int32_t id, uint8_t mode){
242    if(mode==0x0)
243      mode=state_;
244    //if this tick is we dont synchronise, then abort now
245    if(!doSync(id, mode))
246      return 0;
247    uint32_t tempsize = 0;
248    if (this->classID==0)
249      COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl;
250
251    if (this->classID == static_cast<uint32_t>(-1))
252        this->classID = this->getIdentifier()->getNetworkID();
253
254    assert(ClassByID(this->classID));
255    assert(this->classID==this->getIdentifier()->getNetworkID());
256    std::vector<SynchronisableVariableBase*>::iterator i;
257
258    // start copy header
259    SynchronisableHeader header(mem);
260    mem += SynchronisableHeader::getSize();
261    // end copy header
262
263
264    COUT(5) << "Synchronisable getting data from objectID: " << objectID << " classID: " << classID << std::endl;
265    // copy to location
266    for(i=syncList.begin(); i!=syncList.end(); ++i){
267      tempsize += (*i)->getData( mem, mode );
268      //tempsize += (*i)->getSize( mode );
269    }
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);
281    assert(tempsize==size);
282#endif
283    return tempsize;
284  }
285
286
287  /**
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
291   * @return true/false
292   */
293  bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback){
294    if(mode==0x0)
295      mode=state_;
296    std::vector<SynchronisableVariableBase *>::iterator i;
297    if(syncList.empty()){
298      assert(0);
299      COUT(4) << "Synchronisable::updateData syncList is empty" << std::endl;
300      return false;
301    }
302
303    uint8_t* data=mem;
304    // start extract header
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();
311      return true;
312    }
313
314    mem += SynchronisableHeader::getSize();
315    // stop extract header
316
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++)
319    {
320      assert( mem <= data+syncHeader.getDataSize() ); // always make sure we don't exceed the datasize in our stream
321      (*i)->putData( mem, mode, forceCallback );
322    }
323    assert(mem == data+syncHeader.getDataSize());
324    return true;
325  }
326
327  /**
328  * This function returns the total amount of bytes needed by getData to save the whole content of the variables
329  * @param id id of the gamestate
330  * @param mode same as getData
331  * @return amount of bytes
332  */
333  uint32_t Synchronisable::getSize(int32_t id, uint8_t mode){
334    int tsize=SynchronisableHeader::getSize();
335    if (mode==0x0)
336      mode=state_;
337    if (!doSync(id, mode))
338      return 0;
339    assert( mode==state_ );
340    tsize += this->dataSize_;
341    std::vector<SynchronisableVariableBase*>::iterator i;
342    for(i=stringList.begin(); i!=stringList.end(); ++i){
343      tsize += (*i)->getSize( mode );
344    }
345    return tsize;
346  }
347
348  /**
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
352   */
353  bool Synchronisable::doSync(int32_t id, uint8_t mode){
354    if(mode==0x0)
355      mode=state_;
356    return ( (objectMode_&mode)!=0 && (!syncList.empty() ) );
357  }
358
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)
364  {
365    SynchronisableHeader header(mem);
366    assert(header.getObjectID()==this->objectID);
367    return header.isDataAvailable();
368  }
369
370  /**
371   * This function sets the synchronisation mode of the object
372   * If set to 0x0 variables will not be synchronised at all
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   */
378  void Synchronisable::setObjectMode(uint8_t mode){
379    assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3);
380    objectMode_=mode;
381  }
382
383
384}
Note: See TracBrowser for help on using the repository browser.