~aleteoryx/muditaos

ref: 7188802f8e1bed65994ccf777407f88467bb2bb7 muditaos/module-services/service-appmgr/include/service-appmgr/model/ActionsRegistry.hpp -rw-r--r-- 2.2 KiB
7188802f — Mateusz Grzegorzek [BH-669] Fix CMake in service-appmgr 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <service-appmgr/Actions.hpp>

#include <chrono>
#include <deque>
#include <functional>

namespace app::manager
{
    struct ActionEntry
    {
      public:
        using Clock     = std::chrono::steady_clock;
        using Timestamp = std::chrono::time_point<Clock>;

        explicit ActionEntry(actions::ActionId action,
                             actions::ActionParamsPtr &&params = {},
                             Timestamp creationTime            = Clock::now());

        void setTargetApplication(std::string targetApplication) noexcept;
        [[nodiscard]] auto isExpired() const noexcept -> bool;

        actions::Action actionId;
        actions::ActionParamsPtr params;
        std::string target;

      private:
        friend bool operator==(const ActionEntry &lhs, const ActionEntry &rhs) noexcept;
        friend bool operator!=(const ActionEntry &lhs, const ActionEntry &rhs) noexcept;

        Timestamp createdAt = Clock::now();
    };

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

    enum class ActionProcessStatus
    {
        Accepted,
        Skipped,
        Dropped
    };

    class ActionsRegistry
    {
      public:
        using OnNextActionReadyCallback = std::function<ActionProcessStatus(ActionEntry &action)>;

        explicit ActionsRegistry(OnNextActionReadyCallback &&callback);

        void enqueue(ActionEntry &&action);
        void finished();

        [[nodiscard]] auto hasPendingAction() const noexcept -> bool;
        [[nodiscard]] auto getPendingAction() noexcept -> ActionEntry *;

      private:
        [[nodiscard]] auto isCurrentlyProcessing() const noexcept -> bool;
        void addAction(ActionEntry &&action);
        void notifyAboutNextAction();
        void removeExpiredActions();
        void onFinished();

        std::deque<ActionEntry> actions;
        OnNextActionReadyCallback nextActionReady;
        ActionEntry *actionInProgress = nullptr;
    };
} // namespace app::manager