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}/../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

View File

@@ -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
)

View File

@@ -28,7 +28,7 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <adapters/libevent.h>
#include <adapters/libuv.h>
#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;
}

View File

@@ -29,6 +29,7 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <nats.h>
#include <uv.h>
/*---------------------------------- 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:

View File

@@ -28,8 +28,6 @@
/*-------------------------------- INCLUDES ---------------------------------*/
#include <event.h>
#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);;
}

View File

@@ -28,13 +28,13 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <uv.h>
#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 */