seqeron 0.18.0
Loading...
Searching...
No Matches
Gateway.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string_view>
5
7#include "org/limitless/seqeron/hosted/detail/ClientLink.hpp"
9
10#include "org_limitless_seqeron_sbe_hosted/Activated.h"
11#include "org_limitless_seqeron_sbe_hosted/CloseConnection.h"
12#include "org_limitless_seqeron_sbe_hosted/Connection.h"
13#include "org_limitless_seqeron_sbe_hosted/GateClosed.h"
14#include "org_limitless_seqeron_sbe_hosted/OpenConnection.h"
15#include "org_limitless_seqeron_sbe_hosted/Publish.h"
16#include "org_limitless_seqeron_sbe_hosted/Role.h"
17
19
33template<typename Listener>
34class Gateway : public detail::ClientLink<Gateway<Listener>, Listener>
35{
36 using Base = detail::ClientLink<Gateway<Listener>, Listener>;
37 friend Base;
38
39 public:
40 // A frame that belongs to the gateway rather than to one of its connections, and what openConnection answers
41 // when there is none to give.
42 static constexpr std::int32_t NO_CONNECTION = -1;
43
44 struct Config
45 {
46 // The logical gateway's sourceId, the one its rows declare and the host runs; the attach is refused otherwise.
47 std::int32_t sourceId = -1;
48 // Answers the host's SnapshotCut and restores its RestoreRecords; without it every cut is abandoned.
50 // The host's heartbeat counter may be silent this long before the stub gives it up (H-9, section 17.6).
51 std::int64_t hostTimeoutMs = 3000;
52 std::int64_t attachTimeoutMs = 90'000;
53 // Names the host this attaches to, the one its proxy.hostName gives; the attach is refused otherwise.
55 };
56
57 Gateway(Config config, Listener& listener) :
58 Base{ listener, wire::Kind::GATEWAY, config.sourceId,
59 config.snapshots, config.hostTimeoutMs, config.attachTimeoutMs,
60 config.hostName },
61 m_config{ config }
62 {
63 static_assert(app::GatewayListener<Listener>);
64 }
65
67 [[nodiscard]] bool canAccept() const noexcept
68 {
69 return m_serving && this->m_canPublish;
70 }
71
73 [[nodiscard]] bool isServing() const noexcept
74 {
75 return m_serving;
76 }
77
78 [[nodiscard]] std::int32_t sourceId() const noexcept
79 {
80 return m_config.sourceId;
81 }
82
91 std::int32_t openConnection(const char* connectionData, const std::size_t length)
92 {
93 if (!canAccept() || length > protocol::MAX_PAYLOAD_LENGTH)
94 {
95 return NO_CONNECTION;
96 }
97 const std::int32_t id = m_nextConnectionId;
98 const bool offered =
99 this->template offerWindowed<wire::OpenConnection>(length, [&](wire::OpenConnection& open) {
100 open.connectionId(id).putData(connectionData, static_cast<std::uint32_t>(length));
101 });
102 if (!offered)
103 {
104 return NO_CONNECTION;
105 }
106 m_nextConnectionId++;
107 return id;
108 }
109
111 std::int32_t openConnection()
112 {
113 return openConnection(nullptr, 0);
114 }
115
121 void closeConnection(const std::int32_t connectionId)
122 {
123 if (!m_serving)
124 {
125 return;
126 }
127 this->template sendUndeclinable<wire::CloseConnection>(
128 [&](wire::CloseConnection& close) { close.epoch(this->m_epoch).connectionId(connectionId); });
129 }
130
136 {
137 if (!m_serving)
138 {
139 return;
140 }
141 this->template sendUndeclinable<wire::GateClosed>(
142 [&](wire::GateClosed& closed) { closed.epoch(this->m_epoch); });
143 // The host drops whatever follows under this epoch, so publish declines until the next Gate (H-3).
144 m_serving = false;
145 this->m_canPublish = false;
146 }
147
159 [[nodiscard]] protocol::Publish publish(const std::int32_t connectionId, const std::uint16_t payloadId,
160 const std::uint8_t* payload, const std::uint16_t payloadLength)
161 {
162 return publish(connectionId, 0, payloadId, payload, payloadLength);
163 }
164
175 [[nodiscard]] protocol::Publish publish(const std::int32_t connectionId, const std::uint16_t groupMask,
176 const std::uint16_t payloadId, const std::uint8_t* payload,
177 const std::uint16_t payloadLength)
178 {
179 return this->template offerPayload<wire::Publish>(
180 groupMask, payloadId, payloadLength, [&](wire::Publish& command) {
181 command.commandSeq(0)
182 .connectionId(connectionId)
183 .groupMask(groupMask)
184 .payloadId(payloadId)
185 .putBody(reinterpret_cast<const char*>(payload), payloadLength);
186 });
187 }
188
189 private:
190 // The hooks of ClientLink.
191 void onGate(const bool leading)
192 {
193 m_serving = leading;
194 }
195
196 void onOther(const std::uint16_t templateId, char* base, const aeron::util::index_t length,
197 const std::uint16_t blockLength, const std::uint16_t version)
198 {
199 switch (templateId)
200 {
201 case wire::Role::sbeTemplateId():
202 onRole(wire::Role(base, detail::HEADER, length, blockLength, version));
203 break;
204 case wire::Connection::sbeTemplateId():
205 onConnection(wire::Connection(base, detail::HEADER, length, blockLength, version));
206 break;
207 default:
208 break;
209 }
210 }
211
212 // §17.5: answer onActivated with Activated and, once opened, serve at once under the epoch one above the last
213 // Gate's, the one the host's open edge takes (H-3). A Role that repeats, because the dial failed, asks again.
214 void onRole(wire::Role role)
215 {
216 if (role.active() != 0)
217 {
218 const std::int32_t first = static_cast<std::int32_t>(role.firstConnectionId());
219 const bool opened = this->m_listener.onActivated(first);
220 this->template sendUndeclinable<wire::Activated>(
221 [&](wire::Activated& activated) { activated.opened(opened ? 1 : 0); });
222 if (opened)
223 {
224 m_nextConnectionId = first;
225 m_serving = true;
226 this->m_canPublish = true;
227 this->m_epoch++;
228 }
229 return;
230 }
231 this->m_listener.onStandby();
232 m_serving = false;
233 this->m_canPublish = false;
234 }
235
236 void onConnection(wire::Connection connection)
237 {
238 const auto id = static_cast<std::int32_t>(connection.connectionId());
239 if (connection.opened() != 0)
240 {
241 if constexpr (requires(Listener& l, const char* data, std::size_t size) {
242 l.onConnectionOpened(std::int32_t{}, data, size);
243 })
244 {
245 const std::string_view data = connection.getDataAsStringView();
246 this->m_listener.onConnectionOpened(id, data.data(), data.size());
247 }
248 }
249 else if constexpr (requires { this->m_listener.onConnectionClosed(id); })
250 {
251 this->m_listener.onConnectionClosed(id);
252 }
253 }
254
255 Config m_config;
256 std::int32_t m_nextConnectionId = 0;
257 bool m_serving = false;
258};
259
260} // namespace org::limitless::seqeron::hosted
What a source that takes part in snapshot rounds serializes, and restores from (doc/snapshot....
Definition SnapshotListener.hpp:18
The client stub of a hosted Gateway (doc/seqeron-protocol-spec.md §17): the surface of app::Gateway,...
Definition Gateway.hpp:35
void gateClosed()
The edge closed without being asked to: the host opens it again through onActivated,...
Definition Gateway.hpp:135
std::int32_t openConnection(const char *connectionData, const std::size_t length)
Takes one connection into the cluster's view of this gateway, under the next id from the last Role (H...
Definition Gateway.hpp:91
void closeConnection(const std::int32_t connectionId)
That connection has gone.
Definition Gateway.hpp:121
static constexpr std::int32_t NO_CONNECTION
Definition Gateway.hpp:42
protocol::Publish publish(const std::int32_t connectionId, const std::uint16_t groupMask, const std::uint16_t payloadId, const std::uint8_t *payload, const std::uint16_t payloadLength)
The same to the groups groupMask names as well as its own.
Definition Gateway.hpp:175
protocol::Publish publish(const std::int32_t connectionId, const std::uint16_t payloadId, const std::uint8_t *payload, const std::uint16_t payloadLength)
Offers one payload on a connection to this gateway's own group (H-4).
Definition Gateway.hpp:159
std::int32_t openConnection()
Takes one connection whose only identity is its id; see openConnection(const char*,...
Definition Gateway.hpp:111
bool canAccept() const noexcept
Whether a connection may be taken right now: the host's last Gate, or this stub's own Activated(true)...
Definition Gateway.hpp:67
std::int32_t sourceId() const noexcept
Definition Gateway.hpp:78
bool isServing() const noexcept
Whether the edge is open: onActivated returned true and no Gate or Role has closed it since.
Definition Gateway.hpp:73
Gateway(Config config, Listener &listener)
Definition Gateway.hpp:57
What a Gateway's Listener provides: the edge.
Definition Listeners.hpp:49
Definition Application.hpp:12
Publish
Definition Publish.hpp:13
constexpr std::uint16_t MAX_PAYLOAD_LENGTH
Definition SequencedFrame.hpp:79
constexpr const char * PROXY_DEFAULT_HOST_NAME
Definition ProxyProtocol.hpp:11
std::int64_t attachTimeoutMs
Definition Gateway.hpp:52
app::SnapshotListener * snapshots
Definition Gateway.hpp:49
std::int64_t hostTimeoutMs
Definition Gateway.hpp:51
std::int32_t sourceId
Definition Gateway.hpp:47
std::string hostName
Definition Gateway.hpp:54