mirror of
https://github.com/mudita/MuditaOS.git
synced 2026-04-23 00:19:31 -04:00
* Adding document describing how to use logger * Adjusting logs to follow a new guide * Change order in log header: line number is now before function name
51 lines
1.6 KiB
C++
51 lines
1.6 KiB
C++
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
|
|
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
|
|
|
|
#include "MessageHandler.hpp"
|
|
#include <endpoints/EndpointFactory.hpp>
|
|
|
|
#include <log/log.hpp>
|
|
|
|
#include <memory>
|
|
|
|
namespace sdesktop::endpoints
|
|
{
|
|
|
|
MessageHandler::MessageHandler(sys::Service *OwnerService,
|
|
std::function<void()> messageProcessedCallback,
|
|
std::unique_ptr<EndpointFactory> endpointFactory)
|
|
: messageProcessedCallback(std::move(messageProcessedCallback)), OwnerServicePtr(OwnerService),
|
|
endpointFactory(std::move(endpointFactory))
|
|
{}
|
|
|
|
void MessageHandler::parseMessage(const std::string &msg)
|
|
{
|
|
try {
|
|
messageJson = json11::Json::parse(msg, JsonErrorMsg);
|
|
}
|
|
catch (const std::exception &e) {
|
|
LOG_ERROR("Cannot create MessageHandler! err:%s", e.what());
|
|
}
|
|
}
|
|
|
|
void MessageHandler::processMessage()
|
|
{
|
|
auto context = ContextFactory::create(messageJson);
|
|
|
|
LOG_DEBUG("Message details: method: %s, endpoint: %s, uuid: %d",
|
|
magic_enum::enum_name(context->getMethod()).data(),
|
|
magic_enum::enum_name(context->getEndpoint()).data(),
|
|
context->getUuid());
|
|
|
|
auto handler = endpointFactory->create(*context, OwnerServicePtr);
|
|
|
|
if (handler == nullptr) {
|
|
LOG_ERROR("Handler not created. Cannot proceed");
|
|
return;
|
|
}
|
|
handler->handle(*context);
|
|
messageProcessedCallback();
|
|
}
|
|
|
|
} // namespace sdesktop::endpoints
|