Changeset 7153 for code/branches/presentation3/src/libraries/network/synchronisable/Synchronisable.cc
- Timestamp:
- Jul 12, 2010, 1:08:58 PM (14 years ago)
- Location:
- code/branches/presentation3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation3
- Property svn:mergeinfo changed
/code/branches/network2 (added) merged: 6448-6450,6455-6458,6462,6464-6465
- Property svn:mergeinfo changed
-
code/branches/presentation3/src/libraries/network/synchronisable/Synchronisable.cc
r6961 r7153 49 49 * Initializes all Variables and sets the right objectID_ 50 50 */ 51 Synchronisable::Synchronisable(BaseObject* creator ){ 51 Synchronisable::Synchronisable(BaseObject* creator ) 52 { 52 53 RegisterRootObject(Synchronisable); 53 54 static uint32_t idCounter=0; … … 73 74 else 74 75 this->creatorID_ = OBJECTID_UNKNOWN; 75 76 /*searchcreatorID:77 if (creator)78 {79 Synchronisable* synchronisable_creator = orxonox_cast<Synchronisable*>(creator);80 if (synchronisable_creator && synchronisable_creator->objectMode_)81 {82 this->creatorID = synchronisable_creator->getScene()->getObjectID();83 }84 else if (creator != creator->getCreator())85 {86 creator = creator->getCreator();87 goto searchcreatorID;88 }89 }*/90 76 } 91 77 … … 94 80 * Delete all callback objects and remove objectID_ from the objectMap_ 95 81 */ 96 Synchronisable::~Synchronisable(){ 82 Synchronisable::~Synchronisable() 83 { 97 84 // delete callback function objects 98 85 if(!Identifier::isCreatingHierarchy()){ … … 101 88 deletedObjects_.push(objectID_); 102 89 } 103 // delete all Synchronisable Variables from syncList ( which are also in stringList)104 for(std::vector<SynchronisableVariableBase*>::iterator it = syncList .begin(); it!=syncList.end(); it++)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++) 105 92 delete (*it); 106 syncList .clear();107 stringList .clear();93 syncList_.clear(); 94 stringList_.clear(); 108 95 std::map<uint32_t, Synchronisable*>::iterator it; 109 96 it = objectMap_.find(objectID_); … … 118 105 * @param b true if this object is located on a client or on a server 119 106 */ 120 void Synchronisable::setClient(bool b){ 107 void Synchronisable::setClient(bool b) 108 { 121 109 if(b) // client 122 110 state_=0x2; … … 135 123 { 136 124 SynchronisableHeader header(mem); 137 138 if(!header.isDataAvailable()) 139 { 140 mem += header.getDataSize(); 141 return 0; 142 } 125 assert( !header.isDiffed() ); 143 126 144 127 COUT(4) << "fabricating object with id: " << header.getObjectID() << std::endl; … … 160 143 if (!synchronisable_creator) 161 144 { 162 mem += header.getDataSize() ; //.TODO: this suckz.... remove size from header145 mem += header.getDataSize()+SynchronisableHeader::getSize(); //.TODO: this suckz.... remove size from header 163 146 assert(0); // TODO: uncomment this if we have a clean objecthierarchy (with destruction of children of objects) ^^ 164 147 return 0; … … 198 181 * @return true/false 199 182 */ 200 bool Synchronisable::deleteObject(uint32_t objectID_){ 183 bool Synchronisable::deleteObject(uint32_t objectID_) 184 { 201 185 if(!getSynchronisable(objectID_)) 202 186 return false; … … 215 199 * @return pointer to the Synchronisable with the objectID_ 216 200 */ 217 Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID_){ 201 Synchronisable* Synchronisable::getSynchronisable(uint32_t objectID_) 202 { 218 203 std::map<uint32_t, Synchronisable*>::iterator it1; 219 204 it1 = objectMap_.find(objectID_); 220 205 if (it1 != objectMap_.end()) 221 206 return it1->second; 222 223 // ObjectList<Synchronisable>::iterator it;224 // for(it = ObjectList<Synchronisable>::begin(); it; ++it){225 // if( it->getObjectID()==objectID_ ){226 // objectMap_[objectID_] = *it;227 // return *it;228 // }229 // }230 207 // if the objects not in the map it should'nt exist at all anymore 231 208 return NULL; … … 247 224 * @return true: if !doSync or if everything was successfully saved 248 225 */ 249 uint32_t Synchronisable::getData(uint8_t*& mem, int32_t id, uint8_t mode){ 226 uint32_t Synchronisable::getData(uint8_t*& mem, std::vector<uint32_t>& sizes, int32_t id, uint8_t mode) 227 { 228 unsigned int test = 0; 250 229 if(mode==0x0) 251 230 mode=state_; … … 255 234 uint32_t tempsize = 0; 256 235 #ifndef NDEBUG 236 uint8_t* oldmem = mem; 257 237 if (this->classID_==0) 258 238 COUT(3) << "classid 0 " << this->getIdentifier()->getName() << std::endl; … … 272 252 // end copy header 273 253 274 275 COUT(5) << "Synchronisable getting data from objectID_: " << objectID_ << " classID_: " << classID_ << std::endl;254 CCOUT(5) << "getting data from objectID_: " << objectID_ << ", classID_: " << classID_ << std::endl; 255 // COUT(4) << "objectid: " << this->objectID_ << ":"; 276 256 // copy to location 277 for(i=syncList.begin(); i!=syncList.end(); ++i){ 278 tempsize += (*i)->getData( mem, mode ); 257 for(i=syncList_.begin(); i!=syncList_.end(); ++i) 258 { 259 uint32_t varsize = (*i)->getData( mem, mode ); 260 // COUT(4) << " " << varsize; 261 tempsize += varsize; 262 sizes.push_back(varsize); 263 ++test; 279 264 //tempsize += (*i)->getSize( mode ); 280 265 } 281 282 tempsize += SynchronisableHeader::getSize(); 266 // COUT(4) << endl; 267 283 268 header.setObjectID( this->objectID_ ); 284 269 header.setCreatorID( this->creatorID_ ); 285 270 header.setClassID( this->classID_ ); 286 header.setDataAvailable( true );287 271 header.setDataSize( tempsize ); 272 assert( tempsize == mem-oldmem-SynchronisableHeader::getSize() ); 273 assert( test == this->getNrOfVariables() ); 274 header.setDiffed(false); 275 tempsize += SynchronisableHeader::getSize(); 288 276 289 277 #ifndef NDEBUG … … 302 290 * @return true/false 303 291 */ 304 bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback){ 292 bool Synchronisable::updateData(uint8_t*& mem, uint8_t mode, bool forceCallback) 293 { 305 294 if(mode==0x0) 306 295 mode=state_; 307 std::vector<SynchronisableVariableBase *>::iterator i;308 if(syncList.empty()){296 if(syncList_.empty()) 297 { 309 298 assert(0); 310 COUT( 4) << "Synchronisable::updateData syncListis empty" << std::endl;299 COUT(2) << "Synchronisable::updateData syncList_ is empty" << std::endl; 311 300 return false; 312 301 } … … 314 303 uint8_t* data=mem; 315 304 // start extract header 316 SynchronisableHeader syncHeader(mem); 317 assert(syncHeader.getObjectID()==this->objectID_); 318 assert(syncHeader.getCreatorID()==this->creatorID_); 319 assert(syncHeader.getClassID()==this->classID_); 320 if(syncHeader.isDataAvailable()==false){ 321 mem += syncHeader.getDataSize(); 322 return true; 323 } 324 325 mem += SynchronisableHeader::getSize(); 326 // stop extract header 305 SynchronisableHeaderLight syncHeaderLight(mem); 306 assert(syncHeaderLight.getObjectID()==this->getObjectID()); 327 307 328 308 //COUT(5) << "Synchronisable: objectID_ " << syncHeader.getObjectID() << ", classID_ " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << std::endl; 329 for(i=syncList.begin(); i!=syncList.end(); i++) 330 { 331 assert( mem <= data+syncHeader.getDataSize() ); // always make sure we don't exceed the datasize in our stream 332 (*i)->putData( mem, mode, forceCallback ); 333 } 334 assert(mem == data+syncHeader.getDataSize()); 309 if( !syncHeaderLight.isDiffed() ) 310 { 311 SynchronisableHeader syncHeader2(mem); 312 assert( this->getClassID() == syncHeader2.getClassID() ); 313 assert( this->getCreatorID() == syncHeader2.getCreatorID() ); 314 mem += SynchronisableHeader::getSize(); 315 std::vector<SynchronisableVariableBase *>::iterator i; 316 for(i=syncList_.begin(); i!=syncList_.end(); i++) 317 { 318 assert( mem <= data+syncHeader2.getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream 319 (*i)->putData( mem, mode, forceCallback ); 320 } 321 assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeader::getSize() ); 322 } 323 else 324 { 325 mem += SynchronisableHeaderLight::getSize(); 326 // COUT(0) << "objectID: " << this->objectID_ << endl; 327 while( mem < data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() ) 328 { 329 VariableID varID = *(VariableID*)mem; 330 // COUT(0) << "varID: " << varID << endl; 331 assert( varID < syncList_.size() ); 332 mem += sizeof(VariableID); 333 syncList_[varID]->putData( mem, mode, forceCallback ); 334 } 335 assert(mem == data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() ); 336 } 335 337 return true; 336 338 } … … 342 344 * @return amount of bytes 343 345 */ 344 uint32_t Synchronisable::getSize(int32_t id, uint8_t mode){ 345 int tsize=SynchronisableHeader::getSize(); 346 uint32_t Synchronisable::getSize(int32_t id, uint8_t mode) 347 { 348 uint32_t tsize=SynchronisableHeader::getSize(); 346 349 if (mode==0x0) 347 350 mode=state_; … … 351 354 tsize += this->dataSize_; 352 355 std::vector<SynchronisableVariableBase*>::iterator i; 353 for(i=stringList.begin(); i!=stringList.end(); ++i){ 356 for(i=stringList_.begin(); i!=stringList_.end(); ++i) 357 { 354 358 tsize += (*i)->getSize( mode ); 355 359 } … … 362 366 * @return true/false 363 367 */ 364 bool Synchronisable::doSync(int32_t id, uint8_t mode){ 368 bool Synchronisable::doSync(int32_t id, uint8_t mode) 369 { 365 370 if(mode==0x0) 366 371 mode=state_; 367 return ( (this->objectMode_ & mode)!=0 && (!syncList.empty() ) ); 368 } 369 370 /** 371 * This function looks at the header located in the bytestream and checks wheter objectID_ and classID_ match with the Synchronisables ones 372 * @param mem pointer to the bytestream 373 */ 374 bool Synchronisable::isMyData(uint8_t* mem) 375 { 376 SynchronisableHeader header(mem); 377 assert(header.getObjectID()==this->objectID_); 378 return header.isDataAvailable(); 372 return ( (this->objectMode_ & mode)!=0 && (!syncList_.empty() ) ); 379 373 } 380 374 … … 387 381 * @param mode same as in registerVar 388 382 */ 389 void Synchronisable::setSyncMode(uint8_t mode){ 383 void Synchronisable::setSyncMode(uint8_t mode) 384 { 390 385 assert(mode==0x0 || mode==0x1 || mode==0x2 || mode==0x3); 391 386 this->objectMode_=mode; … … 399 394 else 400 395 sv = new SynchronisableVariable<std::string>(variable, mode, cb); 401 syncList.push_back(sv); 402 stringList.push_back(sv); 396 syncList_.push_back(sv); 397 stringList_.push_back(sv); 398 } 399 400 template <> void Synchronisable::unregisterVariable( std::string& variable ) 401 { 402 bool unregistered_nonexistent_variable = true; 403 std::vector<SynchronisableVariableBase*>::iterator it = syncList_.begin(); 404 while(it!=syncList_.end()) 405 { 406 if( ((*it)->getReference()) == &variable ) 407 { 408 delete (*it); 409 syncList_.erase(it); 410 unregistered_nonexistent_variable = false; 411 break; 412 } 413 else 414 ++it; 415 } 416 assert(unregistered_nonexistent_variable == false); 417 418 it = stringList_.begin(); 419 while(it!=stringList_.end()) 420 { 421 if( ((*it)->getReference()) == &variable ) 422 { 423 delete (*it); 424 stringList_.erase(it); 425 return; 426 } 427 else 428 ++it; 429 } 430 unregistered_nonexistent_variable = true; 431 assert(unregistered_nonexistent_variable == false); //if we reach this point something went wrong: 432 // the variable has not been registered before 403 433 } 404 434
Note: See TracChangeset
for help on using the changeset viewer.