diff --git a/Cargo.toml b/Cargo.toml index 30a508355e95a282038c2eca5540fce189f557d0..56ab3c424333ac8c8b127eed20a2f2ec815e9b11 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 e7f3f118e9be74cedd3b03202c7e4fde939a5916..41311e6267afb5bace8879737a9de7a0cd690837 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 cb4093bcfad2dc7b46b61ab7bdbeb086a4fc8d7f..b8550e3786331fed1fd9b7f46b6b2b917af2e96c 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 152e08fbf58ebc218e2bcdccab7b2ac1012e7c66..d92077cde7dc8a75a721b760517b13d8e9ce8559 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 cc5535b32a1be494acfffefabdbf2e268d9c9e69..664ace205f8c81abdb46152b2fb57f7868aa1f26 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 ba291f81ebadeaf06501a4887683e550ab23b502..4e914e186da287d0b3183e0ca3ce01797683fd45 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]