00001 #ifndef TIME_MANAGER_H_ 00002 #define TIME_MANAGER_H_ 00003 00004 #include "syncstatus.h" 00005 #include "SaveStatus.h" 00006 00007 #if defined(_MIPS_SIM) 00008 #pragma set woff 3201 00009 #pragma set woff 1209 00010 #pragma set woff 1424 00011 #pragma set woff 1110 00012 #endif 00013 00014 #include <RTItypes.hh> 00015 #include <fedtime.hh> 00016 00017 #if defined(_MIPS_SIM) 00018 #pragma reset woff 3201 00019 #pragma reset woff 1209 00020 #pragma reset woff 1424 00021 #pragma reset woff 1110 00022 #endif 00023 00024 /** TimeManager - handels the HLA time synchronization. 00025 The TimeManager creates an interface for the Federate to send time synchronization 00026 messages and to receive synchronization messages from the RTI-Ambassador. The 00027 TimeManager class provides some high level methods for time synchronization - these 00028 methods generally start with waitFor... e.g. waitForFederationSynchronized or have 00029 ...WaitFor... as part of the method name. 00030 @ingroup ftk 00031 */ 00032 class TimeManager 00033 { 00034 public: 00035 00036 /** This method creates an instance of the TimeManager class if it doesn't exist. 00037 @return the TimeManager instance. 00038 */ 00039 static TimeManager &Instance(); 00040 00041 00042 /// Destructor does nothing special. 00043 ~TimeManager(); 00044 00045 00046 /** This method sets the federation time. 00047 @param time The new federation time. 00048 */ 00049 void federationTimeSet(RTIfedTime& time) {_federationTime = time;} 00050 00051 00052 /** This method returns the federation time. 00053 @return The federation time. 00054 */ 00055 RTIfedTime& federationTimeGet() {return _federationTime;} 00056 00057 00058 /** This method sets the Federate's local time. 00059 @param time The new local time. 00060 */ 00061 void localTimeSet(RTIfedTime& time) {_localTime = time;} 00062 00063 00064 /** This method returns the Federate's local time. 00065 @return The Federate's local time. 00066 */ 00067 RTIfedTime& localTimeGet() {return _localTime;} 00068 00069 00070 /// This method sets the local and reset time to the Federation time. 00071 void init(); 00072 00073 00074 /** This method resets the local time to 0.0 clears the synchronization flags 00075 and restores the flags. 00076 */ 00077 void reset(); 00078 00079 00080 /** This method enables the time regulation for the Federate. 00081 @param time The Federation time. 00082 @param lookahead The time horizont. 00083 */ 00084 void enableTimeRegulation(const RTIfedTime& time, const RTIfedTime& lookahead); 00085 00086 00087 /// This method disables the time regulation for the Federate. 00088 void disableTimeRegulation(); 00089 00090 00091 /// This method makes the Federate time constrained. 00092 void enableTimeConstrained(); 00093 00094 00095 /// This method disables the time constrained condition for the Federate. 00096 void disableTimeConstrained(); 00097 00098 00099 /** This method implements the HLA time advance request. 00100 @param theTime The new time the Federate asks to advance to. 00101 */ 00102 void timeAdvanceRequest(const RTIfedTime& theTime); 00103 00104 00105 /** This method implements the HLA time advance available method. 00106 @param theTime The new time the Federate asks to advance to. 00107 */ 00108 void timeAdvanceRequestAvailable(const RTIfedTime& theTime); 00109 00110 00111 /** This method implements the HLA next event method. 00112 @param theTime The new time the Federate asks to advance to. 00113 */ 00114 void nextEventRequest(const RTIfedTime& theTime); 00115 00116 00117 /** This method implements the HLA next event available method. 00118 @param theTime The new time the Federate asks to advance to. 00119 */ 00120 void nextEventRequestAvailable(const RTIfedTime& theTime); 00121 00122 00123 /** This method advances the local time. 00124 @param requestedTime New local time. 00125 */ 00126 void advanceLocalTime(const RTIfedTime& requestedTime); 00127 00128 00129 /** This high-level method requests to advance to the time given and 00130 waits until the request is granted (or time out). 00131 */ 00132 void requestTimeAdvanceAndWaitForGrant(); 00133 00134 /// This method requests a time advance to the Federate's local time. 00135 void requestTimeAdvance(); 00136 00137 00138 /** This method checks if the time advance is granted. 00139 @return true if the time advance is granted. 00140 */ 00141 bool isTimeAdvanceGranted(); 00142 00143 00144 /** This method returns the granted flag. 00145 @return the granted flag. 00146 */ 00147 bool isGranted() { return _granted; }; 00148 00149 00150 /** This method returns true if the granted flag is set to false. 00151 @return Flase if granted flag is true. 00152 */ 00153 bool isAdvancing() { return !_granted; }; 00154 00155 00156 /** This method returns the time horizont. 00157 @return lookahed. 00158 */ 00159 RTIfedTime& lookAheadGet() { return _lookAhead; } 00160 00161 00162 /** This method informs the RTI the the synchronization point has been achieved. 00163 @param label The synchronization point label. 00164 */ 00165 void synchronizationPointAchieved(const char *label); 00166 00167 00168 /** This is a high-level method to register a synchronization point and wait 00169 for a reply from the RTI. 00170 @param syncLabel The synchronization point label. 00171 @param timeOut The timeout (default 600 seconds) 00172 */ 00173 bool waitForRegisterSynchronizationPoint(const char* syncLabel, int timeOut = 600); 00174 00175 00176 /** This is a high-level method waits until a specific synchronization point is 00177 announced by the RTI. 00178 @param syncLabel The synchronization point label. 00179 @param timeOut The timeout (default 600 seconds) 00180 */ 00181 bool waitForSynchronizationPointAnnounced(const char* syncLabel, 00182 int timeOut = 600); 00183 00184 00185 /** This is a high-level method waits until the Federation is synchrnized at a 00186 given synchronization point. 00187 @param syncLabel The synchronization point label. 00188 @param timeOut The timeout (default 600 seconds) 00189 */ 00190 bool waitForFederationSynchronization(const char* syncLabel, 00191 int timeOut = 600); 00192 00193 00194 /** This method implements the HLA method, registerSynchronizationSet 00195 @param syncLabel The synchronization point label. 00196 @param syncSet Federate Handle Set. 00197 */ 00198 void registerSynchronizationSet(const char* syncLabel, 00199 const RTI::FederateHandleSet& syncSet); 00200 00201 00202 /** This method implements the HLA method, synchronizationRegistrationConfirmed 00203 (from Federate Ambssador). 00204 @param syncLabel The synchronization point label. 00205 */ 00206 bool synchronizationRegistrationConfirmed(const char* syncLabel); 00207 00208 00209 /** This method implements the HLA method, synchronizationRegistrationFailed 00210 (from Federate Ambssador). 00211 @param syncLabel The synchronization point label. 00212 */ 00213 bool synchronizationRegistrationFailed(const char* syncLabel); 00214 00215 00216 /** This method implements the HLA synchronizationPointAnnounced 00217 (from Federate Ambssador). 00218 @param syncLabel The synchronization point label. 00219 */ 00220 bool synchronizationPointAnnounced(const char* syncLabel); 00221 00222 00223 /** This method implements the HLA method,federationSynchronized 00224 (from Federate Ambssador). 00225 @param syncLabel The synchronization point label. 00226 */ 00227 bool federationSynchronized(const char* syncLabel); 00228 00229 00230 /** This method implements the HLA method requestFederationSave 00231 (to the RTI ambassador). 00232 @param label Synchronization point label. 00233 */ 00234 void requestFederationSave(string label); 00235 00236 00237 /** This method implements the HLA method requestFederationSave 00238 (to the RTI ambassador). 00239 @param label Synchronization point label. 00240 @param theTime Requested Federation time for save. 00241 */ 00242 void requestFederationSave(string label, RTIfedTime& theTime); 00243 00244 00245 /** This method returns the initSave flag. 00246 @param label Synchronization point label. 00247 @return true if a save with this label was initiated. 00248 */ 00249 bool federateSaveInitiated(const char *label); 00250 00251 00252 /** This high-level method waits for a FederationSaveInitiated message from the RTI. 00253 @param syncLabel Synchronization point label. 00254 @param timeOut Time after which this method returns false (default 600 seconds). 00255 */ 00256 bool waitForFederateSaveInitiated(string syncLabel, int timeOut = 600); 00257 00258 00259 /// this method informs the RTI ambassador that Federation save has begun 00260 void federateSaveBegun(); 00261 00262 /** This method returns the federationSaved label. 00263 @param label The synchronization point label. 00264 return True if the Federation is saved. 00265 */ 00266 bool isFederationSaved(string label); 00267 00268 00269 /** This method returns the federationNotSaved label. 00270 @param label The synchronization point label. 00271 return the federationNotSaved label. 00272 */ 00273 bool isFederationNotSaved(string label); 00274 00275 00276 /** This method waits until the Federation is saved. 00277 @param syncLabel Synchronization point label. 00278 @param timeOut Time after which this method returns false (default 600 seconds). 00279 */ 00280 bool waitForFederationSaved(string syncLabel, int timeOut = 600); 00281 00282 00283 /** This method requests to restore the Federation to the given synchronization point. 00284 @param label The synchronization point label. 00285 */ 00286 void requestFederationRestore(string label); 00287 00288 00289 /** This method waits until the RTI requests to restore the Federation to a given 00290 synchronization point. 00291 @param syncLabel Synchronization point label. 00292 @param timeOut Time after which this method returns false (default 600 seconds). 00293 @return true if the Federation restore request is accepted. 00294 */ 00295 bool waitForRequestFederationRestore(string syncLabel, int timeOut = 600); 00296 00297 00298 /** This method implemets the HLA RTI Ambassador method federateRestoreComplete. 00299 It informs the RTI that the Federate has completed its restore. 00300 */ 00301 void federateRestoreComplete(); 00302 00303 00304 /** This method implemets the HLA RTI Ambassador method federateRestoreNotComplete. 00305 It informs the RTI that the Federate was unable restore its state. 00306 */ 00307 void federateRestoreNotComplete(); 00308 00309 00310 // These functions are called by the federateAmbassador 00311 // ---------------------------------------------------- 00312 00313 00314 /** This method sets the registered flag indicating the the registration succeeded. 00315 @param label Synchronization point Label. 00316 */ 00317 void synchronizationPointRegistrationSucceeded(const char *label); 00318 00319 00320 /** This method sets the failed flag indicating the the registration failed. 00321 @param label Synchronization point Label. 00322 */ 00323 void synchronizationPointRegistrationFailed(const char *label); 00324 00325 00326 /** This method sets the announced flag indicating the the registration has been announced. 00327 @param label Synchronization point Label. 00328 */ 00329 void announceSynchronizationPoint(const char *label); 00330 00331 00332 /** This method sets the confirmed flag indicating that the Federation has been 00333 synchronized at synchronization point indicated by the label. 00334 @param label Synchronization point Label. 00335 */ 00336 void federationSynchronizationConfirmed(const char *label); 00337 00338 00339 /** This method sets the time regulation flag and the Federtion time, 00340 indicating that the Fedearte is now time regulating. 00341 @param time Federation time. 00342 */ 00343 void timeRegulationEnabled(const RTI::FedTime &time); 00344 00345 00346 /** This method returns the time regulation flag. 00347 @return True if the Federate is time regulating. 00348 */ 00349 bool timeRegulationEnabled() {return _timeRegulationFlag;} 00350 00351 00352 /** This method sets the time constrained flag and the Federtion time, 00353 indicating that the Fedearte is now time constrained. 00354 @param time Federation time. 00355 */ 00356 void timeConstrainedEnabled(const RTI::FedTime &time); 00357 00358 00359 /** This method returns the time constrained flag. 00360 @return True if the Federate is time constrained. 00361 */ 00362 bool timeConstrainedEnabled() {return _timeConstrainedFlag;} 00363 00364 00365 /** This method sets the time advance granted flag and the Federtion time, 00366 indicating that the Fedeartion time has been advanced to the given time. 00367 @param time Federation time. 00368 */ 00369 void timeAdvanceGrant(const RTI::FedTime &time); 00370 00371 00372 /** This method checks if the Federate is fully time enabled. 00373 @return true if both, the timeRegulation and timeConstrained flags are set. 00374 */ 00375 bool timeEnabled(); 00376 00377 00378 /** This method initiates a new save structure. 00379 @param label Synchronization point Label. 00380 */ 00381 void initiateFederateSave(const char *label); 00382 00383 00384 /// This method sets the saved flag to true on the curren save struct. 00385 void federationSaved(); 00386 00387 00388 /// This method sets the notSaved flag to true on the curren save struct. 00389 void federationNotSaved(); 00390 00391 00392 /** This method sets the requestRestore flag to true. 00393 @param label Synchronization point Label. 00394 */ 00395 void requestFederationRestoreSucceeded (const char *label); 00396 00397 00398 /** This method sets the requestRestoreFailed flag to true. 00399 @param label Synchronization point Label. 00400 @param reason A string indicating the reason for the failure. 00401 */ 00402 void requestFederationRestoreFailed (const char *label, const char *reason); 00403 00404 00405 /** This method sets the requestRestoreFailed flag to true. 00406 @param label Synchronization point Label. 00407 */ 00408 void requestFederationRestoreFailed (const char *label); 00409 00410 00411 /** This method returns the requestRestore flag. 00412 @param label Synchronization point label. 00413 */ 00414 bool didRestoreRequestSucceed(string label); 00415 00416 00417 /** This method returns the requestRestoreFailed flag. 00418 @param label Synchronization point label. 00419 */ 00420 bool didRestoreRequestNotSucceed(string label); 00421 00422 00423 /** This method resets the restored and notRestored flags to false. 00424 @param label Synchronization point label. 00425 */ 00426 void resetRequestFederationRestoreFlags(const char *label); 00427 00428 00429 /** This method set the restoreBegun flag to true and requests the RFI to 00430 clear the ReturnValues from the current run. 00431 */ 00432 void federationRestoreBegun(); 00433 00434 00435 /** This method returns the restoreBegun flag. 00436 @return True if the simulation is resetting to time 0.0. 00437 */ 00438 bool hasFederationRestoreBegun(); 00439 00440 00441 /** This is a high-level mentod that returns true after the Fedeartion has 00442 started a restore or flase if the method timed out. 00443 @param timeOut Settable timeout, default = 600 secsonds. 00444 */ 00445 bool waitForFederationRestoreBegun(int timeOut = 600); 00446 00447 00448 /// This method sets the restoreBegun flag to false. 00449 void resetRestoreBegunFlag(); 00450 00451 00452 /** This method returns true if a Federation restore has begun. 00453 @param label Synchronization point label. 00454 @return true if the Federation is restored. 00455 */ 00456 bool restoreFederate(string label); 00457 00458 00459 /** This method setes the initRestreFlag to true and saves the synchronization 00460 point label. 00461 @param label Synchronization point label. 00462 @param handle The Federate handle. 00463 */ 00464 void initiateFederateRestore(const char *label, RTI::FederateHandle handle); 00465 00466 00467 /** This is a high-level method that returns true when the restore is initiated 00468 or false if the method timed out. 00469 @param syncLabel Synchronization point label. 00470 @param timeOut Method timeout, default = 600 seconds. 00471 */ 00472 bool waitForFederateRestoreInitiated(string syncLabel, int timeOut = 600); 00473 00474 /** This method returns the initRestore flag. 00475 @param label Synchronization point label. 00476 @returns true if the specified restore is initiated. 00477 */ 00478 bool isFederateRestoreInitiated(string label); 00479 00480 00481 /** This method returns the notInitRestore flag. 00482 @param label Synchronization point label. 00483 @returns true if the specified restore request has failed. 00484 */ 00485 bool isFederateRestoreNotInitiated(string label); 00486 00487 00488 /// This method sets the restored flag to true. 00489 void federationRestored(); 00490 00491 00492 /** This method returns the value of the restored flag. 00493 @param label Synchronization point label. 00494 @return true if the Federation is restored. 00495 */ 00496 bool isFederationRestored(string label); 00497 00498 00499 /** This is a high-level method that returns true when the Federation is 00500 restored or false if the method timed out. 00501 @param syncLabel Synchronization point label. 00502 @param timeOut Method timeout, default = 600 seconds. 00503 */ 00504 bool waitForFederationRestored(string syncLabel, int timeOut = 600); 00505 00506 00507 /// This method sets the notRestored flag to true. 00508 void federationNotRestored(); 00509 00510 00511 /** This method returns true if the Fedeeration failed to restore. 00512 @param label Synchronization point label. 00513 @return true if restore failed. 00514 */ 00515 bool isFederationNotRestored(string label); 00516 00517 00518 /// This method informs the RTI that the Federate has completed its save procedure. 00519 void federateSaveComplete(); 00520 00521 00522 /// This method informs the RTI that the Federate has failed its save procedure. 00523 void federateSaveNotComplete(); 00524 00525 00526 /** This method sets the initSave flag to false. 00527 @param label Synchronization point label. 00528 */ 00529 void resetInitSaveFlag(string label); 00530 00531 00532 /** This method sets the initRestore flag to false. 00533 @param label Synchronization point label. 00534 */ 00535 void resetInitRestoreFlag(string label); 00536 00537 00538 /** This method sets the restored and not Restred flags to false. 00539 @param label Synchronization point label. 00540 */ 00541 void resetRestoredFlags(string label); 00542 00543 00544 /** This method sets all the save and restore flags to false. 00545 @param label Synchronization point label. 00546 */ 00547 void resetSaveAndRestoreFlags(string label); 00548 00549 00550 /** This high-level method returns true when the Federation is synchronized. 00551 @param label Synchronization point label. 00552 */ 00553 bool waitForFederationSynchronized(const char* label); 00554 00555 00556 /** This high-level method returns true when the Federation is synchronized. 00557 @param label Synchronization point label. 00558 @param timeOut Method timeout, default = 600 seconds. 00559 */ 00560 bool waitForFederationSaveSynchronized(const char* label, int timeOut = 600); 00561 00562 00563 /** This high-level method returns true when the Federation is 00564 synchronized hand all Fedeartes hace saved their states 00565 or false if the method timed out. 00566 @param label Synchronization point label. 00567 */ 00568 bool waitForFederationSynchronizedAndSaved(const char* label); 00569 00570 private: 00571 00572 /// Disable constructor. 00573 TimeManager(); 00574 00575 /// Disable copy constructor. 00576 TimeManager(const TimeManager&); 00577 00578 /// Disable assignment operator. 00579 TimeManager &operator = (const TimeManager&); 00580 00581 /// Make sure the object gets destroyed when the program exits. 00582 static void Destroy(); 00583 00584 /// Instance of the TimeManager class. 00585 static TimeManager *_theTimeManager; 00586 00587 /// Federation Time. 00588 RTIfedTime _federationTime; 00589 00590 /// The Fedearte's local time. 00591 RTIfedTime _localTime; 00592 00593 /// The time the Fedeartion will have after a reset (0.0). 00594 RTIfedTime _resetTime; 00595 00596 /// A flag indicating time regulation. 00597 bool _timeRegulationFlag; 00598 00599 /// A flag indicating time constrained. 00600 bool _timeConstrainedFlag; 00601 00602 /// Label of the current synchronization point. 00603 string _saveLabel; 00604 00605 /// A class for managing time synchronization states. 00606 SyncStatus _syncStatus; 00607 00608 /// A class for managing save and restore synchronization states. 00609 SaveStatus _saveStatus; 00610 00611 /// A set of Federate handles that participate in a certain save/restore operation. 00612 const RTI::FederateHandleSet *_syncSet; 00613 00614 /// A flag indicating if synchronization restore has begun. 00615 bool _restoreBegun; 00616 00617 /// A flag indicating if the time advance request has been granted. 00618 bool _granted; 00619 00620 /// A time indicating the time horizont. 00621 RTIfedTime _lookAhead; 00622 }; 00623 00624 #endif // TIME_MANAGER_H_ 00625