From ece3d26d1276cb55a4075e5f90c30baa4676e530 Mon Sep 17 00:00:00 2001 From: Covertness <wuyingfengsui@gmail.com> Date: Sun, 16 Aug 2015 16:43:55 +0800 Subject: [PATCH] add reply API for client --- Cargo.toml | 2 +- README.md | 2 +- examples/client_and_server.rs | 14 ++------------ src/client.rs | 18 ++++++++++++++++++ src/lib.rs | 2 +- src/server.rs | 9 +-------- 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 30a5083..56ab3c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "coap" -version = "0.2.0" +version = "0.2.1" description = "A CoAP library" readme = "README.md" documentation = "http://covertness.github.io/coap-rs/coap/index.html" diff --git a/README.md b/README.md index e7f3f11..41311e6 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ First add this to your `Cargo.toml`: ```toml [dependencies] -coap = "0.2.0" +coap = "0.2.1" ``` Then, add this to your crate root: diff --git a/examples/client_and_server.rs b/examples/client_and_server.rs index cb4093b..b8550e3 100644 --- a/examples/client_and_server.rs +++ b/examples/client_and_server.rs @@ -4,18 +4,8 @@ use coap::packet::*; use coap::{CoAPServer, CoAPClient}; fn request_handler(req: Packet, resp: CoAPClient) { - let uri_path = req.get_option(OptionType::UriPath); - assert!(uri_path.is_some()); - let uri_path = uri_path.unwrap(); - let mut packet = Packet::new(); - - packet.header.set_version(1); - packet.header.set_type(PacketType::Acknowledgement); - packet.header.set_code("2.05"); - packet.header.set_message_id(req.header.get_message_id()); - packet.set_token(req.get_token().clone()); - packet.payload = uri_path.front().unwrap().clone(); - resp.send(&packet).unwrap(); + let uri_path = req.get_option(OptionType::UriPath).unwrap(); + resp.reply(&req, uri_path.front().unwrap().clone()).unwrap(); } fn main() { diff --git a/src/client.rs b/src/client.rs index 152e08f..d92077c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -88,6 +88,24 @@ impl CoAPClient { } } + /// Response the client with the specifc payload. + pub fn reply(&self, request_packet: &Packet, payload: Vec<u8>) -> Result<()> { + let mut packet = Packet::new(); + + packet.header.set_version(1); + let response_type = match request_packet.header.get_type() { + PacketType::Confirmable => PacketType::Acknowledgement, + PacketType::NonConfirmable => PacketType::NonConfirmable, + _ => return Err(Error::new(ErrorKind::InvalidInput, "request type error")) + }; + packet.header.set_type(response_type); + packet.header.set_code("2.05"); + packet.header.set_message_id(request_packet.header.get_message_id()); + packet.set_token(request_packet.get_token().clone()); + packet.payload = payload; + self.send(&packet) + } + /// Execute a request. pub fn send(&self, packet: &Packet) -> Result<()> { match packet.to_bytes() { diff --git a/src/lib.rs b/src/lib.rs index cc5535b..664ace2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ //! //! ```toml //! [dependencies] -//! coap = "0.2.0" +//! coap = "0.2.1" //! ``` //! //! Then, add this to your crate root: diff --git a/src/server.rs b/src/server.rs index ba291f8..4e914e1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -146,15 +146,8 @@ mod test { let uri_path = req.get_option(OptionType::UriPath); assert!(uri_path.is_some()); let uri_path = uri_path.unwrap(); - let mut packet = Packet::new(); - packet.header.set_version(1); - packet.header.set_type(PacketType::Acknowledgement); - packet.header.set_code("2.05"); - packet.header.set_message_id(req.header.get_message_id()); - packet.set_token(req.get_token().clone()); - packet.payload = uri_path.front().unwrap().clone(); - resp.send(&packet).unwrap(); + resp.reply(&req, uri_path.front().unwrap().clone()).unwrap(); } #[test] -- GitLab