From: Mathias Froehlich Date: Sat, 25 Feb 2012 17:07:59 +0000 (+0100) Subject: hla: Introduce backend factory infrastructure. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=124db3da8e7243d3aaea5a5cad379b3a682d5fe0;p=simgear.git hla: Introduce backend factory infrastructure. Not finally ready, but provide a factory infrastructure to improove plugability of different rti backend implementations. --- diff --git a/simgear/hla/CMakeLists.txt b/simgear/hla/CMakeLists.txt index 722f1d56..b575c4ce 100644 --- a/simgear/hla/CMakeLists.txt +++ b/simgear/hla/CMakeLists.txt @@ -57,6 +57,7 @@ if(RTI_FOUND) RTI13ObjectClass.cxx RTI13ObjectInstance.cxx RTI13Federate.cxx + RTI13FederateFactory.cxx ) simgear_component(rti13 hla "${RTI13_SOURCES}" "") set_property(TARGET sgrti13 APPEND PROPERTY COMPILE_FLAGS "-I${RTI_INCLUDE_DIR}") @@ -66,5 +67,7 @@ set(RTI_SOURCES RTIObjectClass.cxx RTIObjectInstance.cxx RTIFederate.cxx + RTIFederateFactory.cxx + RTIFederateFactoryRegistry.cxx ) simgear_component(rti hla "${RTI_SOURCES}" "") diff --git a/simgear/hla/HLAFederate.cxx b/simgear/hla/HLAFederate.cxx index b4a75597..4a9d93d4 100644 --- a/simgear/hla/HLAFederate.cxx +++ b/simgear/hla/HLAFederate.cxx @@ -17,8 +17,11 @@ #include "HLAFederate.hxx" -#include "RTI13Federate.hxx" +#include "simgear/debug/logstream.hxx" + #include "RTIFederate.hxx" +#include "RTIFederateFactoryRegistry.hxx" +#include "RTI13FederateFactory.hxx" #include "RTIInteractionClass.hxx" #include "RTIObjectClass.hxx" #include "HLADataElement.hxx" @@ -35,6 +38,8 @@ HLAFederate::HLAFederate() : _timeConstrainedByLocalClock(false), _done(false) { + // For now instantiate the current only available factory here explicitly + RTI13FederateFactory::instance(); } HLAFederate::~HLAFederate() @@ -159,28 +164,9 @@ HLAFederate::setFederateName(const std::string& federateName) bool HLAFederate::connect(Version version, const std::list& stringList) { - 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(stringList); - _version = version; - _connectArguments = stringList; - break; - case RTI1516: - SG_LOG(SG_IO, SG_ALERT, "HLA version RTI1516 not yet(!?) supported."); - // _rtiFederate = new RTI1516Federate(stringList); - break; - case RTI1516E: - SG_LOG(SG_IO, SG_ALERT, "HLA version RTI1516E not yet(!?) supported."); - // _rtiFederate = new RTI1516eFederate(stringList); - break; - default: - SG_LOG(SG_NETWORK, SG_WARN, "HLA: Unknown rti version in connect!"); - } - return _rtiFederate.valid(); + _version = version; + _connectArguments = stringList; + return connect(); } bool @@ -190,17 +176,22 @@ HLAFederate::connect() SG_LOG(SG_NETWORK, SG_WARN, "HLA: Trying to connect to already connected federate!"); return false; } + + SGSharedPtr registry = RTIFederateFactoryRegistry::instance(); + if (!registry) { + SG_LOG(SG_NETWORK, SG_ALERT, "HLA: RTIFederateFactoryRegistry is no longer available!"); + return false; + } + switch (_version) { case RTI13: - _rtiFederate = new RTI13Federate(_connectArguments); + _rtiFederate = registry->create("RTI13", _connectArguments); break; case RTI1516: - SG_LOG(SG_IO, SG_ALERT, "HLA version RTI1516 not yet(!?) supported."); - // _rtiFederate = new RTI1516Federate(_connectArguments); + _rtiFederate = registry->create("RTI1516", _connectArguments); break; case RTI1516E: - SG_LOG(SG_IO, SG_ALERT, "HLA version RTI1516E not yet(!?) supported."); - // _rtiFederate = new RTI1516eFederate(_connectArguments); + _rtiFederate = registry->create("RTI1516E", _connectArguments); break; default: SG_LOG(SG_NETWORK, SG_WARN, "HLA: Unknown rti version in connect!"); diff --git a/simgear/hla/RTI13FederateFactory.cxx b/simgear/hla/RTI13FederateFactory.cxx new file mode 100644 index 00000000..33c03b7e --- /dev/null +++ b/simgear/hla/RTI13FederateFactory.cxx @@ -0,0 +1,49 @@ +// Copyright (C) 2011 - 2012 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 +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#include "RTI13FederateFactory.hxx" + +#include "RTI13Federate.hxx" + +namespace simgear { + +RTI13FederateFactory::RTI13FederateFactory() +{ + _registerAtFactory(); +} + +RTI13FederateFactory::~RTI13FederateFactory() +{ +} + +RTIFederate* +RTI13FederateFactory::create(const std::string& name, const std::list& stringList) const +{ + if (name != "RTI13") + return 0; + return new RTI13Federate(stringList); +} + +const SGSharedPtr& +RTI13FederateFactory::instance() +{ + static SGSharedPtr federateFactory = new RTI13FederateFactory; + return federateFactory; +} + +} + diff --git a/simgear/hla/RTI13FederateFactory.hxx b/simgear/hla/RTI13FederateFactory.hxx new file mode 100644 index 00000000..9e3852ec --- /dev/null +++ b/simgear/hla/RTI13FederateFactory.hxx @@ -0,0 +1,39 @@ +// Copyright (C) 2011 - 2012 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 +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#ifndef RTI13FederateFactory_hxx +#define RTI13FederateFactory_hxx + +#include "RTIFederateFactory.hxx" + +#include "simgear/structure/SGSharedPtr.hxx" + +namespace simgear { + +class RTI13FederateFactory : public RTIFederateFactory { +public: + RTI13FederateFactory(); + virtual ~RTI13FederateFactory(); + + virtual RTIFederate* create(const std::string& name, const std::list& stringList) const; + + static const SGSharedPtr& instance(); +}; + +} + +#endif diff --git a/simgear/hla/RTIFederateFactory.cxx b/simgear/hla/RTIFederateFactory.cxx new file mode 100644 index 00000000..69efa762 --- /dev/null +++ b/simgear/hla/RTIFederateFactory.cxx @@ -0,0 +1,37 @@ +// Copyright (C) 2011 - 2012 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 +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#include "RTIFederateFactory.hxx" + +#include "RTIFederateFactoryRegistry.hxx" + +namespace simgear { + +RTIFederateFactory::~RTIFederateFactory() +{ +} + +void +RTIFederateFactory::_registerAtFactory() +{ + SGSharedPtr registry = RTIFederateFactoryRegistry::instance(); + if (!registry.valid()) + return; + registry->registerFactory(this); +} + +} diff --git a/simgear/hla/RTIFederateFactory.hxx b/simgear/hla/RTIFederateFactory.hxx new file mode 100644 index 00000000..fc368cd7 --- /dev/null +++ b/simgear/hla/RTIFederateFactory.hxx @@ -0,0 +1,40 @@ +// Copyright (C) 2011 - 2012 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 +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#ifndef RTIFederateFactory_hxx +#define RTIFederateFactory_hxx + +#include +#include +#include "simgear/structure/SGReferenced.hxx" + +namespace simgear { + +class RTIFederate; + +class RTIFederateFactory : public SGReferenced { +public: + virtual ~RTIFederateFactory(); + virtual RTIFederate* create(const std::string& name, const std::list& stringList) const = 0; + +protected: + void _registerAtFactory(); +}; + +} + +#endif diff --git a/simgear/hla/RTIFederateFactoryRegistry.cxx b/simgear/hla/RTIFederateFactoryRegistry.cxx new file mode 100644 index 00000000..2e786fe9 --- /dev/null +++ b/simgear/hla/RTIFederateFactoryRegistry.cxx @@ -0,0 +1,60 @@ +// Copyright (C) 2011 - 2012 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 +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#include "RTIFederateFactoryRegistry.hxx" + +#include "simgear/threads/SGGuard.hxx" +#include "RTIFederate.hxx" + +namespace simgear { + +RTIFederateFactoryRegistry::RTIFederateFactoryRegistry() +{ +} + +RTIFederateFactoryRegistry::~RTIFederateFactoryRegistry() +{ +} + +SGSharedPtr +RTIFederateFactoryRegistry::create(const std::string& name, const std::list& stringList) const +{ + SGGuard guard(_mutex); + for (FederateFactoryList::const_iterator i = _federateFactoryList.begin(); i != _federateFactoryList.end(); ++i) { + SGSharedPtr federate = (*i)->create(name, stringList); + if (!federate.valid()) + continue; + return federate; + } + return NULL; +} + +void +RTIFederateFactoryRegistry::registerFactory(RTIFederateFactory* factory) +{ + SGGuard guard(_mutex); + _federateFactoryList.push_back(factory); +} + +const SGSharedPtr& +RTIFederateFactoryRegistry::instance() +{ + static SGSharedPtr registry = new RTIFederateFactoryRegistry; + return registry; +} + +} diff --git a/simgear/hla/RTIFederateFactoryRegistry.hxx b/simgear/hla/RTIFederateFactoryRegistry.hxx new file mode 100644 index 00000000..0a42293f --- /dev/null +++ b/simgear/hla/RTIFederateFactoryRegistry.hxx @@ -0,0 +1,54 @@ +// Copyright (C) 2011 - 2012 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 +// License as published by the Free Software Foundation; either +// version 2 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Library General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// + +#ifndef RTIFederateFactoryRegistry_hxx +#define RTIFederateFactoryRegistry_hxx + +#include +#include +#include "simgear/structure/SGReferenced.hxx" +#include "simgear/structure/SGSharedPtr.hxx" +#include "RTIFederateFactory.hxx" + +#include "simgear/threads/SGThread.hxx" + +namespace simgear { + +class RTIFederateFactoryRegistry : public SGReferenced { +public: + ~RTIFederateFactoryRegistry(); + + SGSharedPtr create(const std::string& name, const std::list& stringList) const; + + void registerFactory(RTIFederateFactory* factory); + + static const SGSharedPtr& instance(); + +private: + RTIFederateFactoryRegistry(); + + RTIFederateFactoryRegistry(const RTIFederateFactoryRegistry&); + RTIFederateFactoryRegistry& operator=(const RTIFederateFactoryRegistry&); + + mutable SGMutex _mutex; + typedef std::list > FederateFactoryList; + FederateFactoryList _federateFactoryList; +}; + +} + +#endif