]> git.mxchange.org Git - simgear.git/commitdiff
hla: Introduce backend factory infrastructure.
authorMathias Froehlich <Mathias.Froehlich@web.de>
Sat, 25 Feb 2012 17:07:59 +0000 (18:07 +0100)
committerMathias Froehlich <Mathias.Froehlich@web.de>
Sun, 26 Feb 2012 07:50:06 +0000 (08:50 +0100)
Not finally ready, but provide a factory infrastructure
to improove plugability of different rti backend implementations.

simgear/hla/CMakeLists.txt
simgear/hla/HLAFederate.cxx
simgear/hla/RTI13FederateFactory.cxx [new file with mode: 0644]
simgear/hla/RTI13FederateFactory.hxx [new file with mode: 0644]
simgear/hla/RTIFederateFactory.cxx [new file with mode: 0644]
simgear/hla/RTIFederateFactory.hxx [new file with mode: 0644]
simgear/hla/RTIFederateFactoryRegistry.cxx [new file with mode: 0644]
simgear/hla/RTIFederateFactoryRegistry.hxx [new file with mode: 0644]

index 722f1d56487aa7d3b09fbf33e24ad406be2f0d91..b575c4ce6a80eea3ff5fa28ce5a4835df14c4bda 100644 (file)
@@ -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}" "")
index b4a75597d52be481d2daa2c9e7b85cab1dbde229..4a9d93d4626ccce9c3489b538aceba91fbc908e2 100644 (file)
 
 #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<std::string>& 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<RTIFederateFactoryRegistry> 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 (file)
index 0000000..33c03b7
--- /dev/null
@@ -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<std::string>& stringList) const
+{
+    if (name != "RTI13")
+        return 0;
+    return new RTI13Federate(stringList);
+}
+
+const SGSharedPtr<RTI13FederateFactory>&
+RTI13FederateFactory::instance()
+{
+    static SGSharedPtr<RTI13FederateFactory> federateFactory = new RTI13FederateFactory;
+    return federateFactory;
+}
+
+}
+
diff --git a/simgear/hla/RTI13FederateFactory.hxx b/simgear/hla/RTI13FederateFactory.hxx
new file mode 100644 (file)
index 0000000..9e3852e
--- /dev/null
@@ -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<std::string>& stringList) const;
+
+    static const SGSharedPtr<RTI13FederateFactory>& instance();
+};
+
+}
+
+#endif
diff --git a/simgear/hla/RTIFederateFactory.cxx b/simgear/hla/RTIFederateFactory.cxx
new file mode 100644 (file)
index 0000000..69efa76
--- /dev/null
@@ -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<RTIFederateFactoryRegistry> 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 (file)
index 0000000..fc368cd
--- /dev/null
@@ -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 <list>
+#include <string>
+#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<std::string>& stringList) const = 0;
+
+protected:
+    void _registerAtFactory();
+};
+
+}
+
+#endif
diff --git a/simgear/hla/RTIFederateFactoryRegistry.cxx b/simgear/hla/RTIFederateFactoryRegistry.cxx
new file mode 100644 (file)
index 0000000..2e786fe
--- /dev/null
@@ -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<RTIFederate>
+RTIFederateFactoryRegistry::create(const std::string& name, const std::list<std::string>& stringList) const
+{
+    SGGuard<SGMutex> guard(_mutex);
+    for (FederateFactoryList::const_iterator i = _federateFactoryList.begin(); i != _federateFactoryList.end(); ++i) {
+        SGSharedPtr<RTIFederate> federate = (*i)->create(name, stringList);
+        if (!federate.valid())
+            continue;
+        return federate;
+    }
+    return NULL;
+}
+
+void
+RTIFederateFactoryRegistry::registerFactory(RTIFederateFactory* factory)
+{
+    SGGuard<SGMutex> guard(_mutex);
+    _federateFactoryList.push_back(factory);
+}
+
+const SGSharedPtr<RTIFederateFactoryRegistry>&
+RTIFederateFactoryRegistry::instance()
+{
+    static SGSharedPtr<RTIFederateFactoryRegistry> registry = new RTIFederateFactoryRegistry;
+    return registry;
+}
+
+}
diff --git a/simgear/hla/RTIFederateFactoryRegistry.hxx b/simgear/hla/RTIFederateFactoryRegistry.hxx
new file mode 100644 (file)
index 0000000..0a42293
--- /dev/null
@@ -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 <list>
+#include <string>
+#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<RTIFederate> create(const std::string& name, const std::list<std::string>& stringList) const;
+
+    void registerFactory(RTIFederateFactory* factory);
+
+    static const SGSharedPtr<RTIFederateFactoryRegistry>& instance();
+
+private:
+    RTIFederateFactoryRegistry();
+
+    RTIFederateFactoryRegistry(const RTIFederateFactoryRegistry&);
+    RTIFederateFactoryRegistry& operator=(const RTIFederateFactoryRegistry&);
+
+    mutable SGMutex _mutex;
+    typedef std::list<SGSharedPtr<RTIFederateFactory> > FederateFactoryList;
+    FederateFactoryList _federateFactoryList;
+};
+
+}
+
+#endif