~aleteoryx/muditaos

ref: 43f7da686efbe2a9268a29cef2bc0a6965c15eaa muditaos/module-sys/Service/details/bus/Bus.hpp -rw-r--r-- 2.9 KiB
43f7da68 — Adam Dobrowolski [MOS-266] Apply suggestions from code review 3 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "system/Common.hpp"
#include "Service/Message.hpp"

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

namespace sys
{
    class Service; // forward declaration

    class Bus
    {
        // Because of incorrect linking of CMake targets Bus methods are accessible from any point of the project.
        // In order to fix this, only BusProxy is able to access the methods of the Bus class.
        friend class BusProxy;

      private:
        /**
         * Sends a message directly to the specified target service.
         * @param message       Message to be sent
         * @param targetName    Target service
         * @param sender        Sender context
         * @return true on success, false otherwise
         */
        bool SendUnicast(std::shared_ptr<Message> message, const std::string &targetName, Service *sender);

        /**
         * Sends a message directly to the specified target service with timeout.
         * @param message       Message to be sent
         * @param targetName    Target service
         * @param sender        Sender context
         * @param timeout       Timeout
         * @return Return code and a response.
         */
        SendResult SendUnicastSync(std::shared_ptr<Message> message,
                                   const std::string &targetName,
                                   Service *sender,
                                   std::uint32_t timeout);

        /// await for response on source message with timeout
        SendResult UnicastSync(const std::shared_ptr<Message> &message, Service *sender, std::uint32_t timeout);

        /**
         * Sends a message to the specified channel.
         * @param message   Message to be sent
         * @param channel   Target channel
         * @param sender    Sender context
         */
        void SendMulticast(std::shared_ptr<Message> message, BusChannel channel, Service *sender);

        /**
         * Sends a message to all registered services.
         * @param message   Message to be sent
         * @param sender    Sender context
         */
        void SendBroadcast(std::shared_ptr<Message> message, Service *sender);

        /**
         * Sends a response to the specified request.
         * @param response  Response to be sent
         * @param request   Request
         * @param sender    Sender context
         */
        void SendResponse(std::shared_ptr<Message> response, std::shared_ptr<Message> request, Service *sender);

        /**
         * Registers a service.
         * @param service   Service to be registered
         */
        static void Add(Service *service);

        /**
         * Unregisters a service.
         * @param service   Service to be unregistered
         */
        static void Remove(Service *service);
    };
} // namespace sys