Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
DispatchFlowBase< NUM_PRIO > Class Template Referenceabstract

This class takes registrations of StateFlows for incoming messages. More...

#include <Dispatcher.hxx>

Inheritance diagram for DispatchFlowBase< NUM_PRIO >:
UntypedStateFlow< QList< NUM_PRIO > > StateFlowWithQueue StateFlowBase Atomic LinkedObject< StateFlowWithQueue > Executable Notifiable QMember Destructable

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.
 
Serviceservice ()
 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 >
StateFlowWithQueuelink_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.
 
QMemberqueue_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.
 
BufferBasemessage ()
 
BufferBasetransfer_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 >
StateFlowWithQueuelink_this ()
 
 LinkedObject ()
 Constructor. Puts *this on the linked list.
 
 ~LinkedObject ()
 Constructor. Removes *this from the linked list.
 
StateFlowWithQueuelink_next ()
 

Protected Attributes

UntypedHandlerlastHandlerToCall_ {nullptr}
 If non-NULL we still need to call this handler.
 
UntypedHandlerfallbackHandler_ {nullptr}
 Handler to give all messages that were not matched by any other handler registration.
 
- Protected Attributes inherited from QMember
QMembernext
 pointer to the next member in the queue
 
- Protected Attributes inherited from LinkedObject< StateFlowWithQueue >
StateFlowWithQueuelink_
 Linked list pointer.
 

Private Attributes

bool negateMatch_
 true if this flow should negate the match condition.
 
vector< HandlerInfohandlers_
 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 StateFlowWithQueuelink_head ()
 
static Atomichead_mu ()
 Locks the list for modification (at any entry!).
 
- Static Protected Member Functions inherited from LinkedObject< StateFlowWithQueue >
static StateFlowWithQueuelink_head ()
 
static Atomichead_mu ()
 Locks the list for modification (at any entry!).
 
- Static Protected Attributes inherited from LinkedObject< StateFlowWithQueue >
static StateFlowWithQueuehead_
 Beginning of the list.
 

Detailed Description

template<int NUM_PRIO>
class DispatchFlowBase< NUM_PRIO >

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.

Member Typedef Documentation

◆ Action

template<int NUM_PRIO>
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.

◆ ID

template<int NUM_PRIO>
typedef uint32_t DispatchFlowBase< NUM_PRIO >::ID
protected

Proxy the identifier type for customers to use.

Definition at line 70 of file Dispatcher.hxx.

◆ UntypedHandler

template<int NUM_PRIO>
typedef void DispatchFlowBase< NUM_PRIO >::UntypedHandler
protected

Generic handler type.

Will be cast to specific handlers in the specific DispatchFlow<...> template.

Definition at line 73 of file Dispatcher.hxx.

Constructor & Destructor Documentation

◆ DispatchFlowBase()

template<int NUM_PRIO>
DispatchFlowBase< NUM_PRIO >::DispatchFlowBase ( Service service)

Construct a dispatchflow.

Parameters
servicethe Service this flow belongs to.

Definition at line 329 of file Dispatcher.hxx.

◆ ~DispatchFlowBase()

template<int NUM_PRIO>
DispatchFlowBase< NUM_PRIO >::~DispatchFlowBase ( )

Definition at line 337 of file Dispatcher.hxx.

Member Function Documentation

◆ again()

template<int NUM_PRIO>
Action StateFlowBase::again ( )
inlineprotected

Call the current state again via call_immediately.

Returns
function pointer to current state handler

Definition at line 280 of file StateFlow.hxx.

◆ allocate_and_clone()

template<int NUM_PRIO>
virtual Action DispatchFlowBase< NUM_PRIO >::allocate_and_clone ( )
protectedpure virtual

Allocates an entry from lastHandlerToCall_, invoking clone() when done: return allocate_and_call(lastHandlerToCall_, STATE(clone));.

Implemented in DispatchFlow< MessageType, NUM_PRIO >.

◆ call_immediately()

template<int NUM_PRIO>
Action StateFlowBase::call_immediately ( Callback  c)
inlineprotected

Imediately call the next state upon return.

Parameters
cCallback "state" to move to
Returns
function pointer to be returned from state function

Definition at line 318 of file StateFlow.hxx.

◆ clone_done()

template<int NUM_PRIO>
StateFlowBase::Action DispatchFlowBase< NUM_PRIO >::clone_done ( )
protected

State after a clone-and-send operation is complete.

Returns
next action

Definition at line 470 of file Dispatcher.hxx.

◆ entry()

◆ get_message_id()

template<int NUM_PRIO>
virtual ID DispatchFlowBase< NUM_PRIO >::get_message_id ( )
protectedpure virtual

