Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp4/src/network/synchronisable/Synchronisable.cc @ 3135

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

second part of the improvement

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