From ab4a18f685068b753f41b3dd7733b1bca2baf9a0 Mon Sep 17 00:00:00 2001 From: NADAL Jean-Baptiste Date: Wed, 13 Nov 2019 23:08:13 +0100 Subject: [PATCH] skeleton wip --- CMakeLists.txt | 1 + src/CMakeLists.txt | 7 ++- src/broker/nats-broker.cpp | 70 ++++++++++++++++++++++++++ src/broker/nats-broker.h | 51 +++++++++++++++++++ src/main.cpp | 52 +++++++++++++++++++ src/server/domo-server.cpp | 89 +++++++++++++++++++++++++++++++++ src/server/domo-server.h | 57 +++++++++++++++++++++ src/{ => tempo}/domo-iot.cpp | 0 src/{ => tempo}/domo-iot.nats.c | 25 ++++----- src/web/web-server.cpp | 48 ++++++++++++++++++ src/web/web-server.h | 44 ++++++++++++++++ 11 files changed, 431 insertions(+), 13 deletions(-) create mode 100644 src/broker/nats-broker.cpp create mode 100644 src/broker/nats-broker.h create mode 100644 src/main.cpp create mode 100644 src/server/domo-server.cpp create mode 100644 src/server/domo-server.h rename src/{ => tempo}/domo-iot.cpp (100%) rename src/{ => tempo}/domo-iot.nats.c (89%) create mode 100644 src/web/web-server.cpp create mode 100644 src/web/web-server.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dacaa40..6a5609e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,5 +10,6 @@ set(LIBRARY_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/build/lib") 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 (src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d7e1469..40b657c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,13 +6,17 @@ set (CMAKE_MODULE_PATH "${MODULE_PATH}") set (CMAKE_CXX_STANDARD 11) include_directories(${CMAKE_SOURCE_DIR}/../nats.c/src) +include_directories(${CMAKE_SOURCE_DIR}/src) #set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -Werror=strict-aliasing") file( GLOB_RECURSE source_files - domo-iot.cpp + main.cpp + server/domo-server.cpp + broker/nats-broker.cpp + web/web-server.cpp ) add_executable (domo-iot ${source_files}) @@ -24,6 +28,7 @@ target_link_libraries (domo-iot pthread event_pthreads civetweb-cpp + json-c rt ) diff --git a/src/broker/nats-broker.cpp b/src/broker/nats-broker.cpp new file mode 100644 index 0000000..35c96c6 --- /dev/null +++ b/src/broker/nats-broker.cpp @@ -0,0 +1,70 @@ +/*! + * nats-broker.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: 13/11/2019 + * + */ + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include + +#include "nats-broker.h" + +#include + +/*! ---------------------------------------------------------------------------- + * @fn NatsBroker + * + * @brief Constructor of the Nats Broker. + */ +NatsBroker::NatsBroker(void) : m_conn(NULL), m_opts(NULL), m_sub(NULL) +{ +} + +/*! ---------------------------------------------------------------------------- + * @fn ~NatsBroker + * + * @brief Destructor of the Nats. + */ +NatsBroker::~NatsBroker(void) +{ +} + +/*! ---------------------------------------------------------------------------- + * @fn setup + * + * @brief Setup the Broker. + */ +int NatsBroker::setup(void) +{ + natsStatus the_status; + + nats_Open(-1); + + // One time initialization of things that we need. + natsLibevent_Init(); + + if (natsOptions_Create(&m_opts) != NATS_OK) + + + return 0; +} diff --git a/src/broker/nats-broker.h b/src/broker/nats-broker.h new file mode 100644 index 0000000..7d89df5 --- /dev/null +++ b/src/broker/nats-broker.h @@ -0,0 +1,51 @@ +/*! + * nats-broker.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: 13/11/2019 + * + */ + +#ifndef _NATS_BROKER_H +#define _NATS_BROKER_H + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include + +/*---------------------------------- Deps -----------------------------------*/ + +/*--------------------------------- CLASS ----------------------------------*/ + +class NatsBroker +{ +public: + NatsBroker(void); + ~NatsBroker(void); + + int setup(void); + +private: + natsConnection *m_conn; + natsOptions *m_opts; + natsSubscription *m_sub; +}; + +#endif /* _NATS_BROKER_H */ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..6221551 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,52 @@ +/*! + * main.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 + +#include "server/domo-server.h" + +/*! ---------------------------------------------------------------------------- + * @fn main + * + * @brief Main function of domo-iot daemon. + */ +int main(int argc, char *argv[]) +{ + int the_return; + + DomoServer the_server; + + if (!the_server.setup()) + { + fprintf(stderr, "Failed to setup the daemon.\n"); + return -1; + } + + the_return = the_server.loop(); + + return the_return; +} diff --git a/src/server/domo-server.cpp b/src/server/domo-server.cpp new file mode 100644 index 0000000..de395c7 --- /dev/null +++ b/src/server/domo-server.cpp @@ -0,0 +1,89 @@ +/*! + * domo-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: 13/11/2019 + * + */ + +/*-------------------------------- INCLUDES ---------------------------------*/ + +#include + +#include "domo-server.h" + +/*! ---------------------------------------------------------------------------- + * @fn DomoServer + * + * @brief Constructor of the Domo Server Object. + */ +DomoServer::DomoServer(void) : m_evt_loop(NULL) +{ +} + +/*! ---------------------------------------------------------------------------- + * @fn ~DomoServer + * + * @brief Destructor of the Domo Server Object. + */ +DomoServer::~DomoServer(void) +{ +} + +/*! ---------------------------------------------------------------------------- + * @fn setup + * + * @brief setup the Domo Server. + */ +bool DomoServer::setup(void) +{ + + if (m_broker.setup()) + { + return false; + } + + m_evt_loop = event_base_new(); + if (m_evt_loop == NULL) + { + return false; + } + + return true; +} + +/*! ---------------------------------------------------------------------------- + * @fn terminate + * + * @brief terminate the Domo Server. + */ +void DomoServer::terminate(void) +{ +} + +/*! ---------------------------------------------------------------------------- + * @fn loop + * + * @brief main loop function. + */ +int DomoServer::loop(void) +{ + return 0; +} diff --git a/src/server/domo-server.h b/src/server/domo-server.h new file mode 100644 index 0000000..4aaf727 --- /dev/null +++ b/src/server/domo-server.h @@ -0,0 +1,57 @@ +/*! + * domo-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: 13/11/2019 + * + */ + +#ifndef _DOMO_SERVER_H +#define _DOMO_SERVER_H + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include "broker/nats-broker.h" +#include "web/web-server.h" + +/*---------------------------------- Deps -----------------------------------*/ + +struct event_base; + +/*--------------------------------- CLASS ----------------------------------*/ + +class DomoServer +{ +public: + DomoServer(void); + ~DomoServer(void); + + bool setup(void); + void terminate(void); + + int loop(void); + +private: + NatsBroker m_broker; + WebServer m_server; + struct event_base *m_evt_loop; +}; + +#endif /* _DOMO_SERVER_H */ \ No newline at end of file diff --git a/src/domo-iot.cpp b/src/tempo/domo-iot.cpp similarity index 100% rename from src/domo-iot.cpp rename to src/tempo/domo-iot.cpp diff --git a/src/domo-iot.nats.c b/src/tempo/domo-iot.nats.c similarity index 89% rename from src/domo-iot.nats.c rename to src/tempo/domo-iot.nats.c index d191642..a780f9f 100644 --- a/src/domo-iot.nats.c +++ b/src/tempo/domo-iot.nats.c @@ -28,6 +28,7 @@ #include #include + #include static void onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure) @@ -48,25 +49,25 @@ static void onMsg(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void */ 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); +// 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(); + //natsLibevent_Init(); // Create a loop. - evLoop = event_base_new(); - if (evLoop == NULL) - s = NATS_ERR; + // evLoop = event_base_new(); + // if (evLoop == NULL) + // s = NATS_ERR; - if (natsOptions_Create(&opts) != NATS_OK) - s = NATS_NO_MEMORY; + // if (natsOptions_Create(&opts) != NATS_OK) + // s = NATS_NO_MEMORY; s = natsOptions_UseGlobalMessageDelivery(opts, true); diff --git a/src/web/web-server.cpp b/src/web/web-server.cpp new file mode 100644 index 0000000..9481caa --- /dev/null +++ b/src/web/web-server.cpp @@ -0,0 +1,48 @@ +/*! + * 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: 13/11/2019 + * + */ + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include "web-server.h" + +/*! ---------------------------------------------------------------------------- + * @fn WebServer + * + * @brief Constructor of the Web Server Object. + */ + +WebServer::WebServer(void) +{ +} + +/*! ---------------------------------------------------------------------------- + * @fn ~WebServer + * + * @brief Destructor of the Web Server Object. + */ + +WebServer::~WebServer(void) +{ +} diff --git a/src/web/web-server.h b/src/web/web-server.h new file mode 100644 index 0000000..46c1aa4 --- /dev/null +++ b/src/web/web-server.h @@ -0,0 +1,44 @@ +/*! + * 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: 13/11/2019 + * + */ + +#ifndef _WEB_SERVER_H +#define _WEB_SERVER_H + +/*------------------------------- INCLUDES ----------------------------------*/ + +/*---------------------------------- Deps -----------------------------------*/ + +/*--------------------------------- CLASS ----------------------------------*/ + +class WebServer +{ +public: + WebServer(void); + ~WebServer(void); + +private: +}; + +#endif /* __WEB_SERVER_H */