diff --git a/CMakeLists.txt b/CMakeLists.txt index 929e00b..5e6cb18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,9 @@ include(cmake/Doxygen.cmake) # Identify and link with the specific "packages" the project uses find_package(nlohmann_json REQUIRED) find_package(httplib REQUIRED) +find_package(frameworkd REQUIRED) +find_package(sdbus-c++ REQUIRED) + #find_package(package_name package_version REQUIRED package_type [other_options]) target_link_libraries( ${PROJECT_NAME} @@ -159,6 +162,7 @@ target_link_libraries( # dependency1 ... PRIVATE nlohmann_json::nlohmann_json + frameworkd::frameworkd httplib::httplib ) if(${PROJECT_NAME}_BUILD_EXECUTABLE AND ${PROJECT_NAME}_ENABLE_UNIT_TESTING) @@ -167,6 +171,7 @@ if(${PROJECT_NAME}_BUILD_EXECUTABLE AND ${PROJECT_NAME}_ENABLE_UNIT_TESTING) PUBLIC httplib::httplib nlohmann_json::nlohmann_json + frameworkd::frameworkd ) endif() diff --git a/config-files/serverd.json b/config-files/serverd.json new file mode 100644 index 0000000..3c3f0cb --- /dev/null +++ b/config-files/serverd.json @@ -0,0 +1,7 @@ +{ + "serviceId": "serverd", + "proxys": { + "serverd": {} + }, + "data": {} +} diff --git a/config-files/zfkd.dbus.serverd.conf b/config-files/zfkd.dbus.serverd.conf new file mode 100644 index 0000000..8c2c1ca --- /dev/null +++ b/config-files/zfkd.dbus.serverd.conf @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/src/message-buffer.hpp b/src/message-buffer.hpp index 4889433..bbac26d 100644 --- a/src/message-buffer.hpp +++ b/src/message-buffer.hpp @@ -32,10 +32,10 @@ class MessagesBuffer { std::unique_ptr m_thread; std::map m_messages; - Message m_currMessage; - int delay; + Message m_currMessage; + void read(); public: diff --git a/src/server.cpp b/src/server.cpp index b103bcc..8fee07b 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1,11 +1,78 @@ #include "message-buffer.hpp" #include "utils.hpp" #include +#include #include #include #include #include +class ServerdService : public RoutineService { + const char* m_host; + int m_port; + + httplib::Server m_svr; + MessagesBuffer m_buffer; + + static const int buffer_size = 10; + +public: + explicit ServerdService(const char* host, const int port) + : RoutineService { "serverd" } + , m_host { host } + , m_port { port } + , m_buffer { buffer_size } + { + } + + const DBusHandler::Path m_requestPath { + "zfkd.dbus.serverd", + "/zfkd/dbus/serverd", + "zfkd.dbus.serverd", + "request" + }; + + void setup() override + { + + m_svr.Post("/", [&](const httplib::Request& req, httplib::Response& res) { + nlohmann::json receivedJson; + receivedJson = nlohmann::json::parse(req.body); + + std::uint8_t id = receivedJson["equipe"]; + nlohmann::json payload = receivedJson["payload"]; + + m_buffer.write(id, payload); + utils::saveJson(receivedJson, std::to_string(id)); + + res.set_content("received message", "text/plain"); + }); + + m_svr.set_error_handler([](const httplib::Request&, httplib::Response& res) { + std::string error = "Error:" + std::to_string(res.status); + res.set_content(error, "text/plain"); + }); + + m_svr.set_exception_handler([](const httplib::Request& req, httplib::Response& res, std::exception& e) { + std::cout << e.what() << std::endl; + res.set_content("exception raised", "text/plain"); + }); + + DBusHandler::registerMethod(m_requestPath, [&](nlohmann::json req) { + return m_buffer.getCurrMessage(); + }); + } + + void routine() override + { + m_svr.listen(m_host, m_port); + } + + void destroy() override + { + } +}; + auto main(int argc, char* argv[]) -> int { const char* host; @@ -21,31 +88,10 @@ auto main(int argc, char* argv[]) -> int throw std::invalid_argument("invalid number of arguments"); } - httplib::Server svr; - MessagesBuffer buffer(10); - - svr.Post("/", [&buffer](const httplib::Request& req, httplib::Response& res) { - nlohmann::json receivedJson; - receivedJson = nlohmann::json::parse(req.body); - - int id = receivedJson["equipe"]; - nlohmann::json payload = receivedJson["payload"]; - - buffer.write(id, payload); - utils::saveJson(receivedJson, std::to_string(id)); - - res.set_content("received message", "text/plain"); - }); - - svr.set_error_handler([](const httplib::Request&, httplib::Response& res) { - std::string error = "Error:" + std::to_string(res.status); - res.set_content(error, "text/plain"); - }); - - svr.set_exception_handler([](const httplib::Request& req, httplib::Response& res, std::exception& e) { - std::cout << e.what() << std::endl; - res.set_content("exception raised", "text/plain"); - }); + Daemon serverd { "serverd.json" }; + ServerdService serverdService { host, port }; + serverd.deploy(serverdService); + serverd.run(); - svr.listen(host, port); + return 0; }