pub struct CoAPClient<T: ClientTransport> { /* private fields */ }
Implementations§
Source§impl CoAPClient<UdpTransport>
impl CoAPClient<UdpTransport>
pub async fn new_with_specific_source<A: ToSocketAddrs, B: ToSocketAddrs>( bind_addr: A, peer_addr: B, ) -> IoResult<Self>
pub async fn new_udp<A: ToSocketAddrs>(addr: A) -> IoResult<Self>
Sourcepub async fn new_with_std_socket<A: ToSocketAddrs>(
socket: UdpSocket,
peer_addr: A,
) -> IoResult<Self>
pub async fn new_with_std_socket<A: ToSocketAddrs>( socket: UdpSocket, peer_addr: A, ) -> IoResult<Self>
Create a client with a std::net
socket
Using a standard socket is useful to get advanced features from socket2 crate
§Examples
use socket2::{Socket, Domain, Type};
use coap::UdpCoAPClient;
let socket = Socket::new(Domain::IPV6, Type::DGRAM, None).expect("Standard socket creation failed");
socket.set_multicast_hops_v6(16).expect("Setting multicast hops failed");
let client = UdpCoAPClient::new_with_std_socket(socket.into(), "[::1]:5683").await.expect("Client creation failed");
Sourcepub async fn send_all_coap(
&self,
request: &mut CoapRequest<SocketAddr>,
segment: u8,
) -> IoResult<()>
pub async fn send_all_coap( &self, request: &mut CoapRequest<SocketAddr>, segment: u8, ) -> IoResult<()>
Send a request to all CoAP devices.
- IPv4 AllCoAP multicast address is ‘224.0.1.187’
- IPv6 AllCoAp multicast addresses are ‘ff0?::fd’ Parameter segment is used with IPv6 to determine the first octet. It’s value can be between 0x0 and 0xf. To address multiple segments, you have to call send_all_coap for each of the segments.
Sourcepub async fn send_multicast(
&self,
request: &mut CoapRequest<SocketAddr>,
addr: &SocketAddr,
) -> IoResult<()>
pub async fn send_multicast( &self, request: &mut CoapRequest<SocketAddr>, addr: &SocketAddr, ) -> IoResult<()>
Send a multicast request to multiple devices.
pub fn set_broadcast(&self, value: bool) -> IoResult<()>
Sourcepub async fn create_receiver_for(
&self,
request: &CoapRequest<SocketAddr>,
) -> MessageReceiver
pub async fn create_receiver_for( &self, request: &CoapRequest<SocketAddr>, ) -> MessageReceiver
creates a receiver based on a specific request this method can be used if you send a multicast request and expect multiple responses. only use this method if you know what you are doing
use coap_lite::{
RequestType
};
use coap::request::RequestBuilder;
use coap::client::UdpCoAPClient;
async fn foo() {
let segment = 0x0;
let client = UdpCoAPClient::new_udp("127.0.0.1:5683")
.await
.unwrap();
let mut request = RequestBuilder::new("test-echo", RequestType::Get)
.data(Some(vec![0x51, 0x55, 0x77, 0xE8]))
.confirmable(true)
.build();
let mut receiver = client.create_receiver_for(&request).await;
client.send_all_coap(&mut request, segment).await.unwrap();
loop {
let recv_packet = receiver.receive().await.unwrap();
assert_eq!(recv_packet.message.payload, b"test-echo".to_vec());
}
}
Source§impl CoAPClient<DtlsConnection>
impl CoAPClient<DtlsConnection>
pub async fn from_udp_dtls_config(config: UdpDtlsConfig) -> IoResult<Self>
Source§impl<T: ClientTransport + 'static> CoAPClient<T>
impl<T: ClientTransport + 'static> CoAPClient<T>
Sourcepub fn from_transport(transport: T) -> Self
pub fn from_transport(transport: T) -> Self
Create a CoAP client with a chosen transport type
Sourcepub async fn get(url: &str) -> IoResult<CoapResponse>
pub async fn get(url: &str) -> IoResult<CoapResponse>
Execute a single get request with a coap url
Sourcepub async fn get_with_timeout(
url: &str,
timeout: Duration,
) -> IoResult<CoapResponse>
pub async fn get_with_timeout( url: &str, timeout: Duration, ) -> IoResult<CoapResponse>
Execute a single get request with a coap url and a specific timeout.
Sourcepub async fn post(url: &str, data: Vec<u8>) -> IoResult<CoapResponse>
pub async fn post(url: &str, data: Vec<u8>) -> IoResult<CoapResponse>
Execute a single post request with a coap url using udp
Sourcepub async fn post_with_timeout(
url: &str,
data: Vec<u8>,
timeout: Duration,
) -> IoResult<CoapResponse>
pub async fn post_with_timeout( url: &str, data: Vec<u8>, timeout: Duration, ) -> IoResult<CoapResponse>
Execute a single post request with a coap url using udp
Sourcepub async fn put(url: &str, data: Vec<u8>) -> IoResult<CoapResponse>
pub async fn put(url: &str, data: Vec<u8>) -> IoResult<CoapResponse>
Execute a put request with a coap url using udp
Sourcepub async fn put_with_timeout(
url: &str,
data: Vec<u8>,
timeout: Duration,
) -> IoResult<CoapResponse>
pub async fn put_with_timeout( url: &str, data: Vec<u8>, timeout: Duration, ) -> IoResult<CoapResponse>
Execute a single put request with a coap url using udp
Sourcepub async fn delete(url: &str) -> IoResult<CoapResponse>
pub async fn delete(url: &str) -> IoResult<CoapResponse>
Execute a single delete request with a coap url using udp
Sourcepub async fn delete_with_timeout(
url: &str,
timeout: Duration,
) -> IoResult<CoapResponse>
pub async fn delete_with_timeout( url: &str, timeout: Duration, ) -> IoResult<CoapResponse>
Execute a single delete request with a coap url using udp
Sourcepub async fn request(
url: &str,
method: Method,
data: Option<Vec<u8>>,
) -> IoResult<CoapResponse>
pub async fn request( url: &str, method: Method, data: Option<Vec<u8>>, ) -> IoResult<CoapResponse>
Execute a single request (GET, POST, PUT, DELETE) with a coap url using udp
Sourcepub async fn request_with_timeout(
url: &str,
method: Method,
data: Option<Vec<u8>>,
timeout: Duration,
) -> IoResult<CoapResponse>
pub async fn request_with_timeout( url: &str, method: Method, data: Option<Vec<u8>>, timeout: Duration, ) -> IoResult<CoapResponse>
Execute a single request (GET, POST, PUT, DELETE) with a coap url and a specfic timeout using udp
Sourcepub async fn send(
&self,
request: CoapRequest<SocketAddr>,
) -> IoResult<CoapResponse>
pub async fn send( &self, request: CoapRequest<SocketAddr>, ) -> IoResult<CoapResponse>
Send a Request via the given transport, and receive a response. users are responsible for filling meaningful fields in the request this method supports blockwise requests
pub async fn observe<H: FnMut(Message) + Send + 'static>( &self, resource_path: &str, handler: H, ) -> IoResult<Sender<ObserveMessage>>
Sourcepub async fn observe_with_timeout<H: FnMut(Message) + Send + 'static>(
&mut self,
resource_path: &str,
handler: H,
timeout: Duration,
) -> IoResult<Sender<ObserveMessage>>
pub async fn observe_with_timeout<H: FnMut(Message) + Send + 'static>( &mut self, resource_path: &str, handler: H, timeout: Duration, ) -> IoResult<Sender<ObserveMessage>>
Observe a resource with the handler and specified timeout using the given transport. Use the oneshot sender to cancel observation. If this sender is dropped without explicitly cancelling it, the observation will continue forever.
Sourcepub async fn observe_with<H: FnMut(Message) + Send + 'static>(
&self,
request: CoapRequest<SocketAddr>,
handler: H,
) -> IoResult<Sender<ObserveMessage>>
pub async fn observe_with<H: FnMut(Message) + Send + 'static>( &self, request: CoapRequest<SocketAddr>, handler: H, ) -> IoResult<Sender<ObserveMessage>>
observe a resource with a given transport using your own request Use this method if you need to set some specific options in your requests. This method will add observe flags and a message id as a fallback Use this method if you plan on re-using the same client for requests
Sourcepub async fn send_single_request(
&self,
request: &CoapRequest<SocketAddr>,
) -> IoResult<CoapResponse>
pub async fn send_single_request( &self, request: &CoapRequest<SocketAddr>, ) -> IoResult<CoapResponse>
sends a request through the transport. If a request is confirmable, it will attempt retries until receiving a response. requests sent using a multicast-address should be non-confirmable the user is responsible for setting meaningful fields in the request Do not use this method unless you need low-level control over the protocol (e.g., multicast), instead use send for client applications.
Sourcepub fn set_receive_timeout(&mut self, dur: Duration)
pub fn set_receive_timeout(&mut self, dur: Duration)
Set the receive timeout.
pub fn set_transport_retries(&mut self, num_retries: usize)
Sourcepub fn set_block1_size(&mut self, block1_max_bytes: usize)
pub fn set_block1_size(&mut self, block1_max_bytes: usize)
Set the maximum size for a block1 request. Default is 1024 bytes