Fray Engine
Loading...
Searching...
No Matches
fray::routing::OutgoingRouter< Key, Frame > Class Template Reference

The router thread's single source of truth about which neighbors are ready, plus the messages waiting for them. Nothing here is thread safe: only the router thread may touch it. Nothing waits on a neighbor either. A message for a neighbor that is not ready stays in its queue until the neighbor says it is ready, its time to live runs out, or the neighbor is declared down and its queue is discarded. More...

#include <outgoing_router.hpp>

Classes

struct  QueueHealth
struct  Entry

Public Types

using Clock = std::chrono::steady_clock
using Deliver = std::function<void(Key, const Frame &)>
using Now = std::function<Clock::time_point()>

Public Member Functions

 OutgoingRouter (Deliver deliver, Now now=[] { return Clock::now();})
 Creates a router with no neighbors known.
NeighborState state (Key neighbor) const
 Current state of a neighbor.
bool isReady (Key neighbor) const
 True when the neighbor's state is NeighborState::ready.
bool setState (Key neighbor, NeighborState value)
 Records a neighbor's state. Setting ready goes through markReady, which also delivers waiting messages.
size_t queued (Key neighbor) const
 Number of messages still waiting for the neighbor.
std::vector< QueueHealth > queueHealth () const
 Describes every non-empty queue (neighbor, depth, age of the oldest message).
bool markReady (Key neighbor)
 Marks the neighbor ready and delivers its waiting messages in order, skipping any that expired. Calls the deliver callback synchronously for each message.
bool markNotReady (Key neighbor)
 The neighbor can no longer be reached for now. Messages sent to it wait in its queue again. Forgets its state (it becomes unknown); the queue is kept.
size_t discardQueue (Key neighbor)
 For a neighbor declared down or unreachable: what was waiting for it may no longer make sense, so it is dropped.
bool send (Key neighbor, Frame frame, uint32_t ttlMs)
 Delivers now if the neighbor is ready, otherwise queues the frame. Never waits. Expired queued messages are swept first when queueing.
size_t sweep ()
 Drops every queued message whose time to live has run out; removes emptied queues.

Private Attributes

Deliver _deliver
Now _now
std::map< Key, NeighborState > _states
std::map< Key, std::deque< Entry > > _queues

Detailed Description

template<class Key, class Frame>
class fray::routing::OutgoingRouter< Key, Frame >

The router thread's single source of truth about which neighbors are ready, plus the messages waiting for them. Nothing here is thread safe: only the router thread may touch it. Nothing waits on a neighbor either. A message for a neighbor that is not ready stays in its queue until the neighbor says it is ready, its time to live runs out, or the neighbor is declared down and its queue is discarded.

Template Parameters
KeyIdentifies a neighbor (ordered; for example ProcessKind).
FrameThe message type held in queues and passed to the deliver callback.

Definition at line 37 of file outgoing_router.hpp.

Member Typedef Documentation

◆ Clock

template<class Key, class Frame>
using fray::routing::OutgoingRouter< Key, Frame >::Clock = std::chrono::steady_clock

Monotonic clock used for message ages and expiry.

Definition at line 41 of file outgoing_router.hpp.

◆ Deliver

template<class Key, class Frame>
using fray::routing::OutgoingRouter< Key, Frame >::Deliver = std::function<void(Key, const Frame &)>

Callback that actually sends a frame to a ready neighbor; receives the neighbor and the frame.

Definition at line 43 of file outgoing_router.hpp.

◆ Now

template<class Key, class Frame>
using fray::routing::OutgoingRouter< Key, Frame >::Now = std::function<Clock::time_point()>

Callback returning the current time; replaceable for tests.

Definition at line 45 of file outgoing_router.hpp.

Constructor & Destructor Documentation

◆ OutgoingRouter()

template<class Key, class Frame>
fray::routing::OutgoingRouter< Key, Frame >::OutgoingRouter ( Deliver deliver,
Now now = [] { return Clock::now(); } )
inlineexplicit

Creates a router with no neighbors known.

Parameters
deliverCalled synchronously on the router thread to send a frame to a ready neighbor.
nowTime source; defaults to the steady clock.

Definition at line 62 of file outgoing_router.hpp.

Member Function Documentation

◆ discardQueue()

template<class Key, class Frame>
size_t fray::routing::OutgoingRouter< Key, Frame >::discardQueue ( Key neighbor)
inline

For a neighbor declared down or unreachable: what was waiting for it may no longer make sense, so it is dropped.

Parameters
neighborNeighbor whose queue is cleared.
Returns
How many messages were dropped.

