remove mosquitto dependency in common library
authorYoungjae Shin <yj99.shin@samsung.com>
Thu, 29 Sep 2022 08:25:26 +0000 (17:25 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Wed, 9 Nov 2022 08:14:24 +0000 (17:14 +0900)
CMakeLists.txt
common/AittDiscovery.cc
common/AittDiscovery.h
common/AittUtil.cc [deleted file]
include/AittUtil.h [deleted file]
include/MQ.h
modules/tcp/Module.cc
modules/webrtc/CMakeLists.txt
src/MosquittoMQ.cc [moved from common/MosquittoMQ.cc with 91% similarity]
src/MosquittoMQ.h [moved from common/MosquittoMQ.h with 95% similarity]
tests/CMakeLists.txt

index 5d19aff..3ee0a7c 100644 (file)
@@ -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} $<TARGET_OBJECTS:MODULE_MANAGER>)
+ADD_LIBRARY(${PROJECT_NAME} SHARED ${AITT_SRC} $<TARGET_OBJECTS:INTERNAL_OBJ>)
 TARGET_LINK_LIBRARIES(${PROJECT_NAME} Threads::Threads ${CMAKE_DL_LIBS} ${AITT_COMMON})
 TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${AITT_NEEDS_LIBRARIES})
 
index feb9569..4c6ed42 100644 (file)
@@ -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)
 {
index d26892e..6725e6f 100644 (file)
@@ -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 (file)
index c26dd47..0000000
+++ /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 <mosquitto.h>
-
-#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 (file)
index 4859fd6..0000000
+++ /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 <string>
-
-namespace aitt {
-
-class AittUtil {
-  public:
-    AittUtil() = default;
-    ~AittUtil() = default;
-
-    static bool CompareTopic(const std::string &left, const std::string &right);
-};
-
-}  // namespace aitt
index 4c4f752..79d22ae 100644 (file)
@@ -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
index 23faae0..81717c5 100644 (file)
@@ -15,7 +15,6 @@
  */
 #include "Module.h"
 
-#include <AittUtil.h>
 #include <flatbuffers/flexbuffers.h>
 #include <unistd.h>
 
@@ -80,7 +79,7 @@ void Module::Publish(const std::string &topic, const void *data, const size_t da
     std::lock_guard<std::mutex> 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) {
index c8c3bed..bd4b279 100644 (file)
@@ -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 $<TARGET_OBJECTS:WEBRTC_OBJ>)
similarity index 91%
rename from common/MosquittoMQ.cc
rename to src/MosquittoMQ.cc
index c2c87e9..4950e96 100644 (file)
@@ -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<MosquittoMQ *>(obj);
 
-    std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> 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)
similarity index 95%
rename from common/MosquittoMQ.h
rename to src/MosquittoMQ.h
index e045b9c..f900b8f 100644 (file)
@@ -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);
 
index 436bfaa..1febe7d 100644 (file)
@@ -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_OBJECTS:INTERNAL_OBJ>)
+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_OBJECTS:MODULE_MANAGER>)
-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_OBJECTS:INTERNAL_OBJ>)
+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)