async_coap_tokio/lib.rs
1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16//! This crate provides [`TokioAsyncUdpSocket`]\: an asynchronous, [Tokio][]-based
17//! implementation of [`AsyncDatagramSocket`] for use with [`DatagramLocalEndpoint`].
18//!
19//! # Example
20//!
21//! ```no_run
22//! use async_coap::prelude::*;
23//! use async_coap::datagram::DatagramLocalEndpoint;
24//! use async_coap_tokio::TokioAsyncUdpSocket;
25//! use futures::prelude::*;
26//! use std::sync::Arc;
27//! use tokio::executor::spawn;
28//!
29//! #[tokio::main]
30//! async fn main() {
31//! let socket = TokioAsyncUdpSocket::bind("[::]:0")
32//! .expect("UDP bind failed");
33//!
34//! // Create a new local endpoint from the socket we just created,
35//! // wrapping it in a `Arc<>` to ensure it can live long enough.
36//! let local_endpoint = Arc::new(DatagramLocalEndpoint::new(socket));
37//!
38//! // Add our local endpoint to the pool, so that it
39//! // can receive packets.
40//! spawn(
41//! local_endpoint
42//! .clone()
43//! .receive_loop_arc(null_receiver!())
44//! .map(|err| panic!("Receive loop terminated: {}", err)),
45//! );
46//!
47//! // Create a remote endpoint instance to represent the
48//! // device we wish to interact with.
49//! let remote_endpoint = local_endpoint
50//! .remote_endpoint_from_uri(uri!("coap://coap.me"))
51//! .expect("Unacceptable scheme or authority in URL");
52//!
53//! // Create a future that sends a request to a specific path
54//! // on the remote endpoint, collecting any blocks in the response
55//! // and returning `Ok(OwnedImmutableMessage)` upon success.
56//! let future = remote_endpoint.send_to(
57//! rel_ref!("large"),
58//! CoapRequest::get() // This is a CoAP GET request
59//! .accept(ContentFormat::TEXT_PLAIN_UTF8) // We only want plaintext
60//! .block2(Some(Default::default())) // Enable block2 processing
61//! .emit_successful_collected_response(), // Collect all blocks
62//! );
63//!
64//! // Wait until we get the result of our request.
65//! let result = future.await;
66//!
67//! assert!(result.is_ok(), "Error: {:?}", result.err().unwrap());
68//! }
69//! ```
70//!
71//! [`AsyncDatagramSocket`]: async-coap::datagram::AsyncDatagramSocket
72//! [`DatagramLocalEndpoint`]: async-coap::datagram::DatagramLocalEndpoint
73//! [Tokio]: https://tokio.rs/
74
75mod tokio_async_udp_socket;
76pub use tokio_async_udp_socket::TokioAsyncUdpSocket;