Request-Response Messaging¶
This package provides a client session with request-response messaging capabilities that can be used to implement application services.
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.
Types of Messages¶
There are three mechanisms to specify the recipient(s) of a message:
-
Send to path: The messages of this type are sent without specifying the receiving session. The server will forward an unaddressed message to any session which was registered as a handler for the path indicated in the message.
-
Send to session: Messages can be sent to a specific session by specifying its ID. To be able to respond to messages addressed to itself, a session still needs to have an internal handler.
-
Send to filter: Messages can also be addressed using a session filter. When receiving such a message, the server will locate all the currently active sessions which match the filter's conditions, and respond to the sender with the number of located sessions; the sender can expect up to that many responses, so it needs to have a handler ready to handle those responses.
diffusion.messaging.
Messaging
(
session
)
Messaging component.
It is not supposed to be instantiated independently; an instance is available
on each Session
instance as session.messaging
.
session
(Session) — The activeSession
to operate on.
services
(ServiceLocator) — Alias for the internal session's service locator.
RequestHandler
— Handler for messaging requests.
add_filter_response_handler
(
session_filter
,handler
)
— Registers a session filter response handler.add_request_handler
(
path
,handler
,session_properties
)
— Register the session as the handler for requests to a given path.add_stream_handler
(
path
,handler
,addressed
)
— Registers a request stream handler.send_request_to_filter
(
session_filter
,path
,request
)
(int) — Send a request to other sessions, specified by the filter.send_request_to_path
(
path
,request
,response_type
)
(DataType, optional) — Send a request to sessions based on a path.send_request_to_session
(
path
,session_id
,request
,response_type
)
(DataType, optional) — Send a request to a single session.
add_stream_handler
(
path
, handler
, addressed=False
)
Registers a request stream handler.
The handler is invoked when the session receives a request sent to the given path or session filter.
path
(str) — The handler will respond to the requests to this path.handler
(Messaging.RequestHandler) — A Handler instance to handle the request.addressed
(bool, optional) —True
to handle the requests addressed to the session's ID or using a session filter;False
for unaddressed requests.
add_filter_response_handler
(
session_filter
, handler
)
Registers a session filter response handler.
The handler is invoked when the session receives a response to a request sent to the session filter.
session_filter
(str) — A session filtering query.handler
(Handler) — A Handler instance to handle the request.
send_request_to_path
(
path
, request
, response_type=None
)
Send a request to sessions based on a path.
path
(str) — The path to send a request to.request
(DataType) — The request to be sent, wrapped into the requiredDataType
class.response_type
(Union(int, str, type of datatype, nonetype), optional) — The type to convert the response to. If omitted, it will be the same as therequest
's data type.
The response value of the provided response_type
.
add_request_handler
(
path
, handler
, session_properties=()
)
Register the session as the handler for requests to a given path.
This method is used to inform the server that any unaddressed requests to the
given path should be forwarded to the active session. The handler to
these requests is added at the same time, using add_stream_handler
internally.
path
(str) — The handler will respond to the requests to this path.handler
(Messaging.RequestHandler) — A callable to handle the request.session_properties
(collection of str, optional) — A list of keys of session properties that should be supplied with each request. To request all fixed properties includeALL_FIXED_PROPERTIES
as a key; any other fixed property keys will be ignored. To request all user properties includeALL_USER_PROPERTIES
as a key; any other user properties will be ignored.
send_request_to_filter
(
session_filter
, path
, request
)
Send a request to other sessions, specified by the filter.
session_filter
(str) — A session filtering query.path
(str) — The path to send a request to.request
(DataType) — The request to be sent, wrapped into the requiredDataType
class.
The number of sessions that correspond to the filter, which is the number of responses that can be expected. When each of the responses is received, the handler registered for the filter will be executed.
send_request_to_session
(
path
, session_id
, request
, response_type=None
)
Send a request to a single session.
path
(str) — The path to send a request to.session_id
(SessionId) — The ID of the session to send the request to.request
(DataType) — The request to be sent, wrapped into the requiredDataType
class.response_type
(Union(int, str, type of datatype, nonetype), optional) — The type to convert the response to. If omitted, it will be the same as therequest
's data type.
The response value of the provided response_type
.
RequestHandler
(
*args
, **kwds
)
Handler for messaging requests.
_ProtocolMeta
— Metaclass for defining Abstract Base Classes (ABCs).
__init_subclass__
(
*args
,**kwargs
)
— This method is called when a class is subclassed.handle
(
event
,**kwargs
)
(DataType) — Execute the callback.
typing.
_ProtocolMeta
(
name
, bases
, namespace
, **kwargs
)
Metaclass for defining Abstract Base Classes (ABCs).
Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class. You can also register unrelated concrete classes (even built-in classes) and unrelated ABCs as 'virtual subclasses' -- these and their descendants will be considered subclasses of the registering ABC by the built-in issubclass() function, but the registering ABC won't show up in their MRO (Method Resolution Order) nor will method implementations defined by the registering ABC be callable (not even via super()).
__instancecheck__
(
cls
,instance
)
— Override for isinstance(instance, cls).__subclasscheck__
(
cls
,subclass
)
— Override for issubclass(subclass, cls).register
(
cls
,subclass
)
— Register a virtual subclass of an ABC.
register
(
cls
, subclass
)
Register a virtual subclass of an ABC.
Returns the subclass, to allow usage as a class decorator.
__subclasscheck__
(
cls
, subclass
)
Override for issubclass(subclass, cls).
__instancecheck__
(
cls
, instance
)
Override for isinstance(instance, cls).
__init_subclass__
(
*args
, **kwargs
)
This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
handle
(
event='request'
, **kwargs
)
→ DataTypeExecute the callback.