update domo
This commit is contained in:
@@ -17,6 +17,7 @@ file(
|
||||
server/domo-server.cpp
|
||||
broker/nats-broker.cpp
|
||||
web/web-server.cpp
|
||||
web/handler/exit-handler.cpp
|
||||
)
|
||||
|
||||
add_executable (domo-iot ${source_files})
|
||||
|
||||
@@ -23,6 +23,9 @@
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <adapters/libevent.h>
|
||||
@@ -31,6 +34,20 @@
|
||||
|
||||
#include <nats.h>
|
||||
|
||||
#define kNatsServerURL "nats.nadal-fr.com"
|
||||
|
||||
|
||||
static void onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure)
|
||||
{
|
||||
printf("Received msg: %s - %.*s\n",
|
||||
natsMsg_GetSubject(msg),
|
||||
natsMsg_GetDataLength(msg),
|
||||
natsMsg_GetData(msg));
|
||||
|
||||
// Need to destroy the message!
|
||||
natsMsg_Destroy(msg);
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn NatsBroker
|
||||
*
|
||||
@@ -54,7 +71,7 @@ NatsBroker::~NatsBroker(void)
|
||||
*
|
||||
* @brief Setup the Broker.
|
||||
*/
|
||||
int NatsBroker::setup(void)
|
||||
int NatsBroker::setup(struct event_base *an_event_loop)
|
||||
{
|
||||
natsStatus the_status;
|
||||
|
||||
@@ -64,7 +81,70 @@ int NatsBroker::setup(void)
|
||||
natsLibevent_Init();
|
||||
|
||||
if (natsOptions_Create(&m_opts) != NATS_OK)
|
||||
the_status = NATS_NO_MEMORY;
|
||||
|
||||
the_status = natsOptions_UseGlobalMessageDelivery(m_opts, true);
|
||||
|
||||
// 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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn terminate
|
||||
*
|
||||
* @brief terminate the connection with the Broker server.
|
||||
*/
|
||||
int NatsBroker::terminate(void)
|
||||
{
|
||||
natsSubscription_Destroy(m_sub);
|
||||
natsConnection_Destroy(m_conn);
|
||||
natsOptions_Destroy(m_opts);
|
||||
|
||||
nats_Close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn connect
|
||||
*
|
||||
* @brief connect to the Broker server.
|
||||
*/
|
||||
int NatsBroker::connect(void)
|
||||
{
|
||||
natsStatus the_status;
|
||||
the_status = natsConnection_Connect(&m_conn, m_opts);
|
||||
|
||||
if (the_status == NATS_OK)
|
||||
the_status = natsConnection_Subscribe(&m_sub, m_conn, "foo", onMsg, NULL);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,10 @@ public:
|
||||
NatsBroker(void);
|
||||
~NatsBroker(void);
|
||||
|
||||
int setup(void);
|
||||
int setup(struct event_base *);
|
||||
int terminate(void);
|
||||
|
||||
int connect(void);
|
||||
|
||||
private:
|
||||
natsConnection *m_conn;
|
||||
|
||||
@@ -23,6 +23,9 @@
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
/*-------------------------------- INCLUDES ---------------------------------*/
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
@@ -23,12 +23,18 @@
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
/*-------------------------------- INCLUDES ---------------------------------*/
|
||||
|
||||
#include <event.h>
|
||||
|
||||
#include "domo-server.h"
|
||||
|
||||
#define kDocumentRoot "."
|
||||
#define kPort "8081"
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn DomoServer
|
||||
*
|
||||
@@ -54,18 +60,23 @@ DomoServer::~DomoServer(void)
|
||||
*/
|
||||
bool DomoServer::setup(void)
|
||||
{
|
||||
|
||||
if (m_broker.setup())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_evt_loop = event_base_new();
|
||||
if (m_evt_loop == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_broker.setup(m_evt_loop))
|
||||
{
|
||||
fprintf(stderr, "Failed to setup the Nats Broker.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_server.setup(kDocumentRoot, kPort, m_evt_loop);
|
||||
|
||||
// TODO
|
||||
m_broker.connect();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -76,6 +87,11 @@ bool DomoServer::setup(void)
|
||||
*/
|
||||
void DomoServer::terminate(void)
|
||||
{
|
||||
m_broker.terminate();
|
||||
|
||||
if (m_evt_loop != NULL)
|
||||
event_base_free(m_evt_loop);
|
||||
libevent_global_shutdown();
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
@@ -85,5 +101,6 @@ void DomoServer::terminate(void)
|
||||
*/
|
||||
int DomoServer::loop(void)
|
||||
{
|
||||
event_base_dispatch(m_evt_loop);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
/*!
|
||||
* domi-iot.cpp
|
||||
*
|
||||
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 08/11/2019
|
||||
*
|
||||
*/
|
||||
|
||||
/*-------------------------------- INCLUDES ---------------------------------*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <adapters/libevent.h>
|
||||
|
||||
#include <nats.h>
|
||||
|
||||
static void onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure)
|
||||
{
|
||||
printf("Received msg: %s - %.*s\n",
|
||||
natsMsg_GetSubject(msg),
|
||||
natsMsg_GetDataLength(msg),
|
||||
natsMsg_GetData(msg));
|
||||
|
||||
// Need to destroy the message!
|
||||
natsMsg_Destroy(msg);
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn main
|
||||
*
|
||||
* @brief Main function of domo-iot daemon.
|
||||
*/
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// natsConnection *conn = NULL;
|
||||
// natsOptions *opts = NULL;
|
||||
// natsSubscription *sub = NULL;
|
||||
// natsStatus s;
|
||||
// struct event_base *evLoop = NULL;
|
||||
// nats_Open(-1);
|
||||
|
||||
printf("Listening on subject 'foo'\n");
|
||||
|
||||
// One time initialization of things that we need.
|
||||
//natsLibevent_Init();
|
||||
|
||||
// Create a loop.
|
||||
// evLoop = event_base_new();
|
||||
// if (evLoop == NULL)
|
||||
// s = NATS_ERR;
|
||||
|
||||
// if (natsOptions_Create(&opts) != NATS_OK)
|
||||
// s = NATS_NO_MEMORY;
|
||||
|
||||
s = natsOptions_UseGlobalMessageDelivery(opts, true);
|
||||
|
||||
// Indicate which loop and callbacks to use once connected.
|
||||
if (s == NATS_OK)
|
||||
s = natsOptions_SetEventLoop(opts, (void *)evLoop,
|
||||
natsLibevent_Attach,
|
||||
natsLibevent_Read,
|
||||
natsLibevent_Write,
|
||||
natsLibevent_Detach);
|
||||
|
||||
s = natsOptions_SetURL(opts, "nats.nadal-fr.com");
|
||||
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Connect(&conn, opts);
|
||||
|
||||
if (s == NATS_OK)
|
||||
s = natsConnection_Subscribe(&sub, conn, "foo", onMsg, NULL);
|
||||
|
||||
// For maximum performance, set no limit on the number of pending messages.
|
||||
if (s == NATS_OK)
|
||||
s = natsSubscription_SetPendingLimits(sub, -1, -1);
|
||||
|
||||
// Run the event loop.
|
||||
// This call will return when the connection is closed (either after
|
||||
// receiving all messages, or disconnected and unable to reconnect).
|
||||
if (s == NATS_OK)
|
||||
{
|
||||
event_base_dispatch(evLoop);
|
||||
}
|
||||
|
||||
// If there was an error, print a stack trace and exit
|
||||
if (s != NATS_OK)
|
||||
{
|
||||
nats_PrintLastErrorStack(stderr);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
// Anything that is created need to be destroyed
|
||||
natsSubscription_Destroy(sub);
|
||||
natsConnection_Destroy(conn);
|
||||
natsOptions_Destroy(opts);
|
||||
|
||||
if (evLoop != NULL)
|
||||
event_base_free(evLoop);
|
||||
|
||||
// To silence reports of memory still in used with valgrind
|
||||
nats_Close();
|
||||
libevent_global_shutdown();
|
||||
|
||||
printf("done\n");
|
||||
return 0;
|
||||
}
|
||||
57
src/web/handler/exit-handler.cpp
Normal file
57
src/web/handler/exit-handler.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
/*!
|
||||
* web-server.cpp
|
||||
*
|
||||
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 14/11/2019
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <event2/event.h>
|
||||
|
||||
#include "exit-handler.h"
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn ExitHandler
|
||||
*
|
||||
* @brief Constructor of the Exit Handler
|
||||
*/
|
||||
ExitHandler::ExitHandler(struct event_base *an_evt_loop) : m_evt_loop(an_evt_loop)
|
||||
{
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn handleGet
|
||||
*
|
||||
* @brief Exit Handler HandleGet method
|
||||
*/
|
||||
bool ExitHandler::handleGet(CivetServer *a_server, struct mg_connection *a_conn)
|
||||
{
|
||||
mg_printf(a_conn,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: "
|
||||
"text/plain\r\nConnection: close\r\n\r\n");
|
||||
mg_printf(a_conn, "Bye!\n");
|
||||
event_base_loopbreak(m_evt_loop);
|
||||
return true;
|
||||
}
|
||||
47
src/web/handler/exit-handler.h
Normal file
47
src/web/handler/exit-handler.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*!
|
||||
* web-server.h
|
||||
*
|
||||
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 14/11/2019
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _EXIT_HANDLER_H
|
||||
#define _EXIT_HANDLER_H
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include "CivetServer.h"
|
||||
|
||||
/*---------------------------------- Deps -----------------------------------*/
|
||||
|
||||
/*--------------------------------- CLASS ----------------------------------*/
|
||||
|
||||
class ExitHandler : public CivetHandler
|
||||
{
|
||||
public:
|
||||
ExitHandler(struct event_base *an_evt_loop);
|
||||
bool handleGet(CivetServer *a_server, struct mg_connection *a_conn);
|
||||
|
||||
private:
|
||||
struct event_base *m_evt_loop;
|
||||
};
|
||||
|
||||
#endif /* _EXIT_HANDLER_H */
|
||||
@@ -23,17 +23,25 @@
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include "CivetServer.h"
|
||||
|
||||
#include "web/handler/exit-handler.h"
|
||||
|
||||
#include "web-server.h"
|
||||
|
||||
#define EXIT_URI "/exit"
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn WebServer
|
||||
*
|
||||
* @brief Constructor of the Web Server Object.
|
||||
*/
|
||||
|
||||
WebServer::WebServer(void)
|
||||
WebServer::WebServer(void) : m_server(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -45,4 +53,26 @@ WebServer::WebServer(void)
|
||||
|
||||
WebServer::~WebServer(void)
|
||||
{
|
||||
delete m_server;
|
||||
}
|
||||
|
||||
/*! ----------------------------------------------------------------------------
|
||||
* @fn setup
|
||||
*
|
||||
* @brief Setup the Web server
|
||||
*/
|
||||
int WebServer::setup(const char *a_document_root, const char *a_port, struct event_base *an_evt_loop)
|
||||
{
|
||||
std::vector<std::string> the_options;
|
||||
|
||||
the_options.push_back("document_root");
|
||||
the_options.push_back(a_document_root);
|
||||
the_options.push_back("listening_ports");
|
||||
the_options.push_back(a_port);
|
||||
|
||||
m_server = new CivetServer(the_options);
|
||||
|
||||
m_server->addHandler(EXIT_URI, new ExitHandler(an_evt_loop));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -28,8 +28,13 @@
|
||||
|
||||
/*------------------------------- INCLUDES ----------------------------------*/
|
||||
|
||||
#include <cstring>
|
||||
|
||||
/*---------------------------------- Deps -----------------------------------*/
|
||||
|
||||
class CivetServer;
|
||||
struct event_base;
|
||||
|
||||
/*--------------------------------- CLASS ----------------------------------*/
|
||||
|
||||
class WebServer
|
||||
@@ -38,7 +43,10 @@ public:
|
||||
WebServer(void);
|
||||
~WebServer(void);
|
||||
|
||||
int setup(const char *a_document_root, const char *a_port, struct event_base *an_evt_loop);
|
||||
|
||||
private:
|
||||
CivetServer *m_server;
|
||||
};
|
||||
|
||||
#endif /* __WEB_SERVER_H */
|
||||
|
||||
Reference in New Issue
Block a user