|
Fray Engine
|
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 |
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.
| Key | Identifies a neighbor (ordered; for example ProcessKind). |
| Frame | The message type held in queues and passed to the deliver callback. |
Definition at line 37 of file outgoing_router.hpp.
| 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.
| 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.
| 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.
|
inlineexplicit |
Creates a router with no neighbors known.
| deliver | Called synchronously on the router thread to send a frame to a ready neighbor. |
| now | Time source; defaults to the steady clock. |
Definition at line 62 of file outgoing_router.hpp.
|
inline |
For a neighbor declared down or unreachable: what was waiting for it may no longer make sense, so it is dropped.
| neighbor | Neighbor whose queue is cleared. |
Definition at line 171 of file outgoing_router.hpp.
|
inline |
True when the neighbor's state is NeighborState::ready.
Definition at line 80 of file outgoing_router.hpp.
|
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.
| neighbor | Neighbor that is no longer ready. |
Definition at line 158 of file outgoing_router.hpp.
|
inline |
Marks the neighbor ready and delivers its waiting messages in order, skipping any that expired. Calls the deliver callback synchronously for each message.
| neighbor | Neighbor that became ready. |
Definition at line 134 of file outgoing_router.hpp.
|
inline |
Number of messages still waiting for the neighbor.
| neighbor | Neighbor to look up. |
Definition at line 104 of file outgoing_router.hpp.
|
inline |
Describes every non-empty queue (neighbor, depth, age of the oldest message).
Definition at line 115 of file outgoing_router.hpp.
|
inline |
Delivers now if the neighbor is ready, otherwise queues the frame. Never waits. Expired queued messages are swept first when queueing.
| neighbor | Destination neighbor. |
| frame | Message to send; moved into the queue when not delivered. |
| ttlMs | Milliseconds the message may wait in the queue; 0 means the message never expires. |
Definition at line 190 of file outgoing_router.hpp.
|
inline |
Records a neighbor's state. Setting ready goes through markReady, which also delivers waiting messages.
| neighbor | Neighbor to update. |
| value | New state. |
Definition at line 89 of file outgoing_router.hpp.
|
inline |
Current state of a neighbor.
| neighbor | Neighbor to look up. |
Definition at line 72 of file outgoing_router.hpp.
|
inline |
Drops every queued message whose time to live has run out; removes emptied queues.
Definition at line 213 of file outgoing_router.hpp.
|
private |
Sends a frame to a ready neighbor.
Definition at line 248 of file outgoing_router.hpp.
|
private |
Time source.
Definition at line 250 of file outgoing_router.hpp.
|
private |
Waiting messages per neighbor, oldest first.
Definition at line 254 of file outgoing_router.hpp.
|
private |
Known state per neighbor; a missing key means unknown.
Definition at line 252 of file outgoing_router.hpp.