~aleteoryx/muditaos

ref: 48d31b876cf6fed438676e4fa73b6e16b1f6b3a7 muditaos/module-services/service-appmgr/model/ActionsRegistry.cpp -rw-r--r-- 3.5 KiB
48d31b87 — tomaszkrosnowski [EGD-6311] Audio settings windows 4 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <service-appmgr/model/ActionsRegistry.hpp>

#include <module-utils/log/log.hpp>
#include <module-utils/magic_enum/include/magic_enum.hpp>

#include <cassert>
#include <stdexcept>

namespace app::manager
{
    namespace
    {
        constexpr auto ExpirationTimeout = std::chrono::seconds{30};
    } // namespace

    ActionEntry::ActionEntry(actions::ActionId action, actions::ActionParamsPtr &&params, Timestamp creationTime)
        : actionId{action}, params{std::move(params)}, createdAt{creationTime}
    {}

    void ActionEntry::setTargetApplication(std::string targetApplication) noexcept
    {
        target = std::move(targetApplication);
    }

    auto ActionEntry::isExpired() const noexcept -> bool
    {
        const auto diff = Clock::now() - createdAt;
        return diff > ExpirationTimeout;
    }

    bool operator==(const ActionEntry &lhs, const ActionEntry &rhs) noexcept
    {
        return lhs.actionId == rhs.actionId && lhs.createdAt == rhs.createdAt && lhs.params == rhs.params;
    }

    bool operator!=(const ActionEntry &lhs, const ActionEntry &rhs) noexcept
    {
        return !operator==(lhs, rhs);
    }

    ActionsRegistry::ActionsRegistry(OnNextActionReadyCallback &&callback) : nextActionReady{std::move(callback)}
    {
        if (!nextActionReady) {
            throw std::invalid_argument{"No OnNextActionReadyCallback passed."};
        }
    }

    void ActionsRegistry::enqueue(ActionEntry &&action)
    {
        LOG_INFO("Enqueue action %s", magic_enum::enum_name(action.actionId).data());

        addAction(std::move(action));
        if (!isCurrentlyProcessing()) {
            notifyAboutNextAction();
        }
    }

    void ActionsRegistry::addAction(ActionEntry &&action)
    {
        actions.push_back(std::move(action));
    }

    auto ActionsRegistry::isCurrentlyProcessing() const noexcept -> bool
    {
        return hasPendingAction();
    }

    void ActionsRegistry::notifyAboutNextAction()
    {
        for (auto &action : actions) {
            if (const auto handled = nextActionReady(action); handled) {
                LOG_INFO(
                    "Pending action %s to %s", magic_enum::enum_name(action.actionId).data(), action.target.c_str());
                actionInProgress = &action;
                return;
            }
        }
    }

    void ActionsRegistry::finished()
    {
        assert(isCurrentlyProcessing());

        LOG_INFO("Finished action %s on %s",
                 magic_enum::enum_name(actionInProgress->actionId).data(),
                 actionInProgress->target.c_str());
        if (const auto it = std::find(actions.begin(), actions.end(), *actionInProgress); it != actions.end()) {
            actions.erase(it);
        }

        actionInProgress = nullptr;
        onFinished();
    }

    void ActionsRegistry::onFinished()
    {
        removeExpiredActions();
        if (!actions.empty()) {
            notifyAboutNextAction();
        }
    }

    void ActionsRegistry::removeExpiredActions()
    {
        actions.erase(
            std::remove_if(actions.begin(), actions.end(), [](const auto &action) { return action.isExpired(); }),
            actions.end());
    }

    auto ActionsRegistry::hasPendingAction() const noexcept -> bool
    {
        return actionInProgress != nullptr;
    }

    auto ActionsRegistry::getPendingAction() noexcept -> ActionEntry *
    {
        return actionInProgress;
    }
} // namespace app::manager