Skip to content

Commit 3814a48

Browse files
committed
added Channel and Gateway entities
1 parent 150c9ca commit 3814a48

File tree

12 files changed

+351
-51
lines changed

12 files changed

+351
-51
lines changed

src/common/ipc/channel.hpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: MIT
4+
//
5+
6+
#ifndef OCVSMD_COMMON_IPC_CHANNEL_HPP_INCLUDED
7+
#define OCVSMD_COMMON_IPC_CHANNEL_HPP_INCLUDED
8+
9+
#include "dsdl_helpers.hpp"
10+
#include "gateway.hpp"
11+
12+
#include <cetl/pf17/cetlpf.hpp>
13+
14+
#include <cstddef>
15+
#include <functional>
16+
#include <utility>
17+
18+
namespace ocvsmd
19+
{
20+
namespace common
21+
{
22+
namespace ipc
23+
{
24+
25+
class AnyChannel
26+
{
27+
public:
28+
struct Connected
29+
{};
30+
31+
struct Disconnected
32+
{};
33+
34+
template <typename Input>
35+
using EventVar = cetl::variant<Input, Connected, Disconnected>;
36+
37+
template <typename Input>
38+
using EventHandler = std::function<void(const EventVar<Input>&)>;
39+
40+
protected:
41+
AnyChannel() = default;
42+
43+
}; // AnyChannel
44+
45+
template <typename Input, typename Output>
46+
class Channel final : public AnyChannel
47+
{
48+
public:
49+
Channel(Channel&& other) noexcept
50+
: gateway_{std::move(other.gateway_)}
51+
, event_handler_{std::move(other.event_handler_)}
52+
{
53+
setupEventHandler();
54+
}
55+
56+
Channel& operator=(Channel&& other) noexcept
57+
{
58+
if (this != &other)
59+
{
60+
gateway_ = std::move(other.gateway_);
61+
event_handler_ = std::move(other.event_handler_);
62+
setupEventHandler();
63+
}
64+
return *this;
65+
}
66+
67+
Channel(const Channel&) = delete;
68+
Channel& operator=(const Channel&) = delete;
69+
70+
~Channel()
71+
{
72+
gateway_->setEventHandler(nullptr);
73+
event_handler_ = nullptr;
74+
}
75+
76+
void send(const Output& output)
77+
{
78+
constexpr std::size_t BufferSize = Output::;
79+
constexpr bool IsOnStack = BufferSize <= MsgSmallPayloadSize;
80+
81+
return tryPerformOnSerialized<Output, Result, BufferSize, IsOnStack>( //
82+
output,
83+
[this](const auto payload) {
84+
//
85+
gateway_->send(payload);
86+
});
87+
}
88+
89+
private:
90+
friend class ClientRouter;
91+
92+
Channel(detail::Gateway::Ptr gateway, EventHandler<Input> event_handler)
93+
: gateway_{std::move(gateway)}
94+
, event_handler_{std::move(event_handler)}
95+
{
96+
CETL_DEBUG_ASSERT(gateway_, "");
97+
CETL_DEBUG_ASSERT(event_handler_, "");
98+
99+
setupEventHandler();
100+
}
101+
102+
void setupEventHandler()
103+
{
104+
gateway_->setEventHandler([this](const auto& event) {
105+
//
106+
event_handler_(event);
107+
});
108+
}
109+
110+
static constexpr std::size_t MsgSmallPayloadSize = 256;
111+
112+
detail::Gateway::Ptr gateway_;
113+
EventHandler<Input> event_handler_;
114+
115+
}; // Channel
116+
117+
} // namespace ipc
118+
} // namespace common
119+
} // namespace ocvsmd
120+
121+
#endif // OCVSMD_COMMON_IPC_CHANNEL_HPP_INCLUDED

src/common/ipc/client_router.cpp

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
#include "client_router.hpp"
77

8+
#include "gateway.hpp"
89
#include "pipe/client_pipe.hpp"
910

1011
#include <cetl/cetl.hpp>
1112

13+
#include <cstdint>
1214
#include <memory>
15+
#include <unordered_map>
1316
#include <utility>
1417

