pamoja.loopback

Idiomatic loopback-broker facade.

An in-process broker: publish on one link, receive on another, with no broker process, no network, and no hardware. It is what makes a message flow testable from a unit test rather than only from a deployment.

 1"""Idiomatic loopback-broker facade.
 2
 3An in-process broker: publish on one link, receive on another, with no broker
 4process, no network, and no hardware. It is what makes a message flow testable
 5from a unit test rather than only from a deployment.
 6"""
 7
 8from __future__ import annotations
 9
10from pamoja._native import LoopbackBroker, LoopbackTransport, Message
11
12__all__ = [
13    "LoopbackBroker",
14    "LoopbackTransport",
15    "Message",
16]
class LoopbackBroker:

An in-process broker.

Every transport built from one broker shares its traffic, so a message one publishes reaches the others that subscribed to the topic.

def rung(self, /):

Creates a link to this broker as a transport, for composing into a ladder or a wrapper.

reachable

Whether the broker is in reach. Set it to False to put every link on it out of range at once, links a ladder owns included: they fail to connect, send, or subscribe until it is True again, keeping their connections and filters through the outage.

class LoopbackTransport:

One in-process link to a broker.

def connect(self, /):

Marks this link connected so it will carry traffic.

def send(self, /, topic, payload):

Publishes a payload to a topic on the broker.

def subscribe(self, /, topic):

Subscribes this link to a topic.

def recv(self, /):

Waits for the next message on a subscribed topic, or None once closed.

def is_connected(self, /):

Whether this link is connected.

def disconnect(self, /):

Marks this link disconnected, so sends over it fail.

class Message:

A message that arrived on a subscribed topic.

CoAP and the loopback broker both hand back a topic and a payload, so one class serves them rather than a near-identical type per transport.

text

The payload as text: words, or a number written out.

Raises ValueError if the payload is not UTF-8 text.

topic

The topic it was published to.

payload

The raw payload bytes.

number

The payload as a number written out as text, such as 21.5.

Raises ValueError if the payload is not text or the text is not a number.