Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

PsSimulatedObject.cxx

Go to the documentation of this file.
00001 /*
00002  * This file is part of openMask © INRIA, CNRS, Universite de Rennes 1 1993-2002, thereinafter the Software
00003  * 
00004  * The Software has been developped within the Siames Project. 
00005  * INRIA, the University of Rennes 1 and CNRS jointly hold intellectual property rights
00006  * 
00007  * The Software has been registered with the Agence pour la Protection des
00008  * Programmes (APP) under registration number IDDN.FR.001.510008.00.S.P.2001.000.41200
00009  *  
00010  * This file may be distributed under the terms of the Q Public License
00011  * version 1.0 as defined by Trolltech AS of Norway and appearing in the file
00012  * LICENSE.QPL included in the packaging of this file.
00013  *
00014  * Licensees holding valid specific licenses issued by INRIA, CNRS or Université de Rennes 1 
00015  * for the software may use this file in accordance with that specific license
00016  *
00017  */
00018 #include "PsName.h"
00019 #include <PsSimulatedObject.h>
00020 //#include <PsnReferenceObjectHandle.h>
00021 //#include <PsnMirrorObjectHandle.h>
00022 #include <PsObjectDescriptor.h>
00023 #include <PsnReferenceObjectHandle.h>
00024 #include <PsnMirrorObjectHandle.h>
00025 #include <PsOutput.h>
00026 #include <PsNameToPointerMap.h>
00027 #include <PsInput.h>
00028 #include <PsString.h>
00029 #include <PsnSemaphore.h>
00030 #include <PsEventIdentifier.h>
00031 #include "PsEvent.h"
00032 #include "PsEventListener.h"
00033 #include "PsSystemEventIdentifier.h"
00034 #ifdef _PSGUI
00035 #include <PsCtrlLocGui.h>
00036 extern "C" IlvBoolean IlvSetLanguage();
00037 #endif
00038 #ifdef _REACT
00039 #include <PsReactController.h>
00040 #endif
00041 
00042 //------------------------------------------------------------------
00043 #include <PsnMirrorObjectHandle.h>
00044 #include "PsSimulatedObjectCreator.h"
00045 #include "PsSynchronisationMessage.h" 
00046 
00047 
00048 PsnObjectHandle * PsSimulatedObject::getObjectHandle() const 
00049 {
00050    return _objectHandle ;
00051 }
00052 
00053 
00054 void PsSimulatedObject::setObjectHandle( PsnObjectHandle * objectHandle ) 
00055 {
00056    assert ( _objectHandle == NULL ) ;
00057    _objectHandle = objectHandle ;
00058 }
00059 
00060 //------------------------------------------------------------------
00061 
00062 const PsObjectDescriptor & PsSimulatedObject::getObjectDescriptor () const {
00063    return _objectDescriptor ;
00064 }
00065 
00066 
00067 
00068 //------------------------------------------------------------------
00069 
00070 PsSimulatedObject::PsSimulatedObject ( PsController & ctrl, const PsObjectDescriptor & objectDecriptor ) : 
00071    PsFlowable(),
00072    _controller ( ctrl ),
00073    _objectDescriptor ( objectDecriptor ),
00074    _objectHandle ( NULL )
00075 {
00076    _objectDescriptor._pointerToSimulatedObject = this ;
00077 }
00078 
00079 //------------------------------------------------------------------
00080 PsSimulatedObject:: ~PsSimulatedObject ()
00081 {
00082   // destroy inputs, outputs and control parameters
00083    
00084   PsNameToPointerMap<PsOutputNT>::iterator pOutput; // iterateur
00085   PsOutputNT *outputCourante;
00086   PsNameToPointerMap<PsOutputNT>::iterator pParam; // iterateur
00087   PsOutputNT *paramCourant;
00088   PsNameToPointerMap<PsInputNT>::iterator pInput; // iterateur
00089   PsInputNT *inputCourant;
00090 
00091   // Pour toutes les outputs, on detruit ;
00092 
00093 #ifdef _DEBUGEXEC
00094   cerr<<"PsSimulatedObject:: ~PsSimulatedObject ()  : destruction des paramètres ";
00095 #endif
00096   for(pParam= _controlParameterTable.begin(); pParam!= _controlParameterTable.end(); pParam++)
00097     {
00098       paramCourant = getPointerToControlParameterNamed ((*pParam).first);
00099       delete paramCourant;
00100     }
00101 #ifdef _DEBUGEXEC
00102   cerr<<"effective"<<endl;
00103   cerr<<"PsSimulatedObject:: ~PsSimulatedObject ()  : destruction des entrées ";
00104 #endif
00105   for(pInput= _inputTable.begin(); pInput!=_inputTable.end();pInput++)
00106     {
00107       inputCourant = getPointerToInputNamed ((*pInput).first);
00108       delete inputCourant ;
00109     }
00110 #ifdef _DEBUGEXEC
00111   cerr<<"effective"<<endl;
00112   cerr<<"PsSimulatedObject:: ~PsSimulatedObject ()  : destruction des outputs ";
00113 #endif
00114   for(pOutput= _outputTable.begin(); pOutput!= _outputTable.end(); pOutput++)
00115     {
00116        outputCourante = getPointerToOutputNamed ((*pOutput).first);
00117        delete outputCourante;
00118     }
00119 #ifdef _DEBUGEXEC
00120   cerr<<"effective"<<endl;
00121   cerr<<"PsSimulatedObject:: ~PsSimulatedObject ()  : encapsulated classes creators destruction ";
00122 #endif
00123  
00124 #ifdef _DEBUGEXEC
00125   cerr<<"effective"<<endl;
00126 #endif
00127   PsNameToPointerMap<PsSimulatedObjectCreator>::iterator pEncapsulatedClassesTable ;
00128   for( pEncapsulatedClassesTable = _encapsulatedClassesTable.begin() ;
00129        pEncapsulatedClassesTable != _encapsulatedClassesTable.end() ;
00130        pEncapsulatedClassesTable++ )
00131      {
00132         delete (*pEncapsulatedClassesTable).second ;
00133      }
00134 
00135   // update the object descriptor
00136   _objectDescriptor._pointerToSimulatedObject = NULL ;
00137 
00138 }
00139 
00140 void PsSimulatedObject::deleteInput(const PsName & nom) {
00141    PsInputNT * obj;
00142    obj = (_inputTable)[nom];
00143    _inputTable.erase(nom);
00144    if(obj!=NULL) {
00145       delete obj;
00146    }
00147 }
00148 
00149 //---------------------------------------------------------------------
00150 
00151 PsOutputNT * PsSimulatedObject::getPointerToControlParameterNamed (const PsName & nomAtt) const {
00152    return (_controlParameterTable.getObjectOfIndex (nomAtt)) ;
00153 } 
00154  
00155 //---------------------------------------------------------------------
00156 
00157 PsOutputNT * PsSimulatedObject::getPointerToOutputNamed (const PsName & nomAtt) const {
00158    PsOutputNT * ref ;
00159    ref = _outputTable.getObjectOfIndex (nomAtt) ;
00160    if (ref == NULL) {
00161 #ifdef _USESSTREAM
00162       ostringstream warningMessage ;
00163       warningMessage<< "PsSimulatedObject::getPointerToOutputNamed: "
00164                     << nomAtt << " is an unknown output for object "<<getName()<<endl ;
00165       PsController::warning (warningMessage.str(), PsController::AllWarnings) ;
00166 #else
00167          ostrstream warningMessage ;
00168          warningMessage<< "PsSimulatedObject::getPointerToOutputNamed: "
00169                        << nomAtt << " is an unknown output for object "<<getName()<<endl ;
00170          warningMessage.put ('\0') ;
00171          PsController::warning (warningMessage.str(), PsController::AllWarnings) ;
00172          delete warningMessage.str() ;
00173 #endif
00174    }
00175    return ref ;
00176 } 
00177 
00178 //---------------------------------------------------------------------
00179 
00180 PsInputNT * PsSimulatedObject::getPointerToInputNamed (const PsName & nomAtt) const {
00181    return (_inputTable.getObjectOfIndex (nomAtt));
00182 } 
00183 
00184 
00185 //------------------------------------------------------------------
00186 // Nom symbolique de l'objet
00187 const PsName & PsSimulatedObject::getName() const {
00188     return _objectDescriptor.getName();
00189 }
00190 
00191 //------------------------------------------------------------------
00192 // accesseur sur le controleur local de la simulation en lecture
00193 
00194 PsController & PsSimulatedObject::getController () const {
00195   return _controller;
00196 }
00197 
00198 
00199 void PsSimulatedObject::init() 
00200 {
00201 }
00202 
00203 
00204 
00205 void PsSimulatedObject::compute ()
00206 {
00207 }
00208 
00209 //------------------------------------------------------------------
00210 
00211 void PsSimulatedObject::finish() 
00212 {
00213 }
00214 
00215 //------------------------------------------------------------------
00216 
00217 float PsSimulatedObject::getPeriod () const 
00218 {
00219    return 1. / _objectDescriptor.getFrequency () ;
00220 }
00221 
00222 //------------------------------------------
00223 
00224 const PsObjectDescriptor & PsSimulatedObject::getFathersDescriptor () const {
00225    if ( _objectDescriptor.getFathersObjectDescriptor () != NULL ) 
00226       {
00227          return *_objectDescriptor.getFathersObjectDescriptor () ;
00228       }
00229    else 
00230       {
00231          return _objectDescriptor ;
00232       }
00233 } 
00234 
00235 //------------------------------------------
00236 
00237 PsSimulatedObject * PsSimulatedObject::getFather () const {
00238    //refObjet n'est pas toujours initialisé en distribué.
00239    if ( _objectDescriptor.getFathersObjectDescriptor() != NULL )
00240       {
00241          return  _controller.getPointerToSimulatedObjectNamed(_objectDescriptor.getFathersObjectDescriptor()->getName());
00242       }
00243    else
00244       {
00245          return NULL ;
00246       }
00247 }
00248 const PsConfigurationParameterDescriptor * PsSimulatedObject::getConfigurationParameters() const {
00249    return _objectDescriptor.getConfigurationParameters();
00250 }
00251 
00252 
00253 const PsNameToPointerMap<PsInputNT> & PsSimulatedObject::getInputTable () const 
00254 {
00255    return _inputTable ;
00256 }
00257 
00258 const PsNameToPointerMap<PsOutputNT> & PsSimulatedObject::getOutputTable () const  
00259 {
00260    return _outputTable ;
00261 }
00262 
00263 const PsNameToPointerMap<PsOutputNT> & PsSimulatedObject::getControlParameterTable () const  
00264 {
00265    return _controlParameterTable ;
00266 }
00267 
00268 //------------------------------------------------------------------
00269 
00270  void PsSimulatedObject::sendEvent ( const PsEvent & event ) {
00271 #ifdef _DEBUGEVT
00272     cerr<<"PsSimulatedObject::sendEvent ( const PsEvent & )"<<endl;
00273     cerr<<"   Sent "<<event.eventId<<" to "<<event.receiver <<" at "<<event.date<<endl;
00274 #endif
00275     PsSimulatedObject * objDest = _controller.getPointerToSimulatedObjectNamed( event.receiver ) ;
00276     if ( objDest == NULL ) 
00277        {
00278           getController().addToPendingEvents( event.clone() ) ; 
00279        }
00280     else if ( objDest->getObjectHandle() == NULL )
00281        {
00282           getController().addToPendingEvents( event.clone() ) ; 
00283        }
00284     else
00285        {
00286           objDest->getObjectHandle()->receiveEvent ( event ) ;
00287        }     
00288  }
00289 
00290  void PsSimulatedObject::sendEvent ( PsEvent * event ) {
00291 #ifdef _DEBUGEVT
00292     cerr<<"PsSimulatedObject::sendEvent ( PsEvent * )"<<endl;
00293     cerr<<"   Sent "<<event->eventId<<" to "<<event->receiver <<" at "<<event->date<<endl;
00294 #endif
00295     PsSimulatedObject * objDest = _controller.getPointerToSimulatedObjectNamed( event->receiver ) ;
00296     if ( objDest == NULL ) 
00297        {
00298           getController().addToPendingEvents( event ) ; 
00299        }
00300     else if ( objDest->getObjectHandle() == NULL )
00301        {
00302           getController().addToPendingEvents( event ) ; 
00303        }
00304     else
00305        {
00306           objDest->getObjectHandle()->receiveEvent ( event ) ;
00307        }     
00308  }
00309 
00310  void PsSimulatedObject::sendEvent ( PsSimulatedObject & receiver , const PsEventIdentifier & eventId ) {
00311 #ifdef _DEBUGEVT
00312     cerr<<"PsSimulatedObject::sendEvent ( PsSimulatedObject & "<<receiver<<" const PsEventIdentifier & "<<eventId <<" )"<<endl;
00313 #endif
00314     sendEvent (&receiver, eventId ) ;
00315 #ifdef _DEBUGEVT
00316     cerr<<"PsSimulatedObject::sendEvent () fin"<<endl;
00317 #endif
00318  }
00319 
00320  void PsSimulatedObject::sendEvent ( PsSimulatedObject * receiver , const PsEventIdentifier & eventId ) {
00321 #ifdef _DEBUGEVT
00322     cerr<<"PsSimulatedObject::sendEvent ( PsSimulatedObject * "<<receiver<<" const PsEventIdentifier & "<<eventId <<" )"<<endl;
00323 #endif
00324     assert ( receiver != NULL ) ;
00325     if ( receiver->getObjectHandle() != NULL )
00326        {
00327           receiver->getObjectHandle()->receiveEvent (new PsEvent (eventId,
00328                                                                   _controller.getSimulatedDate (),
00329                                                                   getName (),
00330                                                                   receiver->getName ()) ) ;
00331        }
00332     else
00333        //could happen during creation when the object handle is not yet created.
00334        {
00335           getController().addToPendingEvents(new PsEvent (eventId,
00336                                                           _controller.getSimulatedDate (),
00337                                                           getName (),
00338                                                           receiver->getName ())  ) ; 
00339        }
00340 #ifdef _DEBUGEVT
00341     cerr<<"PsSimulatedObject::sendEvent () fin"<<endl;
00342 #endif
00343  }
00344  
00345  void PsSimulatedObject::sendEvent ( const PsName & receiver , const PsEventIdentifier & eventId ) {
00346 #ifdef _DEBUGEVT
00347     cerr<<"PsSimulatedObject::sendEvent ( const PsName & , const PsEventIdentifier & )"<<endl;
00348     cerr<<"   Sent "<<eventId<<" to "<<receiver <<" at "<<_controller.getSimulatedDate ()<<endl;
00349 #endif
00350     PsSimulatedObject * objDest= _controller.getPointerToSimulatedObjectNamed( receiver ) ;
00351     if ( (objDest == NULL) ) 
00352        {
00353           getController().addToPendingEvents(new PsEvent (eventId,
00354                                                           _controller.getSimulatedDate (),
00355                                                           getName (),
00356                                                           receiver) )  ; 
00357        }
00358     else if ( objDest->getObjectHandle() == NULL )
00359        {
00360           getController().addToPendingEvents (new PsEvent (eventId,
00361                                                            _controller.getSimulatedDate (),
00362                                                            getName (),
00363                                                            receiver) ) ;
00364        }
00365     else {
00366        objDest->getObjectHandle()->receiveEvent (new PsEvent (eventId,
00367                                                               _controller.getSimulatedDate (),
00368                                                               getName (),
00369                                                               receiver) ) ;
00370     } 
00371  }
00372 
00373 //------------------------------------------------------------------
00374 
00375  bool PsSimulatedObject::processEventsASAP () const {
00376     //default mode : processEvents at the object's frequency.
00377     return false ;
00378  }
00379 
00380 //------------------------------------------------------------------
00381 void PsSimulatedObject::prepareEventProcessing ( list <PsEvent *> & ) {
00382 
00383 }
00384 
00385 bool PsSimulatedObject::processEvent (PsEvent * event) {
00386    return true ;
00387 }
00388 
00389 void PsSimulatedObject::eventsProcessed ( list <PsEvent *> & ) {
00390 
00391 }
00392 
00393 
00394 //------------------------- insertion et extraction d'un objet dans un flot ---------------------
00395 
00396 //------------------------------------------------------------------
00397 
00398 void PsSimulatedObject::extract (istream & in)
00399 {
00400   PsNameToPointerMap<PsOutputNT>::iterator pOutput; // iterateur
00401   PsOutputNT *outputCourante;
00402   PsNameToPointerMap<PsOutputNT>::iterator pParam; // iterateur
00403   PsOutputNT *paramCourant;
00404 
00405   // Pour toutes les outputs, on stocke
00406   for(pOutput= _outputTable.begin(); pOutput!= _outputTable.end(); pOutput++)
00407     {
00408       outputCourante = (*pOutput).second;
00409       in >> *outputCourante;
00410     } 
00411  bool avecParam;
00412   in >> avecParam;
00413 #ifdef _DEBUGPVMMESS
00414   cerr<<endl<<avecParam<<" "<<true<<" "<<false<<endl;;
00415 #endif
00416   if (avecParam) {
00417      //cerr<<"PsSimulatedObject::extract extraction des paramètres de "<<getName()<<" ";
00418      for(pParam= _controlParameterTable.begin(); pParam!= _controlParameterTable.end(); pParam++) {
00419         paramCourant = (*pParam).second;
00420         in >> *paramCourant;
00421         //cerr<<(*pParam).first<<" ";
00422      }
00423      //cerr<<endl;
00424   }
00425 #ifdef DEBUG
00426   cout << "PsSimulatedObject::extract fin : " << getName() << endl ;
00427 #endif
00428 }
00429 
00430 //------------------------------------------------------------------
00431 void PsSimulatedObject::pack ( PsOutgoingSynchronisationMessage & out) const
00432 {
00433    //make sure the object has been encapsulated in an object handle
00434    assert (getObjectHandle() != NULL) ;
00435 
00436 #ifdef _DEBUGPVMMESS
00437    cout << "PsSimulatedObject:"<<this<<":pack"<< endl ;
00438 #endif
00439 
00440    // store the value of all outputs
00441    for(PsNameToPointerMap<PsOutputNT>::const_iterator pOutput = _outputTable.begin(); 
00442        pOutput != _outputTable.end(); 
00443        ++pOutput)
00444      {
00445 #ifdef _DEBUGPVMMESS
00446         cerr<<(*pOutput).first<<" ";
00447 #endif
00448         pOutput->second->pack (out) ;
00449      } 
00450   
00451    bool mettreParam = ( getObjectHandle()->controlParametersChanged() ) || 
00452                       (getSimulatedDate() == PsController::initialSimulationDate) ;
00453 
00454 #ifdef _DEBUGPVMMESS
00455    cerr<<endl ;
00456    if ( mettreParam )
00457       {
00458          cerr<<"PsSimulatedObject:"<<this<<":pack: packing control parameters"<<endl;
00459       }
00460    else
00461       {
00462          cerr<<"PsSimulatedObject:"<<this<<":pack: not packing control parameters"<<endl;
00463       }
00464 #endif
00465 
00466    if(mettreParam) 
00467       {
00468          out<<PsSynchronisationMessage::controlParameters;
00469          for(PsNameToPointerMap<PsOutputNT>::const_iterator pParam= _controlParameterTable.begin(); 
00470              pParam!= _controlParameterTable.end(); 
00471              pParam++) 
00472             {
00473 #ifdef _DEBUGPVMMESS
00474                cout << (*pParam).first << " ";
00475 #endif
00476                (*pParam).second->pack(out) ;
00477             }
00478       }
00479    else
00480       {
00481          out<<PsSynchronisationMessage::noControlParameters;
00482       }
00483 #ifdef DEBUG
00484   cerr << "PsSimulatedObject:"<<this<<":pack done "<< endl ;
00485 #endif
00486   out << PsSynchronisationMessage::endOfSynchronisationFragment ;
00487 }
00488 
00489 void PsSimulatedObject::packAllValues ( PsOutgoingSynchronisationMessage & out) const
00490 {
00491    //make sure the object has been encapsulated in an object handle
00492    assert (getObjectHandle() != NULL) ;
00493 
00494 #ifdef _DEBUGPVMMESS
00495    cout << "PsSimulatedObject:"<<this<<":packAllValues"<< endl ;
00496 #endif
00497 
00498    // store the value of all outputs
00499    for(PsNameToPointerMap<PsOutputNT>::const_iterator pOutput = _outputTable.begin(); 
00500        pOutput != _outputTable.end(); 
00501        ++pOutput)
00502      {
00503 #ifdef _DEBUGPVMMESS
00504         cerr<<(*pOutput).first<<" ";
00505 #endif
00506         pOutput->second->packAllValues (out) ;
00507      } 
00508   
00509 
00510 #ifdef _DEBUGPVMMESS
00511    cerr<<endl ;
00512    cerr<<"PsSimulatedObject:"<<this<<":pack: packing control parameters"<<endl;
00513 #endif
00514 
00515    out<<PsSynchronisationMessage::controlParameters;
00516    for(PsNameToPointerMap<PsOutputNT>::const_iterator pParam= _controlParameterTable.begin(); 
00517        pParam!= _controlParameterTable.end(); 
00518        pParam++) 
00519       {
00520 #ifdef _DEBUGPVMMESS
00521          cout << (*pParam).first << " ";
00522 #endif
00523          (*pParam).second->packAllValues(out) ;
00524       }
00525    
00526 #ifdef DEBUG
00527   cerr << "PsSimulatedObject:"<<this<<":pack done "<< endl ;
00528 #endif
00529   out << PsSynchronisationMessage::endOfSynchronisationFragment ;
00530 }
00531 
00532 
00533 void PsSimulatedObject::unpack (PsIncomingSynchronisationMessage & in) 
00534 {
00535 
00536 #ifdef _DEBUGPVMMESS
00537    cout << "PsSimulatedObject:"<<this<<":unpack"<< endl ;
00538 #endif
00539    PsName attributeName ;
00540    PsNameToPointerMap<PsOutputNT>::iterator pOutput = _outputTable.begin();
00541    
00542    
00543    attributeName.unpack( in ) ;
00544    while ( (attributeName != PsSynchronisationMessage::controlParameters)
00545            && (attributeName != PsSynchronisationMessage::noControlParameters) )
00546       {
00547          if ( pOutput->first == attributeName )
00548             {
00549                (pOutput->second)->unpack(in) ;
00550                ++pOutput ;
00551             }
00552          else
00553             {
00554                do 
00555                   {
00556                      ++pOutput ;
00557                      assert ( pOutput != _outputTable.end() ) ;// a new output has been added : not accepted
00558                   }
00559                while ( pOutput->first != attributeName ) ;
00560 
00561                (pOutput->second)->unpack ( in ) ;
00562                ++pOutput ;
00563             }
00564          attributeName.unpack ( in ) ;
00565       }
00566    
00567    if (attributeName == PsSynchronisationMessage::controlParameters) 
00568       {
00569          //the control parameters have been added to the synchronisation message
00570          pOutput =  _controlParameterTable.begin();
00571          attributeName.unpack ( in ) ;
00572          while ( attributeName != PsSynchronisationMessage::endOfSynchronisationFragment )
00573             {
00574                if ( pOutput->first == attributeName )
00575                   {
00576                      (pOutput->second)->unpack( in ) ;
00577                      ++pOutput ;
00578                   }
00579                else
00580                   {
00581                      do 
00582                         {
00583                            ++pOutput ;
00584                            assert ( pOutput != _controlParameterTable.end() ) ;// a new control parameter has been added : not accepted
00585                         }
00586                      while ( pOutput->first != attributeName ) ;
00587                      
00588                      (pOutput->second)->unpack (in) ;
00589                      ++pOutput ;
00590                   }
00591               attributeName.unpack ( in ) ;
00592             }
00593       }
00594    else
00595       {
00596          // consume the PsnPvmMessage::endOfSyncronisationMessage 
00597          attributeName.unpack ( in ) ;
00598       }    
00599 }
00600 
00601 
00602 void PsSimulatedObject::unpackAllValues (PsIncomingSynchronisationMessage & in) 
00603 {
00604 
00605 #ifdef _DEBUGPVMMESS
00606    cout << "PsSimulatedObject:"<<this<<":unpackAllValues"<< endl ;
00607 #endif
00608    PsName attributeName ;
00609    PsNameToPointerMap<PsOutputNT>::iterator pOutput = _outputTable.begin();
00610    
00611    attributeName.unpack( in ) ;
00612    while ( (attributeName != PsSynchronisationMessage::controlParameters)
00613            && (attributeName != PsSynchronisationMessage::noControlParameters) )
00614       {
00615          if ( pOutput->first == attributeName )
00616             {
00617                (pOutput->second)->unpackAllValues(in) ;
00618                ++pOutput ;
00619             }
00620          else
00621             {
00622                do 
00623                   {
00624                      ++pOutput ;
00625                      assert ( pOutput != _outputTable.end() ) ;// a new output has been added : not accepted
00626                   }
00627                while ( pOutput->first != attributeName ) ;
00628                
00629                (pOutput->second)->unpackAllValues ( in ) ;
00630                ++pOutput ;
00631             }
00632          attributeName.unpack ( in ) ;
00633       }
00634    
00635    if (attributeName == PsSynchronisationMessage::controlParameters) 
00636       {
00637          //the control parameters have been added to the synchronisation message
00638          pOutput =  _controlParameterTable.begin();
00639          attributeName.unpack ( in ) ;
00640          while ( attributeName != PsSynchronisationMessage::endOfSynchronisationFragment )
00641             {
00642                if ( pOutput->first == attributeName )
00643                   {
00644                      (pOutput->second)->unpackAllValues( in ) ;
00645                      ++pOutput ;
00646                   }
00647                else
00648                   {
00649                      do 
00650                         {
00651                            ++pOutput ;
00652                            assert ( pOutput != _controlParameterTable.end() ) ;// a new control parameter has been added : not accepted
00653                         }
00654                      while ( pOutput->first != attributeName ) ;
00655                      
00656                      (pOutput->second)->unpackAllValues (in) ;
00657                      ++pOutput ;
00658                   }
00659               attributeName.unpack ( in ) ;
00660             }
00661       }
00662    else
00663       {
00664          // consume the PsnPvmMessage::endOfSyncronisationMessage 
00665          attributeName.unpack ( in ) ;
00666       }    
00667 }
00668 
00669 
00670 
00671 void PsSimulatedObject::insertInStream (ostream & out) const
00672 {
00673   PsNameToPointerMap<PsOutputNT>::const_iterator pOutput; // iterateur
00674   PsOutputNT * outputCourante;
00675   PsNameToPointerMap<PsOutputNT>::const_iterator pParam; // iterateur
00676   PsOutputNT *paramCourant;
00677 #ifdef DEBUG
00678   cout << "PsSimulatedObject::insertInStream"<< endl ;
00679 #endif
00680 
00681   // Pour toutes les outputs, on stocke
00682   for(pOutput= _outputTable.begin(); pOutput!= _outputTable.end(); pOutput++)
00683     {
00684 #ifdef _DEBUGPVMMESS
00685        cerr<<(*pOutput).first<<" ";
00686 #endif
00687       outputCourante = (*pOutput).second;
00688       out << *outputCourante << " ";
00689 #ifdef _DEBUGPVMMESS
00690       cerr << *outputCourante << " "<<endl;
00691 #endif
00692     }  
00693   bool mettreParam = getObjectHandle()->controlParametersChanged();
00694   if(getSimulatedDate()==PsController::initialSimulationDate) mettreParam=true;
00695 #ifdef _DEBUGPVMMESS
00696   cerr<<endl<<mettreParam<<" "<<true<<" "<<false<<endl;;
00697 #endif
00698   out<<mettreParam<<" ";
00699   if(mettreParam) {
00700      for(pParam= _controlParameterTable.begin(); pParam!= _controlParameterTable.end(); pParam++) {
00701         paramCourant = (*pParam).second;
00702 #ifdef _DEBUGPVMMESS
00703         cout << (*pParam).first << " ";
00704 #endif
00705         out << *paramCourant << " ";
00706      }
00707   }
00708 #ifdef DEBUG
00709   cout << "PsSimulatedObject::insertInStream fini"<< endl ;
00710 #endif
00711 }
00712 
00713 //------------------------------------------------------------------
00714 
00715 
00716 
00717 void PsSimulatedObject::fireSignal(const PsEventIdentifier & sig) 
00718 {
00719   PsEvent event(sig,getSimulatedDate(),getName(),getName()) ;
00720   //does firing a signal during creation have any sense ? yes
00721   if (getObjectHandle() != NULL ) 
00722      {
00723         getObjectHandle()->sendEventsForSignal( event , sig ) ;
00724      }
00725   else
00726      {  
00727         // no registration can have taken place, as this should only happen at object creation,
00728         // so just broadcast the signal
00729         getController().broadcastEventsForSignal ( event, sig ) ;
00730      }
00731 }
00732 
00733 bool PsSimulatedObject::registerForSignalBy ( const PsEventIdentifier & sig, 
00734                                               const PsName & producer )
00735 {
00736    return registerForSignalBy ( sig, producer, sig ) ;
00737 }
00738 
00739 
00740 
00741 bool PsSimulatedObject::registerForSignalBy ( const PsEventIdentifier & sig, 
00742                                               const PsName & producer,
00743                                               const PsEventIdentifier & eventId )
00744 {
00745    PsSimulatedObject * pointerToProducer ;
00746    pointerToProducer = _controller.getPointerToSimulatedObjectNamed ( producer );
00747    if (pointerToProducer != NULL) 
00748       {
00749          return registerForSignalBy(sig, pointerToProducer, eventId);
00750       }
00751    else 
00752       {
00753          return getController().addToPendingRegistrations ( sig , producer, eventId, getName() ) ;
00754       }
00755 }
00756 
00757 
00758 
00759 bool PsSimulatedObject::registerForSignalBy(const PsEventIdentifier & sig, 
00760                                             PsSimulatedObject * producer )
00761 {
00762    return registerForSignalBy ( sig , producer , sig ) ;
00763 }
00764 
00765 
00766 
00767 bool PsSimulatedObject::registerForSignalBy(const PsEventIdentifier & sig, 
00768                                             PsSimulatedObject & producer )
00769 {
00770    return registerForSignalBy ( sig , producer , sig ) ;
00771 }
00772 
00773 
00774 
00775 bool PsSimulatedObject::registerForSignalBy(const PsEventIdentifier & sig, 
00776                                             PsSimulatedObject * producer,
00777                                             const PsEventIdentifier & eventId )
00778 {
00779    assert ( producer != NULL ) ;
00780    //unless registring for oneself during creation, the next if branch should allways be verified
00781    if (producer->getObjectHandle() != NULL)
00782       {
00783          return producer->getObjectHandle()->receiveRegistrationForSignal ( sig, getName(), eventId ) ;
00784       }
00785    else 
00786       {
00787          return getController().addToPendingRegistrations (sig, 
00788                                                            producer->getName() ,
00789                                                            eventId,
00790                                                            getName() ) ;
00791       }
00792 }
00793 
00794 
00795 
00796 bool PsSimulatedObject::registerForSignalBy(const PsEventIdentifier & sig, 
00797                                             PsSimulatedObject & producer,
00798                                             const PsEventIdentifier & eventId )
00799 {
00800    if (producer.getObjectHandle() != NULL)
00801       {
00802          //unless registring for oneself during creation, the next if branch should allways be verified
00803          return producer.getObjectHandle()->receiveRegistrationForSignal ( sig, getName(), eventId ) ;
00804       }
00805    else 
00806       {
00807          return getController().addToPendingRegistrations (sig, 
00808                                                            producer.getName(),
00809                                                            eventId,
00810                                                            getName() ) ;
00811       }
00812 }
00813 
00814 
00815 
00816 bool PsSimulatedObject::registerForSignal( const PsEventIdentifier & sig ) 
00817 {
00818    return _controller.receiveRegistrationForSignal ( sig, getName(), sig);
00819 }
00820 
00821 
00822 
00823 bool PsSimulatedObject::registerForSignal(const PsEventIdentifier & sig,
00824                                           const PsEventIdentifier & eventId) 
00825 {
00826    return _controller.receiveRegistrationForSignal ( sig, getName(), eventId );
00827 }
00828 
00829 
00830 
00831 bool PsSimulatedObject::cancelRegistrationForSignalBy(const PsEventIdentifier & sig, const PsName & producer){
00832    PsSimulatedObject * pointerToProducer ;
00833    pointerToProducer = _controller.getPointerToSimulatedObjectNamed( producer );
00834    if (pointerToProducer != NULL) 
00835       {
00836          return cancelRegistrationForSignalBy(sig,pointerToProducer);
00837       }
00838    else 
00839       {
00840          return getController().removeFromPendingRegistrations ( sig, producer, getName() ) ;
00841       }
00842 }
00843 
00844 bool PsSimulatedObject::cancelRegistrationForSignalBy(const PsEventIdentifier & sig, PsSimulatedObject * producer)
00845 {
00846    assert ( producer != NULL ) ;
00847    //unless canceling a registration for oneself during creation, the next if sould be verified
00848    if ( producer->getObjectHandle() != NULL )
00849       {
00850          return producer->getObjectHandle()->cancelRegistrationForSignal ( sig, getName() ) ;
00851       }
00852    else
00853       {
00854          return getController().removeFromPendingRegistrations (sig, producer->getName(), getName() ) ;
00855       }
00856 }
00857 
00858 bool PsSimulatedObject::cancelRegistrationForSignalBy(const PsEventIdentifier & sig, PsSimulatedObject & producer)
00859 {
00860    //unless canceling a registration for oneself during creation, the next assertion sould be verified
00861    if ( producer.getObjectHandle() != NULL )
00862       {    
00863          return producer.getObjectHandle()->cancelRegistrationForSignal ( sig, getName() ) ;
00864       }
00865    else
00866       {
00867          return getController().removeFromPendingRegistrations (sig, producer.getName(), getName() ) ;
00868       }
00869 }
00870 
00871 
00872 bool PsSimulatedObject::cancelRegistrationForSignal(const PsEventIdentifier & sig) 
00873 {
00874    return _controller.receiveCancellationForSignal(sig,getName());
00875 }
00876 
00877 
00878 const PsDate & PsSimulatedObject::getSimulatedDate () const {
00879    return _controller.getSimulatedDate();
00880 }
00881 
00882 
00883 
00884 bool PsSimulatedObject::isAncestor(const PsName & name) 
00885 {
00886    bool result = false ;
00887    const PsObjectDescriptor * ancestorDescriptor = getObjectDescriptor().getFathersObjectDescriptor() ;
00888    while ( (ancestorDescriptor != NULL) &&
00889            !result )
00890       {
00891          if ( ancestorDescriptor->getName() == name)
00892             {
00893                result = true ;
00894             }
00895          ancestorDescriptor = ancestorDescriptor->getFathersObjectDescriptor() ;
00896       }
00897    return result ;
00898 }
00899 
00900 
00901 
00902 const list < PsEventListener * > & PsSimulatedObject::getEventListeners () const 
00903 {
00904    return _listOfEventListeners ;
00905 }
00906 
00907 void PsSimulatedObject::addEventListener ( PsEventListener & eventListener ) 
00908 {
00909    _listOfEventListeners.push_front( & eventListener) ;
00910 }
00911 
00912 void PsSimulatedObject::registerEventListenerForEvent ( PsEventListener & eventListener, 
00913                                                         const PsEventIdentifier & eventId ) 
00914 {
00915    //this member function should only be called by event listeners when registering the events they are associated to
00916    //this registration process is initiated during the creation of the assocaited object handle
00917    //therefore, the following assertion should allways be verified
00918    assert ( getObjectHandle () != NULL ); 
00919 
00920    getObjectHandle () -> registerEventListenerForEvent ( eventListener , eventId ) ;
00921 
00922    eventListener.addToListenedEventIds ( eventId ) ;
00923 }
00924 
00925 
00926 
00927 void PsSimulatedObject::addInstanceCreator(const PsName & className, PsSimulatedObjectCreator * instanceCreator ) 
00928 {
00929    pair<PsNameToPointerMap < PsSimulatedObjectCreator >::iterator, bool> result = 
00930       _encapsulatedClassesTable.insert (pair<const PsName, PsSimulatedObjectCreator *>(className,instanceCreator) ) ;
00931    if ( !result.second )
00932       //the instance creator was not inserted because an instance creator was allready
00933       {
00934          if ( PsController::warningLevel >= PsController::AllWarnings )
00935             {
00936                cerr<<"PsSimulatedObject::addInstanceCreator "
00937                    <<"replaced an instance creator for class "
00938                    <<className<<endl;
00939             }
00940          delete result.first->second ;
00941          result.first->second = instanceCreator ;
00942       }
00943 }
00944 
00945 
00946 
00947 const PsNameToPointerMap < PsSimulatedObjectCreator > & 
00948 PsSimulatedObject::getEncapsulatedClassesTable () const 
00949 {
00950    return _encapsulatedClassesTable ;
00951 }
00952 
00953 
00954 
00955 PsSimulatedObject * 
00956 PsSimulatedObject::createInstanceOfEncapsulatedClass(const PsObjectDescriptor & objectDescriptor ) const
00957 {
00958 #ifdef _DEBUGOBJECTCREATION
00959   cerr <<"PsSimulatedObject::createInstanceOfEncapsulatedClass (...)"<<endl;
00960 #endif
00961 
00962   PsSimulatedObject * result = NULL ;
00963   PsNameToPointerMap < PsSimulatedObjectCreator >::const_iterator i = _encapsulatedClassesTable.find(objectDescriptor.getClass () ) ;
00964   if (i != _encapsulatedClassesTable.end() )
00965      {
00966         assert ( i->second != NULL ) ;
00967         result = i->second->createSimulatedObject( getController() , objectDescriptor ) ;
00968      }
00969 #ifdef _DEBUGOBJECTCREATION
00970   cerr <<"PsSimulatedObject::createInstanceOfEncapsulatedClass returns "<<result<<endl;
00971 #endif
00972   return result ;
00973 }

logo OpenMask

Documentation generated on Mon Nov 25 15:25:02 2002

Generated with doxygen 1.2.12 by Dimitri van Heesch ,   1997-2001