refactoring of Module Loader
authorYoungjae Shin <yj99.shin@samsung.com>
Fri, 12 Aug 2022 06:58:02 +0000 (15:58 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 15 Sep 2022 05:23:06 +0000 (14:23 +0900)
- remove composition of module loader
- Add NullTranport for robustness

13 files changed:
CMakeLists.txt
include/AITT.h
include/AITTEx.h [moved from common/AITTEx.h with 96% similarity, mode: 0644]
src/AITTImpl.cc
src/AITTImpl.h
src/ModuleLoader.cc [new file with mode: 0644]
src/ModuleLoader.h [moved from src/TransportModuleLoader.h with 57% similarity]
src/NullTransport.cc [new file with mode: 0644]
src/NullTransport.h [new file with mode: 0644]
src/TransportModuleLoader.cc [deleted file]
tests/AITT_test.cc
tests/CMakeLists.txt
tests/ModuleLoader_test.cc [moved from tests/TransportModuleLoader_test.cc with 54% similarity]

index c6fde39..976ac18 100644 (file)
@@ -51,8 +51,8 @@ INCLUDE_DIRECTORIES(include common)
 
 AUX_SOURCE_DIRECTORY(src AITT_SRC)
 
-ADD_LIBRARY(M_LOADER_OBJ OBJECT src/TransportModuleLoader.cc)
-LIST(REMOVE_ITEM AITT_SRC src/TransportModuleLoader.cc)
+ADD_LIBRARY(M_LOADER_OBJ OBJECT src/ModuleLoader.cc src/NullTransport.cc)
+LIST(REMOVE_ITEM AITT_SRC src/ModuleLoader.cc src/NullTransport.cc)
 
 ADD_LIBRARY(${PROJECT_NAME} SHARED ${AITT_SRC} $<TARGET_OBJECTS:M_LOADER_OBJ>)
 TARGET_LINK_LIBRARIES(${PROJECT_NAME} Threads::Threads ${CMAKE_DL_LIBS} ${AITT_COMMON})
index aeeacd8..5817662 100644 (file)
@@ -15,6 +15,7 @@
  */
 #pragma once
 
+#include <AITTEx.h>
 #include <AittTypes.h>
 #include <MSG.h>
 
old mode 100755 (executable)
new mode 100644 (file)
similarity index 96%
rename from common/AITTEx.h
rename to include/AITTEx.h
index 8018c7f..9657dc3
@@ -1,49 +1,48 @@
-/*\r
- * Copyright (c) 2021-2022 Samsung Electronics Co., Ltd All Rights Reserved\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *     http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-#pragma once\r
-\r
-#include <exception>\r
-#include <string>\r
-#include <vector>\r
-\r
-namespace aitt {\r
-\r
-class AITTEx : public std::exception {\r
-  public:\r
-    typedef enum {\r
-        INVALID_ARG,\r
-        NO_MEMORY,\r
-        OPERATION_FAILED,\r
-        SYSTEM_ERR,\r
-        MQTT_ERR,\r
-        NO_DATA\r
-    } ErrCode;\r
-\r
-    AITTEx(ErrCode err_code);\r
-    AITTEx(ErrCode err_code, const std::string& custom_err_msg);\r
-\r
-    ErrCode getErrCode();\r
-    virtual const char* what() const throw() override;\r
-\r
-  private:\r
-    ErrCode err_code;\r
-    std::string err_msg;\r
-\r
-    std::string getErrString() const;\r
-};\r
-\r
-}  // namespace aitt\r
-\r
+/*
+ * Copyright (c) 2021-2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <exception>
+#include <string>
+#include <vector>
+
+namespace aitt {
+
+class AITTEx : public std::exception {
+  public:
+    typedef enum {
+        INVALID_ARG,
+        NO_MEMORY,
+        OPERATION_FAILED,
+        SYSTEM_ERR,
+        MQTT_ERR,
+        NO_DATA
+    } ErrCode;
+
+    AITTEx(ErrCode err_code);
+    AITTEx(ErrCode err_code, const std::string& custom_err_msg);
+
+    ErrCode getErrCode();
+    virtual const char* what() const throw() override;
+
+  private:
+    ErrCode err_code;
+    std::string err_msg;
+
+    std::string getErrString() const;
+};
+
+}  // namespace aitt
index b851625..f5ea893 100644 (file)
 namespace aitt {
 
 AITT::Impl::Impl(AITT &parent, const std::string &id, const std::string &ipAddr, bool clearSession)
-      : public_api(parent),
-        id_(id),
-        mq(id, clearSession),
-        discovery(id),
-        reply_id(0),
-        modules(ipAddr)
+      : public_api(parent), id_(id), mq(id, clearSession), discovery(id), reply_id(0)
 {
     // TODO:
     // Validate ipAddr
-
+    ModuleLoader loader(ipAddr);
+    for (ModuleLoader::Type i = ModuleLoader::TYPE_MQTT; i < ModuleLoader::TYPE_MAX;
+          i = ModuleLoader::Type(i + 1)) {
+        ModuleLoader::ModuleHandle handle = loader.OpenModule(i);
+        if (handle == nullptr)
+            ERR("OpenModule() Fail");
+
+        modules[i] =
+              new ModuleObj(std::move(handle), loader.LoadTransport(handle.get(), discovery));
+    }
     aittThread = std::thread(&AITT::Impl::ThreadMain, this);
 }
 
@@ -56,6 +60,11 @@ AITT::Impl::~Impl(void)
 
     if (aittThread.joinable())
         aittThread.join();
+
+    for (ModuleLoader::Type i = ModuleLoader::TYPE_MQTT; i < ModuleLoader::TYPE_MAX;
+          i = ModuleLoader::Type(i + 1)) {
+        delete modules[i];
+    }
 }
 
 void AITT::Impl::ThreadMain(void)
@@ -89,8 +98,6 @@ void AITT::Impl::ConnectionCB(ConnectionCallback cb, void *user_data, int status
 void AITT::Impl::Connect(const std::string &host, int port, const std::string &username,
       const std::string &password)
 {
-    modules.Init(discovery);
-
     discovery.Start(host, port, username, password);
     mq.Connect(host, port, username, password);
 
@@ -118,10 +125,11 @@ void AITT::Impl::UnsubscribeAll()
         case AITT_TYPE_MQTT:
             mq.Unsubscribe(subscribe_info->second);
             break;
-
         case AITT_TYPE_TCP:
+            GetTransport(ModuleLoader::TYPE_TCP)->Unsubscribe(subscribe_info->second);
+            break;
         case AITT_TYPE_WEBRTC:
-            modules.GetInstance(subscribe_info->first)->Unsubscribe(subscribe_info->second);
+            GetTransport(ModuleLoader::TYPE_WEBRTC)->Unsubscribe(subscribe_info->second);
             break;
 
         default:
@@ -134,6 +142,11 @@ void AITT::Impl::UnsubscribeAll()
     subscribed_list.clear();
 }
 
+inline std::shared_ptr<AittTransport> AITT::Impl::GetTransport(ModuleLoader::Type type)
+{
+    return modules[type]->second;
+}
+
 void AITT::Impl::ConfigureTransportModule(const std::string &key, const std::string &value,
       AittProtocol protocols)
 {
@@ -145,21 +158,17 @@ void AITT::Impl::Publish(const std::string &topic, const void *data, const size_
     if ((protocols & AITT_TYPE_MQTT) == AITT_TYPE_MQTT)
         mq.Publish(topic, data, datalen, qos, retain);
 
-    // NOTE:
-    // Invoke the publish method of the specified transport module
-    if ((protocols & AITT_TYPE_TCP) == AITT_TYPE_TCP) {
-        auto tcpModule = modules.GetInstance(AITT_TYPE_TCP);
-        tcpModule->Publish(topic, data, datalen, qos, retain);
-    }
-    if ((protocols & AITT_TYPE_WEBRTC) == AITT_TYPE_WEBRTC) {
+    if ((protocols & AITT_TYPE_TCP) == AITT_TYPE_TCP)
+        GetTransport(ModuleLoader::TYPE_TCP)->Publish(topic, data, datalen, qos, retain);
+
+    if ((protocols & AITT_TYPE_WEBRTC) == AITT_TYPE_WEBRTC)
         PublishWebRtc(topic, data, datalen, qos, retain);
-    }
 }
 
 void AITT::Impl::PublishWebRtc(const std::string &topic, const void *data, const size_t datalen,
       AittQoS qos, bool retain)
 {
-    auto webrtcModule = modules.GetInstance(AITT_TYPE_WEBRTC);
+    auto webrtcModule = GetTransport(ModuleLoader::TYPE_WEBRTC);
     flexbuffers::Builder fbb;
     fbb.Map([=, &fbb]() {
         fbb.String("Id", id_ + WEBRTC_ID_POSTFIX);
@@ -240,7 +249,7 @@ void AITT::Impl::DetachedCB(SubscribeCallback cb, MSG msg, void *data, const siz
 
 void *AITT::Impl::Unsubscribe(AittSubscribeID subscribe_id)
 {
-    INFO("[%s] %p", __func__, subscribe_id);
+    INFO("subscribe_id : %p", subscribe_id);
     SubscribeInfo *info = reinterpret_cast<SubscribeInfo *>(subscribe_id);
 
     std::unique_lock<std::mutex> lock(subscribed_list_mutex_);
@@ -258,12 +267,12 @@ void *AITT::Impl::Unsubscribe(AittSubscribeID subscribe_id)
         user_data = mq.Unsubscribe(found_info->second);
         break;
     case AITT_TYPE_TCP: {
-        auto tcpModule = modules.GetInstance(AITT_TYPE_TCP);
+        auto tcpModule = GetTransport(ModuleLoader::TYPE_TCP);
         user_data = tcpModule->Unsubscribe(found_info->second);
         break;
     }
     case AITT_TYPE_WEBRTC: {
-        auto webrtcModule = modules.GetInstance(AITT_TYPE_WEBRTC);
+        auto webrtcModule = GetTransport(ModuleLoader::TYPE_WEBRTC);
         user_data = webrtcModule->Unsubscribe(found_info->second);
         break;
     }
@@ -390,7 +399,7 @@ void AITT::Impl::SendReply(MSG *msg, const void *data, const int datalen, bool e
 void *AITT::Impl::SubscribeTCP(SubscribeInfo *handle, const std::string &topic,
       const SubscribeCallback &cb, void *user_data, AittQoS qos)
 {
-    auto tcpModule = modules.GetInstance(AITT_TYPE_TCP);
+    auto tcpModule = GetTransport(ModuleLoader::TYPE_TCP);
     return tcpModule->Subscribe(
           topic,
           [handle, cb](const std::string &topic, const void *data, const size_t datalen,
@@ -409,7 +418,7 @@ void *AITT::Impl::SubscribeTCP(SubscribeInfo *handle, const std::string &topic,
 void *AITT::Impl::SubscribeWebRtc(SubscribeInfo *handle, const std::string &topic,
       const SubscribeCallback &cb, void *user_data, AittQoS qos)
 {
-    auto webrtc_module = modules.GetInstance(AITT_TYPE_WEBRTC);
+    auto webrtc_module = GetTransport(ModuleLoader::TYPE_WEBRTC);
     flexbuffers::Builder fbb;
     fbb.Map([=, &fbb]() {
         fbb.String("Id", id_ + WEBRTC_ID_POSTFIX);
index 08bec4a..b39fdb2 100644 (file)
@@ -28,7 +28,7 @@
 #include "AittDiscovery.h"
 #include "MQ.h"
 #include "MainLoopHandler.h"
-#include "TransportModuleLoader.h"
+#include "ModuleLoader.h"
 
 namespace aitt {
 
@@ -68,6 +68,7 @@ class AITT::Impl {
   private:
     using Blob = std::pair<const void *, int>;
     using SubscribeInfo = std::pair<AittProtocol, void *>;
+    using ModuleObj = std::pair<ModuleLoader::ModuleHandle, std::shared_ptr<AittTransport>>;
 
     void ConnectionCB(ConnectionCallback cb, void *user_data, int status);
     AittSubscribeID SubscribeMQ(SubscribeInfo *info, MainLoopHandler *loop_handle,
@@ -84,6 +85,7 @@ class AITT::Impl {
     void PublishWebRtc(const std::string &topic, const void *data, const size_t datalen,
           AittQoS qos, bool retain);
     void UnsubscribeAll();
+    std::shared_ptr<AittTransport> GetTransport(ModuleLoader::Type type);
 
     AITT &public_api;
     std::string id_;
@@ -92,7 +94,7 @@ class AITT::Impl {
     MQ mq;
     AittDiscovery discovery;
     unsigned short reply_id;
-    TransportModuleLoader modules;
+    ModuleObj *modules[ModuleLoader::TYPE_MAX];
     MainLoopHandler main_loop;
     void ThreadMain(void);
     std::thread aittThread;
diff --git a/src/ModuleLoader.cc b/src/ModuleLoader.cc
new file mode 100644 (file)
index 0000000..2c538c1
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2021-2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ModuleLoader.h"
+
+#include <dlfcn.h>
+
+#include "AITTEx.h"
+#include "NullTransport.h"
+#include "aitt_internal.h"
+
+namespace aitt {
+
+ModuleLoader::ModuleLoader(const std::string &ip) : ip(ip)
+{
+}
+
+std::string ModuleLoader::GetModuleFilename(Type type)
+{
+    if (type == TYPE_TCP)
+        return "libaitt-transport-tcp.so";
+    if (type == TYPE_WEBRTC)
+        return "libaitt-transport-webrtc.so";
+
+    return std::string("Unknown");
+}
+
+ModuleLoader::ModuleHandle ModuleLoader::OpenModule(Type type)
+{
+    std::string filename = GetModuleFilename(type);
+
+    ModuleHandle handle(dlopen(filename.c_str(), RTLD_LAZY | RTLD_LOCAL),
+          [](const void *handle) -> void {
+              if (dlclose(const_cast<void *>(handle)))
+                  ERR("dlclose: %s", dlerror());
+          });
+    if (handle == nullptr)
+        ERR("dlopen: %s", dlerror());
+
+    return handle;
+}
+
+std::shared_ptr<AittTransport> ModuleLoader::LoadTransport(void *handle, AittDiscovery &discovery)
+{
+    AittTransport::ModuleEntry get_instance_fn = reinterpret_cast<AittTransport::ModuleEntry>(
+          dlsym(handle, AittTransport::MODULE_ENTRY_NAME));
+    if (get_instance_fn == nullptr) {
+        ERR("dlsym: %s", dlerror());
+        return std::shared_ptr<AittTransport>(new NullTransport(ip.c_str(), discovery));
+    }
+
+    std::shared_ptr<AittTransport> instance(
+          static_cast<AittTransport *>(get_instance_fn(ip.c_str(), discovery)),
+          [](const AittTransport *instance) -> void { delete instance; });
+    if (instance == nullptr) {
+        ERR("Failed to create a new instance");
+        return std::shared_ptr<AittTransport>(new NullTransport(ip.c_str(), discovery));
+    }
+
+    return instance;
+}
+
+}  // namespace aitt
similarity index 57%
rename from src/TransportModuleLoader.h
rename to src/ModuleLoader.h
index 576c97e..647a82d 100644 (file)
 #include <mutex>
 #include <string>
 
-#include "TransportModuleLoader.h"
+#include "ModuleLoader.h"
 
 namespace aitt {
 
-class TransportModuleLoader {
+class ModuleLoader {
   public:
-    explicit TransportModuleLoader(const std::string &ip);
-    virtual ~TransportModuleLoader() = default;
+    enum Type {
+        TYPE_MQTT = 0,
+        TYPE_TCP,
+        TYPE_WEBRTC,
+        TYPE_RTSP,
+        TYPE_MAX,
+    };
 
-    void Init(AittDiscovery &discovery);
-    std::shared_ptr<AittTransport> GetInstance(AittProtocol protocol);
+    using ModuleHandle = std::unique_ptr<void, void (*)(const void *)>;
 
-  private:
-    using Handler = std::unique_ptr<void, void (*)(const void *)>;
-    using ModuleMap = std::map<AittProtocol, std::pair<Handler, std::shared_ptr<AittTransport>>>;
+    explicit ModuleLoader(const std::string &ip);
+    virtual ~ModuleLoader() = default;
+
+    ModuleHandle OpenModule(Type type);
+    std::shared_ptr<AittTransport> LoadTransport(void *handle, AittDiscovery &discovery);
 
-    std::string GetModuleFilename(AittProtocol protocol);
-    int LoadModule(AittProtocol protocol, AittDiscovery &discovery);
+  private:
+    std::string GetModuleFilename(Type type);
 
-    ModuleMap module_table;
-    std::mutex module_lock;
     std::string ip;
 };
 
diff --git a/src/NullTransport.cc b/src/NullTransport.cc
new file mode 100644 (file)
index 0000000..097f28a
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "NullTransport.h"
+
+#include "aitt_internal.h"
+
+NullTransport::NullTransport(const std::string& ip, AittDiscovery& discovery)
+      : AittTransport(discovery)
+{
+}
+
+void NullTransport::Publish(const std::string& topic, const void* data, const size_t datalen,
+      const std::string& correlation, AittQoS qos, bool retain)
+{
+}
+
+void NullTransport::Publish(const std::string& topic, const void* data, const size_t datalen,
+      AittQoS qos, bool retain)
+{
+}
+
+void* NullTransport::Subscribe(const std::string& topic, const SubscribeCallback& cb, void* cbdata,
+      AittQoS qos)
+{
+    return nullptr;
+}
+
+void* NullTransport::Subscribe(const std::string& topic, const SubscribeCallback& cb,
+      const void* data, const size_t datalen, void* cbdata, AittQoS qos)
+{
+    return nullptr;
+}
+
+void* NullTransport::Unsubscribe(void* handle)
+{
+    return nullptr;
+}
diff --git a/src/NullTransport.h b/src/NullTransport.h
new file mode 100644 (file)
index 0000000..9cfa78e
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <AittTransport.h>
+using AittTransport = aitt::AittTransport;
+using AittDiscovery = aitt::AittDiscovery;
+
+class NullTransport : public AittTransport {
+  public:
+    explicit NullTransport(const std::string &ip, AittDiscovery &discovery);
+    virtual ~NullTransport(void) = default;
+
+    void Publish(const std::string &topic, const void *data, const size_t datalen,
+          const std::string &correlation, AittQoS qos = AITT_QOS_AT_MOST_ONCE,
+          bool retain = false) override;
+
+    void Publish(const std::string &topic, const void *data, const size_t datalen,
+          AittQoS qos = AITT_QOS_AT_MOST_ONCE, bool retain = false) override;
+
+    void *Subscribe(const std::string &topic, const SubscribeCallback &cb, void *cbdata = nullptr,
+          AittQoS qos = AITT_QOS_AT_MOST_ONCE) override;
+
+    void *Subscribe(const std::string &topic, const SubscribeCallback &cb, const void *data,
+          const size_t datalen, void *cbdata = nullptr,
+          AittQoS qos = AITT_QOS_AT_MOST_ONCE) override;
+
+    void *Unsubscribe(void *handle) override;
+};
diff --git a/src/TransportModuleLoader.cc b/src/TransportModuleLoader.cc
deleted file mode 100644 (file)
index a953ac9..0000000
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Copyright (c) 2021-2022 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "TransportModuleLoader.h"
-
-#include <dlfcn.h>
-
-#include "AITTEx.h"
-#include "aitt_internal.h"
-
-namespace aitt {
-
-TransportModuleLoader::TransportModuleLoader(const std::string &ip) : ip(ip)
-{
-}
-
-std::string TransportModuleLoader::GetModuleFilename(AittProtocol protocol)
-{
-    // TODO:
-    // We are able to generate the module name by a particular syntax,
-    // It could be introduced later when we have several modules.
-    if (protocol == AITT_TYPE_TCP)
-        return "libaitt-transport-tcp.so";
-    if (protocol == AITT_TYPE_WEBRTC)
-        return "libaitt-transport-webrtc.so";
-
-    return std::string();
-}
-
-int TransportModuleLoader::LoadModule(AittProtocol protocol, AittDiscovery &discovery)
-{
-    std::string filename = GetModuleFilename(protocol);
-
-    Handler handle(dlopen(filename.c_str(), RTLD_LAZY | RTLD_LOCAL),
-          [](const void *handle) -> void {
-              if (dlclose(const_cast<void *>(handle)))
-                  ERR("dlclose: %s", dlerror());
-          });
-    if (handle == nullptr) {
-        ERR("dlopen: %s", dlerror());
-        return -1;
-    }
-
-    AittTransport::ModuleEntry get_instance_fn = reinterpret_cast<AittTransport::ModuleEntry>(
-          dlsym(handle.get(), AittTransport::MODULE_ENTRY_NAME));
-    if (get_instance_fn == nullptr) {
-        ERR("dlsym: %s", dlerror());
-        return -1;
-    }
-
-    std::shared_ptr<AittTransport> instance(
-          static_cast<AittTransport *>(get_instance_fn(ip.c_str(), discovery)),
-          [](const AittTransport *instance) -> void { delete instance; });
-    if (instance == nullptr) {
-        ERR("Failed to create a new instance");
-        return -1;
-    }
-
-    module_table.emplace(protocol, std::make_pair(std::move(handle), instance));
-
-    return 0;
-}
-
-void TransportModuleLoader::Init(AittDiscovery &discovery)
-{
-    std::lock_guard<std::mutex> lock_from_here(module_lock);
-    if (LoadModule(AITT_TYPE_TCP, discovery) < 0) {
-        ERR("LoadModule(AITT_TYPE_TCP) Fail");
-    }
-
-#ifdef WITH_WEBRTC
-    if (LoadModule(AITT_TYPE_WEBRTC, discovery) < 0) {
-        ERR("LoadModule(AITT_TYPE_WEBRTC) Fail");
-    }
-#endif  // WITH_WEBRTC
-}
-
-std::shared_ptr<AittTransport> TransportModuleLoader::GetInstance(AittProtocol protocol)
-{
-    std::lock_guard<std::mutex> lock_from_here(module_lock);
-
-    auto item = module_table.find(protocol);
-    if (item == module_table.end()) {
-        ERR("Not Initialized");
-        // throw AITTEx(AITTEx::NO_DATA, "Not Initialized");
-        return nullptr;
-    }
-
-    return item->second.second;
-}
-
-}  // namespace aitt
index 1dcd8c6..2305937 100644 (file)
@@ -195,12 +195,14 @@ TEST_F(AITTTest, Positive_Publish_Multiple_Protocols_Anytime)
         aitt.Connect();
         aitt.Publish(testTopic, TEST_MSG, sizeof(TEST_MSG),
               (AittProtocol)(AITT_TYPE_MQTT | AITT_TYPE_TCP));
+        aitt.Publish(testTopic, TEST_MSG, sizeof(TEST_MSG),
+              (AittProtocol)(AITT_TYPE_MQTT | AITT_TYPE_TCP | AITT_TYPE_WEBRTC));
     } catch (std::exception &e) {
         FAIL() << "Unexpected exception: " << e.what();
     }
 }
 
-TEST_F(AITTTest, Positive_Subscribe_Anytime)
+TEST_F(AITTTest, Positive_Subscribe_WebRTC_Anytime)
 {
     try {
         AITT aitt(clientId, LOCAL_IP, true);
@@ -208,7 +210,7 @@ TEST_F(AITTTest, Positive_Subscribe_Anytime)
         aitt.Subscribe(
               testTopic,
               [](aitt::MSG *handle, const void *msg, const size_t szmsg, void *cbdata) -> void {},
-              nullptr, AITT_TYPE_TCP);
+              nullptr, AITT_TYPE_WEBRTC);
     } catch (std::exception &e) {
         FAIL() << "Unexpected exception: " << e.what();
     }
index 307765d..37d00cb 100644 (file)
@@ -46,7 +46,7 @@ ADD_TEST(
 )
 
 ###########################################################################
-ADD_EXECUTABLE(${AITT_UT}_module TransportModuleLoader_test.cc $<TARGET_OBJECTS:M_LOADER_OBJ>)
+ADD_EXECUTABLE(${AITT_UT}_module ModuleLoader_test.cc $<TARGET_OBJECTS:M_LOADER_OBJ>)
 TARGET_LINK_LIBRARIES(${AITT_UT}_module ${UT_NEEDS_LIBRARIES} ${AITT_NEEDS_LIBRARIES} ${CMAKE_DL_LIBS} ${AITT_COMMON})
 TARGET_INCLUDE_DIRECTORIES(${AITT_UT}_module PRIVATE ../src)
 
similarity index 54%
rename from tests/TransportModuleLoader_test.cc
rename to tests/ModuleLoader_test.cc
index 57e69b1..37f8aee 100644 (file)
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "TransportModuleLoader.h"
+#include "ModuleLoader.h"
 
 #include <AITT.h>
 #include <gtest/gtest.h>
 #include "AittTransport.h"
 #include "aitt_internal.h"
 
-class TransportModuleLoaderTest : public testing::Test {
+using ModuleLoader = aitt::ModuleLoader;
+
+class ModuleLoaderTest : public testing::Test {
   public:
-    TransportModuleLoaderTest(void) : discovery("test"), loader("127.0.0.1")
-    {
-        loader.Init(discovery);
-    }
+    ModuleLoaderTest(void) : discovery("test"), loader("127.0.0.1") {}
 
   protected:
     void SetUp() override {}
     void TearDown() override {}
 
     aitt::AittDiscovery discovery;
-    aitt::TransportModuleLoader loader;
+    aitt::ModuleLoader loader;
 };
 
-TEST_F(TransportModuleLoaderTest, Positive_GetInstance_Anytime)
+TEST_F(ModuleLoaderTest, Positive_LoadTransport_Anytime)
 {
-    std::shared_ptr<aitt::AittTransport> module = loader.GetInstance(AITT_TYPE_TCP);
+    ModuleLoader::ModuleHandle handle = loader.OpenModule(ModuleLoader::TYPE_TCP);
+    ASSERT_NE(handle, nullptr);
+
+    std::shared_ptr<aitt::AittTransport> module = loader.LoadTransport(handle.get(), discovery);
     ASSERT_NE(module, nullptr);
 }
 
-TEST_F(TransportModuleLoaderTest, Negative_GetInstance_Anytime)
+TEST_F(ModuleLoaderTest, Negative_LoadTransport_Anytime)
 {
-    std::shared_ptr<aitt::AittTransport> module = loader.GetInstance(AITT_TYPE_MQTT);
-    ASSERT_EQ(module, nullptr);
+    ModuleLoader::ModuleHandle handle = loader.OpenModule(ModuleLoader::TYPE_MQTT);
+    ASSERT_EQ(handle.get(), nullptr);
+
+    auto module = loader.LoadTransport(handle.get(), discovery);
+    ASSERT_NE(module, nullptr);
 }