From 524a9f3928e7defe84484a76feb6a75adfc93d42 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Tue, 3 Dec 2019 22:38:01 +0100 Subject: [PATCH] domo-iot replace libevent by libuv --- CMakeLists.txt | 2 +- src/CMakeLists.txt | 5 +-- src/broker/nats-broker.cpp | 78 ++++++++++++++++++-------------------- src/broker/nats-broker.h | 5 ++- src/server/domo-server.cpp | 11 ++---- src/server/domo-server.h | 6 +-- 6 files changed, 49 insertions(+), 58 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b4df9a0..afaa49d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-modules/") add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../nats.c ${CMAKE_CURRENT_BINARY_DIR}/nats.c) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../json-c ${CMAKE_CURRENT_BINARY_DIR}/json-c) -add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../civetweb ${CMAKE_CURRENT_BINARY_DIR}/civetweb) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../libuv ${CMAKE_CURRENT_BINARY_DIR}/libuv) add_subdirectory (src) add_custom_target (static_analysis diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3dbef6c..c624161 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,11 +25,8 @@ add_executable (domo-iot ${source_files}) target_link_libraries (domo-iot LINK_PUBLIC nats_static - event - pthread - event_pthreads - civetweb-cpp json-c + uv rt ) diff --git a/src/broker/nats-broker.cpp b/src/broker/nats-broker.cpp index 270f037..b24cec4 100644 --- a/src/broker/nats-broker.cpp +++ b/src/broker/nats-broker.cpp @@ -28,7 +28,7 @@ /*------------------------------- INCLUDES ----------------------------------*/ -#include +#include #include "nats-broker.h" @@ -36,7 +36,6 @@ #define kNatsServerURL "nats.nadal-fr.com" - static void onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure) { printf("Received msg: %s - %.*s\n", @@ -71,38 +70,39 @@ NatsBroker::~NatsBroker(void) * * @brief Setup the Broker. */ -int NatsBroker::setup(struct event_base *an_event_loop) +int NatsBroker::setup(uv_loop_t *an_evt_loop) { natsStatus the_status; nats_Open(-1); // One time initialization of things that we need. - natsLibevent_Init(); + natsLibuv_Init(); + + // Libuv is not thread-safe. Almost all calls to libuv need to + // occur from the thread where the loop is running. NATS library + // may have to call into the event loop from different threads. + // This call allows natsLibuv APIs to know if they are executing + // from the event loop thread or not. + natsLibuv_SetThreadLocalLoop(an_evt_loop); if (natsOptions_Create(&m_opts) != NATS_OK) - the_status = NATS_NO_MEMORY; - the_status = natsOptions_UseGlobalMessageDelivery(m_opts, true); + return -1; + + if (natsOptions_UseGlobalMessageDelivery(m_opts, true) != NATS_OK) + return -2; // Indicate which loop and callbacks to use once connected. - if (the_status == NATS_OK) - the_status = natsOptions_SetEventLoop(m_opts, (void *)an_event_loop, - natsLibevent_Attach, - natsLibevent_Read, - natsLibevent_Write, - natsLibevent_Detach); + if (natsOptions_SetEventLoop(m_opts, (void *)an_evt_loop, + natsLibuv_Attach, + natsLibuv_Read, + natsLibuv_Write, + natsLibuv_Detach) != NATS_OK) + return -3; - the_status = natsOptions_SetURL(m_opts, kNatsServerURL); - - if (the_status == NATS_OK) - the_status = natsConnection_Connect(&m_conn, m_opts); - - if (the_status != NATS_OK) - { - nats_PrintLastErrorStack(stderr); - return -1; - } + if (natsOptions_SetURL(m_opts, kNatsServerURL) != NATS_OK) + return -4; return 0; } @@ -110,41 +110,37 @@ int NatsBroker::setup(struct event_base *an_event_loop) /*! ---------------------------------------------------------------------------- * @fn terminate * - * @brief terminate the connection with the Broker server. + * @brief Terminate the Broker. */ int NatsBroker::terminate(void) { - natsSubscription_Destroy(m_sub); - natsConnection_Destroy(m_conn); - natsOptions_Destroy(m_opts); + // Destroy all our objects to avoid report of memory leak + if (m_sub != NULL) + natsSubscription_Destroy(m_sub); + if (m_conn != NULL) + natsConnection_Destroy(m_conn); + if (m_opts != NULL) + natsOptions_Destroy(m_opts); - nats_Close(); return 0; } /*! ---------------------------------------------------------------------------- * @fn connect * - * @brief connect to the Broker server. + * @brief Ccnnect to the Broker server. */ int NatsBroker::connect(void) { - natsStatus the_status; - the_status = natsConnection_Connect(&m_conn, m_opts); + if (natsConnection_Connect(&m_conn, m_opts) != NATS_OK) + return -1; - if (the_status == NATS_OK) - the_status = natsConnection_Subscribe(&m_sub, m_conn, "foo", onMsg, NULL); + if (natsConnection_Subscribe(&m_sub, m_conn, "foo", onMsg, NULL) != NATS_OK) + return -2; // For maximum performance, set no limit on the number of pending messages. - if (the_status == NATS_OK) - the_status = natsSubscription_SetPendingLimits(m_sub, -1, -1); - - // If there was an error, print a stack trace and exit - if (the_status != NATS_OK) - { - nats_PrintLastErrorStack(stderr); - return -1; - } + if (natsSubscription_SetPendingLimits(m_sub, -1, -1)!= NATS_OK) + return -3; return 0; } diff --git a/src/broker/nats-broker.h b/src/broker/nats-broker.h index 7361e31..1f27cb1 100644 --- a/src/broker/nats-broker.h +++ b/src/broker/nats-broker.h @@ -29,6 +29,7 @@ /*------------------------------- INCLUDES ----------------------------------*/ #include +#include /*---------------------------------- Deps -----------------------------------*/ @@ -40,9 +41,9 @@ public: NatsBroker(void); ~NatsBroker(void); - int setup(struct event_base *); + int setup(uv_loop_t *an_evt_loop); int terminate(void); - + int connect(void); private: diff --git a/src/server/domo-server.cpp b/src/server/domo-server.cpp index 555d871..4d35fe4 100644 --- a/src/server/domo-server.cpp +++ b/src/server/domo-server.cpp @@ -28,8 +28,6 @@ /*-------------------------------- INCLUDES ---------------------------------*/ -#include - #include "domo-server.h" #define kDocumentRoot "." @@ -60,7 +58,8 @@ DomoServer::~DomoServer(void) */ bool DomoServer::setup(void) { - m_evt_loop = event_base_new(); + // Create the event loop. + m_evt_loop = uv_default_loop(); if (m_evt_loop == NULL) { return false; @@ -90,8 +89,7 @@ void DomoServer::terminate(void) m_broker.terminate(); if (m_evt_loop != NULL) - event_base_free(m_evt_loop); - libevent_global_shutdown(); + uv_loop_close(m_evt_loop); } /*! ---------------------------------------------------------------------------- @@ -101,6 +99,5 @@ void DomoServer::terminate(void) */ int DomoServer::loop(void) { - event_base_dispatch(m_evt_loop); - return 0; + return uv_run(m_evt_loop, UV_RUN_DEFAULT);; } diff --git a/src/server/domo-server.h b/src/server/domo-server.h index 4aaf727..368b975 100644 --- a/src/server/domo-server.h +++ b/src/server/domo-server.h @@ -28,13 +28,13 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include + #include "broker/nats-broker.h" #include "web/web-server.h" /*---------------------------------- Deps -----------------------------------*/ -struct event_base; - /*--------------------------------- CLASS ----------------------------------*/ class DomoServer @@ -51,7 +51,7 @@ public: private: NatsBroker m_broker; WebServer m_server; - struct event_base *m_evt_loop; + uv_loop_t *m_evt_loop; }; #endif /* _DOMO_SERVER_H */ \ No newline at end of file