From 19fb08fba4dd2b7e67e817436bd49abdefd9e13b Mon Sep 17 00:00:00 2001 From: Viacheslav Demydiuk Date: Thu, 11 Jan 2024 22:50:57 +0200 Subject: [PATCH] Added message MtClientReady in MtService --- MtService/MtMessage.h | 25 ++++++++++++++-- MtService/MtServer.cpp | 67 +++++++++++++++++++++++++++++------------- MtService/MtServer.h | 2 +- 3 files changed, 70 insertions(+), 24 deletions(-) diff --git a/MtService/MtMessage.h b/MtService/MtMessage.h index e4154783..11641097 100755 --- a/MtService/MtMessage.h +++ b/MtService/MtMessage.h @@ -12,7 +12,8 @@ enum MessageType EVENT = 2, EXPERT_LIST = 3, EXPERT_ADDED = 4, - EXPERT_REMOVED = 5 + EXPERT_REMOVED = 5, + CLIENT_READY = 6 }; class MtMessage @@ -23,8 +24,9 @@ public: return std::to_string(GetType()) + MT_MESSAGE_DELIMETER + GetBody(); } -protected: virtual MessageType GetType() const = 0; + +protected: virtual std::string GetBody() const = 0; }; @@ -101,6 +103,25 @@ private: std::string payload_; }; +class MtClientReady : public MtMessage +{ +public: + MtClientReady() + { + } + +private: + MessageType GetType() const override + { + return MessageType::CLIENT_READY; + } + + std::string GetBody() const override + { + return ""; + } +}; + class MtEvent : public MtMessage { public: diff --git a/MtService/MtServer.cpp b/MtService/MtServer.cpp index b67c6fca..891427d6 100755 --- a/MtService/MtServer.cpp +++ b/MtService/MtServer.cpp @@ -2,6 +2,7 @@ #include "MtServer.h" #include +#include #include #include #include @@ -123,13 +124,6 @@ void MtServer::OnAccept(boost::beast::error_code ec, pending_connections_.erase(it); connections_.push_back(connection); log_.Trace("%s: Pushed connection %p to collection", __FUNCTION__, connection.get()); - - std::vector expert_list; - for (const auto& e : experts_) - expert_list.push_back(e.first); - - MtExpertListMsg msg(std::move(expert_list)); - connection->Send(msg.Serialize()); } } }); @@ -172,27 +166,56 @@ void MtServer::OnAccept(boost::beast::error_code ec, }); connection->OnMessageReceived.connect([con = connection->weak_from_this(), this](const std::string& msg) { log_.Trace("MtServer::OnMessageReceived: %s", msg.c_str()); - auto command = ParseMessage(msg); - if (command) + + std::string::size_type pos = msg.find(';'); + if (pos == std::string::npos) { - if (experts_.count(command->getExpertHandle()) > 0) + log_.Warning("MtServer::OnMessageReceived: invalid message type."); + return; + } + + std::string msg_type_str = msg.substr(0, pos);; + auto msg_type = std::atoi(msg_type_str.c_str()); + if (msg_type == MessageType::COMMAND) + { + auto command = ParseCommand(msg); + if (command) { - experts_[command->getExpertHandle()]->expert->Process(std::move(command), [&](const MtResponse& response) { - auto connection = con.lock(); - if (connection) - connection->Send(response.Serialize()); - else - log_.Warning("MtServer::OnMessageReceived: connection is not valid (null)"); - }); + if (experts_.count(command->getExpertHandle()) > 0) + { + experts_[command->getExpertHandle()]->expert->Process(std::move(command), [&](const MtResponse& response) { + auto connection = con.lock(); + if (connection) + connection->Send(response.Serialize()); + else + log_.Warning("MtServer::OnMessageReceived: connection is not valid (null)"); + }); + } + else + { + log_.Warning("MtServer::OnMessageReceived: Expert is not found with handle %d", command->getExpertHandle()); + } } else { - log_.Warning("MtServer::OnMessageReceived: Expert is not found with handle %d", command->getExpertHandle()); + log_.Warning("MtServer::OnMessageReceived: Failed to parse command from message: %s", msg.c_str()); } } + else if (msg_type == MessageType::CLIENT_READY) + { + std::vector expert_list; + for (const auto& e : experts_) + expert_list.push_back(e.first); + + MtExpertListMsg msg(std::move(expert_list)); + auto connection = con.lock(); + if (connection) + connection->Send(msg.Serialize()); + } else { - log_.Warning("MtServer::OnMessageReceived: Failed to parse command from message: %s", msg.c_str()); + log_.Warning("MtServer::OnMessageReceived: unsupported message type %d", msg_type); + return; } }); pending_connections_.push_back(connection); @@ -203,9 +226,9 @@ void MtServer::OnAccept(boost::beast::error_code ec, } } -std::unique_ptr MtServer::ParseMessage(const std::string& msg) +std::unique_ptr MtServer::ParseCommand(const std::string& msg) { - std::unique_ptr command; + std::string::size_type pos = msg.find(';'); using namespace boost::spirit; using rule_s = qi::rule; @@ -224,6 +247,8 @@ std::unique_ptr MtServer::ParseMessage(const std::string& msg) rule command_type_p = +qi::char_(';') >> word_p[boost::phoenix::ref(command_type) = qi::_1]; rule payload_p = +qi::char_(';') >> word_p[boost::phoenix::ref(payload) = qi::_1]; + std::unique_ptr command; + bool ok = qi::parse(msg.begin(), msg.end(), message_type_p >> -expert_handle_p >> -command_id_p >> -command_type_p >> -payload_p); if (ok) { diff --git a/MtService/MtServer.h b/MtService/MtServer.h index eebb3422..229a1889 100755 --- a/MtService/MtServer.h +++ b/MtService/MtServer.h @@ -48,7 +48,7 @@ private: void OnAccept(boost::beast::error_code ec, boost::asio::ip::tcp::socket socket); - std::unique_ptr ParseMessage(const std::string& msg); + std::unique_ptr ParseCommand(const std::string& msg); void OnEvent(const MtEvent& event); void OnDeinitExpert(int handle);