7#include "org/limitless/seqeron/hosted/detail/ClientLink.hpp"
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"
33template<
typename Listener>
34class Gateway :
public detail::ClientLink<Gateway<Listener>, Listener>
36 using Base = detail::ClientLink<Gateway<Listener>, Listener>;
58 Base{ listener, wire::Kind::GATEWAY, config.
sourceId,
59 config.snapshots, config.hostTimeoutMs, config.attachTimeoutMs,
69 return m_serving && this->m_canPublish;
78 [[nodiscard]] std::int32_t
sourceId() const noexcept
91 std::int32_t
openConnection(
const char* connectionData,
const std::size_t length)
97 const std::int32_t
id = m_nextConnectionId;
99 this->
template offerWindowed<wire::OpenConnection>(length, [&](wire::OpenConnection& open) {
100 open.connectionId(
id).putData(connectionData,
static_cast<std::uint32_t
>(length));
106 m_nextConnectionId++;
127 this->
template sendUndeclinable<wire::CloseConnection>(
128 [&](wire::CloseConnection& close) { close.epoch(this->m_epoch).connectionId(connectionId); });
141 this->
template sendUndeclinable<wire::GateClosed>(
142 [&](wire::GateClosed& closed) { closed.epoch(this->m_epoch); });
145 this->m_canPublish =
false;
160 const std::uint8_t* payload,
const std::uint16_t payloadLength)
162 return publish(connectionId, 0, payloadId, payload, payloadLength);
176 const std::uint16_t payloadId,
const std::uint8_t* payload,
177 const std::uint16_t payloadLength)
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);
191 void onGate(
const bool leading)
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)
201 case wire::Role::sbeTemplateId():
202 onRole(wire::Role(base, detail::HEADER, length, blockLength, version));
204 case wire::Connection::sbeTemplateId():
205 onConnection(wire::Connection(base, detail::HEADER, length, blockLength, version));
214 void onRole(wire::Role role)
216 if (role.active() != 0)
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); });
224 m_nextConnectionId = first;
226 this->m_canPublish =
true;
231 this->m_listener.onStandby();
233 this->m_canPublish =
false;
236 void onConnection(wire::Connection connection)
238 const auto id =
static_cast<std::int32_t
>(connection.connectionId());
239 if (connection.opened() != 0)
241 if constexpr (
requires(Listener& l,
const char* data, std::size_t size) {
242 l.onConnectionOpened(std::int32_t{}, data, size);
245 const std::string_view data = connection.getDataAsStringView();
246 this->m_listener.onConnectionOpened(
id, data.data(), data.size());
249 else if constexpr (
requires { this->m_listener.onConnectionClosed(
id); })
251 this->m_listener.onConnectionClosed(
id);
256 std::int32_t m_nextConnectionId = 0;
257 bool m_serving =
false;
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
Definition Gateway.hpp:45
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