1518
namespace ocvsmd
@@ -26,12 +29,89 @@ class ClientRouterImpl final : public ClientRouter
2629
public:
2730
explicit ClientRouterImpl(pipe::ClientPipe::Ptr client_pipe)
2831
: client_pipe_{std::move(client_pipe)}
32+
, next_tag_{0}
2933
{
3034
CETL_DEBUG_ASSERT(client_pipe_, "");
3135
}
3236

37+
// ClientRouter
38+
39+
CETL_NODISCARD detail::Gateway::Ptr makeGateway() override
40+
{
41+
const Tag new_tag = ++next_tag_;
42+
auto gateway = GatewayImpl::create(new_tag, *this);
43+
tag_to_gateway_[new_tag] = gateway;
44+
return gateway;
45+
}
46+
3347
private:
34-
pipe::ClientPipe::Ptr client_pipe_;
48+
using Tag = std::uint64_t;
49+
50+
class GatewayImpl final : public std::enable_shared_from_this<GatewayImpl>, public detail::Gateway
51+
{
52+
struct Private
53+
{
54+
explicit Private() = default;
55+
};
56+
57+
public:
58+
static std::shared_ptr<GatewayImpl> create(const Tag tag, ClientRouterImpl& router)
59+
{
60+
return std::make_shared<GatewayImpl>(Private(), tag, router);
61+
}
62+
63+
GatewayImpl(Private, const Tag tag, ClientRouterImpl& router)
64+
: tag_{tag}
65+
, router_{router}
66+
{
67+
}
68+
69+
GatewayImpl(const GatewayImpl&) = delete;
70+
GatewayImpl(GatewayImpl&&) noexcept = delete;
71+
GatewayImpl& operator=(const GatewayImpl&) = delete;
72+
GatewayImpl& operator=(GatewayImpl&&) noexcept = delete;
73+
74+
~GatewayImpl()
75+
{
76+
setEventHandler(nullptr);
77+
}
78+
79+
void send(const Payload payload) override
80+
{
81+
router_.client_pipe_->sendMessage(payload);
82+
}
83+
84+
void event(const Event::Var& event) override
85+
{
86+
if (event_handler_)
87+
{
88+
event_handler_(event);
89+
}
90+
}
91+
92+
void setEventHandler(EventHandler event_handler) override
93+
{
94+
if (event_handler)
95+
{
96+
event_handler_ = std::move(event_handler);
97+
router_.tag_to_gateway_[tag_] = shared_from_this();
98+
}
99+
else
100+
{
101+
router_.tag_to_gateway_.erase(tag_);
102+
}
103+
}
104+
105+
private:
106+
const Tag tag_;
107+
ClientRouterImpl& router_;
108+
EventHandler event_handler_;
109+
110+
}; // GatewayImpl
111+
112+
pipe::ClientPipe::Ptr client_pipe_;
113+
Tag next_tag_;
114+
std::unordered_map<Tag, detail::Gateway::Ptr> tag_to_gateway_;
35115

36116
}; // ClientRouterImpl
37117

src/common/ipc/client_router.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
#ifndef OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED
77
#define OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED
88

9+
#include "channel.hpp"
10+
#include "gateway.hpp"
911
#include "pipe/client_pipe.hpp"
1012

13+
#include <cetl/cetl.hpp>
14+
1115
#include <memory>
1216

1317
namespace ocvsmd
@@ -24,16 +28,24 @@ class ClientRouter
2428

2529
static Ptr make(pipe::ClientPipe::Ptr client_pipe);
2630

27-
ClientRouter(ClientRouter&&) = delete;
28-
ClientRouter(const ClientRouter&) = delete;
29-
ClientRouter& operator=(ClientRouter&&) = delete;
30-
ClientRouter& operator=(const ClientRouter&) = delete;
31+
ClientRouter(const ClientRouter&) = delete;
32+
ClientRouter(ClientRouter&&) noexcept = delete;
33+
ClientRouter& operator=(const ClientRouter&) = delete;
34+
ClientRouter& operator=(ClientRouter&&) noexcept = delete;
3135

3236
virtual ~ClientRouter() = default;
3337

38+
template <typename Input, typename Output>
39+
CETL_NODISCARD Channel<Input, Output> makeChannel(AnyChannel::EventHandler<Input> event_handler)
40+
{
41+
return Channel<Input, Output>{makeGateway(), event_handler};
42+
}
43+
3444
protected:
3545
ClientRouter() = default;
3646

