feat: started building integration point
This commit is contained in:
parent
f565d330db
commit
dc1aed487e
7 changed files with 193 additions and 51 deletions
|
|
@ -1,4 +1,6 @@
|
|||
#include "ydi_server.h"
|
||||
#include "ydi_networking.h"
|
||||
#include <core/os/time.h>
|
||||
#include <core/templates/vector.h>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
|
|
@ -15,13 +17,46 @@ struct Service {
|
|||
std::optional<zmq::socket_t> socket{ std::nullopt };
|
||||
std::unordered_set<ClueID> revealedClues{};
|
||||
std::recursive_mutex mtx{};
|
||||
std::atomic<bool> stop_threads{ false };
|
||||
std::optional<std::string> client{ std::nullopt };
|
||||
double lastHeart{ 0.0 };
|
||||
double lastBeat{ 0.0 };
|
||||
|
||||
std::atomic<bool> stop_threads{ false };
|
||||
};
|
||||
|
||||
std::optional<Service> service{ std::nullopt };
|
||||
thread_local std::optional<std::thread> receiveThread{ std::nullopt };
|
||||
thread_local std::optional<std::thread> pingThread{ std::nullopt };
|
||||
std::optional<std::thread> receiveThread{ std::nullopt };
|
||||
std::optional<std::thread> pingThread{ std::nullopt };
|
||||
|
||||
void handle_reveal_clue(zmq::multipart_t const &message) {
|
||||
}
|
||||
|
||||
void handle_authorised_message(std::string_view const &sender, std::string_view const &type, zmq::multipart_t &message) {
|
||||
if (type == "BEAT") {
|
||||
service->lastBeat = Time::get_singleton()->get_unix_time_from_system();
|
||||
} else if(type == "REVEAL") {
|
||||
handle_reveal_clue(message);
|
||||
} else {
|
||||
multipart(sender, "NOK", "UNKOWN_COMMAND", message).send(*service->socket);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_message(zmq::multipart_t &message) {
|
||||
std::string_view const sender{ message.at(0).to_string_view() };
|
||||
std::string_view const type{ message.at(1).to_string_view() };
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
if (service->client) {
|
||||
if (sender != service->client) {
|
||||
multipart(sender,"NOK", "UNAUTHORIZED_REQUEST").send(*service->socket);
|
||||
}
|
||||
handle_authorised_message(sender, type, message);
|
||||
} else if (type == "CONNECT") {
|
||||
service->client.emplace(sender);
|
||||
multipart(sender, "OK", message).send(*service->socket);
|
||||
} else {
|
||||
multipart(sender,"NOK", "UNAUTHORIZED_REQUEST", message).send(*service->socket);
|
||||
}
|
||||
}
|
||||
|
||||
void receive_thread_entry() {
|
||||
using namespace std::chrono_literals;
|
||||
|
|
@ -30,6 +65,7 @@ void receive_thread_entry() {
|
|||
std::this_thread::sleep_for(20ms);
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
if (incoming.recv(*service->socket)) {
|
||||
handle_message(incoming);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -41,11 +77,12 @@ void ping_thread_entry() {
|
|||
return;
|
||||
}
|
||||
}
|
||||
static zmq::multipart_t ping{ make_multipart(*service->client, std::string("PING")) };
|
||||
static zmq::multipart_t ping{ multipart(*service->client, "HEART") };
|
||||
while (!service->stop_threads) {
|
||||
std::this_thread::sleep_for(1s);
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
ping.send(*service->socket);
|
||||
service->lastHeart = Time::get_singleton()->get_unix_time_from_system();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,10 +117,14 @@ void open() {
|
|||
void close() {
|
||||
if (service) {
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
service->socket->close();
|
||||
service->socket.reset();
|
||||
service->context->close();
|
||||
service->context.reset();
|
||||
if (service->socket) {
|
||||
service->socket->close();
|
||||
service->socket.reset();
|
||||
}
|
||||
if (service->context) {
|
||||
service->context->close();
|
||||
service->context.reset();
|
||||
}
|
||||
service.reset();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue