From: Youngjae Shin Date: Thu, 29 Sep 2022 08:25:26 +0000 (+0900) Subject: remove mosquitto dependency in common library X-Git-Tag: accepted/tizen/unified/20221115.172906~23 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2a1d1ab9ac14b323c1f50ad507094232f6057233;p=platform%2Fcore%2Fml%2Faitt.git remove mosquitto dependency in common library --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d19aff..3ee0a7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,11 +53,11 @@ INCLUDE_DIRECTORIES(include common) AUX_SOURCE_DIRECTORY(src AITT_SRC) -SET(MODULE_MAN_SRC src/ModuleManager.cc src/NullTransport.cc) -ADD_LIBRARY(MODULE_MANAGER OBJECT ${MODULE_MAN_SRC}) -list(REMOVE_ITEM AITT_SRC ${MODULE_MAN_SRC}) +SET(AITT_INTERNAL_SRC src/ModuleManager.cc src/NullTransport.cc src/MosquittoMQ.cc) +ADD_LIBRARY(INTERNAL_OBJ OBJECT ${AITT_INTERNAL_SRC}) +list(REMOVE_ITEM AITT_SRC ${AITT_INTERNAL_SRC}) -ADD_LIBRARY(${PROJECT_NAME} SHARED ${AITT_SRC} $) +ADD_LIBRARY(${PROJECT_NAME} SHARED ${AITT_SRC} $) TARGET_LINK_LIBRARIES(${PROJECT_NAME} Threads::Threads ${CMAKE_DL_LIBS} ${AITT_COMMON}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${AITT_NEEDS_LIBRARIES}) diff --git a/common/AittDiscovery.cc b/common/AittDiscovery.cc index feb9569..4c6ed42 100644 --- a/common/AittDiscovery.cc +++ b/common/AittDiscovery.cc @@ -84,6 +84,11 @@ void AittDiscovery::RemoveDiscoveryCB(int callback_id) callbacks.erase(it); } +bool AittDiscovery::CompareTopic(const std::string &left, const std::string &right) +{ + return discovery_mq->CompareTopic(left, right); +} + void AittDiscovery::DiscoveryMessageCallback(MSG *mq, const std::string &topic, const void *msg, const int szmsg, void *user_data) { diff --git a/common/AittDiscovery.h b/common/AittDiscovery.h index d26892e..6725e6f 100644 --- a/common/AittDiscovery.h +++ b/common/AittDiscovery.h @@ -39,6 +39,7 @@ class AittDiscovery { void UpdateDiscoveryMsg(AittProtocol protocol, const void *msg, size_t length); int AddDiscoveryCB(AittProtocol protocol, const DiscoveryCallback &cb); void RemoveDiscoveryCB(int callback_id); + bool CompareTopic(const std::string &left, const std::string &right); private: struct DiscoveryBlob { diff --git a/common/AittUtil.cc b/common/AittUtil.cc deleted file mode 100644 index c26dd47..0000000 --- a/common/AittUtil.cc +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 "AittUtil.h" - -#include - -#include "AittException.h" -#include "aitt_internal.h" - -bool aitt::AittUtil::CompareTopic(const std::string& left, const std::string& right) -{ - bool result = false; - int ret = mosquitto_topic_matches_sub(left.c_str(), right.c_str(), &result); - if (ret != MOSQ_ERR_SUCCESS) { - ERR("mosquitto_topic_matches_sub(%s, %s) Fail(%s)", left.c_str(), right.c_str(), - mosquitto_strerror(ret)); - throw AittException(AittException::MQTT_ERR); - } - return result; -} diff --git a/include/AittUtil.h b/include/AittUtil.h deleted file mode 100644 index 4859fd6..0000000 --- a/include/AittUtil.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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 - -namespace aitt { - -class AittUtil { - public: - AittUtil() = default; - ~AittUtil() = default; - - static bool CompareTopic(const std::string &left, const std::string &right); -}; - -} // namespace aitt diff --git a/include/MQ.h b/include/MQ.h index 4c4f752..79d22ae 100644 --- a/include/MQ.h +++ b/include/MQ.h @@ -55,6 +55,7 @@ class MQ { virtual void *Subscribe(const std::string &topic, const SubscribeCallback &cb, void *user_data = nullptr, int qos = 0) = 0; virtual void *Unsubscribe(void *handle) = 0; + virtual bool CompareTopic(const std::string &left, const std::string &right) = 0; }; } // namespace aitt diff --git a/modules/tcp/Module.cc b/modules/tcp/Module.cc index 23faae0..81717c5 100644 --- a/modules/tcp/Module.cc +++ b/modules/tcp/Module.cc @@ -15,7 +15,6 @@ */ #include "Module.h" -#include #include #include @@ -80,7 +79,7 @@ void Module::Publish(const std::string &topic, const void *data, const size_t da std::lock_guard auto_lock_publish(publishTableLock); for (PublishMap::iterator it = publishTable.begin(); it != publishTable.end(); ++it) { // NOTE: Find entries that have matched with the given topic - if (!aitt::AittUtil::CompareTopic(it->first, topic)) + if (!discovery.CompareTopic(it->first, topic)) continue; for (HostMap::iterator hostIt = it->second.begin(); hostIt != it->second.end(); ++hostIt) { diff --git a/modules/webrtc/CMakeLists.txt b/modules/webrtc/CMakeLists.txt index c8c3bed..bd4b279 100644 --- a/modules/webrtc/CMakeLists.txt +++ b/modules/webrtc/CMakeLists.txt @@ -1,6 +1,7 @@ SET(AITT_WEBRTC aitt-transport-webrtc) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src) PKG_CHECK_MODULES(AITT_WEBRTC_NEEDS REQUIRED capi-media-camera @@ -10,7 +11,7 @@ PKG_CHECK_MODULES(AITT_WEBRTC_NEEDS REQUIRED INCLUDE_DIRECTORIES(${AITT_WEBRTC_NEEDS_INCLUDE_DIRS}) LINK_DIRECTORIES(${AITT_WEBRTC_NEEDS_LIBRARY_DIRS}) -FILE(GLOB AITT_WEBRTC_SRC *.cc) +FILE(GLOB AITT_WEBRTC_SRC *.cc ${CMAKE_SOURCE_DIR}/src/MosquittoMQ.cc) list(REMOVE_ITEM AITT_WEBRTC_SRC ${CMAKE_CURRENT_SOURCE_DIR}/Module.cc) ADD_LIBRARY(WEBRTC_OBJ OBJECT ${AITT_WEBRTC_SRC}) ADD_LIBRARY(${AITT_WEBRTC} SHARED ../transport_entry.cc Module.cc $) diff --git a/common/MosquittoMQ.cc b/src/MosquittoMQ.cc similarity index 91% rename from common/MosquittoMQ.cc rename to src/MosquittoMQ.cc index c2c87e9..4950e96 100644 --- a/common/MosquittoMQ.cc +++ b/src/MosquittoMQ.cc @@ -26,7 +26,6 @@ #include "AittException.h" #include "AittTypes.h" -#include "AittUtil.h" #include "aitt_internal.h" namespace aitt { @@ -179,30 +178,33 @@ void MosquittoMQ::MessageCallback(mosquitto *handle, void *obj, const mosquitto_ RET_IF(obj == nullptr); MosquittoMQ *mq = static_cast(obj); - std::lock_guard auto_lock(mq->callback_lock); - mq->subscribers_iterating = true; - mq->subscriber_iterator = mq->subscribers.begin(); - while (mq->subscriber_iterator != mq->subscribers.end()) { - auto subscribe_data = *(mq->subscriber_iterator); + mq->MessageCB(msg, props); +} + +void MosquittoMQ::MessageCB(const mosquitto_message *msg, const mosquitto_property *props) +{ + std::lock_guard auto_lock(callback_lock); + subscribers_iterating = true; + subscriber_iterator = subscribers.begin(); + while (subscriber_iterator != subscribers.end()) { + auto subscribe_data = *(subscriber_iterator); if (nullptr == subscribe_data) { ERR("Invalid subscribe data"); - mq->subscriber_iterator++; + ++subscriber_iterator; continue; } - bool result = AittUtil::CompareTopic(subscribe_data->topic.c_str(), msg->topic); - if (result) - mq->InvokeCallback(*mq->subscriber_iterator, msg, props); + if (CompareTopic(subscribe_data->topic.c_str(), msg->topic)) + InvokeCallback(*subscriber_iterator, msg, props); - if (!mq->subscriber_iterator_updated) - mq->subscriber_iterator++; + if (!subscriber_iterator_updated) + ++subscriber_iterator; else - mq->subscriber_iterator_updated = false; + subscriber_iterator_updated = false; } - mq->subscribers_iterating = false; - mq->subscribers.insert(mq->subscribers.end(), mq->new_subscribers.begin(), - mq->new_subscribers.end()); - mq->new_subscribers.clear(); + subscribers_iterating = false; + subscribers.insert(subscribers.end(), new_subscribers.begin(), new_subscribers.end()); + new_subscribers.clear(); } void MosquittoMQ::InvokeCallback(SubscribeData *subscriber, const mosquitto_message *msg, @@ -386,6 +388,18 @@ void *MosquittoMQ::Unsubscribe(void *sub_handle) return user_data; } +bool MosquittoMQ::CompareTopic(const std::string &left, const std::string &right) +{ + bool result = false; + int ret = mosquitto_topic_matches_sub(left.c_str(), right.c_str(), &result); + if (ret != MOSQ_ERR_SUCCESS) { + ERR("mosquitto_topic_matches_sub(%s, %s) Fail(%s)", left.c_str(), right.c_str(), + mosquitto_strerror(ret)); + return false; + } + return result; +} + MosquittoMQ::SubscribeData::SubscribeData(const std::string &in_topic, const SubscribeCallback &in_cb, void *in_user_data) : topic(in_topic), cb(in_cb), user_data(in_user_data) diff --git a/common/MosquittoMQ.h b/src/MosquittoMQ.h similarity index 95% rename from common/MosquittoMQ.h rename to src/MosquittoMQ.h index e045b9c..f900b8f 100644 --- a/common/MosquittoMQ.h +++ b/src/MosquittoMQ.h @@ -48,6 +48,7 @@ class MosquittoMQ : public MQ { void *Subscribe(const std::string &topic, const SubscribeCallback &cb, void *user_data = nullptr, int qos = 0); void *Unsubscribe(void *handle); + bool CompareTopic(const std::string &left, const std::string &right); private: struct SubscribeData { @@ -63,6 +64,7 @@ class MosquittoMQ : public MQ { const mosquitto_property *props); static void MessageCallback(mosquitto *, void *, const mosquitto_message *, const mosquitto_property *); + void MessageCB(const mosquitto_message *msg, const mosquitto_property *props); void InvokeCallback(SubscribeData *subscriber, const mosquitto_message *msg, const mosquitto_property *props); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 436bfaa..1febe7d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,11 +3,11 @@ SET(AITT_UT ${PROJECT_NAME}_ut) ADD_DEFINITIONS(-DLOG_STDOUT) PKG_CHECK_MODULES(UT_NEEDS REQUIRED gmock_main) -INCLUDE_DIRECTORIES(${UT_NEEDS_INCLUDE_DIRS}) +INCLUDE_DIRECTORIES(${UT_NEEDS_INCLUDE_DIRS} ../src) LINK_DIRECTORIES(${UT_NEEDS_LIBRARY_DIRS}) ########################################################################### -SET(AITT_UT_SRC AITT_test.cc RequestResponse_test.cc MainLoopHandler_test.cc aitt_c_test.cc AITT_TCP_test.cc MosquittoMQ_test.cc) +SET(AITT_UT_SRC AITT_test.cc RequestResponse_test.cc MainLoopHandler_test.cc aitt_c_test.cc AITT_TCP_test.cc) ADD_EXECUTABLE(${AITT_UT} ${AITT_UT_SRC}) TARGET_LINK_LIBRARIES(${AITT_UT} Threads::Threads ${UT_NEEDS_LIBRARIES} ${PROJECT_NAME}) @@ -31,8 +31,8 @@ INSTALL(TARGETS ${AITT_UT}_manual DESTINATION ${AITT_TEST_BINDIR}) ########################################################################### AUX_SOURCE_DIRECTORY(../mock MOCK_SRC) -ADD_EXECUTABLE(${AITT_UT}_mq MosquittoMQ_mocktest.cc ${MOCK_SRC}) -TARGET_LINK_LIBRARIES(${AITT_UT}_mq ${UT_NEEDS_LIBRARIES} Threads::Threads ${AITT_COMMON}) +ADD_EXECUTABLE(${AITT_UT}_mq MosquittoMQ_mocktest.cc ${MOCK_SRC} $) +TARGET_LINK_LIBRARIES(${AITT_UT}_mq ${UT_NEEDS_LIBRARIES} Threads::Threads ${CMAKE_DL_LIBS} ${AITT_COMMON}) TARGET_INCLUDE_DIRECTORIES(${AITT_UT}_mq PRIVATE ../mock) INSTALL(TARGETS ${AITT_UT}_mq DESTINATION ${AITT_TEST_BINDIR}) @@ -46,19 +46,18 @@ ADD_TEST( ) ########################################################################### -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) +ADD_EXECUTABLE(${AITT_UT}_local ModuleLoader_test.cc MosquittoMQ_test.cc $) +TARGET_LINK_LIBRARIES(${AITT_UT}_local ${UT_NEEDS_LIBRARIES} ${AITT_NEEDS_LIBRARIES} ${CMAKE_DL_LIBS} ${AITT_COMMON}) -INSTALL(TARGETS ${AITT_UT}_module DESTINATION ${AITT_TEST_BINDIR}) +INSTALL(TARGETS ${AITT_UT}_local DESTINATION ${AITT_TEST_BINDIR}) ADD_TEST( NAME - ${AITT_UT}_module + ${AITT_UT}_local COMMAND ${CMAKE_COMMAND} -E env - LD_LIBRARY_PATH=../modules/webrtc/:../modules/tcp/:../:../common/:$ENV{LD_LIBRARY_PATH} - ${CMAKE_CURRENT_BINARY_DIR}/${AITT_UT}_module --gtest_filter=*_Anytime + LD_LIBRARY_PATH=../modules/tcp/:../modules/webrtc/:../:../common/:$ENV{LD_LIBRARY_PATH} + ${CMAKE_CURRENT_BINARY_DIR}/${AITT_UT}_local --gtest_filter=*_Anytime ) ########################################################################### ADD_EXECUTABLE(${AITT_UT}_stream AITT_Stream_test.cc)