Returns the current message's ID.

Implemented in DispatchFlow< MessageType, NUM_PRIO >.

◆ iterate()

template<int NUM_PRIO>
StateFlowBase::Action DispatchFlowBase< NUM_PRIO >::iterate ( )
protected

Iterate on potential handlers, matching the ID of the incoming message to the handlers' masks.

Returns
next action

Definition at line 428 of file Dispatcher.hxx.

◆ iteration_done()

template<int NUM_PRIO>
StateFlowBase::Action DispatchFlowBase< NUM_PRIO >::iteration_done ( )
protected

State when the entire iteration is done.

Returns
next action

Definition at line 478 of file Dispatcher.hxx.

◆ register_fallback_handler()

template<int NUM_PRIO>
void DispatchFlowBase< NUM_PRIO >::register_fallback_handler ( UntypedHandler handler)
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.

Parameters
handleris the handler pointer for the fallback handler.

Definition at line 104 of file Dispatcher.hxx.

◆ register_handler()

template<int NUM_PRIO>
void DispatchFlowBase< NUM_PRIO >::register_handler ( UntypedHandler handler,
ID  id,
ID  mask 
)
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.

Parameters
idis the identifier of the message to listen to.
maskis the mask of the ID matcher. 0=match all; 0xff...f=match one
handleris 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.

◆ release_and_exit()

template<int NUM_PRIO>
Action StateFlowWithQueue::release_and_exit ( )
inlineprotected

Terminates the processing of the current message.

Flows should end with this action. Frees the current message.

Returns
the action for checking for new messages.

Definition at line 1029 of file StateFlow.hxx.

◆ send_transfer()

template<int NUM_PRIO>
virtual void DispatchFlowBase< NUM_PRIO >::send_transfer ( )
protectedpure virtual

Sends the current message to lastHandlerToCall, transferring ownership.

Implemented in DispatchFlow< MessageType, NUM_PRIO >.

◆ size()

template<int NUM_PRIO>
size_t DispatchFlowBase< NUM_PRIO >::size ( )
Returns
the number of handlers registered.

Definition at line 343 of file Dispatcher.hxx.

◆ unregister_handler()

template<int NUM_PRIO>
void DispatchFlowBase< NUM_PRIO >::unregister_handler ( UntypedHandler handler,
ID  id,
ID  mask 
)
protected

Removes a specific instance of a handler from this dispatcher.

Parameters
handlerhandler pointer to unregister.
idbits to unregister the handler for
maskmask to unregister the handler for.
Todo:
(balazs.racz) optimize by looking at the current index - 1.

Definition at line 378 of file Dispatcher.hxx.

◆ unregister_handler_all()

template<int NUM_PRIO>
void DispatchFlowBase< NUM_PRIO >::unregister_handler_all ( UntypedHandler handler)
protected

Removes all instances of a handler from this dispatcher.

Parameters
handleris the handler to unregister from all instances.

Definition at line 402 of file Dispatcher.hxx.

Friends And Related Symbol Documentation

◆ GenericHubFlow

template<int NUM_PRIO>
template<class T >
friend class GenericHubFlow
friend

Definition at line 154 of file Dispatcher.hxx.

Member Data Documentation

◆ currentIndex_

template<int NUM_PRIO>
size_t DispatchFlowBase< NUM_PRIO >::currentIndex_
private

Index of the next handler to look at.

Definition at line 188 of file Dispatcher.hxx.

◆ fallbackHandler_

template<int NUM_PRIO>
UntypedHandler* DispatchFlowBase< NUM_PRIO >::fallbackHandler_ {nullptr}
protected

Handler to give all messages that were not matched by any other handler registration.

Definition at line 195 of file Dispatcher.hxx.

◆ handlers_

template<int NUM_PRIO>
vector<HandlerInfo> DispatchFlowBase< NUM_PRIO >::handlers_
private

Registered handlers.

Definition at line 185 of file Dispatcher.hxx.

◆ lastHandlerToCall_

template<int NUM_PRIO>
UntypedHandler* DispatchFlowBase< NUM_PRIO >::lastHandlerToCall_ {nullptr}
protected

If non-NULL we still need to call this handler.

Definition at line 192 of file Dispatcher.hxx.

◆ lock_

template<int NUM_PRIO>
OSMutex DispatchFlowBase< NUM_PRIO >::lock_
private

Protects handler add / remove against iteration.

Definition at line 198 of file Dispatcher.hxx.

◆ negateMatch_

template<int NUM_PRIO>
bool DispatchFlowBase< NUM_PRIO >::negateMatch_
private

true if this flow should negate the match condition.

Definition at line 152 of file Dispatcher.hxx.


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