coral
A C++ library for distributed co-simulation
Public Member Functions | Static Public Attributes | List of all members
coral::net::Reactor Class Reference

An implementation of the reactor pattern. More...

#include "coral/net/reactor.hpp"

Public Member Functions

void AddSocket (zmq::socket_t &socket, SocketHandler handler)
 Adds a handler for the given socket.
 
void RemoveSocket (zmq::socket_t &socket) CORAL_NOEXCEPT
 Removes all handlers for the given socket. More...
 
void AddNativeSocket (NativeSocket socket, NativeSocketHandler handler)
 Adds a handler for the given native socket.
 
void RemoveNativeSocket (NativeSocket socket) CORAL_NOEXCEPT
 Removes all handlers for the given native socket. More...
 
int AddTimer (std::chrono::milliseconds interval, int count, TimerHandler handler)
 Adds a timer. More...
 
void RemoveTimer (int id)
 Removes a timer. More...
 
void RestartTimerInterval (int id)
 Resets the time to the next event for a timer. More...
 
void Run ()
 Runs the messaging loop. More...
 
void Stop ()
 Stops the messaging loop. More...
 

Static Public Attributes

static const int invalidTimerID
 A number which will never be returned by AddTimer().
 

Detailed Description

An implementation of the reactor pattern.

This class polls a number of sockets, and when a socket has incoming messages, it dispatches to the registered handler function(s) for that socket. If multiple sockets have incoming messages, or there are multiple handlers for one socket, the functions are called in the order they were added.

It also supports timed events, where a handler function is called a certain number of times (or indefinitely) with a fixed time interval. Timers are only active when the messaging loop is running, i.e. between Run() and Stop().

Member Function Documentation

int coral::net::Reactor::AddTimer ( std::chrono::milliseconds  interval,
int  count,
TimerHandler  handler 
)

Adds a timer.

If the messaging loop is running, the first event will be triggered at interval after this function is called. Otherwise, the first event will be triggered interval after Run() is called.

Parameters
[in]intervalThe time between events.
[in]countThe total number of events. If negative, the timer runs indefinitely.
[in]handlerThe event handler.
Returns
an index which may later be used to remove the timer.
Exceptions
std::invalid_argumentif count is zero or interval is negative.
void coral::net::Reactor::RemoveNativeSocket ( NativeSocket  socket)

Removes all handlers for the given native socket.

If this function is called by a socket handler, no more handlers will be called for the removed socket, even if the last poll indicated that it has incoming messages.

If the given socket was never registered with AddNativeSocket(), this function simply returns without doing anything.

void coral::net::Reactor::RemoveSocket ( zmq::socket_t &  socket)

Removes all handlers for the given socket.

If this function is called by a socket handler, no more handlers will be called for the removed socket, even if the last poll indicated that it has incoming messages.

If the given socket was never registered with AddSocket(), this function simply returns without doing anything.

void coral::net::Reactor::RemoveTimer ( int  id)

Removes a timer.

Exceptions
std::invalid_argumentif id is not a valid timer ID.
void coral::net::Reactor::RestartTimerInterval ( int  id)

Resets the time to the next event for a timer.

This function sets the elapsed time for the current iteration of a timer to zero. It does not change the number of remaining events.

Exceptions
std::invalid_argumentif id is not a valid timer ID.
void coral::net::Reactor::Run ( )

Runs the messaging loop.

This function does not return before Stop() is called (by one of the socket/timer handlers) or an error occurs.

If a socket/timer handler throws an exception, the messaging loop will stop and the exception will propagate out of Run().

Exceptions
zmq::error_tif ZMQ reports an error.
void coral::net::Reactor::Stop ( )

Stops the messaging loop.

This method may be called by a socket/timer handler, and will exit the messaging loop once all handlers for the current event(s) have been called.


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