From 75cfcda03e91650307a6995aa9d140345589de4b Mon Sep 17 00:00:00 2001 From: Youngjae Shin Date: Fri, 12 Aug 2022 15:58:02 +0900 Subject: [PATCH] refactoring of Module Loader - remove composition of module loader - Add NullTranport for robustness --- CMakeLists.txt | 4 +- include/AITT.h | 1 + {common => include}/AITTEx.h | 97 ++++++++++--------- src/AITTImpl.cc | 59 +++++++----- src/AITTImpl.h | 6 +- src/ModuleLoader.cc | 76 +++++++++++++++ src/{TransportModuleLoader.h => ModuleLoader.h} | 30 +++--- src/NullTransport.cc | 50 ++++++++++ src/NullTransport.h | 42 +++++++++ src/TransportModuleLoader.cc | 105 --------------------- tests/AITT_test.cc | 6 +- tests/CMakeLists.txt | 2 +- ...rtModuleLoader_test.cc => ModuleLoader_test.cc} | 29 +++--- 13 files changed, 296 insertions(+), 211 deletions(-) rename {common => include}/AITTEx.h (96%) mode change 100755 => 100644 create mode 100644 src/ModuleLoader.cc rename src/{TransportModuleLoader.h => ModuleLoader.h} (57%) create mode 100644 src/NullTransport.cc create mode 100644 src/NullTransport.h delete mode 100644 src/TransportModuleLoader.cc rename tests/{TransportModuleLoader_test.cc => ModuleLoader_test.cc} (54%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c6fde39..976ac18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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_LINK_LIBRARIES(${PROJECT_NAME} Threads::Threads ${CMAKE_DL_LIBS} ${AITT_COMMON}) diff --git a/include/AITT.h b/include/AITT.h index aeeacd8..5817662 100644 --- a/include/AITT.h +++ b/include/AITT.h @@ -15,6 +15,7 @@ */ #pragma once +#include #include #include diff --git a/common/AITTEx.h b/include/AITTEx.h old mode 100755 new mode 100644 similarity index 96% rename from common/AITTEx.h rename to include/AITTEx.h index 8018c7f..9657dc3 --- a/common/AITTEx.h +++ b/include/AITTEx.h @@ -1,49 +1,48 @@ -/* - * 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 -#include -#include - -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 - +/* + * 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 +#include +#include + +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 diff --git a/src/AITTImpl.cc b/src/AITTImpl.cc index b851625..f5ea893 100644 --- a/src/AITTImpl.cc +++ b/src/AITTImpl.cc @@ -31,16 +31,20 @@ 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 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(subscribe_id); std::unique_lock 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); diff --git a/src/AITTImpl.h b/src/AITTImpl.h index 08bec4a..b39fdb2 100644 --- a/src/AITTImpl.h +++ b/src/AITTImpl.h @@ -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; using SubscribeInfo = std::pair; + using ModuleObj = std::pair>; 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 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 index 0000000..2c538c1 --- /dev/null +++ b/src/ModuleLoader.cc @@ -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 + +#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(handle))) + ERR("dlclose: %s", dlerror()); + }); + if (handle == nullptr) + ERR("dlopen: %s", dlerror()); + + return handle; +} + +std::shared_ptr ModuleLoader::LoadTransport(void *handle, AittDiscovery &discovery) +{ + AittTransport::ModuleEntry get_instance_fn = reinterpret_cast( + dlsym(handle, AittTransport::MODULE_ENTRY_NAME)); + if (get_instance_fn == nullptr) { + ERR("dlsym: %s", dlerror()); + return std::shared_ptr(new NullTransport(ip.c_str(), discovery)); + } + + std::shared_ptr instance( + static_cast(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(new NullTransport(ip.c_str(), discovery)); + } + + return instance; +} + +} // namespace aitt diff --git a/src/TransportModuleLoader.h b/src/ModuleLoader.h similarity index 57% rename from src/TransportModuleLoader.h rename to src/ModuleLoader.h index 576c97e..647a82d 100644 --- a/src/TransportModuleLoader.h +++ b/src/ModuleLoader.h @@ -23,27 +23,31 @@ #include #include -#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 GetInstance(AittProtocol protocol); + using ModuleHandle = std::unique_ptr; - private: - using Handler = std::unique_ptr; - using ModuleMap = std::map>>; + explicit ModuleLoader(const std::string &ip); + virtual ~ModuleLoader() = default; + + ModuleHandle OpenModule(Type type); + std::shared_ptr 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 index 0000000..097f28a --- /dev/null +++ b/src/NullTransport.cc @@ -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 index 0000000..9cfa78e --- /dev/null +++ b/src/NullTransport.h @@ -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 +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 index a953ac9..0000000 --- a/src/TransportModuleLoader.cc +++ /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 - -#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(handle))) - ERR("dlclose: %s", dlerror()); - }); - if (handle == nullptr) { - ERR("dlopen: %s", dlerror()); - return -1; - } - - AittTransport::ModuleEntry get_instance_fn = reinterpret_cast( - dlsym(handle.get(), AittTransport::MODULE_ENTRY_NAME)); - if (get_instance_fn == nullptr) { - ERR("dlsym: %s", dlerror()); - return -1; - } - - std::shared_ptr instance( - static_cast(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 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 TransportModuleLoader::GetInstance(AittProtocol protocol) -{ - std::lock_guard 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 diff --git a/tests/AITT_test.cc b/tests/AITT_test.cc index 1dcd8c6..2305937 100644 --- a/tests/AITT_test.cc +++ b/tests/AITT_test.cc @@ -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(); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 307765d..37d00cb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -46,7 +46,7 @@ ADD_TEST( ) ########################################################################### -ADD_EXECUTABLE(${AITT_UT}_module TransportModuleLoader_test.cc $) +ADD_EXECUTABLE(${AITT_UT}_module ModuleLoader_test.cc $) 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) diff --git a/tests/TransportModuleLoader_test.cc b/tests/ModuleLoader_test.cc similarity index 54% rename from tests/TransportModuleLoader_test.cc rename to tests/ModuleLoader_test.cc index 57e69b1..37f8aee 100644 --- a/tests/TransportModuleLoader_test.cc +++ b/tests/ModuleLoader_test.cc @@ -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 #include @@ -21,29 +21,34 @@ #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 module = loader.GetInstance(AITT_TYPE_TCP); + ModuleLoader::ModuleHandle handle = loader.OpenModule(ModuleLoader::TYPE_TCP); + ASSERT_NE(handle, nullptr); + + std::shared_ptr 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 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); } -- 2.7.4