~aleteoryx/muditaos

ref: a870922ec320fac19dd46cedeaf40357f7a58bb9 muditaos/module-services/service-desktop/parser/MessageHandler.cpp -rw-r--r-- 1.6 KiB
a870922e — Lukasz Mastalerz [CP-1837] Random crashes while copying files 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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("[MsgHandler]\nmethod: %s\nendpoint: %s\nuuid: %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("No way to handle!");
            return;
        }
        handler->handle(*context);
        messageProcessedCallback();
    }

} // namespace sdesktop::endpoints