47+
CETL_NODISCARD virtual detail::Gateway::Ptr makeGateway() = 0;
48+
3749
}; // ClientRouter
3850

3951
} // namespace ipc

src/common/ipc/gateway.hpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: MIT
4+
//
5+
6+
#ifndef OCVSMD_COMMON_IPC_GATEWAY_HPP_INCLUDED
7+
#define OCVSMD_COMMON_IPC_GATEWAY_HPP_INCLUDED
8+
9+
#include <cetl/pf17/cetlpf.hpp>
10+
#include <cetl/pf20/cetlpf.hpp>
11+
12+
#include <cstdint>
13+
#include <functional>
14+
#include <memory>
15+
16+
namespace ocvsmd
17+
{
18+
namespace common
19+
{
20+
namespace ipc
21+
{
22+
namespace detail
23+
{
24+
25+
class Gateway
26+
{
27+
public:
28+
using Ptr = std::shared_ptr<Gateway>;
29+
30+
using Payload = cetl::span<const std::uint8_t>;
31+
32+
struct Event
33+
{
34+
struct Connected
35+
{};
36+
struct Disconnected
37+
{};
38+
struct Message
39+
{
40+
Payload payload;
41+
42+
}; // Message
43+
44+
using Var = cetl::variant<Message, Connected, Disconnected>;
45+
46+
}; // Event
47+
48+
using EventHandler = std::function<void(const Event::Var&)>;
49+
50+
Gateway(const Gateway&) = delete;
51+
Gateway(Gateway&&) noexcept = delete;
52+
Gateway& operator=(const Gateway&) = delete;
53+
Gateway& operator=(Gateway&&) noexcept = delete;
54+
55+
virtual void send(const Payload payload) = 0;
56+
virtual void event(const Event::Var& event) = 0;
57+
virtual void setEventHandler(EventHandler event_handler) = 0;
58+
59+
protected:
60+
Gateway() = default;
61+
~Gateway() = default;
62+
63+
}; // Gateway
64+
65+
} // namespace detail
66+
} // namespace ipc
67+
} // namespace common
68+
} // namespace ocvsmd
69+
70+
#endif // OCVSMD_COMMON_IPC_GATEWAY_HPP_INCLUDED

src/common/ipc/pipe/client_pipe.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// SPDX-License-Identifier: MIT
44
//
55

6-
#ifndef OCVSMD_COMMON_IPC_CLIENT_PIPE_HPP_INCLUDED
7-
#define OCVSMD_COMMON_IPC_CLIENT_PIPE_HPP_INCLUDED
6+
#ifndef OCVSMD_COMMON_IPC_PIPE_CLIENT_PIPE_HPP_INCLUDED
7+
#define OCVSMD_COMMON_IPC_PIPE_CLIENT_PIPE_HPP_INCLUDED
88

99
#include <cetl/pf17/cetlpf.hpp>
1010
#include <cetl/pf20/cetlpf.hpp>
@@ -21,6 +21,7 @@ namespace ipc
2121
{
2222
namespace pipe
2323
{
24+
2425
class ClientPipe
2526
{
2627
public:
@@ -46,10 +47,10 @@ class ClientPipe
4647

4748
using EventHandler = std::function<int(const Event::Var&)>;
4849

49-
ClientPipe(ClientPipe&&) = delete;
50-
ClientPipe(const ClientPipe&) = delete;
51-
ClientPipe& operator=(ClientPipe&&) = delete;
52-
ClientPipe& operator=(const ClientPipe&) = delete;
50+
ClientPipe(const ClientPipe&) = delete;
51+
ClientPipe(ClientPipe&&) noexcept = delete;
52+
ClientPipe& operator=(const ClientPipe&) = delete;
53+
ClientPipe& operator=(ClientPipe&&) noexcept = delete;
5354

5455
virtual ~ClientPipe() = default;
5556

@@ -66,4 +67,4 @@ class ClientPipe
6667
} // namespace common
6768
} // namespace ocvsmd
6869

69-
#endif // OCVSMD_COMMON_IPC_CLIENT_PIPE_HPP_INCLUDED
70+
#endif // OCVSMD_COMMON_IPC_PIPE_CLIENT_PIPE_HPP_INCLUDED

0 commit comments

Comments
 (0)