+HLAObjectInstance*
+HLAFederate::createObjectInstance(HLAObjectClass* objectClass, const std::string& name)
+{
+ return new HLAObjectInstance(objectClass);
+}
+
+void
+HLAFederate::setDone(bool done)
+{
+ _done = done;
+}
+
+bool
+HLAFederate::getDone() const
+{
+ return _done;
+}
+
+bool
+HLAFederate::readObjectModel()
+{
+ // Depending on the actual version, try to find an apropriate
+ // file format for the given file. The first one is always the
+ // version native object model file format.
+ switch (getVersion()) {
+ case RTI13:
+ if (readRTI13ObjectModelTemplate(getFederationObjectModel()))
+ return true;
+ if (readRTI1516ObjectModelTemplate(getFederationObjectModel()))
+ return true;
+ return readRTI1516EObjectModelTemplate(getFederationObjectModel());
+ case RTI1516:
+ if (readRTI1516ObjectModelTemplate(getFederationObjectModel()))
+ return true;
+ if (readRTI1516EObjectModelTemplate(getFederationObjectModel()))
+ return true;
+ return readRTI13ObjectModelTemplate(getFederationObjectModel());
+ case RTI1516E:
+ if (readRTI1516EObjectModelTemplate(getFederationObjectModel()))
+ return true;
+ if (readRTI1516ObjectModelTemplate(getFederationObjectModel()))
+ return true;
+ return readRTI13ObjectModelTemplate(getFederationObjectModel());
+ default:
+ return false;
+ }
+}
+
+bool
+HLAFederate::subscribe()
+{
+ for (InteractionClassMap::iterator i = _interactionClassMap.begin(); i != _interactionClassMap.end(); ++i) {
+ if (!i->second->subscribe())
+ return false;
+ }
+
+ for (ObjectClassMap::iterator i = _objectClassMap.begin(); i != _objectClassMap.end(); ++i) {
+ if (!i->second->subscribe())
+ return false;
+ }
+
+ return true;
+}
+
+bool
+HLAFederate::publish()
+{
+ for (InteractionClassMap::iterator i = _interactionClassMap.begin(); i != _interactionClassMap.end(); ++i) {
+ if (!i->second->publish())
+ return false;
+ }
+
+ for (ObjectClassMap::iterator i = _objectClassMap.begin(); i != _objectClassMap.end(); ++i) {
+ if (!i->second->publish())
+ return false;
+ }
+
+ return true;
+}
+
+bool
+HLAFederate::init()
+{
+ // We need to talk to the rti
+ if (!connect())
+ return false;
+ // Join ...
+ if (_createFederationExecution) {
+ if (!createJoinFederationExecution())
+ return false;
+ } else {
+ if (!join())
+ return false;
+ }
+ // Read the xml file containing the object model
+ if (!readObjectModel()) {
+ shutdown();
+ return false;
+ }
+ // start being time constrained if required
+ if (_timeConstrained) {
+ if (!enableTimeConstrained()) {
+ shutdown();
+ return false;
+ }
+ }
+ // Now that we are potentially time constrained, we can subscribe.
+ // This is to make sure we do not get any time stamped message
+ // converted to a non time stamped message by the rti.
+ if (!subscribe()) {
+ shutdown();
+ return false;
+ }
+ // Before we publish anything start getting regulating if required
+ if (_timeRegulating) {
+ if (!enableTimeRegulation()) {
+ shutdown();
+ return false;
+ }
+ }
+ // Note that starting from here, we need to be careful with things
+ // requireing unbounded time. The rest of the federation might wait
+ // for us to finish!
+
+ // Compute the time offset from the system time to the simulation time
+ if (_timeConstrainedByLocalClock) {
+ if (!enableTimeConstrainedByLocalClock()) {
+ SG_LOG(SG_NETWORK, SG_WARN, "Cannot enable time constrained by local clock!");
+ shutdown();
+ return false;
+ }
+ }
+
+ // Publish what we want to write
+ if (!publish()) {
+ shutdown();
+ return false;
+ }
+
+ return true;
+}
+
+bool
+HLAFederate::update()
+{
+ return timeAdvanceBy(_timeIncrement);
+}
+
+bool
+HLAFederate::shutdown()
+{
+ // On shutdown, just try all in order.
+ // If something goes wrong, continue and try to get out here as good as possible.
+ bool ret = true;
+
+ if (_createFederationExecution) {
+ if (!resignDestroyFederationExecution())
+ ret = false;
+ } else {
+ if (!resign())
+ ret = false;
+ }
+
+ if (!disconnect())
+ ret = false;
+
+ return ret;
+}
+
+bool
+HLAFederate::exec()
+{
+ if (!init())
+ return false;
+
+ while (!getDone()) {
+ if (!update()) {
+ shutdown();
+ return false;
+ }
+ }
+
+ if (!shutdown())
+ return false;
+
+ return true;
+}
+
+void
+HLAFederate::_clearRTI()
+{
+ for (InteractionClassMap::iterator i = _interactionClassMap.begin(); i != _interactionClassMap.end(); ++i)
+ i->second->_clearRTIInteractionClass();
+ for (ObjectInstanceMap::iterator i = _objectInstanceMap.begin(); i != _objectInstanceMap.end(); ++i)
+ i->second->_clearRTIObjectInstance();
+ for (ObjectClassMap::iterator i = _objectClassMap.begin(); i != _objectClassMap.end(); ++i)
+ i->second->_clearRTIObjectClass();
+
+ _rtiFederate = 0;
+}
+
+bool
+HLAFederate::_insertInteractionClass(const SGSharedPtr<HLAInteractionClass>& interactionClass)
+{
+ if (!interactionClass.valid())
+ return false;
+ if (_interactionClassMap.find(interactionClass->getName()) != _interactionClassMap.end()) {
+ SG_LOG(SG_IO, SG_ALERT, "HLA: _insertInteractionClass: object instance with name \""
+ << interactionClass->getName() << "\" already known to federate!");
+ return false;
+ }
+ _interactionClassMap.insert(InteractionClassMap::value_type(interactionClass->getName(), interactionClass));
+ return true;
+}
+
+bool
+HLAFederate::_insertObjectClass(const SGSharedPtr<HLAObjectClass>& objectClass)
+{
+ if (!objectClass.valid())
+ return false;
+ if (_objectClassMap.find(objectClass->getName()) != _objectClassMap.end()) {
+ SG_LOG(SG_IO, SG_ALERT, "HLA: _insertObjectClass: object instance with name \""
+ << objectClass->getName() << "\" already known to federate!");
+ return false;
+ }
+ _objectClassMap.insert(ObjectClassMap::value_type(objectClass->getName(), objectClass));
+ return true;
+}
+
+bool
+HLAFederate::_insertObjectInstance(const SGSharedPtr<HLAObjectInstance>& objectInstance)
+{
+ if (!objectInstance.valid())
+ return false;
+ if (objectInstance->getName().empty()) {
+ SG_LOG(SG_IO, SG_ALERT, "HLA: _insertObjectInstance: trying to insert object instance with empty name!");
+ return false;
+ }
+ if (_objectInstanceMap.find(objectInstance->getName()) != _objectInstanceMap.end()) {
+ SG_LOG(SG_IO, SG_WARN, "HLA: _insertObjectInstance: object instance with name \""
+ << objectInstance->getName() << "\" already known to federate!");
+ return false;
+ }
+ _objectInstanceMap.insert(ObjectInstanceMap::value_type(objectInstance->getName(), objectInstance));
+ return true;
+}
+
+void
+HLAFederate::_eraseObjectInstance(const std::string& name)
+{
+ ObjectInstanceMap::iterator i = _objectInstanceMap.find(name);
+ if (i == _objectInstanceMap.end()) {
+ SG_LOG(SG_IO, SG_WARN, "HLA: _eraseObjectInstance: object instance with name \""
+ << name << "\" not known to federate!");
+ return;
+ }
+ _objectInstanceMap.erase(i);
+}
+