Skip to content

Commit

Permalink
server: refactor the code to use the RoutineService Template from the…
Browse files Browse the repository at this point in the history
… fkd
  • Loading branch information
grillo authored and grillo committed Apr 15, 2022
1 parent 4e23883 commit 23c7001
Showing 1 changed file with 70 additions and 26 deletions.
96 changes: 70 additions & 26 deletions src/server.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,76 @@
#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 svr;
MessagesBuffer m_buffer;

public:
explicit ServerdService(const char* host, const int port, std::string serviceId)
: RoutineService { serviceId }
, m_host { host }
, m_port { port }
, m_buffer { 10 }
{
}

const DBusHandler::Path m_requestPath {
"zfkd.dbus.serverd",
"/zfkd/dbus/serverd",
"zfkd.dbus.serverd",
"request"
};

void setup() override
{

svr.Post("/", [&](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"];

m_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");
});

DBusHandler::registerMethod(m_requestPath, [&](nlohmann::json req) {
return m_buffer.getCurrMessage();
});
}

void routine() override
{
svr.listen(m_host, m_port);
}

void destroy() override
{
}
};

auto main(int argc, char* argv[]) -> int
{
const char* host;
Expand All @@ -21,31 +86,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");
serverd.deploy(serverdService);
serverd.run();

svr.listen(host, port);
return 0;
}

0 comments on commit 23c7001

Please sign in to comment.