libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
e3_connector.hpp
Go to the documentation of this file.
1/**
2 * @file e3_connector.hpp
3 * @brief Abstract E3 Connector interface for transport layer
4 *
5 * Defines the abstract interface that all transport connectors must implement.
6 * Ported from the original C implementation's E3Connector structure.
7 *
8 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
9 * SPDX-License-Identifier: Apache-2.0
10 */
11
12#ifndef LIBE3_E3_CONNECTOR_HPP
13#define LIBE3_E3_CONNECTOR_HPP
14
15#include "types.hpp"
16#include <cstdint>
17#include <memory>
18#include <string>
19#include <functional>
20#include <vector>
21
22namespace libe3 {
23
24/**
25 * @brief What a PDU is for, so that a link that has streams can pick one
26 *
27 * A connector does not decode the PDUs it carries. The caller says which kind
28 * a PDU is, and a link with more than one stream (SCTP) sends indications on
29 * one and everything else on the other.
30 */
31enum class PduClass : uint8_t {
32 PROCEDURE = 0, ///< A request, a response, a control, a release: anything but an indication
33 INDICATION = 1 ///< An indication message
34};
35
36/**
37 * @brief Socket and admission options of the POSIX link
38 *
39 * The ZeroMQ link ignores all of them. The defaults are those of E3Config.
40 */
42 /// SO_SNDBUF and SO_RCVBUF of SCTP sockets, set before the association exists; 0 keeps the kernel's
43 size_t socket_buffer_bytes{2 * 1024 * 1024};
44 /// SCTP delayed SACK; false turns it off, on both peers, as OCUDU does
45 bool sctp_delayed_sack{false};
46 /// RAN role: literal addresses a peer may connect from (TCP and SCTP); empty accepts any
47 std::vector<std::string> allowed_peers;
48 /// RAN role: most peers connected at once; 0 is unlimited
50};
51
52/**
53 * @brief Abstract base class for E3AP transport connectors
54 *
55 * This class defines the transport-agnostic interface that all connectors
56 * must implement. The design mirrors the original C implementation's
57 * function pointer approach, but uses virtual methods for type safety.
58 *
59 * Derived classes:
60 * - PosixE3Connector: POSIX socket-based transport (TCP, SCTP, IPC); one
61 * endpoint per dApp carries the setup, inbound and outbound traffic
62 * - ZmqE3Connector: ZeroMQ-based transport
63 */
65public:
66 virtual ~E3Connector() = default;
67
68 // Non-copyable, movable
69 E3Connector(const E3Connector&) = delete;
71 /** @brief Move constructor. */
73 /**
74 * @brief Move assignment.
75 * @return This connector
76 */
78
79 /**
80 * @brief Set up the initial connection (setup channel)
81 *
82 * This establishes the REQ/REP channel used for E3 Setup procedures.
83 * @return ErrorCode::SUCCESS on success, error code otherwise
84 */
86
87 /**
88 * @brief Receive a setup request
89 *
90 * Blocks until a setup request is received on the setup channel.
91 * @param buffer Buffer to store received data
92 * @return Number of bytes received, or negative error code
93 */
94 virtual int recv_setup_request(std::vector<uint8_t>& buffer) = 0;
95
96 /**
97 * @brief Send a setup response
98 * @param data Response data to send
99 * @return ErrorCode::SUCCESS on success, error code otherwise
100 */
101 virtual ErrorCode send_response(const std::vector<uint8_t>& data) = 0;
102
103 /**
104 * @brief Set up the inbound connection (SUB channel)
105 *
106 * This establishes the channel for receiving control actions from dApps.
107 * @return ErrorCode::SUCCESS on success, error code otherwise
108 */
110
111 /**
112 * @brief Receive data from the inbound channel
113 * @param buffer Buffer to store received data
114 * @return Number of bytes received; 0 on timeout (nothing yet);
115 * ErrorCode::NOT_CONNECTED once the peer has closed (dApp role);
116 * any other negative ErrorCode on a transport error
117 */
118 virtual int receive(std::vector<uint8_t>& buffer) = 0;
119
120 /**
121 * @brief Set up the outbound connection (PUB channel)
122 *
123 * This establishes the channel for sending indications to dApps.
124 * @return ErrorCode::SUCCESS on success, error code otherwise
125 */
127
128 /**
129 * @brief Send data on the outbound channel
130 * @param data Data to send
131 * @return ErrorCode::SUCCESS on success, error code otherwise
132 */
133 virtual ErrorCode send(const std::vector<uint8_t>& data) = 0;
134
135 /**
136 * @brief Send data to one peer, or to all of them (RAN role)
137 *
138 * The default broadcasts with send(), which is what a connector that cannot
139 * tell its peers apart does.
140 *
141 * @param peer_token The peer named by last_peer_token() or last_setup_peer_token(); 0 sends to every peer
142 * @param data Data to send
143 * @param kind What the PDU is for
144 * @return ErrorCode::SUCCESS on success, an error code otherwise
145 */
146 virtual ErrorCode send_to(uint64_t peer_token, const std::vector<uint8_t>& data, PduClass kind) {
147 (void)peer_token;
148 (void)kind;
149 return send(data);
150 }
151
152 // =========================================================================
153 // Client-side (dApp role) operations
154 //
155 // Mirrors of the server-side methods above but with bind/listen replaced
156 // by connect. Default implementations return INVALID_PARAM so connectors
157 // that have not been taught the dApp role surface a clear error instead
158 // of silently misbehaving. ZmqE3Connector and PosixE3Connector override
159 // these.
160 // =========================================================================
161
162 /**
163 * @brief Connect the setup channel (dApp role: REQ connect).
164 * @return ErrorCode::SUCCESS on success, ErrorCode::INVALID_PARAM if the connector has no dApp role
165 */
167 return ErrorCode::INVALID_PARAM;
168 }
169
170 /**
171 * @brief Send a SetupRequest on the setup channel (dApp role).
172 * @return ErrorCode::SUCCESS on success, an error code otherwise
173 */
174 virtual ErrorCode send_setup_request_client(const std::vector<uint8_t>& /*data*/) {
175 return ErrorCode::INVALID_PARAM;
176 }
177
178 /**
179 * @brief Receive a SetupResponse on the setup channel (dApp role).
180 * @return Number of bytes received, 0 on timeout, a negative ErrorCode on error
181 */
182 virtual int recv_setup_response_client(std::vector<uint8_t>& /*buffer*/) {
183 return static_cast<int>(ErrorCode::INVALID_PARAM);
184 }
185
186 /**
187 * @brief Connect the inbound channel (dApp role: SUB connect to the
188 * RAN's publisher endpoint).
189 * @return ErrorCode::SUCCESS on success, an error code otherwise
190 */
192 return ErrorCode::INVALID_PARAM;
193 }
194
195 /**
196 * @brief Connect the outbound channel (dApp role: PUB connect to the
197 * RAN's subscriber endpoint).
198 * @return ErrorCode::SUCCESS on success, an error code otherwise
199 */
201 return ErrorCode::INVALID_PARAM;
202 }
203
204 /**
205 * @brief Clean up and release all resources
206 */
207 virtual void dispose() = 0;
208
209 /**
210 * @brief Interrupt blocking operations for shutdown
211 *
212 * Called before joining threads to unblock any blocking socket operations.
213 * Default implementation does nothing (for connectors that use timeouts).
214 */
215 virtual void shutdown() {}
216
217 /**
218 * @brief Which peer supplied the bytes the last receive() returned (RAN role)
219 *
220 * Call it on the thread that calls receive(), right after a receive() that
221 * returned bytes. A token names one accepted connection and is never reused
222 * by the same connector, so a closed peer's token cannot name a later one.
223 *
224 * @return The peer's token, or 0 for a connector that cannot tell its
225 * peers apart (the default, and ZeroMQ)
226 */
227 virtual uint64_t last_peer_token() const { return 0; }
228
229 /**
230 * @brief Which peer sent the setup request the last recv_setup_request() returned (RAN role)
231 *
232 * Call it on the thread that calls recv_setup_request(), right after a call
233 * that returned bytes. The token is the one last_peer_token() gives for the
234 * same peer's later bytes, so a RAN can tie a dApp to its connection at setup.
235 *
236 * @return The peer's token, or 0 for a connector that cannot tell its peers
237 * apart (the default, and ZeroMQ)
238 */
239 virtual uint64_t last_setup_peer_token() const { return 0; }
240
241 /**
242 * @brief Peers whose connection has closed since the last call (RAN role)
243 *
244 * Call it on the thread that calls receive(). Each token is returned once,
245 * and only after the bytes that peer sent before closing were delivered.
246 * The default returns nothing: a connector that cannot tell its peers apart
247 * cannot tell that one left.
248 *
249 * @return The tokens of the peers that closed, oldest first
250 */
251 virtual std::vector<uint64_t> take_closed_peers() { return {}; }
252
253 /**
254 * @brief Get the link layer type
255 * @return The link layer this connector runs on
256 */
257 virtual E3LinkLayer link_layer() const noexcept = 0;
258
259 /**
260 * @brief Get the transport layer type
261 * @return The transport layer this connector runs on
262 */
263 virtual E3TransportLayer transport_layer() const noexcept = 0;
264
265 /**
266 * @brief Check if connector is connected
267 * @return true once the connector has set up its connections
268 */
269 virtual bool is_connected() const noexcept = 0;
270
271 /**
272 * @brief Set role (RAN or DAPP). Affects bind vs connect direction and
273 * cleanup behaviour. Must be set before any setup_* call. Default RAN.
274 * @param role The role of this peer
275 */
276 void set_role(E3Role role) noexcept { role_ = role; }
277
278 /**
279 * @brief Get current role.
280 * @return The role of this peer
281 */
282 E3Role role() const noexcept { return role_; }
283
284 /**
285 * @brief Get setup endpoint
286 * @return The endpoint of the setup channel
287 */
288 const std::string& setup_endpoint() const noexcept { return setup_endpoint_; }
289
290 /**
291 * @brief Get inbound endpoint
292 * @return The endpoint of the inbound channel
293 */
294 const std::string& inbound_endpoint() const noexcept { return inbound_endpoint_; }
295
296 /**
297 * @brief Get outbound endpoint
298 * @return The endpoint of the outbound channel
299 */
300 const std::string& outbound_endpoint() const noexcept { return outbound_endpoint_; }
301
302protected:
303 E3Connector() = default;
304
305 std::string setup_endpoint_; ///< Endpoint of the setup channel
306 std::string inbound_endpoint_; ///< Endpoint of the inbound channel
307 std::string outbound_endpoint_; ///< Endpoint of the outbound channel
308 E3Role role_{E3Role::RAN}; ///< Role of this peer
309};
310
311/**
312 * @brief Factory function to create appropriate connector
313 *
314 * Creates a connector instance based on the link and transport layers specified.
315 *
316 * @param link_layer Link layer to use (ZMQ or POSIX)
317 * @param transport_layer Transport layer to use (SCTP, TCP, or IPC)
318 * @param setup_endpoint Endpoint for setup channel
319 * @param inbound_endpoint Endpoint for inbound channel
320 * @param outbound_endpoint Endpoint for outbound channel
321 * @param setup_port Port of the setup channel (SCTP and TCP; IPC uses the endpoint path)
322 * @param inbound_port Port of the inbound channel (SCTP and TCP)
323 * @param outbound_port Port of the outbound channel (SCTP and TCP)
324 * @param io_threads Number of I/O threads (for ZMQ)
325 * @param role Role of the peer (RAN or dApp)
326 * @param posix Socket and admission options of the POSIX link (ignored by ZMQ)
327 * @return Unique pointer to created connector, nullptr on failure
328 *
329 * The POSIX link serves everything on the setup endpoint and port, and ignores
330 * the inbound and outbound ones.
331 */
332std::unique_ptr<E3Connector> create_connector(
333 E3LinkLayer link_layer,
334 E3TransportLayer transport_layer,
335 const std::string& setup_endpoint,
336 const std::string& inbound_endpoint,
337 const std::string& outbound_endpoint,
338 uint16_t setup_port,
339 uint16_t inbound_port,
340 uint16_t outbound_port,
341 size_t io_threads = 2,
342 E3Role role = E3Role::RAN,
343 const PosixOptions& posix = PosixOptions{}
344);
345
346} // namespace libe3
347
348#endif // LIBE3_E3_CONNECTOR_HPP
virtual ErrorCode setup_inbound_connection()=0
Set up the inbound connection (SUB channel).
virtual ~E3Connector()=default
virtual int recv_setup_request(std::vector< uint8_t > &buffer)=0
Receive a setup request.
E3Connector()=default
void set_role(E3Role role) noexcept
Set role (RAN or DAPP). Affects bind vs connect direction and cleanup behaviour. Must be set before a...
E3Role role_
Role of this peer.
std::string inbound_endpoint_
Endpoint of the inbound channel.
std::string outbound_endpoint_
Endpoint of the outbound channel.
virtual bool is_connected() const noexcept=0
Check if connector is connected.
std::string setup_endpoint_
Endpoint of the setup channel.
const std::string & inbound_endpoint() const noexcept
Get inbound endpoint.
E3Connector & operator=(E3Connector &&)=default
Move assignment.
virtual ErrorCode setup_outbound_connection()=0
Set up the outbound connection (PUB channel).
const std::string & setup_endpoint() const noexcept
Get setup endpoint.
virtual ErrorCode send_to(uint64_t peer_token, const std::vector< uint8_t > &data, PduClass kind)
Send data to one peer, or to all of them (RAN role).
virtual void shutdown()
Interrupt blocking operations for shutdown.
virtual uint64_t last_peer_token() const
Which peer supplied the bytes the last receive() returned (RAN role).
virtual uint64_t last_setup_peer_token() const
Which peer sent the setup request the last recv_setup_request() returned (RAN role).
virtual void dispose()=0
Clean up and release all resources.
virtual E3LinkLayer link_layer() const noexcept=0
Get the link layer type.
virtual ErrorCode send_response(const std::vector< uint8_t > &data)=0
Send a setup response.
virtual int receive(std::vector< uint8_t > &buffer)=0
Receive data from the inbound channel.
virtual E3TransportLayer transport_layer() const noexcept=0
Get the transport layer type.
const std::string & outbound_endpoint() const noexcept
Get outbound endpoint.
E3Connector(E3Connector &&)=default
Move constructor.
virtual ErrorCode setup_initial_connection_client()
Connect the setup channel (dApp role: REQ connect).
virtual std::vector< uint64_t > take_closed_peers()
Peers whose connection has closed since the last call (RAN role).
E3Connector & operator=(const E3Connector &)=delete
virtual int recv_setup_response_client(std::vector< uint8_t > &)
Receive a SetupResponse on the setup channel (dApp role).
E3Connector(const E3Connector &)=delete
virtual ErrorCode send(const std::vector< uint8_t > &data)=0
Send data on the outbound channel.
virtual ErrorCode setup_initial_connection()=0
Set up the initial connection (setup channel).
virtual ErrorCode setup_outbound_connection_client()
Connect the outbound channel (dApp role: PUB connect to the RAN's subscriber endpoint).
E3Role role() const noexcept
Get current role.
virtual ErrorCode setup_inbound_connection_client()
Connect the inbound channel (dApp role: SUB connect to the RAN's publisher endpoint).
virtual ErrorCode send_setup_request_client(const std::vector< uint8_t > &)
Send a SetupRequest on the setup channel (dApp role).
std::unique_ptr< E3Connector > create_connector(E3LinkLayer link_layer, E3TransportLayer transport_layer, const std::string &setup_endpoint, const std::string &inbound_endpoint, const std::string &outbound_endpoint, uint16_t setup_port, uint16_t inbound_port, uint16_t outbound_port, size_t io_threads=2, E3Role role=E3Role::RAN, const PosixOptions &posix=PosixOptions{})
Factory function to create appropriate connector.
ErrorCode
Error codes returned by libe3 operations Use shared error-code list (see libe3/error_codes....
Definition types.hpp:177
E3TransportLayer
Transport layer types (matches Python E3TransportLayer).
Definition types.hpp:85
E3Role
Role this E3 endpoint plays in the protocol.
Definition types.hpp:77
@ RAN
RAN function (binds sockets; today's only behaviour).
Definition types.hpp:78
E3LinkLayer
Link layer types (matches Python E3LinkLayer).
Definition types.hpp:62
PduClass
What a PDU is for, so that a link that has streams can pick one.
@ PROCEDURE
A request, a response, a control, a release: anything but an indication.
@ INDICATION
An indication message.
Socket and admission options of the POSIX link.
bool sctp_delayed_sack
SCTP delayed SACK; false turns it off, on both peers, as OCUDU does.
size_t maximum_associations
RAN role: most peers connected at once; 0 is unlimited.
size_t socket_buffer_bytes
SO_SNDBUF and SO_RCVBUF of SCTP sockets, set before the association exists; 0 keeps the kernel's.
std::vector< std::string > allowed_peers
RAN role: literal addresses a peer may connect from (TCP and SCTP); empty accepts any.
E3AP Protocol Types - Vendor-neutral type definitions.