~aleteoryx/muditaos

ref: 39a09e862722d241acd1897ed913b710b198002b muditaos/module-sys/Service/BusProxy.hpp -rw-r--r-- 1.5 KiB
39a09e86 — Wiktor S. Ovalle Correa [EGD-6746] New SimCard implementation 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
// 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 "Message.hpp"
#include "Watchdog.hpp"

#include <cstdint>
#include <memory>
#include <string>
#include <vector>

namespace sys
{
    class Service; // Forward declaration
    class Bus;     // Forward declaration

    class BusProxy
    {
      public:
        static constexpr auto defaultTimeout = 5000U;

        ~BusProxy() noexcept;

        bool sendUnicast(std::shared_ptr<Message> message, const std::string &targetName);
        SendResult sendUnicastSync(std::shared_ptr<Message> message,
                                   const std::string &targetName,
                                   std::uint32_t timeout);
        void sendMulticast(std::shared_ptr<Message> message, BusChannel channel);
        void sendBroadcast(std::shared_ptr<Message> message);

        template <typename Msg, typename... Params> bool sendUnicast(Params &&... params)
        {
            auto msg = std::make_shared<Msg>(std::forward<Params>(params)...);
            return sendUnicast(msg, Msg::target);
        }

        std::vector<BusChannel> channels;

      private:
        friend class Service;
        explicit BusProxy(Service *owner, Watchdog &watchdog);

        void sendResponse(std::shared_ptr<Message> response, std::shared_ptr<Message> request);
        void connect();
        void disconnect();

        Service *owner;
        Watchdog &watchdog;
        std::unique_ptr<Bus> busImpl;
    };
} // namespace sys