]> git.mxchange.org Git - simgear.git/commitdiff
hla: Move callbacks into the rti federate implementation.
authorMathias Froehlich <Mathias.Froehlich@web.de>
Sat, 1 Oct 2011 07:10:57 +0000 (09:10 +0200)
committerMathias Froehlich <Mathias.Froehlich@web.de>
Sat, 1 Oct 2011 07:10:57 +0000 (09:10 +0200)
simgear/hla/RTI13Ambassador.hxx
simgear/hla/RTI13Federate.cxx
simgear/hla/RTI13Federate.hxx
simgear/hla/RTI13ObjectClass.cxx
simgear/hla/RTI13ObjectClass.hxx
simgear/hla/RTI13ObjectInstance.cxx
simgear/hla/RTI13ObjectInstance.hxx
simgear/hla/RTIObjectInstance.hxx

index 5446f8583d4dded819cd2d793af3f6adfdd3164a..2465e349cc9bfd83a0d48f61b84b066edc1524c4 100644 (file)
@@ -15,8 +15,8 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 
-#ifndef RTIAmbassador_hxx
-#define RTIAmbassador_hxx
+#ifndef RTI13Ambassador_hxx
+#define RTI13Ambassador_hxx
 
 #include <cstdlib>
 #include <list>
 
 namespace simgear {
 
-class RTI13Ambassador : public SGWeakReferenced {
+class RTI13Federate;
+
+class RTI13Ambassador : public SGReferenced {
 public:
-    RTI13Ambassador() :
-        _federateAmbassador(*this),
-        _timeRegulationEnabled(false),
-        _timeConstrainedEnabled(false),
-        _timeAdvancePending(false)
-    { }
-    virtual ~RTI13Ambassador()
+    ~RTI13Ambassador()
     { }
 
-    // processes the queues that filled up during the past
-    void processQueues()
-    {
-        while (!_queueCallbackList.empty()) {
-            (*_queueCallbackList.front())();
-            _queueCallbackList.pop_front();
-        }
-
-        while (!_objectInstancePendingCallbackList.empty()) {
-            (*_objectInstancePendingCallbackList.begin())->flushPendingRequests();
-            _objectInstancePendingCallbackList.erase(_objectInstancePendingCallbackList.begin());
-        }
-    }
-
-    bool getTimeRegulationEnabled() const
-    { return _timeRegulationEnabled; }
-    bool getTimeConstrainedEnabled() const
-    { return _timeConstrainedEnabled; }
-    bool getTimeAdvancePending() const
-    { return _timeAdvancePending; }
-    const SGTimeStamp& getCurrentLogicalTime() const
-    { return _federateTime; }
-
-    bool getFederationSynchronizationPointAnnounced(const std::string& label)
-    { return _pendingSyncLabels.find(label) != _pendingSyncLabels.end(); }
-    bool getFederationSynchronized(const std::string& label)
-    {
-        std::set<std::string>::iterator i = _syncronizedSyncLabels.find(label);
-        if (i == _syncronizedSyncLabels.end())
-            return false;
-        _syncronizedSyncLabels.erase(i);
-        return true;
-    }
-
     void createFederationExecution(const std::string& name, const std::string& objectModel)
     { _rtiAmbassador.createFederationExecution(name.c_str(), objectModel.c_str()); }
     void destroyFederationExecution(const std::string& name)
     { _rtiAmbassador.destroyFederationExecution(name.c_str()); }
 
-    RTI::FederateHandle joinFederationExecution(const std::string& federate, const std::string& federation)
-    { return _rtiAmbassador.joinFederationExecution(federate.c_str(), federation.c_str(), &_federateAmbassador); }
+    RTI::FederateHandle joinFederationExecution(const std::string& federate, const std::string& federation, RTI::FederateAmbassador* federateAmbassador)
+    { return _rtiAmbassador.joinFederationExecution(federate.c_str(), federation.c_str(), federateAmbassador); }
     void resignFederationExecution()
     { _rtiAmbassador.resignFederationExecution(RTI::DELETE_OBJECTS_AND_RELEASE_ATTRIBUTES); }
 
@@ -114,13 +76,8 @@ public:
     void unsubscribeObjectClass(const RTI::ObjectClassHandle& handle)
     { _rtiAmbassador.unsubscribeObjectClass(handle); }
 
-    RTI13ObjectInstance* registerObjectInstance(const RTI13ObjectClass* objectClass, HLAObjectInstance* hlaObjectInstance)
-    {
-        RTI::ObjectHandle objectHandle = _rtiAmbassador.registerObjectInstance(objectClass->getHandle());
-        RTI13ObjectInstance* objectInstance = new RTI13ObjectInstance(objectHandle, hlaObjectInstance, objectClass, this, true);
-        _objectInstanceMap[objectHandle] = objectInstance;
-        return objectInstance;
-    }
+    RTI::ObjectHandle registerObjectInstance(const RTI::ObjectClassHandle& handle)
+    { return _rtiAmbassador.registerObjectInstance(handle); }
     void updateAttributeValues(const RTI::ObjectHandle& objectHandle, const RTI::AttributeHandleValuePairSet& attributeValues,
                                const SGTimeStamp& timeStamp, const RTIData& tag)
     { _rtiAmbassador.updateAttributeValues(objectHandle, attributeValues, toFedTime(timeStamp), tag.data()); }
@@ -133,38 +90,11 @@ public:
     // { _rtiAmbassador.sendInteraction(interactionClassHandle, parameters, tag.data()); }
 
     void deleteObjectInstance(const RTI::ObjectHandle& objectHandle, const SGTimeStamp& timeStamp, const RTIData& tag)
-    {
-        RTI::EventRetractionHandle h = _rtiAmbassador.deleteObjectInstance(objectHandle, toFedTime(timeStamp), tag.data());
-        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
-        if (i == _objectInstanceMap.end()) {
-            SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class: ObjectInstance not found.");
-            return;
-        }
-        _objectInstancePendingCallbackList.erase(i->second);
-        _objectInstanceMap.erase(i);
-    }
+    { RTI::EventRetractionHandle h = _rtiAmbassador.deleteObjectInstance(objectHandle, toFedTime(timeStamp), tag.data()); }
     void deleteObjectInstance(const RTI::ObjectHandle& objectHandle, const RTIData& tag)
-    {
-        _rtiAmbassador.deleteObjectInstance(objectHandle, tag.data());
-        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
-        if (i == _objectInstanceMap.end()) {
-            SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class: ObjectInstance not found.");
-            return;
-        }
-        _objectInstancePendingCallbackList.erase(i->second);
-        _objectInstanceMap.erase(i);
-    }
+    { _rtiAmbassador.deleteObjectInstance(objectHandle, tag.data()); }
     void localDeleteObjectInstance(const RTI::ObjectHandle& objectHandle)
-    {
-        _rtiAmbassador.localDeleteObjectInstance(objectHandle);
-        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
-        if (i == _objectInstanceMap.end()) {
-            SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class: ObjectInstance not found.");
-            return;
-        }
-        _objectInstancePendingCallbackList.erase(i->second);
-        _objectInstanceMap.erase(i);
-    }
+    { _rtiAmbassador.localDeleteObjectInstance(objectHandle); }
 
     void requestObjectAttributeValueUpdate(const RTI::ObjectHandle& handle, const RTI::AttributeHandleSet& attributeHandleSet)
     { _rtiAmbassador.requestObjectAttributeValueUpdate(handle, attributeHandleSet); }
@@ -354,17 +284,17 @@ public:
     void enableTimeRegulation(const SGTimeStamp& federateTime, const SGTimeStamp& lookahead)
     { _rtiAmbassador.enableTimeRegulation(toFedTime(federateTime), toFedTime(lookahead)); }
     void disableTimeRegulation()
-    { _rtiAmbassador.disableTimeRegulation(); _timeRegulationEnabled = false; }
+    { _rtiAmbassador.disableTimeRegulation();}
 
     void enableTimeConstrained()
     { _rtiAmbassador.enableTimeConstrained(); }
     void disableTimeConstrained()
-    { _rtiAmbassador.disableTimeConstrained(); _timeConstrainedEnabled = false; }
+    { _rtiAmbassador.disableTimeConstrained(); }
 
     void timeAdvanceRequest(const SGTimeStamp& time)
-    { _rtiAmbassador.timeAdvanceRequest(toFedTime(time)); _timeAdvancePending = true; }
+    { _rtiAmbassador.timeAdvanceRequest(toFedTime(time)); }
     void timeAdvanceRequestAvailable(const SGTimeStamp& time)
-    { _rtiAmbassador.timeAdvanceRequestAvailable(toFedTime(time)); _timeAdvancePending = true; }
+    { _rtiAmbassador.timeAdvanceRequestAvailable(toFedTime(time)); }
 
     // bool queryLBTS(double& time)
     // {
@@ -396,19 +326,6 @@ public:
         timeStamp = toTimeStamp(fedTime);
     }
 
-    RTI13ObjectClass* createObjectClass(const std::string& name, HLAObjectClass* hlaObjectClass)
-    {
-        RTI::ObjectClassHandle objectClassHandle;
-        objectClassHandle = getObjectClassHandle(name);
-        if (_objectClassMap.find(objectClassHandle) != _objectClassMap.end()) {
-            SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not create object class, object class already exists!");
-            return 0;
-        }
-        RTI13ObjectClass* rtiObjectClass;
-        rtiObjectClass = new RTI13ObjectClass(hlaObjectClass, objectClassHandle, this);
-        _objectClassMap[objectClassHandle] = rtiObjectClass;
-        return rtiObjectClass;
-    }
     RTI::ObjectClassHandle getObjectClassHandle(const std::string& name)
     { return _rtiAmbassador.getObjectClassHandle(name.c_str()); }
     std::string getObjectClassName(const RTI::ObjectClassHandle& handle)
@@ -469,17 +386,6 @@ public:
     //     return parameterName;
     // }
 
-    RTI13ObjectInstance* getObjectInstance(const std::string& name)
-    {
-        RTI::ObjectHandle objectHandle;
-        objectHandle = getObjectInstanceHandle(name);
-        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
-        if (i == _objectInstanceMap.end()) {
-            SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class: ObjectInstance not found.");
-            return 0;
-        }
-        return i->second;
-    }
     RTI::ObjectHandle getObjectInstanceHandle(const std::string& name)
     { return _rtiAmbassador.getObjectInstanceHandle(name.c_str()); }
     std::string getObjectInstanceName(const RTI::ObjectHandle& objectHandle)
@@ -535,498 +441,6 @@ public:
     bool tick(double minimum, double maximum)
     { return _rtiAmbassador.tick(minimum, maximum); }
 
-    void addObjectInstanceForCallback(RTIObjectInstance* objectIntance)
-    { _objectInstancePendingCallbackList.insert(objectIntance); }
-
-private:
-    /// Generic callback to execute some notification on objects in a way that they are not prone to
-    /// ConcurrentAccess exceptions.
-    class QueueCallback : public SGReferenced {
-    public:
-        virtual ~QueueCallback() {}
-        virtual void operator()() = 0;
-    };
-
-    class RemoveObjectCallback : public QueueCallback {
-    public:
-        RemoveObjectCallback(SGSharedPtr<RTIObjectInstance> objectInstance, const RTIData& tag) :
-            _objectInstance(objectInstance),
-            _tag(tag)
-        { }
-        virtual void operator()()
-        {
-            _objectInstance->removeInstance(_tag);
-        }
-    private:
-        SGSharedPtr<RTIObjectInstance> _objectInstance;
-        RTIData _tag;
-    };
-
-    /// Just the interface class doing the callbacks into the parent class
-    struct FederateAmbassador : public RTI::FederateAmbassador {
-        FederateAmbassador(RTI13Ambassador& rtiAmbassador) :
-            _rtiAmbassador(rtiAmbassador)
-        {
-        }
-        virtual ~FederateAmbassador()
-        throw (RTI::FederateInternalError)
-        {
-        }
-
-        /// RTI federate ambassador callback functions.
-        virtual void synchronizationPointRegistrationSucceeded(const char* label)
-            throw (RTI::FederateInternalError)
-        {
-        }
-
-        virtual void synchronizationPointRegistrationFailed(const char* label)
-            throw (RTI::FederateInternalError)
-        {
-        }
-
-        virtual void announceSynchronizationPoint(const char* label, const char* tag)
-            throw (RTI::FederateInternalError)
-        {
-            _rtiAmbassador._pendingSyncLabels.insert(toStdString(label));
-        }
-
-        virtual void federationSynchronized(const char* label)
-            throw (RTI::FederateInternalError)
-        {
-            std::string s = toStdString(label);
-            _rtiAmbassador._pendingSyncLabels.erase(s);
-            _rtiAmbassador._syncronizedSyncLabels.insert(s);
-        }
-
-        virtual void initiateFederateSave(const char* label)
-            throw (RTI::UnableToPerformSave,
-                   RTI::FederateInternalError)
-        {
-        }
-
-        virtual void federationSaved()
-            throw (RTI::FederateInternalError)
-        {
-        }
-
-        virtual void federationNotSaved()
-            throw (RTI::FederateInternalError)
-        {
-        }
-
-        virtual void requestFederationRestoreSucceeded(const char* label)
-            throw (RTI::FederateInternalError)
-        {
-        }
-
-        virtual void requestFederationRestoreFailed(const char* label, const char* reason)
-            throw (RTI::FederateInternalError)
-        {
-        }
-
-        virtual void federationRestoreBegun()
-            throw (RTI::FederateInternalError)
-        {
-        }
-
-        virtual void initiateFederateRestore(const char* label, RTI::FederateHandle federateHandle)
-            throw (RTI::SpecifiedSaveLabelDoesNotExist,
-                   RTI::CouldNotRestore,
-                   RTI::FederateInternalError)
-        {
-        }
-
-        virtual void federationRestored()
-            throw (RTI::FederateInternalError)
-        {
-        }
-
-        virtual void federationNotRestored()
-            throw (RTI::FederateInternalError)
-        {
-        }
-
-        // Declaration Management
-        virtual void startRegistrationForObjectClass(RTI::ObjectClassHandle objectClassHandle)
-            throw (RTI::ObjectClassNotPublished,
-                   RTI::FederateInternalError)
-        {
-            ObjectClassMap::iterator i = _rtiAmbassador._objectClassMap.find(objectClassHandle);
-            if (i == _rtiAmbassador._objectClassMap.end())
-                return;
-            if (!i->second.valid())
-                return;
-            i->second->startRegistration();
-        }
-
-        virtual void stopRegistrationForObjectClass(RTI::ObjectClassHandle objectClassHandle)
-            throw (RTI::ObjectClassNotPublished,
-                   RTI::FederateInternalError)
-        {
-            ObjectClassMap::iterator i = _rtiAmbassador._objectClassMap.find(objectClassHandle);
-            if (i == _rtiAmbassador._objectClassMap.end())
-                return;
-            if (!i->second.valid())
-                return;
-            i->second->stopRegistration();
-        }
-
-        virtual void turnInteractionsOn(RTI::InteractionClassHandle interactionClassHandle)
-            throw (RTI::InteractionClassNotPublished,
-                   RTI::FederateInternalError)
-        {
-        }
-
-        virtual void turnInteractionsOff(RTI::InteractionClassHandle interactionClassHandle)
-            throw (RTI::InteractionClassNotPublished,
-                   RTI::FederateInternalError)
-        {
-        }
-
-
-        // Object Management
-        virtual void discoverObjectInstance(RTI::ObjectHandle objectHandle, RTI::ObjectClassHandle objectClassHandle, const char* tag)
-            throw (RTI::CouldNotDiscover,
-                   RTI::ObjectClassNotKnown,
-                   RTI::FederateInternalError)
-        {
-            ObjectClassMap::iterator i = _rtiAmbassador._objectClassMap.find(objectClassHandle);
-            if (i == _rtiAmbassador._objectClassMap.end())
-                throw RTI::ObjectClassNotKnown("Federate: discoverObjectInstance()!");
-            if (!i->second.valid())
-                return;
-            SGSharedPtr<RTI13ObjectInstance> objectInstance = new RTI13ObjectInstance(objectHandle, 0, i->second, &_rtiAmbassador, false);
-            _rtiAmbassador._objectInstanceMap[objectHandle] = objectInstance;
-            _rtiAmbassador._objectInstancePendingCallbackList.insert(objectInstance);
-            i->second->discoverInstance(objectInstance.get(), tagToData(tag));
-        }
-
-        virtual void reflectAttributeValues(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleValuePairSet& attributeValuePairSet,
-                                            const RTI::FedTime& fedTime, const char* tag, RTI::EventRetractionHandle eventRetractionHandle)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::FederateOwnsAttributes,
-                   RTI::InvalidFederationTime,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("Reflect attributes for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->reflectAttributeValues(attributeValuePairSet, toTimeStamp(fedTime), tagToData(tag));
-        }
-
-        virtual void reflectAttributeValues(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleValuePairSet& attributeValuePairSet,
-                                            const char* tag)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::FederateOwnsAttributes,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("Reflect attributes for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->reflectAttributeValues(attributeValuePairSet, tagToData(tag));
-        }
-
-        virtual void receiveInteraction(RTI::InteractionClassHandle interactionClassHandle, const RTI::ParameterHandleValuePairSet& parameters,
-                                        const RTI::FedTime& fedTime, const char* tag, RTI::EventRetractionHandle eventRetractionHandle)
-            throw (RTI::InteractionClassNotKnown,
-                   RTI::InteractionParameterNotKnown,
-                   RTI::InvalidFederationTime,
-                   RTI::FederateInternalError)
-        {
-        }
-
-        virtual void receiveInteraction(RTI::InteractionClassHandle interactionClassHandle,
-                                        const RTI::ParameterHandleValuePairSet& parameters, const char* tag)
-            throw (RTI::InteractionClassNotKnown,
-                   RTI::InteractionParameterNotKnown,
-                   RTI::FederateInternalError)
-        {
-        }
-
-        virtual void removeObjectInstance(RTI::ObjectHandle objectHandle, const RTI::FedTime& fedTime,
-                                          const char* tag, RTI::EventRetractionHandle eventRetractionHandle)
-            throw (RTI::ObjectNotKnown,
-                   RTI::InvalidFederationTime,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("Federate: removeObjectInstance()!");
-            if (i->second.valid())
-                _rtiAmbassador._queueCallbackList.push_back(new RemoveObjectCallback(i->second, tagToData(tag)));
-            _rtiAmbassador._objectInstancePendingCallbackList.erase(i->second);
-            _rtiAmbassador._objectInstanceMap.erase(i);
-        }
-
-        virtual void removeObjectInstance(RTI::ObjectHandle objectHandle, const char* tag)
-            throw (RTI::ObjectNotKnown,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("Federate: removeObjectInstance()!");
-            if (i->second.valid())
-                _rtiAmbassador._queueCallbackList.push_back(new RemoveObjectCallback(i->second, tagToData(tag)));
-            _rtiAmbassador._objectInstancePendingCallbackList.erase(i->second);
-            _rtiAmbassador._objectInstanceMap.erase(i);
-        }
-
-        virtual void attributesInScope(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("Attributes in scope for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->attributesInScope(attributes);
-        }
-
-        virtual void attributesOutOfScope(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("Attributes in scope for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->attributesOutOfScope(attributes);
-        }
-
-        virtual void provideAttributeValueUpdate(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::AttributeNotOwned,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("Reflect attributes for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->provideAttributeValueUpdate(attributes);
-        }
-
-        virtual void turnUpdatesOnForObjectInstance(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotOwned,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("Turn on attributes for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->turnUpdatesOnForObjectInstance(attributes);
-        }
-
-        virtual void turnUpdatesOffForObjectInstance(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotOwned,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("Turn off attributes for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->turnUpdatesOffForObjectInstance(attributes);
-        }
-
-        // Ownership Management
-        virtual void requestAttributeOwnershipAssumption(RTI::ObjectHandle objectHandle,
-                                                         const RTI::AttributeHandleSet& attributes, const char* tag)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::AttributeAlreadyOwned,
-                   RTI::AttributeNotPublished,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("requestAttributeOwnershipAssumption for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->requestAttributeOwnershipAssumption(attributes, tagToData(tag));
-        }
-
-        virtual void attributeOwnershipDivestitureNotification(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::AttributeNotOwned,
-                   RTI::AttributeDivestitureWasNotRequested,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("attributeOwnershipDivestitureNotification for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->attributeOwnershipDivestitureNotification(attributes);
-        }
-
-        virtual void attributeOwnershipAcquisitionNotification(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::AttributeAcquisitionWasNotRequested,
-                   RTI::AttributeAlreadyOwned,
-                   RTI::AttributeNotPublished,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("attributeOwnershipAcquisitionNotification for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->attributeOwnershipAcquisitionNotification(attributes);
-        }
-
-        virtual void attributeOwnershipUnavailable(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::AttributeAlreadyOwned,
-                   RTI::AttributeAcquisitionWasNotRequested,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("attributeOwnershipUnavailable for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->attributeOwnershipUnavailable(attributes);
-        }
-
-        virtual void requestAttributeOwnershipRelease(RTI::ObjectHandle objectHandle,
-                                                      const RTI::AttributeHandleSet& attributes, const char* tag)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::AttributeNotOwned,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("requestAttributeOwnershipRelease for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->requestAttributeOwnershipRelease(attributes, tagToData(tag));
-        }
-
-        virtual void confirmAttributeOwnershipAcquisitionCancellation(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::AttributeAlreadyOwned,
-                   RTI::AttributeAcquisitionWasNotCanceled,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("confirmAttributeOwnershipAcquisitionCancellation for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->confirmAttributeOwnershipAcquisitionCancellation(attributes);
-        }
-
-        virtual void informAttributeOwnership(RTI::ObjectHandle objectHandle, RTI::AttributeHandle attributeHandle,
-                                              RTI::FederateHandle federateHandle)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("informAttributeOwnership for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->informAttributeOwnership(attributeHandle, federateHandle);
-        }
-
-        virtual void attributeIsNotOwned(RTI::ObjectHandle objectHandle, RTI::AttributeHandle attributeHandle)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("attributeIsNotOwned for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->attributeIsNotOwned(attributeHandle);
-        }
-
-        virtual void attributeOwnedByRTI(RTI::ObjectHandle objectHandle, RTI::AttributeHandle attributeHandle)
-            throw (RTI::ObjectNotKnown,
-                   RTI::AttributeNotKnown,
-                   RTI::FederateInternalError)
-        {
-            ObjectInstanceMap::iterator i = _rtiAmbassador._objectInstanceMap.find(objectHandle);
-            if (i == _rtiAmbassador._objectInstanceMap.end())
-                throw RTI::ObjectNotKnown("attributeOwnedByRTI for unknown object!");
-            if (!i->second.valid())
-                return;
-            i->second->attributeOwnedByRTI(attributeHandle);
-        }
-
-        // Time Management
-        virtual void timeRegulationEnabled(const RTI::FedTime& fedTime)
-            throw (RTI::InvalidFederationTime,
-                   RTI::EnableTimeRegulationWasNotPending,
-                   RTI::FederateInternalError)
-        {
-            _rtiAmbassador._timeRegulationEnabled = true;
-            _rtiAmbassador._federateTime = toTimeStamp(fedTime);
-            SG_LOG(SG_NETWORK, SG_INFO, "RTI: timeRegulationEnabled: " << _rtiAmbassador._federateTime);
-        }
-
-        virtual void timeConstrainedEnabled(const RTI::FedTime& fedTime)
-            throw (RTI::InvalidFederationTime,
-                   RTI::EnableTimeConstrainedWasNotPending,
-                   RTI::FederateInternalError)
-        {
-            _rtiAmbassador._timeConstrainedEnabled = true;
-            _rtiAmbassador._federateTime = toTimeStamp(fedTime);
-            SG_LOG(SG_NETWORK, SG_INFO, "RTI: timeConstrainedEnabled: " << _rtiAmbassador._federateTime);
-        }
-
-        virtual void timeAdvanceGrant(const RTI::FedTime& fedTime)
-            throw (RTI::InvalidFederationTime,
-                   RTI::TimeAdvanceWasNotInProgress,
-                   RTI::FederateInternalError)
-        {
-            _rtiAmbassador._federateTime = toTimeStamp(fedTime);
-            _rtiAmbassador._timeAdvancePending = false;
-            SG_LOG(SG_NETWORK, SG_INFO, "RTI: timeAdvanceGrant: " << _rtiAmbassador._federateTime);
-        }
-
-        virtual void requestRetraction(RTI::EventRetractionHandle eventRetractionHandle)
-            throw (RTI::EventNotKnown,
-                   RTI::FederateInternalError)
-        {
-            // No retraction concept yet
-        }
-
-    private:
-        const RTIData& tagToData(const char* tag)
-        {
-            if (tag)
-                _cachedTag.setData(tag, std::strlen(tag) + 1);
-            else
-                _cachedTag.setData("", 1);
-            return _cachedTag;
-        }
-        RTIData _cachedTag;
-
-        RTI13Ambassador& _rtiAmbassador;
-    };
 
     static SGTimeStamp toTimeStamp(const RTI::FedTime& fedTime)
     {
@@ -1052,45 +466,9 @@ private:
         return s;
     }
 
-    static std::string toStdString(const char* n)
-    {
-        if (!n)
-            return std::string();
-        return std::string(n);
-    }
-
     // The connection class
     RTI::RTIambassador _rtiAmbassador;
-
-    // The class with all the callbacks.
-    FederateAmbassador _federateAmbassador;
-
-    // All the sync labels we got an announcement for
-    std::set<std::string> _pendingSyncLabels;
-    std::set<std::string> _syncronizedSyncLabels;
-
-    // All that calls back into user code is just queued.
-    // That is to make sure we do not call recursively into the RTI
-    typedef std::list<SGSharedPtr<QueueCallback> > QueueCallbackList;
-    QueueCallbackList _queueCallbackList;
-    // All object instances that need to be called due to some event are noted here
-    // That is to make sure we do not call recursively into the RTI
-    typedef std::set<SGSharedPtr<RTIObjectInstance> > ObjectInstanceSet;
-    ObjectInstanceSet _objectInstancePendingCallbackList;
-
-    // Top level information for dispatching federate object attribute updates
-    typedef std::map<RTI::ObjectHandle, SGSharedPtr<RTI13ObjectInstance> > ObjectInstanceMap;
-    // Map of all available objects
-    ObjectInstanceMap _objectInstanceMap;
-
-    // Top level information for dispatching creation of federate objects
-    typedef std::map<RTI::ObjectClassHandle, SGSharedPtr<RTI13ObjectClass> > ObjectClassMap;
-    ObjectClassMap _objectClassMap;
-
-    bool _timeRegulationEnabled;
-    bool _timeConstrainedEnabled;
-    bool _timeAdvancePending;
-    SGTimeStamp _federateTime;
+    SGWeakPtr<RTI13Federate> _federate;
 };
 
 } // namespace simgear
index a77fbaea8a8aded9c4650fc3dad3584428eda7fc..32913c233f28055c2a91971e128ca2a52c4b2850 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009 - 2010  Mathias Froehlich - Mathias.Froehlich@web.de
+// Copyright (C) 2009 - 2011  Mathias Froehlich - Mathias.Froehlich@web.de
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Library General Public
 
 namespace simgear {
 
+static std::string toStdString(const char* n)
+{
+    if (!n)
+        return std::string();
+    return std::string(n);
+}
+
+/// Just the interface class doing the callbacks into the parent class
+struct RTI13Federate::FederateAmbassador : public RTI::FederateAmbassador {
+    FederateAmbassador() :
+        _timeRegulationEnabled(false),
+        _timeConstrainedEnabled(false),
+        _timeAdvancePending(false)
+    {
+    }
+    virtual ~FederateAmbassador()
+        throw (RTI::FederateInternalError)
+    {
+    }
+
+    /// RTI federate ambassador callback functions.
+    virtual void synchronizationPointRegistrationSucceeded(const char* label)
+        throw (RTI::FederateInternalError)
+    {
+    }
+
+    virtual void synchronizationPointRegistrationFailed(const char* label)
+        throw (RTI::FederateInternalError)
+    {
+    }
+
+    virtual void announceSynchronizationPoint(const char* label, const char* tag)
+        throw (RTI::FederateInternalError)
+    {
+        _pendingSyncLabels.insert(toStdString(label));
+    }
+
+    virtual void federationSynchronized(const char* label)
+        throw (RTI::FederateInternalError)
+    {
+        std::string s = toStdString(label);
+        _pendingSyncLabels.erase(s);
+        _syncronizedSyncLabels.insert(s);
+    }
+
+    virtual void initiateFederateSave(const char* label)
+        throw (RTI::UnableToPerformSave,
+               RTI::FederateInternalError)
+    {
+    }
+
+    virtual void federationSaved()
+        throw (RTI::FederateInternalError)
+    {
+    }
+
+    virtual void federationNotSaved()
+        throw (RTI::FederateInternalError)
+    {
+    }
+
+    virtual void requestFederationRestoreSucceeded(const char* label)
+        throw (RTI::FederateInternalError)
+    {
+    }
+
+    virtual void requestFederationRestoreFailed(const char* label, const char* reason)
+        throw (RTI::FederateInternalError)
+    {
+    }
+
+    virtual void federationRestoreBegun()
+        throw (RTI::FederateInternalError)
+    {
+    }
+
+    virtual void initiateFederateRestore(const char* label, RTI::FederateHandle federateHandle)
+        throw (RTI::SpecifiedSaveLabelDoesNotExist,
+               RTI::CouldNotRestore,
+               RTI::FederateInternalError)
+    {
+    }
+
+    virtual void federationRestored()
+        throw (RTI::FederateInternalError)
+    {
+    }
+
+    virtual void federationNotRestored()
+        throw (RTI::FederateInternalError)
+    {
+    }
+
+    // Declaration Management
+    virtual void startRegistrationForObjectClass(RTI::ObjectClassHandle objectClassHandle)
+        throw (RTI::ObjectClassNotPublished,
+               RTI::FederateInternalError)
+    {
+        ObjectClassMap::iterator i = _objectClassMap.find(objectClassHandle);
+        if (i == _objectClassMap.end())
+            return;
+        if (!i->second.valid())
+            return;
+        i->second->startRegistration();
+    }
+
+    virtual void stopRegistrationForObjectClass(RTI::ObjectClassHandle objectClassHandle)
+        throw (RTI::ObjectClassNotPublished,
+               RTI::FederateInternalError)
+    {
+        ObjectClassMap::iterator i = _objectClassMap.find(objectClassHandle);
+        if (i == _objectClassMap.end())
+            return;
+        if (!i->second.valid())
+            return;
+        i->second->stopRegistration();
+    }
+
+    virtual void turnInteractionsOn(RTI::InteractionClassHandle interactionClassHandle)
+        throw (RTI::InteractionClassNotPublished,
+               RTI::FederateInternalError)
+    {
+    }
+
+    virtual void turnInteractionsOff(RTI::InteractionClassHandle interactionClassHandle)
+        throw (RTI::InteractionClassNotPublished,
+               RTI::FederateInternalError)
+    {
+    }
+
+
+    // Object Management
+    virtual void discoverObjectInstance(RTI::ObjectHandle objectHandle, RTI::ObjectClassHandle objectClassHandle, const char* tag)
+        throw (RTI::CouldNotDiscover,
+               RTI::ObjectClassNotKnown,
+               RTI::FederateInternalError)
+    {
+        ObjectClassMap::iterator i = _objectClassMap.find(objectClassHandle);
+        if (i == _objectClassMap.end())
+            throw RTI::ObjectClassNotKnown("Federate: discoverObjectInstance()!");
+        if (!i->second.valid())
+            return;
+        SGSharedPtr<RTI13ObjectInstance> objectInstance = new RTI13ObjectInstance(objectHandle, 0, i->second, _rtiAmbassador.get(), false);
+        _objectInstanceMap[objectHandle] = objectInstance;
+        _queueCallbackList.push_back(new DiscoverObjectCallback(i->second, objectInstance, tagToData(tag)));
+    }
+
+    virtual void reflectAttributeValues(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleValuePairSet& attributeValuePairSet,
+                                        const RTI::FedTime& fedTime, const char* tag, RTI::EventRetractionHandle eventRetractionHandle)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::FederateOwnsAttributes,
+               RTI::InvalidFederationTime,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("Reflect attributes for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->reflectAttributeValues(attributeValuePairSet, RTI13Ambassador::toTimeStamp(fedTime), tagToData(tag));
+    }
+
+    virtual void reflectAttributeValues(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleValuePairSet& attributeValuePairSet,
+                                        const char* tag)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::FederateOwnsAttributes,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("Reflect attributes for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->reflectAttributeValues(attributeValuePairSet, tagToData(tag));
+    }
+
+    virtual void receiveInteraction(RTI::InteractionClassHandle interactionClassHandle, const RTI::ParameterHandleValuePairSet& parameters,
+                                    const RTI::FedTime& fedTime, const char* tag, RTI::EventRetractionHandle eventRetractionHandle)
+        throw (RTI::InteractionClassNotKnown,
+               RTI::InteractionParameterNotKnown,
+               RTI::InvalidFederationTime,
+               RTI::FederateInternalError)
+    {
+    }
+
+    virtual void receiveInteraction(RTI::InteractionClassHandle interactionClassHandle,
+                                    const RTI::ParameterHandleValuePairSet& parameters, const char* tag)
+        throw (RTI::InteractionClassNotKnown,
+               RTI::InteractionParameterNotKnown,
+               RTI::FederateInternalError)
+    {
+    }
+
+    virtual void removeObjectInstance(RTI::ObjectHandle objectHandle, const RTI::FedTime& fedTime,
+                                      const char* tag, RTI::EventRetractionHandle eventRetractionHandle)
+        throw (RTI::ObjectNotKnown,
+               RTI::InvalidFederationTime,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("Federate: removeObjectInstance()!");
+        if (i->second.valid())
+            _queueCallbackList.push_back(new RemoveObjectCallback(i->second, tagToData(tag)));
+        _objectInstanceMap.erase(i);
+    }
+
+    virtual void removeObjectInstance(RTI::ObjectHandle objectHandle, const char* tag)
+        throw (RTI::ObjectNotKnown,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("Federate: removeObjectInstance()!");
+        if (i->second.valid())
+            _queueCallbackList.push_back(new RemoveObjectCallback(i->second, tagToData(tag)));
+        _objectInstanceMap.erase(i);
+    }
+
+    virtual void attributesInScope(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("Attributes in scope for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->attributesInScope(attributes);
+    }
+
+    virtual void attributesOutOfScope(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("Attributes in scope for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->attributesOutOfScope(attributes);
+    }
+
+    virtual void provideAttributeValueUpdate(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::AttributeNotOwned,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("Reflect attributes for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->provideAttributeValueUpdate(attributes);
+    }
+
+    virtual void turnUpdatesOnForObjectInstance(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotOwned,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("Turn on attributes for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->turnUpdatesOnForObjectInstance(attributes);
+    }
+
+    virtual void turnUpdatesOffForObjectInstance(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotOwned,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("Turn off attributes for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->turnUpdatesOffForObjectInstance(attributes);
+    }
+
+    // Ownership Management
+    virtual void requestAttributeOwnershipAssumption(RTI::ObjectHandle objectHandle,
+                                                     const RTI::AttributeHandleSet& attributes, const char* tag)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::AttributeAlreadyOwned,
+               RTI::AttributeNotPublished,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("requestAttributeOwnershipAssumption for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->requestAttributeOwnershipAssumption(attributes, tagToData(tag));
+    }
+
+    virtual void attributeOwnershipDivestitureNotification(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::AttributeNotOwned,
+               RTI::AttributeDivestitureWasNotRequested,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("attributeOwnershipDivestitureNotification for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->attributeOwnershipDivestitureNotification(attributes);
+    }
+
+    virtual void attributeOwnershipAcquisitionNotification(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::AttributeAcquisitionWasNotRequested,
+               RTI::AttributeAlreadyOwned,
+               RTI::AttributeNotPublished,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("attributeOwnershipAcquisitionNotification for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->attributeOwnershipAcquisitionNotification(attributes);
+    }
+
+    virtual void attributeOwnershipUnavailable(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::AttributeAlreadyOwned,
+               RTI::AttributeAcquisitionWasNotRequested,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("attributeOwnershipUnavailable for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->attributeOwnershipUnavailable(attributes);
+    }
+
+    virtual void requestAttributeOwnershipRelease(RTI::ObjectHandle objectHandle,
+                                                  const RTI::AttributeHandleSet& attributes, const char* tag)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::AttributeNotOwned,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("requestAttributeOwnershipRelease for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->requestAttributeOwnershipRelease(attributes, tagToData(tag));
+    }
+
+    virtual void confirmAttributeOwnershipAcquisitionCancellation(RTI::ObjectHandle objectHandle, const RTI::AttributeHandleSet& attributes)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::AttributeAlreadyOwned,
+               RTI::AttributeAcquisitionWasNotCanceled,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("confirmAttributeOwnershipAcquisitionCancellation for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->confirmAttributeOwnershipAcquisitionCancellation(attributes);
+    }
+
+    virtual void informAttributeOwnership(RTI::ObjectHandle objectHandle, RTI::AttributeHandle attributeHandle,
+                                          RTI::FederateHandle federateHandle)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("informAttributeOwnership for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->informAttributeOwnership(attributeHandle, federateHandle);
+    }
+
+    virtual void attributeIsNotOwned(RTI::ObjectHandle objectHandle, RTI::AttributeHandle attributeHandle)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("attributeIsNotOwned for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->attributeIsNotOwned(attributeHandle);
+    }
+
+    virtual void attributeOwnedByRTI(RTI::ObjectHandle objectHandle, RTI::AttributeHandle attributeHandle)
+        throw (RTI::ObjectNotKnown,
+               RTI::AttributeNotKnown,
+               RTI::FederateInternalError)
+    {
+        ObjectInstanceMap::iterator i = _objectInstanceMap.find(objectHandle);
+        if (i == _objectInstanceMap.end())
+            throw RTI::ObjectNotKnown("attributeOwnedByRTI for unknown object!");
+        if (!i->second.valid())
+            return;
+        i->second->attributeOwnedByRTI(attributeHandle);
+    }
+
+    // Time Management
+    virtual void timeRegulationEnabled(const RTI::FedTime& fedTime)
+        throw (RTI::InvalidFederationTime,
+               RTI::EnableTimeRegulationWasNotPending,
+               RTI::FederateInternalError)
+    {
+        _timeRegulationEnabled = true;
+        _federateTime = RTI13Ambassador::toTimeStamp(fedTime);
+        SG_LOG(SG_NETWORK, SG_INFO, "RTI: timeRegulationEnabled: " << _federateTime);
+    }
+
+    virtual void timeConstrainedEnabled(const RTI::FedTime& fedTime)
+        throw (RTI::InvalidFederationTime,
+               RTI::EnableTimeConstrainedWasNotPending,
+               RTI::FederateInternalError)
+    {
+        _timeConstrainedEnabled = true;
+        _federateTime = RTI13Ambassador::toTimeStamp(fedTime);
+        SG_LOG(SG_NETWORK, SG_INFO, "RTI: timeConstrainedEnabled: " << _federateTime);
+    }
+
+    virtual void timeAdvanceGrant(const RTI::FedTime& fedTime)
+        throw (RTI::InvalidFederationTime,
+               RTI::TimeAdvanceWasNotInProgress,
+               RTI::FederateInternalError)
+    {
+        _federateTime = RTI13Ambassador::toTimeStamp(fedTime);
+        _timeAdvancePending = false;
+        SG_LOG(SG_NETWORK, SG_INFO, "RTI: timeAdvanceGrant: " << _federateTime);
+    }
+
+    virtual void requestRetraction(RTI::EventRetractionHandle eventRetractionHandle)
+        throw (RTI::EventNotKnown,
+               RTI::FederateInternalError)
+    {
+        // No retraction concept yet
+    }
+
+    // processes the queues that filled up during the past
+    void processQueues()
+    {
+        while (!_queueCallbackList.empty()) {
+            (*_queueCallbackList.front())();
+            _queueCallbackList.pop_front();
+        }
+    }
+
+    bool getFederationSynchronizationPointAnnounced(const std::string& label)
+    { return _pendingSyncLabels.find(label) != _pendingSyncLabels.end(); }
+    bool getFederationSynchronized(const std::string& label)
+    {
+        std::set<std::string>::iterator i = _syncronizedSyncLabels.find(label);
+        if (i == _syncronizedSyncLabels.end())
+            return false;
+        _syncronizedSyncLabels.erase(i);
+        return true;
+    }
+
+    /// Generic callback to execute some notification on objects in a way that they are not prone to
+    /// ConcurrentAccess exceptions.
+    class QueueCallback : public SGReferenced {
+    public:
+        virtual ~QueueCallback() {}
+        virtual void operator()() = 0;
+    };
+
+    class DiscoverObjectCallback : public QueueCallback {
+    public:
+        DiscoverObjectCallback(SGSharedPtr<RTIObjectClass> objectClass, SGSharedPtr<RTIObjectInstance> objectInstance, const RTIData& tag) :
+            _objectClass(objectClass),
+            _objectInstance(objectInstance),
+            _tag(tag)
+        { }
+        virtual void operator()()
+        {
+            _objectClass->discoverInstance(_objectInstance.get(), _tag);
+            _objectInstance->requestObjectAttributeValueUpdate();
+        }
+    private:
+        SGSharedPtr<RTIObjectClass> _objectClass;
+        SGSharedPtr<RTIObjectInstance> _objectInstance;
+        RTIData _tag;
+    };
+    class RemoveObjectCallback : public QueueCallback {
+    public:
+        RemoveObjectCallback(SGSharedPtr<RTIObjectInstance> objectInstance, const RTIData& tag) :
+            _objectInstance(objectInstance),
+            _tag(tag)
+        { }
+        virtual void operator()()
+        {
+            _objectInstance->removeInstance(_tag);
+        }
+    private:
+        SGSharedPtr<RTIObjectInstance> _objectInstance;
+        RTIData _tag;
+    };
+
+    // The rtiambassador to issue requests
+    SGSharedPtr<RTI13Ambassador> _rtiAmbassador;
+
+    // All the sync labels we got an announcement for
+    std::set<std::string> _pendingSyncLabels;
+    std::set<std::string> _syncronizedSyncLabels;
+
+    // All that calls back into user code is just queued.
+    // That is to make sure we do not call recursively into the RTI
+    typedef std::list<SGSharedPtr<QueueCallback> > QueueCallbackList;
+    QueueCallbackList _queueCallbackList;
+
+    // Top level information for dispatching federate object attribute updates
+    typedef std::map<RTI::ObjectHandle, SGSharedPtr<RTI13ObjectInstance> > ObjectInstanceMap;
+    // Map of all available objects
+    ObjectInstanceMap _objectInstanceMap;
+
+    // Top level information for dispatching creation of federate objects
+    typedef std::map<RTI::ObjectClassHandle, SGSharedPtr<RTI13ObjectClass> > ObjectClassMap;
+    ObjectClassMap _objectClassMap;
+
+    bool _timeRegulationEnabled;
+    bool _timeConstrainedEnabled;
+    bool _timeAdvancePending;
+    SGTimeStamp _federateTime;
+
+private:
+    const RTIData& tagToData(const char* tag)
+    {
+        if (tag)
+            _cachedTag.setData(tag, std::strlen(tag) + 1);
+        else
+            _cachedTag.setData("", 1);
+        return _cachedTag;
+    }
+    RTIData _cachedTag;
+};
+
 RTI13Federate::RTI13Federate(const std::list<std::string>& stringList) :
+    _joined(false),
     _tickTimeout(10),
-    _ambassador(new RTI13Ambassador)
+    _ambassador(new RTI13Ambassador),
+    _federateAmbassador(new FederateAmbassador)
 {
+    _ambassador->_federate = this;
+    _federateAmbassador->_rtiAmbassador = _ambassador;
     if (stringList.empty()) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Ignoring non empty connect arguments while connecting to an RTI13 federation!");
     }
@@ -32,6 +592,9 @@ RTI13Federate::RTI13Federate(const std::list<std::string>& stringList) :
 
 RTI13Federate::~RTI13Federate()
 {
+    if (_joined)
+        _ambassador->resignFederationExecution();
+    delete _federateAmbassador;
 }
 
 bool
@@ -80,11 +643,12 @@ bool
 RTI13Federate::join(const std::string& federateType, const std::string& federationName)
 {
     try {
-        _federateHandle = _ambassador->joinFederationExecution(federateType, federationName);
+        _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;
     } catch (RTI::FederateAlreadyExecutionMember& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not join federation execution: " << e._name << " " << e._reason);
@@ -119,6 +683,7 @@ RTI13Federate::resign()
     try {
         _ambassador->resignFederationExecution();
         SG_LOG(SG_NETWORK, SG_INFO, "RTI: Resigned from federation.");
+        _joined = false;
         _federateHandle = -1;
         return true;
     } catch (RTI::FederateOwnsAttributes& e) {
@@ -167,9 +732,9 @@ RTI13Federate::registerFederationSynchronizationPoint(const std::string& label,
 bool
 RTI13Federate::waitForFederationSynchronizationPointAnnounced(const std::string& label)
 {
-    while (!_ambassador->getFederationSynchronizationPointAnnounced(label)) {
+    while (!_federateAmbassador->getFederationSynchronizationPointAnnounced(label)) {
         _ambassador->tick(_tickTimeout, 0);
-        _ambassador->processQueues();
+        _federateAmbassador->processQueues();
     }
     return true;
 }
@@ -205,9 +770,9 @@ RTI13Federate::synchronizationPointAchieved(const std::string& label)
 bool
 RTI13Federate::waitForFederationSynchronized(const std::string& label)
 {
-    while (!_ambassador->getFederationSynchronized(label)) {
+    while (!_federateAmbassador->getFederationSynchronized(label)) {
         _ambassador->tick(_tickTimeout, 0);
-        _ambassador->processQueues();
+        _federateAmbassador->processQueues();
     }
     return true;
 }
@@ -220,7 +785,7 @@ RTI13Federate::enableTimeConstrained()
         return false;
     }
 
-    if (_ambassador->getTimeConstrainedEnabled()) {
+    if (_federateAmbassador->_timeConstrainedEnabled) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Time constrained is already enabled.");
         return false;
     }
@@ -253,9 +818,9 @@ RTI13Federate::enableTimeConstrained()
         return false;
     }
 
-    while (!_ambassador->getTimeConstrainedEnabled()) {
+    while (!_federateAmbassador->_timeConstrainedEnabled) {
         _ambassador->tick(_tickTimeout, 0);
-        _ambassador->processQueues();
+        _federateAmbassador->processQueues();
     }
 
     return true;
@@ -269,13 +834,14 @@ RTI13Federate::disableTimeConstrained()
         return false;
     }
 
-    if (!_ambassador->getTimeConstrainedEnabled()) {
+    if (!_federateAmbassador->_timeConstrainedEnabled) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Time constrained is not enabled.");
         return false;
     }
 
     try {
         _ambassador->disableTimeConstrained();
+        _federateAmbassador->_timeConstrainedEnabled = false;
     } catch (RTI::TimeConstrainedWasNotEnabled& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not resign federation execution: " << e._name << " " << e._reason);
         return false;
@@ -307,7 +873,7 @@ RTI13Federate::enableTimeRegulation(const SGTimeStamp& lookahead)
         return false;
     }
 
-    if (_ambassador->getTimeRegulationEnabled()) {
+    if (_federateAmbassador->_timeRegulationEnabled) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Time regulation already enabled.");
         return false;
     }
@@ -346,9 +912,9 @@ RTI13Federate::enableTimeRegulation(const SGTimeStamp& lookahead)
         return false;
     }
 
-    while (!_ambassador->getTimeRegulationEnabled()) {
+    while (!_federateAmbassador->_timeRegulationEnabled) {
         _ambassador->tick(_tickTimeout, 0);
-        _ambassador->processQueues();
+        _federateAmbassador->processQueues();
     }
 
     return true;
@@ -362,13 +928,14 @@ RTI13Federate::disableTimeRegulation()
         return false;
     }
 
-    if (!_ambassador->getTimeRegulationEnabled()) {
+    if (!_federateAmbassador->_timeRegulationEnabled) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Time regulation is not enabled.");
         return false;
     }
 
     try {
         _ambassador->disableTimeRegulation();
+        _federateAmbassador->_timeRegulationEnabled = false;
     } catch (RTI::TimeRegulationWasNotEnabled& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not resign federation execution: " << e._name << " " << e._reason);
         return false;
@@ -400,7 +967,7 @@ RTI13Federate::timeAdvanceRequestBy(const SGTimeStamp& dt)
         return false;
     }
 
-    SGTimeStamp fedTime = _ambassador->getCurrentLogicalTime() + dt;
+    SGTimeStamp fedTime = _federateAmbassador->_federateTime + dt;
     return timeAdvanceRequest(fedTime);
 }
 
@@ -414,6 +981,7 @@ RTI13Federate::timeAdvanceRequest(const SGTimeStamp& fedTime)
 
     try {
         _ambassador->timeAdvanceRequest(fedTime);
+        _federateAmbassador->_timeAdvancePending = true;
     } catch (RTI::InvalidFederationTime& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not resign federation execution: " << e._name << " " << e._reason);
         return false;
@@ -446,9 +1014,9 @@ RTI13Federate::timeAdvanceRequest(const SGTimeStamp& fedTime)
         return false;
     }
 
-    while (_ambassador->getTimeAdvancePending()) {
+    while (_federateAmbassador->_timeAdvancePending) {
         _ambassador->tick(_tickTimeout, 0);
-        _ambassador->processQueues();
+        _federateAmbassador->processQueues();
     }
 
     return true;
@@ -547,7 +1115,7 @@ bool
 RTI13Federate::tick()
 {
     bool result = _ambassador->tick();
-    _ambassador->processQueues();
+    _federateAmbassador->processQueues();
     return result;
 }
 
@@ -555,7 +1123,7 @@ bool
 RTI13Federate::tick(const double& minimum, const double& maximum)
 {
     bool result = _ambassador->tick(minimum, maximum);
-    _ambassador->processQueues();
+    _federateAmbassador->processQueues();
     return result;
 }
 
@@ -563,7 +1131,16 @@ RTI13ObjectClass*
 RTI13Federate::createObjectClass(const std::string& objectClassName, HLAObjectClass* hlaObjectClass)
 {
     try {
-        return _ambassador->createObjectClass(objectClassName, hlaObjectClass);
+        RTI::ObjectClassHandle objectClassHandle;
+        objectClassHandle = _ambassador->getObjectClassHandle(objectClassName);
+        if (_federateAmbassador->_objectClassMap.find(objectClassHandle) != _federateAmbassador->_objectClassMap.end()) {
+            SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not create object class, object class already exists!");
+            return 0;
+        }
+        RTI13ObjectClass* rtiObjectClass;
+        rtiObjectClass = new RTI13ObjectClass(hlaObjectClass, objectClassHandle, _ambassador.get());
+        _federateAmbassador->_objectClassMap[objectClassHandle] = rtiObjectClass;
+        return rtiObjectClass;
     } catch (RTI::NameNotFound& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class: " << e._name << " " << e._reason);
         return 0;
@@ -583,7 +1160,14 @@ RTI13ObjectInstance*
 RTI13Federate::getObjectInstance(const std::string& objectInstanceName)
 {
     try {
-        return _ambassador->getObjectInstance(objectInstanceName);
+        RTI::ObjectHandle objectHandle;
+        objectHandle = _ambassador->getObjectInstanceHandle(objectInstanceName);
+        FederateAmbassador::ObjectInstanceMap::iterator i = _federateAmbassador->_objectInstanceMap.find(objectHandle);
+        if (i == _federateAmbassador->_objectInstanceMap.end()) {
+            SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class: ObjectInstance not found.");
+            return 0;
+        }
+        return i->second;
     } catch (RTI::ObjectNotKnown& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class: " << e._name << " " << e._reason);
         return 0;
@@ -599,4 +1183,10 @@ RTI13Federate::getObjectInstance(const std::string& objectInstanceName)
     }
 }
 
+void
+RTI13Federate::insertObjectInstance(RTI13ObjectInstance* objectInstance)
+{
+    _federateAmbassador->_objectInstanceMap[objectInstance->getHandle()] = objectInstance;
+}
+
 }
index 839e7d508b743108284e4910d1f06fe3295636ba..6f3b9b03b0802fe733223c6014641f429c696823 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009 - 2010  Mathias Froehlich - Mathias.Froehlich@web.de
+// Copyright (C) 2009 - 2011  Mathias Froehlich - Mathias.Froehlich@web.de
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Library General Public
@@ -71,6 +71,7 @@ public:
     virtual RTI13ObjectClass* createObjectClass(const std::string& name, HLAObjectClass* hlaObjectClass);
 
     virtual RTI13ObjectInstance* getObjectInstance(const std::string& name);
+    void insertObjectInstance(RTI13ObjectInstance* objectInstance);
 
 private:
     RTI13Federate(const RTI13Federate&);
@@ -78,6 +79,7 @@ private:
 
     /// The federate handle
     RTI::FederateHandle _federateHandle;
+    bool _joined;
 
     /// The timeout for the single callback tick function in
     /// syncronous operations that need to wait for a callback
@@ -85,6 +87,10 @@ private:
 
     /// RTI connection
     SGSharedPtr<RTI13Ambassador> _ambassador;
+
+    /// Callbacks from the rti are handled here.
+    struct FederateAmbassador;
+    FederateAmbassador* _federateAmbassador;
 };
 
 }
index e2ccd4973e21fed9cfae89d33bca5b9a99242c5c..fcc41682cbdda9520f996dc142c9d2e14c624ad3 100644 (file)
@@ -37,12 +37,11 @@ RTI13ObjectClass::~RTI13ObjectClass()
 std::string
 RTI13ObjectClass::getName() const
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return std::string();
     }
-    return ambassador->getObjectClassName(_handle);
+    return _ambassador->getObjectClassName(_handle);
 }
 
 unsigned
@@ -54,14 +53,13 @@ RTI13ObjectClass::getNumAttributes() const
 unsigned
 RTI13ObjectClass::getAttributeIndex(const std::string& name) const
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return ~0u;
     }
 
     try {
-        RTI::AttributeHandle attributeHandle = ambassador->getAttributeHandle(name, _handle);
+        RTI::AttributeHandle attributeHandle = _ambassador->getAttributeHandle(name, _handle);
 
         AttributeHandleIndexMap::const_iterator i = _attributeHandleIndexMap.find(attributeHandle);
         if (i !=  _attributeHandleIndexMap.end())
@@ -93,14 +91,13 @@ RTI13ObjectClass::getAttributeIndex(const std::string& name) const
 unsigned
 RTI13ObjectClass::getOrCreateAttributeIndex(const std::string& name)
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return ~0u;
     }
 
     try {
-        RTI::AttributeHandle attributeHandle = ambassador->getAttributeHandle(name, _handle);
+        RTI::AttributeHandle attributeHandle = _ambassador->getAttributeHandle(name, _handle);
 
         AttributeHandleIndexMap::const_iterator i = _attributeHandleIndexMap.find(attributeHandle);
         if (i !=  _attributeHandleIndexMap.end())
@@ -169,8 +166,7 @@ RTI13ObjectClass::getOrCreateAttributeIndex(const std::string& name)
 bool
 RTI13ObjectClass::publish(const std::set<unsigned>& indexSet)
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return false;
     }
@@ -185,7 +181,7 @@ RTI13ObjectClass::publish(const std::set<unsigned>& indexSet)
             attributeHandleSet->add(_attributeHandleVector[*i]);
         }
 
-        ambassador->publishObjectClass(_handle, *attributeHandleSet);
+        _ambassador->publishObjectClass(_handle, *attributeHandleSet);
 
         for (unsigned i = 0; i < getNumAttributes(); ++i) {
             _attributeDataVector[i]._published = true;
@@ -225,14 +221,13 @@ RTI13ObjectClass::publish(const std::set<unsigned>& indexSet)
 bool
 RTI13ObjectClass::unpublish()
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return false;
     }
 
     try {
-        ambassador->unpublishObjectClass(_handle);
+        _ambassador->unpublishObjectClass(_handle);
 
         for (unsigned i = 0; i < getNumAttributes(); ++i) {
             _attributeDataVector[i]._published = false;
@@ -272,8 +267,7 @@ RTI13ObjectClass::unpublish()
 bool
 RTI13ObjectClass::subscribe(const std::set<unsigned>& indexSet, bool active)
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return false;
     }
@@ -289,7 +283,7 @@ RTI13ObjectClass::subscribe(const std::set<unsigned>& indexSet, bool active)
             attributeHandleSet->add(_attributeHandleVector[*i]);
         }
 
-        ambassador->subscribeObjectClassAttributes(_handle, *attributeHandleSet, active);
+        _ambassador->subscribeObjectClassAttributes(_handle, *attributeHandleSet, active);
 
         for (unsigned i = 0; i < getNumAttributes(); ++i) {
             _attributeDataVector[i]._subscribed = true;
@@ -326,14 +320,13 @@ RTI13ObjectClass::subscribe(const std::set<unsigned>& indexSet, bool active)
 bool
 RTI13ObjectClass::unsubscribe()
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return 0;
     }
 
     try {
-        ambassador->unsubscribeObjectClass(_handle);
+        _ambassador->unsubscribeObjectClass(_handle);
 
         for (unsigned i = 0; i < getNumAttributes(); ++i) {
             _attributeDataVector[i]._subscribed = false;
@@ -370,14 +363,22 @@ RTI13ObjectClass::unsubscribe()
 RTIObjectInstance*
 RTI13ObjectClass::registerObjectInstance(HLAObjectInstance* hlaObjectInstance)
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return 0;
     }
 
+    SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
+    if (!federate.valid()) {
+        SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
+        return 0;
+    }
+
     try {
-        return ambassador->registerObjectInstance(this, hlaObjectInstance);
+        RTI::ObjectHandle objectHandle = _ambassador->registerObjectInstance(getHandle());
+        RTI13ObjectInstance* objectInstance = new RTI13ObjectInstance(objectHandle, hlaObjectInstance, this, _ambassador.get(), true);
+        federate->insertObjectInstance(objectInstance);
+        return objectInstance;
     } catch (RTI::ObjectClassNotDefined& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
         return 0;
index e8c25cd0d88af1144d0ccb4894b78487ebb63abd..c4949ca9cffe434316dfd62e8ce9442280e8d54e 100644 (file)
@@ -73,7 +73,7 @@ public:
 
 private:
     RTI::ObjectClassHandle _handle;
-    SGWeakPtr<RTI13Ambassador> _ambassador;
+    SGSharedPtr<RTI13Ambassador> _ambassador;
 
     typedef std::map<RTI::AttributeHandle, unsigned> AttributeHandleIndexMap;
     AttributeHandleIndexMap _attributeHandleIndexMap;
index 87f6a42d8fe4a5b570f63af42122279371406654..723f9b1739b9c13f1b6b06a8f315d6d09c2b2cb4 100644 (file)
@@ -50,14 +50,13 @@ RTI13ObjectInstance::get13ObjectClass() const
 std::string
 RTI13ObjectInstance::getName() const
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return std::string();
     }
 
     try {
-        return ambassador->getObjectInstanceName(_handle);
+        return _ambassador->getObjectInstanceName(_handle);
     } catch (RTI::ObjectNotKnown& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
         return std::string();
@@ -77,28 +76,21 @@ RTI13ObjectInstance::getName() const
 }
 
 void
-RTI13ObjectInstance::addToRequestQueue()
+RTI13ObjectInstance::deleteObjectInstance(const RTIData& tag)
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return;
     }
 
-    ambassador->addObjectInstanceForCallback(this);
-}
-
-void
-RTI13ObjectInstance::deleteObjectInstance(const RTIData& tag)
-{
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
-        SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
+    SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
+    if (!federate.valid()) {
+        SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
         return;
     }
 
     try {
-        ambassador->deleteObjectInstance(_handle, tag);
+        _ambassador->deleteObjectInstance(_handle, tag);
     } catch (RTI::ObjectNotKnown& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
     } catch (RTI::DeletePrivilegeNotHeld& e) {
@@ -119,14 +111,19 @@ RTI13ObjectInstance::deleteObjectInstance(const RTIData& tag)
 void
 RTI13ObjectInstance::deleteObjectInstance(const SGTimeStamp& timeStamp, const RTIData& tag)
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return;
     }
 
+    SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
+    if (!federate.valid()) {
+        SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
+        return;
+    }
+
     try {
-        ambassador->deleteObjectInstance(_handle, timeStamp, tag);
+        _ambassador->deleteObjectInstance(_handle, timeStamp, tag);
     } catch (RTI::ObjectNotKnown& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
     } catch (RTI::DeletePrivilegeNotHeld& e) {
@@ -149,14 +146,19 @@ RTI13ObjectInstance::deleteObjectInstance(const SGTimeStamp& timeStamp, const RT
 void
 RTI13ObjectInstance::localDeleteObjectInstance()
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return;
     }
 
+    SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
+    if (!federate.valid()) {
+        SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
+        return;
+    }
+
     try {
-        ambassador->localDeleteObjectInstance(_handle);
+        _ambassador->localDeleteObjectInstance(_handle);
     } catch (RTI::ObjectNotKnown& e) {
         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
     } catch (RTI::FederateOwnsAttributes& e) {
@@ -221,8 +223,7 @@ RTI13ObjectInstance::reflectAttributeValues(const RTI::AttributeHandleValuePairS
 void
 RTI13ObjectInstance::requestObjectAttributeValueUpdate()
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return;
     }
@@ -238,7 +239,7 @@ RTI13ObjectInstance::requestObjectAttributeValueUpdate()
         if (!attributeHandleSet->size())
             return;
 
-        ambassador->requestObjectAttributeValueUpdate(_handle, *attributeHandleSet);
+        _ambassador->requestObjectAttributeValueUpdate(_handle, *attributeHandleSet);
 
         for (unsigned i = 0; i < numAttributes; ++i)
             setRequestAttributeUpdate(i, false);
@@ -285,8 +286,7 @@ RTI13ObjectInstance::provideAttributeValueUpdate(const RTI::AttributeHandleSet&
 void
 RTI13ObjectInstance::updateAttributeValues(const RTIData& tag)
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return;
     }
@@ -312,7 +312,7 @@ RTI13ObjectInstance::updateAttributeValues(const RTIData& tag)
         if (!_attributeValuePairSet->size())
             return;
 
-        ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, tag);
+        _ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, tag);
 
         for (unsigned i = 0; i < numAttributes; ++i) {
             setAttributeUpdated(i);
@@ -343,8 +343,7 @@ RTI13ObjectInstance::updateAttributeValues(const RTIData& tag)
 void
 RTI13ObjectInstance::updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag)
 {
-    SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
-    if (!ambassador.valid()) {
+    if (!_ambassador.valid()) {
         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
         return;
     }
@@ -370,7 +369,7 @@ RTI13ObjectInstance::updateAttributeValues(const SGTimeStamp& timeStamp, const R
         if (!_attributeValuePairSet->size())
             return;
 
-        ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, timeStamp, tag);
+        _ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, timeStamp, tag);
 
         for (unsigned i = 0; i < numAttributes; ++i) {
             setAttributeUpdated(i);
index ed73aa099934bab3c4a4f62b50a2fd929ed42c40..654bff92cbc1755812ce7414342678cd4f2d782b 100644 (file)
@@ -61,8 +61,6 @@ public:
 
     virtual std::string getName() const;
 
-    virtual void addToRequestQueue();
-
     virtual void deleteObjectInstance(const RTIData& tag);
     virtual void deleteObjectInstance(const SGTimeStamp& timeStamp, const RTIData& tag);
     virtual void localDeleteObjectInstance();
@@ -95,7 +93,7 @@ public:
 private:
     RTI::ObjectHandle _handle;
     SGSharedPtr<const RTI13ObjectClass> _objectClass;
-    SGWeakPtr<RTI13Ambassador> _ambassador;
+    SGSharedPtr<RTI13Ambassador> _ambassador;
 
     // cached storage for updates
     std::auto_ptr<RTI::AttributeHandleValuePairSet> _attributeValuePairSet;
index fb8f096a39546324ef7235466149fd9bb6e9deef..3edbed60338bbc018a4e59f396bee3ed4e708158 100644 (file)
@@ -50,9 +50,6 @@ public:
     unsigned getAttributeIndex(const std::string& name) const;
     std::string getAttributeName(unsigned index) const;
 
-    // FIXME: factor out an ambassador base
-    virtual void addToRequestQueue() = 0;
-
     virtual void deleteObjectInstance(const RTIData& tag) = 0;
     virtual void deleteObjectInstance(const SGTimeStamp& timeStamp, const RTIData& tag) = 0;
     virtual void localDeleteObjectInstance() = 0;
@@ -213,7 +210,6 @@ public:
         if (request) {
             if (!_pendingAttributeUpdateRequest) {
                 _pendingAttributeUpdateRequest = true;
-                addToRequestQueue();
             }
         }
     }