Definition at line 171 of file outgoing_router.hpp.

◆ isReady()

template<class Key, class Frame>
bool fray::routing::OutgoingRouter< Key, Frame >::isReady ( Key neighbor) const
inline

True when the neighbor's state is NeighborState::ready.

Definition at line 80 of file outgoing_router.hpp.

◆ markNotReady()

template<class Key, class Frame>
bool fray::routing::OutgoingRouter< Key, Frame >::markNotReady ( Key neighbor)
inline

The neighbor can no longer be reached for now. Messages sent to it wait in its queue again. Forgets its state (it becomes unknown); the queue is kept.

Parameters
neighborNeighbor that is no longer ready.
Returns
true when it was ready before.

Definition at line 158 of file outgoing_router.hpp.

◆ markReady()

template<class Key, class Frame>
bool fray::routing::OutgoingRouter< Key, Frame >::markReady ( Key neighbor)
inline

Marks the neighbor ready and delivers its waiting messages in order, skipping any that expired. Calls the deliver callback synchronously for each message.

Parameters
neighborNeighbor that became ready.
Returns
true only when the neighbor was not ready before, so callers act once per transition.

Definition at line 134 of file outgoing_router.hpp.

◆ queued()

template<class Key, class Frame>
size_t fray::routing::OutgoingRouter< Key, Frame >::queued ( Key neighbor) const
inline

Number of messages still waiting for the neighbor.

Parameters
neighborNeighbor to look up.
Returns
The queue length, 0 if none.

Definition at line 104 of file outgoing_router.hpp.

◆ queueHealth()

template<class Key, class Frame>
std::vector< QueueHealth > fray::routing::OutgoingRouter< Key, Frame >::queueHealth ( ) const
inline

Describes every non-empty queue (neighbor, depth, age of the oldest message).

Returns
One QueueHealth per neighbor with waiting messages.

Definition at line 115 of file outgoing_router.hpp.

◆ send()

template<class Key, class Frame>
bool fray::routing::OutgoingRouter< Key, Frame >::send ( Key neighbor,
Frame frame,
uint32_t ttlMs )
inline

Delivers now if the neighbor is ready, otherwise queues the frame. Never waits. Expired queued messages are swept first when queueing.

Parameters
neighborDestination neighbor.
frameMessage to send; moved into the queue when not delivered.
ttlMsMilliseconds the message may wait in the queue; 0 means the message never expires.
Returns
true when the message was delivered immediately; false when it was queued.

Definition at line 190 of file outgoing_router.hpp.

◆ setState()

template<class Key, class Frame>
bool fray::routing::OutgoingRouter< Key, Frame >::setState ( Key neighbor,
NeighborState value )
inline

Records a neighbor's state. Setting ready goes through markReady, which also delivers waiting messages.

Parameters
neighborNeighbor to update.
valueNew state.
Returns
true if the state changed (for ready: it was not ready before); false if unchanged.

Definition at line 89 of file outgoing_router.hpp.

◆ state()

template<class Key, class Frame>
NeighborState fray::routing::OutgoingRouter< Key, Frame >::state ( Key neighbor) const
inline

Current state of a neighbor.

Parameters
neighborNeighbor to look up.
Returns
Its state, or NeighborState::unknown if none was recorded.

Definition at line 72 of file outgoing_router.hpp.

◆ sweep()

template<class Key, class Frame>
size_t fray::routing::OutgoingRouter< Key, Frame >::sweep ( )
inline

Drops every queued message whose time to live has run out; removes emptied queues.

Returns
How many were dropped.

Definition at line 213 of file outgoing_router.hpp.

Member Data Documentation

◆ _deliver

template<class Key, class Frame>
Deliver fray::routing::OutgoingRouter< Key, Frame >::_deliver
private

Sends a frame to a ready neighbor.

Definition at line 248 of file outgoing_router.hpp.

◆ _now

template<class Key, class Frame>
Now fray::routing::OutgoingRouter< Key, Frame >::_now
private

Time source.

Definition at line 250 of file outgoing_router.hpp.

◆ _queues

template<class Key, class Frame>
std::map<Key, std::deque<Entry> > fray::routing::OutgoingRouter< Key, Frame >::_queues
private

Waiting messages per neighbor, oldest first.

Definition at line 254 of file outgoing_router.hpp.

◆ _states

template<class Key, class Frame>
std::map<Key, NeighborState> fray::routing::OutgoingRouter< Key, Frame >::_states
private

Known state per neighbor; a missing key means unknown.

Definition at line 252 of file outgoing_router.hpp.


The documentation for this class was generated from the following file: