Skip to main content

LoopbackTransport

Struct LoopbackTransport 

Source
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::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

Source

pub fn new(broker: LoopbackBroker) -> Self

Creates a disconnected transport bound to broker.

§Arguments
  • broker - the shared broker this transport publishes to and receives from.
§Returns

A disconnected transport ready for connect.

Source

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.

Source

pub async fn recv(&mut self) -> Result<Option<Message>>

Awaits the next message from any subscribed topic.

§Returns

Some(message) for the next message, or None once the broker and all other transports have been dropped.

§Errors

Returns Error::Closed if the transport is not connected.

Source

pub fn disconnect(&mut self)

Disconnects the transport from the broker.

Its registration is pruned from the broker on the next publish.

Trait Implementations§

Source§

impl Transport for LoopbackTransport

Source§

async fn connect(&mut self) -> Result<()>

Establishes the connection to the broker, peer, or bus. Read more
Source§

async fn send(&mut self, topic: &str, payload: &[u8]) -> Result<()>

Publishes a payload to a topic. Read more
Source§

async fn subscribe(&mut self, topic: &str) -> Result<()>

Subscribes to a topic so that matching payloads are routed to this transport. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.