Skip to content

Commit

Permalink
Merge pull request #28 from zenitheesc/feature/fkd-integration
Browse files Browse the repository at this point in the history
Feature/fkd integration
  • Loading branch information
Grillo-0 authored Jul 7, 2022
2 parents 125dcab + 6297f8f commit 410d370
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 28 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,17 @@ 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}
# PUBLIC
# dependency1 ...
PRIVATE
nlohmann_json::nlohmann_json
frameworkd::frameworkd
httplib::httplib
)
if(${PROJECT_NAME}_BUILD_EXECUTABLE AND ${PROJECT_NAME}_ENABLE_UNIT_TESTING)
Expand All @@ -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()

Expand Down
7 changes: 7 additions & 0 deletions config-files/serverd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"serviceId": "serverd",
"proxys": {
"serverd": {}
},
"data": {}
}
10 changes: 10 additions & 0 deletions config-files/zfkd.dbus.serverd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE busconfig PUBLIC
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<policy user="root">
<allow own="zfkd.dbus.serverd"/>
<allow send_destination="zfkd.dbus"/>
<allow send_interface="zfkd.dbus.serverd"/>
</policy>
</busconfig>
4 changes: 2 additions & 2 deletions src/message-buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class MessagesBuffer {
std::unique_ptr<std::thread> m_thread;

std::map<std::uint8_t, Message> m_messages;
Message m_currMessage;

int delay;

Message m_currMessage;

void read();

public:
Expand Down
98 changes: 72 additions & 26 deletions src/server.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,78 @@
#include "message-buffer.hpp"
#include "utils.hpp"
#include <exception>
#include <frameworkd/classes/daemon/daemon.hpp>
#include <httplib.h>
#include <nlohmann/json.hpp>
#include <string>
#include <thread>

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;
Expand All @@ -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;
}

0 comments on commit 410d370

Please sign in to comment.