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

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

Source§

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.

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
§

fn send_text( &mut self, topic: &str, text: &str, ) -> impl Future<Output = Result<(), Error>> + Send

Publishes text to a topic: words, or a number written out. 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.