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
impl CoapServer
Sourcepub fn publisher(&self) -> CoapPublisher
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.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Reports whether the server holds a bound socket.
§Returns
true from a successful connect until
disconnect.
Sourcepub fn local_addr(&self) -> Option<SocketAddr>
pub fn local_addr(&self) -> Option<SocketAddr>
Sourcepub async fn disconnect(&mut self) -> Result<()>
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
impl Receive for CoapServer
Source§impl Transport for CoapServer
impl Transport for CoapServer
Source§async fn send(&mut self, topic: &str, payload: &[u8]) -> Result<()>
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.