pub struct LoopbackTransport { /* private fields */ }Expand description
An in-process transport that routes through a shared LoopbackBroker.
A transport is created disconnected; connect registers
it with the broker so it can publish and receive. Inbound messages are read
with recv.
§Examples
use pamoja_core::{Receive, Transport};
use pamoja_loopback::{LoopbackBroker, LoopbackTransport};
let broker = LoopbackBroker::new();
let mut subscriber = LoopbackTransport::new(broker.clone());
let mut publisher = LoopbackTransport::new(broker);
subscriber.connect().await?;
publisher.connect().await?;
subscriber.subscribe("sensors/+/temperature").await?;
publisher.send("sensors/1/temperature", b"21.5").await?;
let message = subscriber.recv().await?.expect("a message");
assert_eq!(message.topic, "sensors/1/temperature");
assert_eq!(message.payload, b"21.5");Implementations§
Source§impl LoopbackTransport
impl LoopbackTransport
Sourcepub fn new(broker: LoopbackBroker) -> Self
pub fn new(broker: LoopbackBroker) -> Self
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Reports whether the transport is connected to its broker.
§Returns
true once connect has succeeded and before
disconnect is called.
Sourcepub fn disconnect(&mut self)
pub fn disconnect(&mut self)
Disconnects the transport from the broker.
Messages already queued for it are dropped, and its registration is pruned
from the broker on the next publish. Its filters are kept, so a later
connect hears the same topics again.
Trait Implementations§
Source§impl Receive for LoopbackTransport
impl Receive for LoopbackTransport
Source§async fn recv(&mut self) -> Result<Option<Message>>
async fn recv(&mut self) -> Result<Option<Message>>
Awaits the next message from any subscribed topic.
A link holds its broker, so a connected link never ends on its own: this waits until a message arrives, however long that takes. Dropping the future before then leaves the next message queued.
§Returns
Some(message) for the next message.
§Errors
Returns Error::Closed if the transport is
not connected.