saturnin.protocol.fbsp¶
Saturnin reference implementation of Firebird Butler Service Protocol.
See https://firebird-butler.readthedocs.io/en/latest/rfc/4/FBSP.html
Constants¶
- saturnin.protocol.fbsp.HEADER_FMT_FULL: Final[str] = '!4sBBH8s'¶
FBSP protocol control frame
structformat
- saturnin.protocol.fbsp.HEADER_FMT: Final[str] = '!4xBBH8s'¶
FBSP protocol control frame
structformat without FOURCC
- saturnin.protocol.fbsp.PROTO_HELLO: Final[str] = 'firebird.butler.FBSPHelloDataframe'¶
Protobuf message for FBSP HELLO message
- saturnin.protocol.fbsp.PROTO_WELCOME: Final[str] = 'firebird.butler.FBSPWelcomeDataframe'¶
Protobuf message for FBSP WELCOME message
- saturnin.protocol.fbsp.PROTO_CANCEL_REQ: Final[str] = 'firebird.butler.FBSPCancelRequests'¶
Protobuf message for FBSP CANCEL REQUEST message
Enums¶
- class saturnin.protocol.fbsp.MsgType(value)[source]¶
Bases:
IntEnumFBSP Message Type
- CANCEL = 7¶
- CLOSE = 9¶
- DATA = 6¶
- ERROR = 31¶
- HELLO = 1¶
- NOOP = 3¶
- REPLY = 5¶
- REQUEST = 4¶
- STATE = 8¶
- UNKNOWN = 0¶
- WELCOME = 2¶
- class saturnin.protocol.fbsp.MsgFlag(value)[source]¶
Bases:
IntFlagFBSP message flag
- _generate_next_value_(start, count, last_values)¶
Generate the next value when not given.
name: the name of the member start: the initial start value or None count: the number of existing members last_values: the last value assigned or None
- ACK_REPLY = 2¶
- ACK_REQ = 1¶
- MORE = 4¶
- NONE = 0¶
- class saturnin.protocol.fbsp.ErrorCode(value)[source]¶
Bases:
IntEnumFBSP Error Code
- BAD_REQUEST = 3¶
- CONFLICT = 14¶
- ERROR = 5¶
- FAILED_DEPENDENCY = 9¶
- FBSP_VERSION_NOT_SUPPORTED = 2001¶
- FORBIDDEN = 10¶
- GONE = 13¶
- INSUFFICIENT_STORAGE = 16¶
- INTERNAL_ERROR = 6¶
- INVALID_MESSAGE = 1¶
- NOT_FOUND = 12¶
- NOT_IMPLEMENTED = 4¶
- PAYLOAD_TOO_LARGE = 15¶
- PROTOCOL_VIOLATION = 2¶
- REQUEST_CANCELLED = 17¶
- REQUEST_TIMEOUT = 7¶
- SERVICE_UNAVAILABLE = 2000¶
- TOO_MANY_REQUESTS = 8¶
- UNAUTHORIZED = 11¶
Classes¶
- class saturnin.protocol.fbsp.FBSPMessage[source]¶
Bases:
MessageFirebird Butler Service Protocol (FBSP) Message.
- _pack_data() list[source]¶
Called when serialization is requested.
Note: Default implementation returns empty list.
- Returns:
A list of ZMQ-compatible frames (bytes or zmq.Frame) representing the message-specific payload.
- Return type:
- _set_hdr(header: bytes) None[source]¶
Initialize new message from header.
- Parameters:
header (bytes)
- Return type:
None
- _unpack_data(data: list) None[source]¶
Called when all fields of the message are set. Usefull for data deserialization.
Note: Default implementation does nothing.
- Parameters:
data (list) – A list of remaining ZMQ frames after the header, to be deserialized into message-specific attributes.
- Return type:
None
- as_zmsg() TZMQMessage[source]¶
Returns message as sequence of ZMQ data frames.
- Return type:
- clear() None[source]¶
Clears message data.
Important
The
msg_typeremains the same (i.e. you can’t change the message type once message instance was created), but all other data are cleared.- Return type:
None
- clear_flag(flag: MsgFlag) None[source]¶
Clear flag specified by
flagmask.- Parameters:
flag (MsgFlag)
- Return type:
None
- from_zmsg(zmsg: TZMQMessage) None[source]¶
Populate message data from sequence of ZMQ data frames.
- Parameters:
zmsg (TZMQMessage) – Sequence of ZMQ frames. The first frame (header) is assumed to have been pre-processed by the message factory to set initial attributes like
msg_type. This method processes subsequent frames.- Raises:
InvalidMessageError – If message is not a valid protocol message.
- Return type:
None
- get_keys() Iterable[source]¶
Returns iterable of dictionary keys to be used with
Protocol.handlers. Keys must be provided in order of precedence (from more specific to general).- Return type:
- class saturnin.protocol.fbsp.HandshakeMessage[source]¶
Bases:
FBSPMessageBase FBSP client/service handshake message (
HELLOorWELCOME). The message includes basic information about the Peer.- _unpack_data(data: list) None[source]¶
Called when all fields of the message are set. Usefull for data deserialization.
- Parameters:
data (list)
- Return type:
None
- data: ProtoMessage¶
Protobuf message instance holding peer information.
- class saturnin.protocol.fbsp.HelloMessage[source]¶
Bases:
HandshakeMessageThe
HELLOmessage is a Client request to open a Connection to the Service. The message includes basic information about the Client and Connection parameters required by the Client.- data: FBSPHelloDataframe¶
FBSPHelloDataframeprotobuf message with peer information
- class saturnin.protocol.fbsp.WelcomeMessage[source]¶
Bases:
HandshakeMessageThe
WELCOMEmessage is the response of the Service to theHELLOmessage sent by the Client, which confirms the successful creation of the required Connection and announces basic parameters of the Service and the Connection.- data: FBSPWelcomeDataframe¶
FBSPWelcomeDataframeprotobuf message with peer information
- class saturnin.protocol.fbsp.APIMessage[source]¶
Bases:
FBSPMessageBase FBSP client/service API message (
REQUEST,REPLY,STATE). The message includes information about the API call (interface ID and API Code).
- class saturnin.protocol.fbsp.StateMessage[source]¶
Bases:
APIMessageThe
STATEmessage is a Client request to the Service.- _pack_data() list[source]¶
Called when serialization is requested.
- Returns:
A list containing the serialized
FBSPStateInformationprotobuf message.- Return type:
- _unpack_data(data: list) None[source]¶
Called when all fields of the message are set. Usefull for data deserialization.
- Parameters:
data (list) – A list containing the serialized
FBSPStateInformationprotobuf message.- Return type:
None
- state_info: FBSPStateInformation¶
FBSPStateInformationprotobuf message with state information
- class saturnin.protocol.fbsp.DataMessage[source]¶
Bases:
FBSPMessageThe
DATAmessage is intended for delivery of arbitrary data between connected peers.
- class saturnin.protocol.fbsp.CancelMessage[source]¶
Bases:
FBSPMessageThe
CANCELmessage represents a request for a Service to stop processing the previous request from the Client.- _pack_data() list[source]¶
Called when serialization is requested.
- Returns:
A list containing the serialized
FBSPCancelRequestsprotobuf message.- Return type:
- _unpack_data(data: list) None[source]¶
Called when all fields of the message are set. Usefull for data deserialization.
- Parameters:
data (list) – A list containing the serialized
FBSPCancelRequestsprotobuf message.- Return type:
None
- request: FBSPCancelRequests¶
FBSPCancelRequestsprotobuf message
- class saturnin.protocol.fbsp.ErrorMessage[source]¶
Bases:
FBSPMessageThe
ERRORmessage notifies the Client about error condition detected by Service.- _pack_data() list[source]¶
Called when serialization is requested.
- Returns:
A list containing the serialized
ErrorDescriptionprotobuf messages.- Return type:
- _unpack_data(data: list) None[source]¶
Called when all fields of the message are set. Usefull for data deserialization.
- Parameters:
data (list) – A list containing the serialized
ErrorDescriptionprotobuf messages.- Return type:
None
- add_error() ErrorDescription[source]¶
Return newly created
ErrorDescriptionassociated with message.- Return type:
ErrorDescription
- class saturnin.protocol.fbsp.FBSPSession[source]¶
Bases:
SessionFBSP session that holds information about attached peer.
- greeting: HelloMessage¶
HelloMessage(for service sessions) orWelcomeMessage(for client sessions) received from peer during handshake.
- class saturnin.protocol.fbsp._FBSP(*args, **kwargs)[source]¶
Bases:
Protocol4/FBSP - Firebird Butler Service Protocol
- _FBSP__message_factory(zmsg: TypeAliasForwardRef('~saturnin.base.transport.TZMQMessage') | None = None) Message¶
Internal message factory.
- Parameters:
zmsg (TypeAliasForwardRef('~saturnin.base.transport.TZMQMessage') | None) – The raw ZMQ message list, used here to extract the header for determining message type and initializing the correct
FBSPMessagesubclass.- Return type:
- create_ack_reply(msg: FBSPMessage) FBSPMessage[source]¶
Returns new message that is an ACK-REPLY response message.
- Parameters:
msg (FBSPMessage) – Message to be acknowledged.
- Return type:
- create_data_for(msg: APIMessage) DataMessage[source]¶
Create new DataMessage for reply to specific reuest message.
- Parameters:
message – Request message instance that data relates to
msg (APIMessage)
- Return type:
- create_message_for(message_type: MsgType, token: Token = b'\x00\x00\x00\x00\x00\x00\x00\x00', type_data: int = 0, flags: MsgFlag = <MsgFlag.NONE: 0>) FBSPMessage[source]¶
Create new
FBSPMessagechild class instance for particular FBSP message type.- Parameters:
- Returns:
New
FBSPMessagesubclass instance, initialized with the provided header details.- Return type:
- validate(zmsg: TZMQMessage) None[source]¶
Verifies that sequence of ZMQ data frames is a valid FBSP protocol message.
If this validation passes without exception, then
convert_msg()of the same message must be successful as well.- Parameters:
zmsg (TZMQMessage) – ZeroMQ multipart message to be validated against FBSP rules (FourCC, version, header structure, type-specific frames).
- Raises:
InvalidMessageError – If ZMQ message is not a valid FBSP message.
- Return type:
None
- MESSAGE_MAP: ClassVar[dict[MsgType, FBSPMessage]] = {MsgType.HELLO: <class 'saturnin.protocol.fbsp.HelloMessage'>, MsgType.WELCOME: <class 'saturnin.protocol.fbsp.WelcomeMessage'>, MsgType.NOOP: <class 'saturnin.protocol.fbsp.FBSPMessage'>, MsgType.REQUEST: <class 'saturnin.protocol.fbsp.APIMessage'>, MsgType.REPLY: <class 'saturnin.protocol.fbsp.APIMessage'>, MsgType.DATA: <class 'saturnin.protocol.fbsp.DataMessage'>, MsgType.CANCEL: <class 'saturnin.protocol.fbsp.CancelMessage'>, MsgType.STATE: <class 'saturnin.protocol.fbsp.StateMessage'>, MsgType.CLOSE: <class 'saturnin.protocol.fbsp.FBSPMessage'>, MsgType.ERROR: <class 'saturnin.protocol.fbsp.ErrorMessage'>}¶
Mapping from message type to specific Message class
- class saturnin.protocol.fbsp.FBSPService(*args, **kwargs)[source]¶
Bases:
_FBSP4/FBSP - Firebird Butler Service Protocol - Service side.
- __init__(*, session_type: type[FBSPSession] = <class 'saturnin.protocol.fbsp.FBSPSession'>, service: ServiceDescriptor, peer: PeerDescriptor)[source]¶
- Parameters:
session_type (type[FBSPSession]) – Class for session objects.
service (ServiceDescriptor) – Agent descriptor for service.
peer (PeerDescriptor) – Peer descriptor for service.
- accept_new_session(channel: Channel, routing_id: RoutingID, msg: FBSPMessage) bool[source]¶
Validates incoming message that initiated new session/transmission.
Calls
on_accept_clientevent handler that may reject the client by raisingStopErrorexception with appropriate error code to be sent to the client in ERROR message.- Parameters:
channel (Channel) – Channel that received the message.
routing_id (RoutingID) – Routing ID of the sender.
msg (FBSPMessage) – Received message.
- Return type:
- close(channel: Channel)[source]¶
Close all connections to attached clients.
- Parameters:
channel (Channel) – Channel used for transmission
- create_error_for(msg: FBSPMessage, error_code: ErrorCode) ErrorMessage[source]¶
Create new ErrorMessage that relates to specific message.
- Parameters:
msg (FBSPMessage) –
FBSPMessageinstance that error relates toerror_code (ErrorCode) – Error code
- Return type:
- create_reply_for(msg: APIMessage) APIMessage[source]¶
Create new ReplyMessage for specific RequestMessage.
- Parameters:
msg (APIMessage) – Request message instance that reply relates to
- Return type:
- create_state_for(msg: APIMessage, state: State) StateMessage[source]¶
Create new
StateMessagethat relates to specificRequestMessage.- Parameters:
msg (APIMessage) – Request message instance that state relates to
state (State) – State code
- Return type:
- create_welcome_reply(msg: HelloMessage) WelcomeMessage[source]¶
Create new
WelcomeMessagethat is a reply to client’s HELLO.- Parameters:
msg (HelloMessage) –
HelloMessagefrom the client- Return type:
- handle_ack_reply(channel: Channel, session: FBSPSession, msg: FBSPMessage) None[source]¶
Process ACK-REPLY messages received from client.
If message has ACK-REPLY flag set, it calls
on_ack_receivedevent handler. Otherwise it raisesStopErrorwith PROTOCOL_VIOLATION error code.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
None
Note
All exceptions raised in handler are handled by
handle_exception.
- handle_cancel_msg(channel: Channel, session: FBSPSession, msg: CancelMessage) None[source]¶
Process
CANCELmessage received from peer.Calls
on_cancelevent handler. According to FBSP, the service must send the ERROR message with appropriate error code (if request was successfuly cancelled, the code must be17 - Request Cancelled). To follow the common pattern, the event handler must raise anStopErrorexception withcode attributeset to the error code to be returned in ERROR message.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (CancelMessage) – Received message.
- Raises:
StopError – With error code
4 - Not Implementedif event handler is not defined.- Return type:
None
Note
All exceptions raised in handler are handled by
handle_exception.
- handle_close_msg(channel: Channel, session: FBSPSession, msg: FBSPMessage) None[source]¶
Process
CLOSEmessage received from client.Calls
on_session_closedevent handler and then discards the session.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
None
Important
All exceptions raised by event handler are silently ignored.
- handle_data_msg(channel: Channel, session: FBSPSession, msg: DataMessage) None[source]¶
Process
DATAmessage received from peer.If message is an ACK-REPLY, it calls
on_ack_receivedevent handler. Otherwise it callson_dataevent handler (that must also handle the ACK-REQ if set).If
on_dataevent handler is not defined, the handler sends ERROR message with code2 - Protocol violationby raising theStopError.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (DataMessage) – Received message.
- Return type:
None
Note
All exceptions raised in handler are handled by
handle_exception.
- handle_exception(channel: Channel, session: Session, msg: Message, exc: Exception) None[source]¶
Called by
handle_msg()on exception in message handler.Sends
ERRORmessage and then callson_exceptionhandler.Important
The error code is extracted ONLY from
StopErrorexception. Other exception types will result in6 - Internal Service Errorerror code.
- handle_hello_msg(channel: Channel, session: FBSPSession, msg: HelloMessage) None[source]¶
Process
HELLOmessage received from client.Sends
WELCOMEmessage to the client.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (HelloMessage) – Received message.
- Return type:
None
Note
The HELLO message was preprocessed and approved by
accept_new_session().Note
All exceptions raised in handler are handled by
handle_exception.
- handle_noop_msg(channel: Channel, session: FBSPSession, msg: FBSPMessage) None[source]¶
Process
NOOPmessage received from client.If message is an ACK-REPLY, it calls
on_ack_receivedevent handler. Otherwise it sends ACK-REPLY if requested and then callson_noopevent handler.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
None
Note
All exceptions raised in handler are handled by
handle_exception.
- handle_request_msg(channel: Channel, session: FBSPSession, msg: APIMessage) None[source]¶
Process
REQUESTmessage received from client.Calls the appropriate API handler set via
register_api_handler().Important
The registered API handler is responsible for sending the
ACK-REPLYmessage if the incoming requestmsghas theACK_REQflag set. According to FBSP specification, the request should be acknowledged once the Service has decided to accept it and before starting fulfillment. The handler may also send anERRORmessage instead of anACK-REPLY.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (APIMessage) – Received message.
- Return type:
None
Note
All exceptions raised in handler are handled by
handle_exception.
- handle_unexpected_msg(channel: Channel, session: FBSPSession, msg: FBSPMessage) None[source]¶
Process unexpected valid message received from client.
Sends
ERRORmessage to the client with error code2 - Protocol violation.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
None
- register_api_handler(api_code: ButlerInterface, handler: Callable) None[source]¶
Register handler for REQUEST message for particular service API code.
- Parameters:
api_code (ButlerInterface) – API code.
handler (Callable) – A callable that handles the API code matching the signature (channel: Channel, session: FBSPSession, msg: APIMessage) -> None
- Return type:
None
- send_close(channel: Channel, session: FBSPSession) None[source]¶
Sends
CLOSEmessage to the client associated with session and callson_session_closedevent handler. The message passed to the event handler is the initialHELLOmessage from client.- Parameters:
channel (Channel) – Channel associate with data pipe.
session (FBSPSession) – Session associated with transmission.
- Return type:
None
Important
All exceptions raised by event handler are silently ignored.
- send_error(channel: Channel, session: FBSPSession, relates_to: FBSPMessage, error_code: ErrorCode, exc: Exception | None = None) None[source]¶
Sends
ERRORmessage to the client associated with session.- Parameters:
channel (Channel) – Channel associate with data pipe.
session (FBSPSession) – Session associated with transmission.
relates_to (FBSPMessage) – FBSP message to which this ERROR is related.
error_code (ErrorCode) – Error code.
exc (Exception | None) – Exception that caused the error.
- Return type:
None
- on_accept_client¶
eventsocketcalled when HELLO message is received from client.- Parameters:
channel – Channel that received the message.
msg – Received message.
The event handler may reject the client by raising the
StopErrorexception withcodeattribute containing theErrorCodeto be returned in ERROR message.If event handler does not raise an exception, the client is accepted, new session is created and WELCOME message is sent to the client.
- on_ack_received¶
eventsocketcalled when ACK-REPLY NOOP, DATA, REPLY or STATE message is received.- Parameters:
channel – Channel that received the message.
session – Session instance.
msg – Received ACK-REPLY message.
Note
All exceptions raised by handler are handled by
handle_exception.
- on_cancel¶
eventsocketcalled when CANCEL message is received from client.- Parameters:
channel – Channel that received the message.
session – Session instance.
msg – Received message.
The event handler must raise an
Errorexception withcodeattribute set to the error code to be returned in ERROR message. If request was successfuly cancelled, the code must beErrorCode.REQUEST_CANCELLED.
- on_data¶
eventsocketcalled when DATA message is received.Important
If handler is defined, it must send the ACK-REPLY if received message has ACK-REQ set. It’s up to service interface whether ACK-REPLY would be sent before, or after data contained within message are processed.
- Parameters:
channel – Channel that received the message.
session – Session instance.
msg – Received message.
Note
All exceptions raised in handler are handled by
handle_exception.
- on_noop¶
eventsocketcalled when NOOP message is received, and after ACK-REPLY (if requested) is send.- Parameters:
channel – Channel associated with data pipe.
session – Session associated with peer.
Note
All exceptions raised in handler are handled by
handle_exception.
- on_session_closed¶
eventsocketcalled when CLOSE message is received or sent, to release any resources associated with current transmission.- Parameters:
channel – Channel associated with data pipe.
session – Session associated with peer.
msg – Received/sent CLOSE message.
exc – Exception that caused the error.
Note
All exceptions raised in handler are handled by
handle_exception.
- welcome_df: FBSPWelcomeDataframe¶
- class saturnin.protocol.fbsp.FBSPClient(*args, **kwargs)[source]¶
Bases:
_FBSP4/FBSP - Firebird Butler Service Protocol - Client side.
This is the RAW version of client side FBSP protocol for clients that directly process FBSP messages received from service. Such clients call
Channel.receive()and then process the returned message or sentinel.This protocol implementation handles only essential message processing:
Message parsing.
ACK-REQ for received STATE and NOOP messages.
Returns INVALID sentinel for received HELLO and CANCEL messages.
Closes the session when CLOSE message is received.
- __init__(*, session_type: type[FBSPSession] = <class 'saturnin.protocol.fbsp.FBSPSession'>)[source]¶
- Parameters:
session_type (type[FBSPSession]) – Class for session objects.
- create_request_for(session: FBSPSession, api_code: ButlerInterface, token: Token) APIMessage[source]¶
Returns new
REQUESTmessage for specific API call.- Parameters:
session (FBSPSession) – Session instance
api_code (ButlerInterface) – API Code
token (Token) – Message token
- Return type:
- exception_for(msg: ErrorMessage) ServiceError[source]¶
Returns a .ServiceError exception populated with details from the provided ERROR message.
- Parameters:
msg (ErrorMessage) – ERROR message received
- Return type:
- handle_close_msg(channel: Channel, session: FBSPSession, msg: FBSPMessage) FBSPMessage[source]¶
Process
CLOSEmessage received from service.Discards the session and returns the CLOSE message received.
- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
- handle_fbsp_msg(channel: Channel, session: FBSPSession, msg: FBSPMessage) FBSPMessage[source]¶
FBSP message handler that simply returns received message.
If message is a
STATEmessage with ACK-REQ, sends ACK-REPLY. ACK-REQ for other messages must be handled by caller.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
Note
All exceptions raised in handler are handled by
handle_exception.
- handle_noop_msg(channel: Channel, session: FBSPSession, msg: FBSPMessage) None[source]¶
Process
NOOPmessage received from service.Sends ACK-REPLY if requested.
- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
None
Note
All exceptions are handled by
handle_exception.
- handle_unexpected_msg(channel: Channel, session: FBSPSession, msg: FBSPMessage) FBSPMessage[source]¶
FBSP message handler that returns INVALID sentinel.
- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
- handle_welcome_msg(channel: Channel, session: FBSPSession, msg: WelcomeMessage) WelcomeMessage[source]¶
Process
WELCOMEmessage received from service.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (WelcomeMessage) – Received message.
- Return type:
- has_api(api: type[ButlerInterface]) bool[source]¶
Returns True if attached service supports specified interface.
- Parameters:
api (type[ButlerInterface]) – Service API (interface) enumeration.
- Return type:
- send_close(channel: Channel, session: FBSPSession) None[source]¶
Sends
CLOSEmessage to the service associated with session.- Parameters:
channel (Channel) – Channel associate with data pipe.
session (FBSPSession) – Session associated with transmission.
- Return type:
None
- send_hello(channel: Channel, session: FBSPSession, agent: AgentDescriptor, peer: PeerDescriptor, token: Token = b'\x00\x00\x00\x00\x00\x00\x00\x00') None[source]¶
Sends
HELLOmessage to the service.- Parameters:
channel (Channel) – Channel used for connection to the service.
session (FBSPSession) – Session associated with service.
agent (AgentDescriptor) – Agent descriptor for client.
peer (PeerDescriptor) – Peer descriptor for client.
token (Token) – FBSP message token to be used in HELLO message.
- Return type:
None
- class saturnin.protocol.fbsp.FBSPEventClient(*args, **kwargs)[source]¶
Bases:
FBSPClient4/FBSP - Firebird Butler Service Protocol - Client side.
This is the EVENT version of client side FBSP protocol for clients that process FBSP messages received from service indirectly. Such clients use central I/O loop that process incomming messages in uniform way, and actual processing is done via event handlers.
- __init__(*, session_type: type[FBSPSession] = <class 'saturnin.protocol.fbsp.FBSPSession'>)[source]¶
- Parameters:
session_type (type[FBSPSession]) – Class for session objects.
- handle_ack_reply(channel: Channel, session: FBSPSession, msg: FBSPMessage) None[source]¶
Process ACK-REPLY messages received from service.
If message has ACK-REPLY flag set, it calls
on_ack_receivedevent handler. Otherwise it’s ignored because it violates the protocol.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
None
Note
All exceptions are handled by
handle_exception.
- handle_close_msg(channel: Channel, session: FBSPSession, msg: FBSPMessage) None[source]¶
Process
CLOSEmessage received from service.Calls
on_session_closedevent handler and then discards the session.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
None
- handle_data_msg(channel: Channel, session: FBSPSession, msg: DataMessage) None[source]¶
Process
DATAmessage received from serviec.If message is an ACK-REPLY, it calls
on_ack_receivedevent handler. Otherwise it callson_dataevent handler that must handle the ACK-REQ if set. Ifon_dataevent handler is not defined, the ACK-REPLY message is send by this handler.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (DataMessage) – Received message.
- Return type:
None
Note
All exceptions are handled by
handle_exception.
- handle_error_msg(channel: Channel, session: FBSPSession, msg: ErrorMessage) None[source]¶
Process
ERRORmessage received from service.Calls
on_errorevent handler.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (ErrorMessage) – Received message.
- Return type:
None
Note
All exceptions are handled by
handle_exception.
- handle_noop_msg(channel: Channel, session: FBSPSession, msg: FBSPMessage) None[source]¶
Process
NOOPmessage received from service.If message is an ACK-REPLY, it calls
on_ack_receivedevent handler. Otherwise it sends ACK-REPLY if requested and then callson_noopevent handler.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (FBSPMessage) – Received message.
- Return type:
None
Note
All exceptions are handled by
handle_exception.
- handle_reply_msg(channel: Channel, session: FBSPSession, msg: APIMessage) None[source]¶
Process
REPLYmessage received from service.Calls the appropriate API handler set via
register_api_handler().Important
The registered API handler must send the
ACK-REPLYmessage if requested. According to FBSP specification, the reply must be acknowledged without any delay, unless a previous agreement between the Client and the Service exists to handle it differently (for example when Client is prepared to accept subsequentDATAor other messages from Service).- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (APIMessage) – Received message.
- Return type:
None
Note
All exceptions are handled by
handle_exception.
- handle_state_msg(channel: Channel, session: FBSPSession, msg: StateMessage) None[source]¶
Process
STATEmessage received from service.Sends ACK-REPLY if requested and calls
on_stateevent handler.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (StateMessage) – Received message.
- Return type:
None
Note
All exceptions are handled by
handle_exception.
- handle_unexpected_msg(channel: Channel, session: FBSPSession, msg: HelloMessage) None[source]¶
Process unexpected valid message received from service.
Raises
StopErrorwith error code2 - Protocol violation.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (HelloMessage) – Received message.
- Return type:
None
Note
All exceptions are handled by
handle_exception.
- handle_welcome_msg(channel: Channel, session: FBSPSession, msg: WelcomeMessage) None[source]¶
Process
WELCOMEmessage received from service.Calls
on_service_connectedevent handler.- Parameters:
channel (Channel) – Channel that received the message.
session (FBSPSession) – Session instance.
msg (WelcomeMessage) – Received message.
- Return type:
None
- register_api_handler(api_code: ButlerInterface, handler: Callable) None[source]¶
Register handler for response messages for particular service API code.
- Parameters:
api_code (ButlerInterface) – API code.
handler (Callable) – Callable that handles the API code matching the signature (channel: Channel, session: FBSPSession, msg: FBSPMessage) -> None.
- Return type:
None
- on_ack_received¶
eventsocketcalled when ACK-REPLY message is received.- Parameters:
channel – Channel that received the message.
session – Session instance.
msg – Received ACK-REPLY message.
Note
All exceptions are handled by
handle_exception.
- on_data¶
eventsocketcalled whenDATAmessage is received.Important
If handler is defined, it must send the ACK-REPLY if received message has ACK-REQ set. It’s up to service interface whether ACK-REPLY would be sent before, or after data contained within message are processed.
- Parameters:
channel – Channel that received the message.
session – Session instance.
msg – Received message.
Note
All exceptions are handled by
handle_exception.
- on_error¶
eventsocketcalled whenERRORmessage is received.- Parameters:
channel – Channel that received the message.
session – Session instance.
msg – Received message.
Note
All exceptions are handled by
handle_exception.
- on_noop¶
eventsocketcalled whenNOOPmessage is received, and after ACK-REPLY (if requested) is send.- Parameters:
channel – Channel associated with data pipe.
session – Session associated with peer.
- on_service_connected¶
eventsocketcalled whenWELCOMEmessage is received from service.- Parameters:
channel – Channel that received the message.
session – Session instance.
msg – Received message.
protocol – This FBSPEventClient instance, passed to allow the handler to make further protocol calls if needed.
The event handler should register all service API handlers for
REPLYmessages (seeFBSPEventClient.register_api_handler()).
- on_session_closed¶
eventsocketcalled whenCLOSEmessage is received or sent, to release any resources associated with current transmission.- Parameters:
channel – Channel associated with data pipe.
session – Session associated with peer.
msg – Received/sent CLOSE message.
exc – Exception that caused the error.
- on_state¶
eventsocketcalled whenSTATEmessage is received.- Parameters:
channel – Channel that received the message.
session – Session instance.
msg – Received message.
Note
All exceptions are handled by
handle_exception.