domo-iot replace libevent by libuv

This commit is contained in:
2019-12-03 22:38:01 +01:00
parent e5bd19f12d
commit 524a9f3928
6 changed files with 49 additions and 58 deletions

View File

@@ -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}/../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}/../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_subdirectory (src)
add_custom_target (static_analysis add_custom_target (static_analysis

View File

@@ -25,11 +25,8 @@ add_executable (domo-iot ${source_files})
target_link_libraries (domo-iot target_link_libraries (domo-iot
LINK_PUBLIC LINK_PUBLIC
nats_static nats_static
event
pthread
event_pthreads
civetweb-cpp
json-c json-c
uv
rt rt
) )

View File

@@ -28,7 +28,7 @@
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <adapters/libevent.h> #include <adapters/libuv.h>
#include "nats-broker.h" #include "nats-broker.h"
@@ -36,7 +36,6 @@
#define kNatsServerURL "nats.nadal-fr.com" #define kNatsServerURL "nats.nadal-fr.com"
static void onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure) static void onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure)
{ {
printf("Received msg: %s - %.*s\n", printf("Received msg: %s - %.*s\n",
@@ -71,38 +70,39 @@ NatsBroker::~NatsBroker(void)
* *
* @brief Setup the Broker. * @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; natsStatus the_status;
nats_Open(-1); nats_Open(-1);
// One time initialization of things that we need. // 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) 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. // Indicate which loop and callbacks to use once connected.
if (the_status == NATS_OK) if (natsOptions_SetEventLoop(m_opts, (void *)an_evt_loop,
the_status = natsOptions_SetEventLoop(m_opts, (void *)an_event_loop, natsLibuv_Attach,
natsLibevent_Attach, natsLibuv_Read,
natsLibevent_Read, natsLibuv_Write,
natsLibevent_Write, natsLibuv_Detach) != NATS_OK)
natsLibevent_Detach); return -3;
the_status = natsOptions_SetURL(m_opts, kNatsServerURL); if (natsOptions_SetURL(m_opts, kNatsServerURL) != NATS_OK)
return -4;
if (the_status == NATS_OK)
the_status = natsConnection_Connect(&m_conn, m_opts);
if (the_status != NATS_OK)
{
nats_PrintLastErrorStack(stderr);
return -1;
}
return 0; return 0;
} }
@@ -110,41 +110,37 @@ int NatsBroker::setup(struct event_base *an_event_loop)
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn terminate * @fn terminate
* *
* @brief terminate the connection with the Broker server. * @brief Terminate the Broker.
*/ */
int NatsBroker::terminate(void) int NatsBroker::terminate(void)
{ {
natsSubscription_Destroy(m_sub); // Destroy all our objects to avoid report of memory leak
natsConnection_Destroy(m_conn); if (m_sub != NULL)
natsOptions_Destroy(m_opts); natsSubscription_Destroy(m_sub);
if (m_conn != NULL)
natsConnection_Destroy(m_conn);
if (m_opts != NULL)
natsOptions_Destroy(m_opts);
nats_Close();
return 0; return 0;
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
* @fn connect * @fn connect
* *
* @brief connect to the Broker server. * @brief Ccnnect to the Broker server.
*/ */
int NatsBroker::connect(void) int NatsBroker::connect(void)
{ {
natsStatus the_status; if (natsConnection_Connect(&m_conn, m_opts) != NATS_OK)
the_status = natsConnection_Connect(&m_conn, m_opts); return -1;
if (the_status == NATS_OK) if (natsConnection_Subscribe(&m_sub, m_conn, "foo", onMsg, NULL) != NATS_OK)
the_status = natsConnection_Subscribe(&m_sub, m_conn, "foo", onMsg, NULL); return -2;
// For maximum performance, set no limit on the number of pending messages. // For maximum performance, set no limit on the number of pending messages.
if (the_status == NATS_OK) if (natsSubscription_SetPendingLimits(m_sub, -1, -1)!= NATS_OK)
the_status = natsSubscription_SetPendingLimits(m_sub, -1, -1); return -3;
// If there was an error, print a stack trace and exit
if (the_status != NATS_OK)
{
nats_PrintLastErrorStack(stderr);
return -1;
}
return 0; return 0;
} }

View File

@@ -29,6 +29,7 @@
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <nats.h> #include <nats.h>
#include <uv.h>
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
@@ -40,9 +41,9 @@ public:
NatsBroker(void); NatsBroker(void);
~NatsBroker(void); ~NatsBroker(void);
int setup(struct event_base *); int setup(uv_loop_t *an_evt_loop);
int terminate(void); int terminate(void);
int connect(void); int connect(void);
private: private:

View File

@@ -28,8 +28,6 @@
/*-------------------------------- INCLUDES ---------------------------------*/ /*-------------------------------- INCLUDES ---------------------------------*/
#include <event.h>
#include "domo-server.h" #include "domo-server.h"
#define kDocumentRoot "." #define kDocumentRoot "."
@@ -60,7 +58,8 @@ DomoServer::~DomoServer(void)
*/ */
bool DomoServer::setup(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) if (m_evt_loop == NULL)
{ {
return false; return false;
@@ -90,8 +89,7 @@ void DomoServer::terminate(void)
m_broker.terminate(); m_broker.terminate();
if (m_evt_loop != NULL) if (m_evt_loop != NULL)
event_base_free(m_evt_loop); uv_loop_close(m_evt_loop);
libevent_global_shutdown();
} }
/*! ---------------------------------------------------------------------------- /*! ----------------------------------------------------------------------------
@@ -101,6 +99,5 @@ void DomoServer::terminate(void)
*/ */
int DomoServer::loop(void) int DomoServer::loop(void)
{ {
event_base_dispatch(m_evt_loop); return uv_run(m_evt_loop, UV_RUN_DEFAULT);;
return 0;
} }

View File

@@ -28,13 +28,13 @@
/*------------------------------- INCLUDES ----------------------------------*/ /*------------------------------- INCLUDES ----------------------------------*/
#include <uv.h>
#include "broker/nats-broker.h" #include "broker/nats-broker.h"
#include "web/web-server.h" #include "web/web-server.h"
/*---------------------------------- Deps -----------------------------------*/ /*---------------------------------- Deps -----------------------------------*/
struct event_base;
/*--------------------------------- CLASS ----------------------------------*/ /*--------------------------------- CLASS ----------------------------------*/
class DomoServer class DomoServer
@@ -51,7 +51,7 @@ public:
private: private:
NatsBroker m_broker; NatsBroker m_broker;
WebServer m_server; WebServer m_server;
struct event_base *m_evt_loop; uv_loop_t *m_evt_loop;
}; };
#endif /* _DOMO_SERVER_H */ #endif /* _DOMO_SERVER_H */