|
Open Model Railroad Network (OpenMRN)
|
This class takes registrations of StateFlows for incoming messages. More...
#include <Dispatcher.hxx>
Classes | |
| struct | HandlerInfo |
| Internal information we store about each registered handler: identifier, mask, handler pointer. More... | |
Public Types | |
| typedef StateFlowBase::Action | Action |
| Allows using Action without having StateFlowBase:: prefix in front of it. | |
Public Member Functions | |
| DispatchFlowBase (Service *service) | |
| Construct a dispatchflow. | |
| size_t | size () |
Public Member Functions inherited from UntypedStateFlow< QList< NUM_PRIO > > | |
| UntypedStateFlow (Service *service) | |
| Constructor. | |
Public Member Functions inherited from StateFlowWithQueue | |
| void | notify () override |
| Wakeup call arrived. Schedules *this on the executor. | |
| bool | is_waiting () |
Public Member Functions inherited from StateFlowBase | |
| void | run () override |
| Callback from the executor. | |
| Service * | service () |
| Return a pointer to the service I am bound to. | |
Public Member Functions inherited from Executable | |
| void | test_deletion () |
Public Member Functions inherited from QMember | |
| void | init () |
| Initiailize a QMember, in place of a public placement construction. | |
Public Member Functions inherited from LinkedObject< StateFlowWithQueue > | |
| StateFlowWithQueue * | link_next () |
Protected Types | |
| typedef uint32_t | ID |
| Proxy the identifier type for customers to use. | |
| typedef void | UntypedHandler |
| Generic handler type. | |
Protected Types inherited from UntypedStateFlow< QList< NUM_PRIO > > | |
| typedef Action(StateFlowBase::* | Callback) () |
| State Flow callback prototype. | |
Protected Types inherited from StateFlowBase | |
| typedef Action(StateFlowBase::* | Callback) () |
| State Flow callback prototype. | |
Protected Member Functions | |
| void | register_handler (UntypedHandler *handler, ID id, ID mask) |
| Adds a new handler to this dispatcher. | |
| void | unregister_handler (UntypedHandler *handler, ID id, ID mask) |
| Removes a specific instance of a handler from this dispatcher. | |
| void | unregister_handler_all (UntypedHandler *handler) |
| Removes all instances of a handler from this dispatcher. | |
| void | register_fallback_handler (UntypedHandler *handler) |
| Sets one handler to receive all messages that no other handler has matched. | |
| virtual ID | get_message_id ()=0 |
| Returns the current message's ID. | |
| virtual Action | allocate_and_clone ()=0 |
| Allocates an entry from lastHandlerToCall_, invoking clone() when done: return allocate_and_call(lastHandlerToCall_, STATE(clone));. | |
| virtual void | send_transfer ()=0 |
| Sends the current message to lastHandlerToCall, transferring ownership. | |
| Action | entry () override |
| Entry into the StateFlow activity. | |
| Action | iterate () |
| Iterate on potential handlers, matching the ID of the incoming message to the handlers' masks. | |
| Action | clone_done () |
| State after a clone-and-send operation is complete. | |
| Action | iteration_done () |
| State when the entire iteration is done. | |
| Action | call_immediately (Callback c) |
| Imediately call the next state upon return. | |
| Action | again () |
| Call the current state again via call_immediately. | |
| Action | release_and_exit () |
| Terminates the processing of the current message. | |
Protected Member Functions inherited from UntypedStateFlow< QList< NUM_PRIO > > | |
| void | send (BufferBase *msg, unsigned priority=UINT_MAX) |
| Sends a message to the state flow for processing. | |
| QMember * | queue_next (unsigned *priority) OVERRIDE |
| Takes the front entry in the queue. | |
| bool | queue_empty () OVERRIDE |
| Action | call_immediately (Callback c) |
| Imediately call the next state upon return. | |
Protected Member Functions inherited from StateFlowWithQueue | |
| StateFlowWithQueue (Service *service) | |
| Constructor. | |
| virtual void | release ()=0 |
| Releases the current message buffer back to the pool it came from. | |
| Action | exit () |
| Terminates the processing of this flow. | |
| Action | release_and_exit () |
| Terminates the processing of the current message. | |
| BufferBase * | message () |
| BufferBase * | transfer_message () |
| Releases ownership of the current message. | |
| void | reset_message (BufferBase *message, unsigned priority) |
| Sets the current message being processed. | |
| unsigned | priority () |
| void | set_priority (unsigned priority) |
| Overrides the current priority. | |
| void | start_flow_at_init (Callback c) |
| Call this from the constructor of the child class to do some work before the main queue processing loop begins. | |
Protected Member Functions inherited from StateFlowBase | |
| StateFlowBase (Service *service) | |
| Constructor. | |
| ~StateFlowBase () | |
| Destructor. | |
| void | reset_flow (Callback c) |
| Resets the flow to the specified state. | |
| bool | is_state (Callback c) |
| bool | is_terminated () |
| void | start_flow (Callback c) |
| Resets the flow to the specified state and starts it. | |
| Action | again () |
| Call the current state again via call_immediately. | |
| Action | exit () |
| Terminate current StateFlow activity. | |
| Action | delete_this () |
| Terminates the flow and deletes *this. | |
| Action | set_terminated () |
| Sets the flow to terminated state. | |
| Action | call_immediately (Callback c) |
| Imediately call the next state upon return. | |
| Action | wait () |
| Wait for an asynchronous call. | |
| Action | wait_and_call (Callback c) |
| Wait for resource to become available before proceeding to next state. | |
| template<class T > | |
| Action | allocate_and_call (FlowInterface< Buffer< T > > *target_flow, Callback c, Pool *pool=nullptr) |
| Allocates a buffer from a pool and proceed to the next state when allocation is successful. | |
| Action | allocate_and_call (Callback c, QAsync *queue) |
| Allocates an entry from an asynchronous queue, and transitions to a state once the allocation is complete. | |
| template<class T > | |
| Buffer< T > * | full_allocation_result (FlowInterface< Buffer< T > > *target_flow) |
| Takes the result of the asynchronous allocation without resetting the object. | |
| template<class T > | |
| T * | full_allocation_result (TypedQAsync< T > *queue) |
| Takes the result of the asynchronous allocation without resetting the object. | |
| template<class T > | |
| void | cast_allocation_result (T **member) |
| Takes the result of the asynchronous allocation without resetting the object. | |
| template<class T > | |
| Buffer< T > * | get_allocation_result (FlowInterface< Buffer< T > > *target_flow) |
| Takes the result of the asynchronous allocation. | |
| Action | yield_and_call (Callback c) |
| Place the current flow to the back of the executor, and transition to a new state after we get the CPU again. | |
| Action | yield () |
| Place the current flow to the back of the executor, and re-try the current state after we get the CPU again. | |
| Action | sleep_and_call (::Timer *timer, long long timeout_nsec, Callback c) |
| Suspends execution of this control flow for a specified time. | |
| template<class T , typename... Args> | |
| Action | invoke_subflow_and_wait (FlowInterface< Buffer< T > > *target_flow, Callback c, Args &&... args) |
| Calls a helper flow to perform some actions. | |
| Action | read_repeated (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO) |
| Blocks until size bytes are read and then invokes the next state. | |
| Action | read_single (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO) |
| Attempts to read at most size_t bytes, and blocks the caller until at least one byte is read. | |
| Action | read_nonblocking (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO) |
| Attempts to read at most size bytes, and then invokes the next state, even if only zero bytes are available right now. | |
| Action | read_repeated_with_timeout (StateFlowTimedSelectHelper *helper, long long timeout_nsec, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO) |
| Blocks until size bytes are read, or a timeout expires. | |
| Action | internal_try_read () |
| Implementation state that gets repeatedly called upon every wakeup and tries to make progress on reading. | |
| Action | write_repeated (StateFlowSelectHelper *helper, int fd, const void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO) |
| Writes some data into a file descriptor, repeating the operation as necessary until all bytes are written. | |
| Action | internal_try_write () |
| Implementation state that gets repeatedly called upon every wakeup and tries to make progress on writing. | |
Protected Member Functions inherited from QMember | |
| QMember () | |
| Constructor. | |
| ~QMember () | |
| Destructor. | |
Protected Member Functions inherited from Atomic | |
| void | lock () |
| void | unlock () |
Protected Member Functions inherited from LinkedObject< StateFlowWithQueue > | |
| StateFlowWithQueue * | link_this () |
| LinkedObject () | |
| Constructor. Puts *this on the linked list. | |
| ~LinkedObject () | |
| Constructor. Removes *this from the linked list. | |
| StateFlowWithQueue * | link_next () |
Protected Attributes | |
| UntypedHandler * | lastHandlerToCall_ {nullptr} |
| If non-NULL we still need to call this handler. | |
| UntypedHandler * | fallbackHandler_ {nullptr} |
| Handler to give all messages that were not matched by any other handler registration. | |
Protected Attributes inherited from QMember | |
| QMember * | next |
| pointer to the next member in the queue | |
Protected Attributes inherited from LinkedObject< StateFlowWithQueue > | |
| StateFlowWithQueue * | link_ |
| Linked list pointer. | |
Private Attributes | |
| bool | negateMatch_ |
| true if this flow should negate the match condition. | |
| vector< HandlerInfo > | handlers_ |
| Registered handlers. | |
| size_t | currentIndex_ |
| Index of the next handler to look at. | |
| OSMutex | lock_ |
| Protects handler add / remove against iteration. | |
Friends | |
| template<class T > | |
| class | GenericHubFlow |
Additional Inherited Members | |
Static Public Member Functions inherited from StateFlowBase | |
| template<class T , typename... Args> | |
| static void | invoke_subflow_and_ignore_result (FlowInterface< Buffer< T > > *target_flow, Args &&... args) |
| Calls a helper flow to perform some actions. | |
Static Public Member Functions inherited from LinkedObject< StateFlowWithQueue > | |
| static StateFlowWithQueue * | link_head () |
| static Atomic * | head_mu () |
| Locks the list for modification (at any entry!). | |
Static Protected Member Functions inherited from LinkedObject< StateFlowWithQueue > | |
| static StateFlowWithQueue * | link_head () |
| static Atomic * | head_mu () |
| Locks the list for modification (at any entry!). | |
Static Protected Attributes inherited from LinkedObject< StateFlowWithQueue > | |
| static StateFlowWithQueue * | head_ |
| Beginning of the list. | |
This class takes registrations of StateFlows for incoming messages.
When a message shows up, all the Flows that match that message will be invoked.
Handlers are called in no particular order.
Definition at line 51 of file Dispatcher.hxx.
| typedef StateFlowBase::Action DispatchFlowBase< NUM_PRIO >::Action |
Allows using Action without having StateFlowBase:: prefix in front of it.
Definition at line 61 of file Dispatcher.hxx.
|
protected |
Proxy the identifier type for customers to use.
Definition at line 70 of file Dispatcher.hxx.
|
protected |
Generic handler type.
Will be cast to specific handlers in the specific DispatchFlow<...> template.
Definition at line 73 of file Dispatcher.hxx.
| DispatchFlowBase< NUM_PRIO >::DispatchFlowBase | ( | Service * | service | ) |
Construct a dispatchflow.
| service | the Service this flow belongs to. |
Definition at line 329 of file Dispatcher.hxx.
| DispatchFlowBase< NUM_PRIO >::~DispatchFlowBase | ( | ) |
Definition at line 337 of file Dispatcher.hxx.
|
inlineprotected |
Call the current state again via call_immediately.
Definition at line 280 of file StateFlow.hxx.
|
protectedpure virtual |
Allocates an entry from lastHandlerToCall_, invoking clone() when done: return allocate_and_call(lastHandlerToCall_, STATE(clone));.
Implemented in DispatchFlow< MessageType, NUM_PRIO >.
Imediately call the next state upon return.
| c | Callback "state" to move to |
Definition at line 318 of file StateFlow.hxx.
|
protected |
State after a clone-and-send operation is complete.
Definition at line 470 of file Dispatcher.hxx.
|
overrideprotectedvirtual |
Entry into the StateFlow activity.
Pure virtual which must be defined by derived class. Must eventually (through some number of states) call release_and_exit() to transition to getting next message.
Implements StateFlowWithQueue.
Reimplemented in TypedStateFlow< Buffer< CanHubData >, DispatchFlowBase< BASE_NUM_PRIO > >, TypedStateFlow< Buffer< CanMessageData >, DispatchFlowBase< BASE_NUM_PRIO > >, TypedStateFlow< Buffer< D >, DispatchFlowBase< BASE_NUM_PRIO > >, TypedStateFlow< Buffer< GenMessage >, DispatchFlowBase< BASE_NUM_PRIO > >, TypedStateFlow< Buffer< HubData >, DispatchFlowBase< BASE_NUM_PRIO > >, TypedStateFlow< Buffer< ThrottleCommand >, DispatchFlowBase< BASE_NUM_PRIO > >, and TypedStateFlow< MessageType, DispatchFlowBase< BASE_NUM_PRIO > >.
Definition at line 420 of file Dispatcher.hxx.
|
protectedpure virtual |
Returns the current message's ID.
Implemented in DispatchFlow< MessageType, NUM_PRIO >.
|
protected |
Iterate on potential handlers, matching the ID of the incoming message to the handlers' masks.
Definition at line 428 of file Dispatcher.hxx.
|
protected |
State when the entire iteration is done.
Definition at line 478 of file Dispatcher.hxx.
|
inlineprotected |
Sets one handler to receive all messages that no other handler has matched.
May be called only once in the lifetime of a dispatcher object.
| handler | is the handler pointer for the fallback handler. |
Definition at line 104 of file Dispatcher.hxx.
|
protected |
Adds a new handler to this dispatcher.
A handler will be called if incoming_id & mask == id & mask. If negateMatch_ then the handler will be called if incoming_id & mask != id & mask.
| id | is the identifier of the message to listen to. |
| mask | is the mask of the ID matcher. 0=match all; 0xff...f=match one |
| handler | is the flow to forward message to. It must stay alive so long as *this is alive or the handler is removed. |
Definition at line 358 of file Dispatcher.hxx.
|
inlineprotected |
Terminates the processing of the current message.
Flows should end with this action. Frees the current message.
Definition at line 1029 of file StateFlow.hxx.
|
protectedpure virtual |
Sends the current message to lastHandlerToCall, transferring ownership.
Implemented in DispatchFlow< MessageType, NUM_PRIO >.
| size_t DispatchFlowBase< NUM_PRIO >::size | ( | ) |
Definition at line 343 of file Dispatcher.hxx.
|
protected |
Removes a specific instance of a handler from this dispatcher.
| handler | handler pointer to unregister. |
| id | bits to unregister the handler for |
| mask | mask to unregister the handler for. |
Definition at line 378 of file Dispatcher.hxx.
|
protected |
Removes all instances of a handler from this dispatcher.
| handler | is the handler to unregister from all instances. |
Definition at line 402 of file Dispatcher.hxx.
Definition at line 154 of file Dispatcher.hxx.
|
private |
Index of the next handler to look at.
Definition at line 188 of file Dispatcher.hxx.
|
protected |
Handler to give all messages that were not matched by any other handler registration.
Definition at line 195 of file Dispatcher.hxx.
|
private |
Registered handlers.
Definition at line 185 of file Dispatcher.hxx.
|
protected |
If non-NULL we still need to call this handler.
Definition at line 192 of file Dispatcher.hxx.
|
private |
Protects handler add / remove against iteration.
Definition at line 198 of file Dispatcher.hxx.
|
private |
true if this flow should negate the match condition.
Definition at line 152 of file Dispatcher.hxx.