summaryrefslogtreecommitdiff
path: root/src/state_machine.hpp
blob: d5f32658657e84e561a90150d7bfc3733af57210 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma once
#include "interfaces/mount_point_state_machine.hpp"
#include "state/initial_state.hpp"
#include "utils.hpp"

#include <boost/asio/steady_timer.hpp>
#include <functional>
#include <memory>
#include <sdbusplus/asio/object_server.hpp>
#include <system_error>

struct MountPointStateMachine : public interfaces::MountPointStateMachine
{
    MountPointStateMachine(boost::asio::io_context& ioc,
                           DeviceMonitor& devMonitor, const std::string& name,
                           const Configuration::MountPoint& config) :
        ioc{ioc},
        name{name}, config{config}
    {
        devMonitor.addDevice(config.nbdDevice);
    }

    MountPointStateMachine& operator=(MountPointStateMachine&&) = delete;

    std::string_view getName() const override
    {
        return name;
    }

    Configuration::MountPoint& getConfig() override
    {
        return config;
    }

    std::optional<Target>& getTarget() override
    {
        return target;
    }

    BasicState& getState() override
    {
        return *state;
    }

    int& getExitCode() override
    {
        return exitCode;
    }

    boost::asio::io_context& getIoc() override
    {
        return ioc;
    }

    void changeState(std::unique_ptr<BasicState> newState)
    {
        state = std::move(newState);
        LogMsg(Logger::Info, name, " state changed to ", state->getStateName());
        if ((newState = state->onEnter()))
        {
            changeState(std::move(newState));
        }
    }

    template <class EventT>
    void emitEvent(EventT&& event)
    {
        LogMsg(Logger::Info, name, " received ", event.eventName, " while in ",
               state->getStateName());

        if (auto newState = state->handleEvent(std::move(event)))
        {
            changeState(std::move(newState));
        }
    }

    void emitRegisterDBusEvent(
        std::shared_ptr<sdbusplus::asio::connection> bus,
        std::shared_ptr<sdbusplus::asio::object_server> objServer) override
    {
        emitEvent(RegisterDbusEvent(bus, objServer));
    }

    void emitMountEvent(std::optional<Target> newTarget) override
    {
        emitEvent(MountEvent(std::move(newTarget)));
    }

    void emitUnmountEvent() override
    {
        emitEvent(UnmountEvent());
    }

    void emitSubprocessStoppedEvent() override
    {
        emitEvent(SubprocessStoppedEvent());
    }

    void emitUdevStateChangeEvent(const NBDDevice& dev,
                                  StateChange devState) override
    {
        if (config.nbdDevice == dev)
        {
            emitEvent(UdevStateChangeEvent(devState));
        }
        else
        {
            LogMsg(Logger::Debug, name, " Ignoring request.");
        }
    }

    virtual void
        notificationInitialize(std::shared_ptr<sdbusplus::asio::connection> con,
                               const std::string& svc, const std::string& iface,
                               const std::string& name) override
    {
        auto signal = std::make_unique<utils::SignalSender>(std::move(con), svc,
                                                            iface, name);

        auto timer = std::make_unique<boost::asio::steady_timer>(ioc);

        completionNotification = std::make_unique<utils::NotificationWrapper>(
            std::move(signal), std::move(timer));
    }
    void notificationStart()
    {
        auto notificationHandler = [this](const boost::system::error_code& ec) {
            if (ec == boost::system::errc::operation_canceled)
            {
                return;
            }

            LogMsg(Logger::Error,
                   "[App] timedout when waiting for target state");

            completionNotification->notify(
                std::make_error_code(std::errc::device_or_resource_busy));
        };

        LogMsg(Logger::Debug, "Started notification");
        completionNotification->start(
            std::move(notificationHandler),
            std::chrono::seconds(
                config.timeout.value_or(
                    Configuration::MountPoint::defaultTimeout) +
                5));
    }

    virtual void notify(const std::error_code& ec = {}) override
    {
        completionNotification->notify(ec);
    }

    boost::asio::io_context& ioc;
    std::string name;
    Configuration::MountPoint config;
    std::unique_ptr<utils::NotificationWrapper> completionNotification;

    std::optional<Target> target;
    std::unique_ptr<BasicState> state = std::make_unique<InitialState>(*this);
    int exitCode = -1;
};