Diffusion C API 6.13.0
Loading...
Searching...
No Matches
Messaging

Functions

SESSION_ID_T * diffusion_request_context_get_session_id (const DIFFUSION_REQUEST_CONTEXT_T *request_context)
 Return a copy of the request context's session ID.
char * diffusion_request_context_get_path (const DIFFUSION_REQUEST_CONTEXT_T *request_context)
 Return a copy of the request context's request path.
HASH_T * diffusion_request_context_get_session_properties (const DIFFUSION_REQUEST_CONTEXT_T *request_context)
 Return a copy of the request context's session properties.
void send_request_to_session (SESSION_T *session, SEND_REQUEST_TO_SESSION_PARAMS_T params)
 Send a request to a specific session.
void add_request_handler (SESSION_T *session, ADD_REQUEST_HANDLER_PARAMS_T params)
 Add a server registered request handler.
void send_request (SESSION_T *session, SEND_REQUEST_PARAMS_T params)
 Send a request to a path.
DIFFUSION_REQUEST_STREAM_T * set_request_stream (SESSION_T *session, const char *path, DIFFUSION_DATATYPE request_datatype, DIFFUSION_DATATYPE response_datatype, const DIFFUSION_REQUEST_STREAM_T *request_stream)
 Set a request stream to handle requests to a specified path.
DIFFUSION_REQUEST_STREAM_T * remove_request_stream (SESSION_T *session, const char *request_path)
 Remove a request stream bound to a specified path.

Detailed Description

Provides a client session with request-response messaging capabilities that can be used to implement application services.

Messaging feature.

Request-response messaging allows a session to send requests to other sessions. Each receiving session provides a corresponding response, which is returned to the sending session. Each request and response carries an application provided value.

The method used to send a request determines which sessions will receive it. Each request is routed using the provided message path – an application provided string. Two addressing schemes are provided: unaddressed requests and addressed requests.

Unaddressed requests

A session can provide an application service by implementing a handler and registering it with the server. This is somewhat similar to implementing a REST service, except that interactions between the sender and receiver are asynchronous.

Unaddressed requests sent using send_request are routed by the server to a handler that has been pre-registered by another session, and matches the message path.

Handlers are registered with addRequestHandler. Each session may register at most one handler for a given message path. Optionally, one or more session property names can be provided (see session.h for a full description of session properties), in which case the values of the session properties for each recipient session will be returned along with its response. To add a request handler, the control client session must have GLOBAL_PERMISSION_REGISTER_HANDLER permission. If registering to receive session property values, the session must also have GLOBAL_PERMISSION_VIEW_SESSION permission.

Routing works as follows:

  1. The session sends the request, providing the message path, the request value and data type, and the expected response type.
  2. The server uses the message path to apply access control. The sender must have the PATH_PERMISSION_SEND_TO_MESSAGE_HANDLER path permission for the message path, or the request will be rejected.
  3. The server uses the message path to select a pre-registered handler and route the request to the appropriate recipient session. The server will consider all registered handlers and select one registered for the most specific path. If multiple sessions have registered a handler registered for a path, one will be chosen arbitrarily. If there is no registered handler matching the message path, the request will be rejected.
  4. Otherwise, the server forwards the request to one of the sessions registered to handle the message path. The message path is also passed to the recipient session, providing a hierarchical context.
  5. The recipient session processes the request and returns a response to the server, which forwards the response to the sending session.

Registration works across a cluster of servers. If no matching handler is registered on the server to which the sending session is connected, the request will be routed to another server in the cluster that has one.

Addressed requests

Addressed requests provide a way to perform actions on a group of sessions, or to notify sessions of one-off events (for repeating streams of events, use a topic instead).

An addressed request can be sent to a set of sessions using send_request_to_filter. For the details of session filters, see session.h. Sending a request to a filter will match zero or more sessions. Each response received will be passed to the provided callbacks in SEND_REQUEST_TO_FILTER_PARAMS_T. As a convenience, an addressed request can be sent a specific session using send_request_to_session that accepts a session id.

