Skip to main content

CoapServer

Struct CoapServer 

Source
pub struct CoapServer { /* private fields */ }
Expand description

A CoAP server that implements the core [Transport] and [Receive] traits.

It is the far end of a CoapTransport: a gateway that nodes send their readings to and observe their commands on. A PUT or POST to a path matching one of its subscribe filters is answered 2.04 Changed and delivered to recv, and one to any other path is answered 4.04 Not Found. send sets the state of a resource, which a GET reads and every observer of the path is notified of, as RFC 7641 describes.

§Examples

use pamoja_coap::CoapServer;
use pamoja_core::{Receive, Transport};

let mut gateway = CoapServer::new("0.0.0.0:5683");
gateway.connect().await?;
gateway.subscribe("sensors/#").await?;
gateway.send_text("commands/valve", "closed").await?;

while let Some(reading) = gateway.recv().await? {
    println!("{}: {}", reading.topic, reading.text()?);
}

Implementations§

Source§

impl CoapServer

Source

pub fn new(bind: impl Into<String>) -> Self

Creates a server that will listen on a local address, without binding it.

§Arguments
  • bind - the local host:port, such as "0.0.0.0:5683" for the plaintext CoAP port on every interface, or port 0 for a free one.
§Returns

A server ready for connect.

Source

pub fn publisher(&self) -> CoapPublisher

Takes a handle that publishes resource states while a receive waits.

§Returns

A publisher sharing this server’s resources and socket.

Source

pub fn is_connected(&self) -> bool

Reports whether the server holds a bound socket.

§Returns

true from a successful connect until disconnect.

Source

pub fn local_addr(&self) -> Option<SocketAddr>

The address the server listens on, which names the port the system chose when it bound port 0.

§Returns

The local address, or None before connect.

Source

pub fn observers(&self, path: &str) -> usize

Counts the clients observing a resource.

§Arguments
  • path - the resource’s path.
§Returns

How many observers the resource has.

Source

pub async fn disconnect(&mut self) -> Result<()>

Closes the socket and stops the background task.

The resources and filters are kept for the next connect, and the observers are dropped, since their registrations were made to the socket that closed.

§Returns

Ok(()) once the socket is released.

§Errors

This call is best-effort and always returns Ok(()).

Trait Implementations§

Source§

impl Receive for CoapServer

Source§

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

Awaits the next reading sent to a path the server takes.

§Returns

Some(message) for the next reading, carrying the path it was sent to.

§Errors

Returns [Error::Closed] if the server is not connected.

Source§

impl Transport for CoapServer

Source§

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

Binds the server’s socket and starts answering requests.

§Returns

Ok(()) once the socket is bound.

§Errors

Returns [Error::Transport] if the address cannot be bound, such as a port another program holds.

Source§

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

Sets the state of a resource and notifies every observer of it, as CoapPublisher::publish does.

§Arguments
  • topic - the resource’s path.
  • payload - its new state, which a GET now reads.
§Returns

Ok(()) once each observer’s notification has left.

§Errors

Returns [Error::Closed] if the server is not connected.

Source§

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

Takes the readings sent to the paths a filter matches.

§Arguments
  • topic - the filter, with + for one level and # for the rest, as [topic_matches] reads them.
§Returns

Ok(()) once the filter is in place. A server takes readings for its filters whether or not it is connected.

§

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.