YAMI4 - C++ general-purpose interface
yami::agent Class Reference

Message broker. More...

#include <agent.h>

List of all members.

Public Types

typedef long long outgoing_message_id
 Outgoing message identifier type.

Public Member Functions

 agent (const parameters &options=parameters())
 Constructor.
 agent (event_callback &event_listener, const parameters &options=parameters())
 Constructor.
 ~agent ()
 Destructor.
std::string add_listener (const std::string &listener)
 Adds new listener.
void remove_listener (const std::string &listener)
 Removes existing listener.
template<typename functor >
void register_object (const std::string &object_name, functor &f)
 Registers the new logical destination object.
void register_value_publisher (const std::string &object_name, value_publisher &publisher)
 Registers the value publisher as a new logical object.
void unregister_object (const std::string &object_name)
 Unregisters the logical destination object.
void open_connection (const std::string &target)
 Opens the new connection.
void open_connection (const std::string &target, const parameters &options)
 Opens the new connection with overriding options.
std::auto_ptr< outgoing_messagesend (const std::string &target, const std::string &object_name, const std::string &message_name, const serializable &content=parameters(), std::size_t priority=0, bool auto_connect=true)
 Sends the new outgoing message.
void send (outgoing_message &message, const std::string &target, const std::string &object_name, const std::string &message_name, const serializable &content=parameters(), std::size_t priority=0, bool auto_connect=true)
 Sends the outgoing message.
template<typename functor >
outgoing_message_id send (functor &f, const std::string &target, const std::string &object_name, const std::string &message_name, const serializable &content=parameters(), std::size_t priority=0, bool auto_connect=true)
 Sends the outgoing message.
bool clean_outgoing_message_callback (outgoing_message_id id)
 Cleans internal resources for the given message callback.
void send_one_way (const std::string &target, const std::string &object_name, const std::string &message_name, const serializable &content=parameters(), std::size_t priority=0, bool auto_connect=true)
 Sends the new outgoing message.
void close_connection (const std::string &target, std::size_t priority=0)
 Closes the given communication channel.
void hard_close_connection (const std::string &target)
 Immediately closes the given communication channel.
template<typename functor >
void register_connection_event_monitor (functor &f)
 Registers the monitor for connection-related events.
template<typename functor >
void register_io_error_logger (functor &f)
 Registers the logger for I/O errors.
void get_outgoing_flow_state (std::size_t &current_level, std::size_t &high_water_mark, std::size_t &low_water_mark) const
 Obtains the state of overall outgoing flow.
void get_channel_usage (int &max_allowed, int &used)
 Returns the selector's channel usage counters.
std::size_t get_pending_outgoing_bytes (const std::string &target)
 Returns the size of outgoing queue for the given channel.

Detailed Description

The message broker that encapsulates physical channel management, incoming and outgoing message queues, listeners and resource management.

A single agent object can manage many listeners, which are responsible for accepting remote connections, and many incoming and outgoing connections.

The agent objects can be created and destroyed without constraints on the stack, on the free store or as static objects.

The objects of this class can be safely used by multiple threads.


Constructor & Destructor Documentation

agent::agent ( const parameters options = parameters())

Creates the message broker and starts its internal threads. The broker created with this constructor has no listener.

agent::agent ( event_callback event_listener,
const parameters options = parameters() 
)

Creates the message broker with event notification callback. The broker created with this constructor has no listener.

agent::~agent ( )

The destructor stops the internal threads and cleans up all internal data structures.

Note: The messages and replies that were posted for transmission and that have not yet been fully transmitted are abandoned; in the case of outgoing messages their state is properly notified about that fact.


Member Function Documentation

std::string agent::add_listener ( const std::string &  listener)

Adds a new listener for the given target address.

The supported target formats are:

  • "tcp://host:port" for TCP/IP connections, where host can be provided in the symbolic or numeric form
  • "tcp://*:port" for TCP/IP connections, for "any" local address
  • "tcp://port" for TCP/IP connections, for "any" local address
  • "udp://host:port" for UDP communication, with rules as for TCP/IP
  • "unix://path" for Unix connections

The port for TCP/IP and UDP protocols can be 0 or *, in which case the actual port number is assigned by the system.

Parameters:
listenerThe target name for the new listener.
Returns:
The locally resolved listener name. This name can be used to remove the listener later on.
void agent::remove_listener ( const std::string &  listener)

Removes the listener denoted by its actual target name. Note that the actual target name might be different from the name provided when the listener was created, due to target resolution. The name which should be used for listener removal is the name that is returned by the add_listener function.

template<typename functor >
void yami::agent::register_object ( const std::string &  object_name,
functor &  f 
) [inline]

Registers the new "object" that can be a logical destination for incoming messages.

Parameters:
object_nameThe name of the newly registered object. If an object with this name is already registered, the registration data is replaced.
fThe callable entity that can accept the incoming_message as the invocation parameter.
void agent::register_value_publisher ( const std::string &  object_name,
value_publisher publisher 
)
Parameters:
object_nameThe name of the newly registered object. If an object with this name is already registered, the registration data is replaced.
publisherThe value publisher to be registered.
void agent::unregister_object ( const std::string &  object_name)

It is permitted to request unregistration for an object that does not exist - such operation has no effect.

Note: Due to performance and design tradeoffs it is not guaranteed that no more messages will be ever dispatched to the given object when this function returns. In fact, some of the messages that have been received by agent and not yet dispatched might be still dispatched shortly after this function returns. Only those messages that are received by agent after this function returns are guaranteed not to be dispatched to the unregistered object. This might be particularly important with regard to the lifetime of the callable entity that was provided when the given object has been registered.

