From: Mathias Froehlich Date: Sat, 1 Oct 2011 15:44:37 +0000 (+0200) Subject: hla: Provide createJoin and resignDestroy methods. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=9e27511ef9fe29fc1df28509ae3ddb9728cf6242;p=simgear.git hla: Provide createJoin and resignDestroy methods. --- diff --git a/simgear/hla/HLAFederate.cxx b/simgear/hla/HLAFederate.cxx index 59a5f4b0..fa1cc576 100644 --- a/simgear/hla/HLAFederate.cxx +++ b/simgear/hla/HLAFederate.cxx @@ -35,24 +35,106 @@ HLAFederate::~HLAFederate() { } -std::string +HLAFederate::Version +HLAFederate::getVersion() const +{ + return _version; +} + +bool +HLAFederate::setVersion(HLAFederate::Version version) +{ + if (_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_ALERT, "HLA: Ignoring HLAFederate parameter setting on already connected federate!"); + return false; + } + _version = version; + return true; +} + +const std::list& +HLAFederate::getConnectArguments() const +{ + return _connectArguments; +} + +bool +HLAFederate::setConnectArguments(const std::list& connectArguments) +{ + if (_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_ALERT, "HLA: Ignoring HLAFederate parameter setting on already connected federate!"); + return false; + } + _connectArguments = connectArguments; + return true; +} + +const std::string& +HLAFederate::getFederationExecutionName() const +{ + return _federationExecutionName; +} + +bool +HLAFederate::setFederationExecutionName(const std::string& federationExecutionName) +{ + if (_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_ALERT, "HLA: Ignoring HLAFederate parameter setting on already connected federate!"); + return false; + } + _federationExecutionName = federationExecutionName; + return true; +} + +const std::string& +HLAFederate::getFederationObjectModel() const +{ + return _federationObjectModel; +} + +bool +HLAFederate::setFederationObjectModel(const std::string& federationObjectModel) +{ + if (_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_ALERT, "HLA: Ignoring HLAFederate parameter setting on already connected federate!"); + return false; + } + _federationObjectModel = federationObjectModel; + return true; +} + +const std::string& HLAFederate::getFederateType() const { - if (!_rtiFederate.valid()) { - SG_LOG(SG_NETWORK, SG_WARN, "HLA: Accessing unconnected federate!"); - return std::string(); + return _federateType; +} + +bool +HLAFederate::setFederateType(const std::string& federateType) +{ + if (_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_ALERT, "HLA: Ignoring HLAFederate parameter setting on already connected federate!"); + return false; } - return _rtiFederate->getFederateType(); + _federateType = federateType; + return true; } -std::string -HLAFederate::getFederationName() const +const std::string& +HLAFederate::getFederateName() const { - if (!_rtiFederate.valid()) { - SG_LOG(SG_NETWORK, SG_WARN, "HLA: Accessing unconnected federate!"); - return std::string(); + return _federateName; +} + +bool +HLAFederate::setFederateName(const std::string& federateName) +{ + if (_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_ALERT, "HLA: Ignoring HLAFederate parameter setting on already connected federate!"); + return false; } - return _rtiFederate->getFederationName(); + _federateName = federateName; + return true; } bool @@ -65,6 +147,8 @@ HLAFederate::connect(Version version, const std::list& stringList) switch (version) { case RTI13: _rtiFederate = new RTI13Federate(stringList); + _version = version; + _connectArguments = stringList; break; case RTI1516: SG_LOG(SG_IO, SG_ALERT, "HLA version RTI1516 not yet(!?) supported."); @@ -80,6 +164,31 @@ HLAFederate::connect(Version version, const std::list& stringList) return _rtiFederate.valid(); } +bool +HLAFederate::connect() +{ + if (_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_WARN, "HLA: Trying to connect to already connected federate!"); + return false; + } + switch (_version) { + case RTI13: + _rtiFederate = new RTI13Federate(_connectArguments); + break; + case RTI1516: + SG_LOG(SG_IO, SG_ALERT, "HLA version RTI1516 not yet(!?) supported."); + // _rtiFederate = new RTI1516Federate(_connectArguments); + break; + case RTI1516E: + SG_LOG(SG_IO, SG_ALERT, "HLA version RTI1516E not yet(!?) supported."); + // _rtiFederate = new RTI1516eFederate(_connectArguments); + break; + default: + SG_LOG(SG_NETWORK, SG_WARN, "HLA: Unknown rti version in connect!"); + } + return _rtiFederate.valid(); +} + bool HLAFederate::disconnect() { @@ -98,7 +207,15 @@ HLAFederate::createFederationExecution(const std::string& federation, const std: SG_LOG(SG_NETWORK, SG_WARN, "HLA: Accessing unconnected federate!"); return false; } - return _rtiFederate->createFederationExecution(federation, objectModel); + + RTIFederate::FederationManagementResult createResult; + createResult = _rtiFederate->createFederationExecution(federation, objectModel); + if (createResult == RTIFederate::FederationManagementFatal) + return false; + + _federationExecutionName = federation; + _federationObjectModel = objectModel; + return true; } bool @@ -108,7 +225,45 @@ HLAFederate::destroyFederationExecution(const std::string& federation) SG_LOG(SG_NETWORK, SG_WARN, "HLA: Accessing unconnected federate!"); return false; } - return _rtiFederate->destroyFederationExecution(federation); + + RTIFederate::FederationManagementResult destroyResult; + destroyResult = _rtiFederate->destroyFederationExecution(federation); + if (destroyResult == RTIFederate::FederationManagementFatal) + return false; + + return true; +} + +bool +HLAFederate::createFederationExecution() +{ + if (!_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_WARN, "HLA: Accessing unconnected federate!"); + return false; + } + + RTIFederate::FederationManagementResult createResult; + createResult = _rtiFederate->createFederationExecution(_federationExecutionName, _federationObjectModel); + if (createResult != RTIFederate::FederationManagementSuccess) + return false; + + return true; +} + +bool +HLAFederate::destroyFederationExecution() +{ + if (!_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_WARN, "HLA: Accessing unconnected federate!"); + return false; + } + + RTIFederate::FederationManagementResult destroyResult; + destroyResult = _rtiFederate->destroyFederationExecution(_federationExecutionName); + if (destroyResult != RTIFederate::FederationManagementSuccess) + return false; + + return true; } bool @@ -118,7 +273,29 @@ HLAFederate::join(const std::string& federateType, const std::string& federation SG_LOG(SG_NETWORK, SG_WARN, "HLA: Accessing unconnected federate!"); return false; } - return _rtiFederate->join(federateType, federation); + + RTIFederate::FederationManagementResult joinResult; + joinResult = _rtiFederate->join(federateType, federation); + if (joinResult == RTIFederate::FederationManagementFatal) + return false; + + return true; +} + +bool +HLAFederate::join() +{ + if (!_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_WARN, "HLA: Accessing unconnected federate!"); + return false; + } + + RTIFederate::FederationManagementResult joinResult; + joinResult = _rtiFederate->join(_federateType, _federationExecutionName); + if (joinResult != RTIFederate::FederationManagementSuccess) + return false; + + return true; } bool @@ -131,6 +308,61 @@ HLAFederate::resign() return _rtiFederate->resign(); } +bool +HLAFederate::createJoinFederationExecution() +{ + if (!_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_WARN, "HLA: Accessing unconnected federate!"); + return false; + } + + for (;;) { + // Try to join. + RTIFederate::FederationManagementResult joinResult; + joinResult = _rtiFederate->join(_federateType, _federationExecutionName); + switch (joinResult) { + case RTIFederate::FederationManagementSuccess: + // Fast return on success + return true; + case RTIFederate::FederationManagementFatal: + // Abort on fatal errors + return false; + default: + break; + }; + + // If not already joinable, try to create the requested federation + RTIFederate::FederationManagementResult createResult; + createResult = _rtiFederate->createFederationExecution(_federationExecutionName, _federationObjectModel); + switch (createResult) { + case RTIFederate::FederationManagementFatal: + // Abort on fatal errors + return false; + default: + // Try again to join + break; + } + } +} + +bool +HLAFederate::resignDestroyFederationExecution() +{ + if (!_rtiFederate.valid()) { + SG_LOG(SG_NETWORK, SG_WARN, "HLA: Accessing unconnected federate!"); + return false; + } + + // Resign ourselves + bool success = _rtiFederate->resign(); + + // and try to destroy, non fatal if still some federates joined + if (_rtiFederate->destroyFederationExecution(_federationExecutionName) == RTIFederate::FederationManagementFatal) + success = false; + + return success; +} + bool HLAFederate::enableTimeConstrained() { diff --git a/simgear/hla/HLAFederate.hxx b/simgear/hla/HLAFederate.hxx index 5af83396..f3b84afe 100644 --- a/simgear/hla/HLAFederate.hxx +++ b/simgear/hla/HLAFederate.hxx @@ -32,33 +32,55 @@ class RTIFederate; class HLAFederate : public SGWeakReferenced { public: + HLAFederate(); + virtual ~HLAFederate(); + enum Version { RTI13, RTI1516, RTI1516E }; - HLAFederate(); - virtual ~HLAFederate(); + Version getVersion() const; + bool setVersion(HLAFederate::Version version); + + const std::list& getConnectArguments() const; + bool setConnectArguments(const std::list& connectArguments); + + const std::string& getFederationExecutionName() const; + bool setFederationExecutionName(const std::string& federationExecutionName); + + const std::string& getFederationObjectModel() const; + bool setFederationObjectModel(const std::string& federationObjectModel); + + const std::string& getFederateType() const; + bool setFederateType(const std::string& federateType); + + const std::string& getFederateName() const; + bool setFederateName(const std::string& federateName); /// connect to an rti bool connect(Version version, const std::list& stringList); + bool connect(); bool disconnect(); - /// Get the name of the joined federate/federation - std::string getFederateType() const; - std::string getFederationName() const; - /// Create a federation execution /// Semantically this methods should be static, /// but the nonstatic case could reuse the connection to the server - /// FIXME: cannot determine from the return value if we created the execution bool createFederationExecution(const std::string& federation, const std::string& objectModel); bool destroyFederationExecution(const std::string& federation); + bool createFederationExecution(); + bool destroyFederationExecution(); /// Join with federateType the federation execution bool join(const std::string& federateType, const std::string& federation); + bool join(); bool resign(); + + /// Try to create and join the federation execution. + bool createJoinFederationExecution(); + bool resignDestroyFederationExecution(); + bool enableTimeConstrained(); bool disableTimeConstrained(); @@ -108,6 +130,14 @@ public: private: SGSharedPtr _rtiFederate; + Version _version; + std::list _connectArguments; + + std::string _federationExecutionName; + std::string _federationObjectModel; + std::string _federateType; + std::string _federateName; + typedef std::map > ObjectClassMap; ObjectClassMap _objectClassMap; diff --git a/simgear/hla/RTI13Federate.cxx b/simgear/hla/RTI13Federate.cxx index e79f4865..bb86d12f 100644 --- a/simgear/hla/RTI13Federate.cxx +++ b/simgear/hla/RTI13Federate.cxx @@ -597,83 +597,81 @@ RTI13Federate::~RTI13Federate() delete _federateAmbassador; } -bool +RTI13Federate::FederationManagementResult RTI13Federate::createFederationExecution(const std::string& federationName, const std::string& objectModel) { try { _ambassador->createFederationExecution(federationName, objectModel); - return true; + return FederationManagementSuccess; } catch (RTI::FederationExecutionAlreadyExists& e) { - return true; + return FederationManagementFail; } catch (RTI::CouldNotOpenFED& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not create federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } catch (RTI::ErrorReadingFED& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not create federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } catch (RTI::ConcurrentAccessAttempted& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not create federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } catch (RTI::RTIinternalError& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not create federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } } -bool +RTI13Federate::FederationManagementResult RTI13Federate::destroyFederationExecution(const std::string& federation) { try { _ambassador->destroyFederationExecution(federation); - return true; + return FederationManagementSuccess; } catch (RTI::FederatesCurrentlyJoined& e) { - return true; + return FederationManagementFail; } catch (RTI::FederationExecutionDoesNotExist& e) { - return true; + return FederationManagementFail; } catch (RTI::ConcurrentAccessAttempted& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not destroy federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } catch (RTI::RTIinternalError& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not destroy federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } } -bool +RTI13Federate::FederationManagementResult RTI13Federate::join(const std::string& federateType, const std::string& federationName) { try { _federateHandle = _ambassador->joinFederationExecution(federateType, federationName, _federateAmbassador); SG_LOG(SG_NETWORK, SG_INFO, "RTI: Joined federation \"" << federationName << "\" as \"" << federateType << "\""); - setFederateType(federateType); - setFederationName(federationName); _joined = true; - return true; + return FederationManagementSuccess; } catch (RTI::FederateAlreadyExecutionMember& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not join federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } catch (RTI::FederationExecutionDoesNotExist& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not join federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFail; } catch (RTI::CouldNotOpenFED& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not join federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } catch (RTI::ErrorReadingFED& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not join federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } catch (RTI::ConcurrentAccessAttempted& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not join federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } catch (RTI::SaveInProgress& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not join federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } catch (RTI::RestoreInProgress& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not join federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } catch (RTI::RTIinternalError& e) { SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not join federation execution: " << e._name << " " << e._reason); - return false; + return FederationManagementFatal; } } diff --git a/simgear/hla/RTI13Federate.hxx b/simgear/hla/RTI13Federate.hxx index e8808e3a..0a8cee4d 100644 --- a/simgear/hla/RTI13Federate.hxx +++ b/simgear/hla/RTI13Federate.hxx @@ -37,11 +37,13 @@ public: RTI13Federate(const std::list& stringList); virtual ~RTI13Federate(); - virtual bool createFederationExecution(const std::string& federation, const std::string& objectModel); - virtual bool destroyFederationExecution(const std::string& federation); + /// Create a federation execution + /// Semantically this methods should be static, + virtual FederationManagementResult createFederationExecution(const std::string& federation, const std::string& objectModel); + virtual FederationManagementResult destroyFederationExecution(const std::string& federation); /// Join with federateName the federation execution federation - virtual bool join(const std::string& federateType, const std::string& federation); + virtual FederationManagementResult join(const std::string& federateType, const std::string& federation); virtual bool resign(); /// Synchronization Point handling diff --git a/simgear/hla/RTIFederate.hxx b/simgear/hla/RTIFederate.hxx index cc1a797f..b7aa8854 100644 --- a/simgear/hla/RTIFederate.hxx +++ b/simgear/hla/RTIFederate.hxx @@ -32,21 +32,19 @@ public: RTIFederate(); virtual ~RTIFederate(); - /// Get the name of the joined federate/federation - const std::string& getFederateType() const - { return _federateType; } - const std::string& getFederationName() const - { return _federationName; } - /// Create a federation execution /// Semantically this methods should be static, - /// but the nonstatic case could reuse the connection to the server - /// FIXME: cannot determine from the return value if we created the execution - virtual bool createFederationExecution(const std::string& federation, const std::string& objectModel) = 0; - virtual bool destroyFederationExecution(const std::string& federation) = 0; + enum FederationManagementResult { + FederationManagementSuccess, + FederationManagementFail, + FederationManagementFatal + }; + + virtual FederationManagementResult createFederationExecution(const std::string& federation, const std::string& objectModel) = 0; + virtual FederationManagementResult destroyFederationExecution(const std::string& federation) = 0; /// Join with federateName the federation execution federation - virtual bool join(const std::string& federateType, const std::string& federation) = 0; + virtual FederationManagementResult join(const std::string& federateType, const std::string& federation) = 0; virtual bool resign() = 0; /// Synchronization Point handling @@ -81,21 +79,9 @@ public: virtual RTIObjectInstance* getObjectInstance(const std::string& name) = 0; -protected: - void setFederateType(const std::string& federateType) - { _federateType = federateType; } - void setFederationName(const std::string& federationName) - { _federationName = federationName; } - private: RTIFederate(const RTIFederate&); RTIFederate& operator=(const RTIFederate&); - - /// The federates name - std::string _federateType; - - /// The federation execution name - std::string _federationName; }; }