MessagesΒΆ
BaseMessage
is a base class for all BCI Framework messages.
Every message should inherit this class. Every message has a :attribute:`sender` field.
To add more fields to your message you have to define them using a Field
class:
class BrokerHelloMsg(BaseMessage):
__TYPE__ = 'BROKER_HELLO'
peer_url = Field(List[str], str)
broker_url = Field(str)
Field
class accepts a number of arguments: accepted types of this field. If you add None
to the types it will mean that this field is optional. During instantialization of the message fields are checked
for validity. All types should be serializable to JSON (the default serializer). If you need to have a message with
custom data you should provide serialize_data()
and deserialize_data()
methods.
__TYPE__ is an optional attribute which you can provide if you need specific network header in the message (ex. external Peers in other langueges, etc). If you don’t provide it __TYPE__ will be autogenerated by converting message class name from CamelCase to snake_case.
eg: BrokerHelloMsg would be: broker_hello_msg
On the networking level every message is sent in 2 parts as ZMQ message/packet. First part is the header (ASCII): type_string^sender_id_string^, second part is the serialized data (JSON or your custom serialized data).
BCI Framework peers can send any message in one of two modes:
- broadcast mode - message is sent using
Peer.send_message
and delivered only to Peers that subscribed to such messages. Peer.query
- direct mode - message is send directly (using REQ-REP sockets) to some other peer or Broker.
Peers can subscribe to message type and register a handler for it by calling
Peer.subscribe_for_specific_msg_subtype
method,
Peer.subscribe_for_all_msg_subtype
method
or by decorating handler method with subscribe_message_handler()
.
Technically message is send using PUB socket connected to Broker’s XSUB socket and can received by SUB sockets inside peers that connect to Broker’s XPUB.