Parameters:
object_nameThe name of the object to be unregistered.
void agent::open_connection ( const std::string &  target)

Opens the new channel or does nothing if the channel already exists.

This function is not necessary with automatic connection recovery option in send and send_one_way.

Parameters:
targetThe name of the target endpoint. This name should correspond to the listener name in some target agent object.
void agent::open_connection ( const std::string &  target,
const parameters options 
)

Opens the new channel or does nothing if the channel already exists. If the new channel is created, it will use the overriding options from those which are defined.

This function is not necessary with automatic connection recovery option in send and send_one_way.

Parameters:
targetThe name of the target endpoint. This name should correspond to the listener name in some target agent object.
optionsThe set of options that will override agent's values.
std::auto_ptr< outgoing_message > agent::send ( const std::string &  target,
const std::string &  object_name,
const std::string &  message_name,
const serializable content = parameters(),
std::size_t  priority = 0,
bool  auto_connect = true 
)

Sends the new outgoing message to the given destination.

Parameters:
targetThe name of the target endpoint. This name should correspond to the listener name in some target agent object.
object_nameThe name of the logical destination object in the target agent.
message_nameThe name of the message.
contentThe content of the message.
priorityThe priority of the message.
auto_connectThe flag controlling automatic (re)connection.
Returns:
The outgoing_message object that allows to track the progress of this message, its status and obtain response data.

Note: This function implicitly opens a new communication channel if it is not already open. This channel is kept open until it is explicitly closed (see the close_connection function) or until the agent is destroyed or the communication error is detected.

void agent::send ( outgoing_message message,
const std::string &  target,
const std::string &  object_name,
const std::string &  message_name,
const serializable content = parameters(),
std::size_t  priority = 0,
bool  auto_connect = true 
)

Sends the outgoing message to the given destination and reinitializes the message object in-place.

This function behaves as the other version of send, except for the outgoing_message object creation policy.

template<typename functor >
outgoing_message_id yami::agent::send ( functor &  f,
const std::string &  target,
const std::string &  object_name,
const std::string &  message_name,
const serializable content = parameters(),
std::size_t  priority = 0,
bool  auto_connect = true 
) [inline]

Sends the outgoing message to the given destination and executes the callback whenever the status of the message changes. The callback should accept the outgoing_message object as its parameter. Note: the callback should not block the processing unnecessarily.

This function behaves as the other version of send, except for the outgoing_message object creation policy.

bool agent::clean_outgoing_message_callback ( outgoing_message_id  id)

Cleans internal resources associated with the given message callback.

Parameters:
idOutgoing message identifier, as obtained from send.
Returns:
false if the operation could not be completed due to hazard with the already pending callback
void agent::send_one_way ( const std::string &  target,
const std::string &  object_name,
const std::string &  message_name,
const serializable content = parameters(),
std::size_t  priority = 0,
bool  auto_connect = true 
)

Sends the new outgoing message to the given destination, without the possibility to track its progress.

See the description and notes for the send function.

void agent::close_connection ( const std::string &  target,
std::size_t  priority = 0 
)

Closes the channel identified by name.

The priority allows to properly handle the existing outgoing messages that are waiting in the outgoing queue for transmission. The existing messages with lower priority are abandoned, whereas the existing messages with priority equal or higher to the one provided as parameter are retained in the outgoing queue and are properly pushed for transmission before the channel is physically closed. The channel is closed immediately only if there are no messages waiting in its outgoing queue.

Parameters:
targetThe name of the target endpoint.
priorityProprity of the request, respects existing messages in the outgoing queue.
void agent::hard_close_connection ( const std::string &  target)

Closes the channel identified by name.

The channel is closed immediately and those messages that are waiting in its outgoing queue are abandoned. Integrity of the message that was already partly transmitted is not guaranteed.

Parameters:
targetThe name of the target endpoint.
template<typename functor >
void yami::agent::register_connection_event_monitor ( functor &  f) [inline]

Registers the monitor for connection events.

Note: The monitor callback is intentionally not synchronized. Use this function after constructing the agent, but before opening any connections.

Parameters:
fThe callable entity that can accept the std::string as the connection name and connection_event as event description.
template<typename functor >
void yami::agent::register_io_error_logger ( functor &  f) [inline]

Registers the logger for I/O errors.

Note: The logger callback is intentionally not synchronized. Use this function after constructing the agent, but before opening any connections.

Parameters:
fThe callable entity that can accept the int as the error code and const char * as error description.
void agent::get_outgoing_flow_state ( std::size_t &  current_level,
std::size_t &  high_water_mark,
std::size_t &  low_water_mark 
) const

Obtains the state of overall outgoing flow.

Note: The outgoing flow is a combination of all outgoing traffic, and is not tied to any particular communication channel.

Parameters:
current_levelThe current level of the outgoing flow.
high_water_markThe high water mark.
low_water_markThe low water mark.
void agent::get_channel_usage ( int &  max_allowed,
int &  used 
)

Note: The information obtained with this function can be in constant flow, as incoming and outgoing channels are created and closed.

Parameters:
max_allowedThe maximum number of channels that the agent can handle internall or 0 if the limit is unknown.
usedTotal number of handled channels and listeners.
std::size_t agent::get_pending_outgoing_bytes ( const std::string &  target)
Parameters:
targetThe name of the target endpoint.
Returns:
The size of outgoing queue in bytes.