Sending an addressed request requires PATH_PERMISSION_SEND_TO_SESSION permission.

If the sending session is connected to a server belonging to a cluster, the recipient sessions can be connected to other servers in the cluster. The filter will be evaluated against all sessions hosted by the cluster.

To receive addressed requests, a session must set up a local request stream to handle the specific message path, using set_request_stream. When a request is received for the message path, the DIFFUSION_REQUEST_STREAM_T.request_stream_on_request_cb method on the stream is triggered. The session should respond using the provided DIFFUSION_RESPONDER_HANDLE_T. Streams receive a DIFFUSION_REQUEST_STREAM_T.request_stream_on_close_cb callback when unregistered and a DIFFUSION_REQUEST_STREAM_T.request_stream_on_error_cb callback if the session is closed.

If a request is sent to a session that does not have a matching stream for the message path, an error will be returned to the sending session.

Function Documentation

◆ add_request_handler()

void add_request_handler ( SESSION_T * session,
ADD_REQUEST_HANDLER_PARAMS_T params )

Add a server registered request handler.

Parameters
sessionThe session handle. If NULL, this function returns immediately.
paramsParameters defining the request handler to be added.

◆ diffusion_request_context_get_path()

char * diffusion_request_context_get_path ( const DIFFUSION_REQUEST_CONTEXT_T * request_context)

Return a copy of the request context's request path.

free should be called on this pointer when no longer needed.

Parameters
request_contextthe request context to retrieve the request path from.
Returns
a copy of the request context's request path. NULL, if request_context is NULL.

◆ diffusion_request_context_get_session_id()

SESSION_ID_T * diffusion_request_context_get_session_id ( const DIFFUSION_REQUEST_CONTEXT_T * request_context)

Return a copy of the request context's session ID.

session_id_free should be called on this pointer when no longer needed.

Parameters
request_contextthe request context to retrieve the session ID from.
Returns
a copy of the request context's session ID. NULL, if request_context is NULL.

◆ diffusion_request_context_get_session_properties()

HASH_T * diffusion_request_context_get_session_properties ( const DIFFUSION_REQUEST_CONTEXT_T * request_context)

Return a copy of the request context's session properties.

hash_free should be called on this pointer when no longer needed.

Parameters
request_contextthe request context to retrieve session properties from.
Returns
a copy of the request context's session properties. NULL, if request_context is NULL.

◆ remove_request_stream()

DIFFUSION_REQUEST_STREAM_T * remove_request_stream ( SESSION_T * session,
const char * request_path )

Remove a request stream bound to a specified path.

Parameters
sessionThe session handle. If NULL, this function returns immediately.
request_pathThe path at which to remove the request stream. If NULL, this function returns immediately.
Returns
the request stream that was removed. NULL, if there is no request stream bound to this path.

◆ send_request()

void send_request ( SESSION_T * session,
SEND_REQUEST_PARAMS_T params )

Send a request to a path.

Parameters
sessionThe session handle. If NULL, this function returns immediately.
paramsParameters defining the request to be sent.

◆ send_request_to_session()

void send_request_to_session ( SESSION_T * session,
SEND_REQUEST_TO_SESSION_PARAMS_T params )

Send a request to a specific session.

Parameters
sessionThe session handle. If NULL, this function returns immediately.
paramsParameters defining the request to send a request to a specific session.

◆ set_request_stream()

DIFFUSION_REQUEST_STREAM_T * set_request_stream ( SESSION_T * session,
const char * path,
DIFFUSION_DATATYPE request_datatype,
DIFFUSION_DATATYPE response_datatype,
const DIFFUSION_REQUEST_STREAM_T * request_stream )

Set a request stream to handle requests to a specified path.

Parameters
sessionThe session handle. If NULL, this function returns immediately.
pathPath to receive requests on.
request_datatypeThe request datatype.
response_datatypeThe response datatype.
request_streamRequest stream to handle requests to this path
Returns
the current REQUEST_STREAM_T at this path. NULL, if this is the first stream to be set to the path.