Skip to content
Snippets Groups Projects
Commit ece3d26d authored by Covertness's avatar Covertness
Browse files

add reply API for client

parent 41d9610f
No related branches found
No related tags found
No related merge requests found
[package] [package]
name = "coap" name = "coap"
version = "0.2.0" version = "0.2.1"
description = "A CoAP library" description = "A CoAP library"
readme = "README.md" readme = "README.md"
documentation = "http://covertness.github.io/coap-rs/coap/index.html" documentation = "http://covertness.github.io/coap-rs/coap/index.html"
......
...@@ -17,7 +17,7 @@ First add this to your `Cargo.toml`: ...@@ -17,7 +17,7 @@ First add this to your `Cargo.toml`:
```toml ```toml
[dependencies] [dependencies]
coap = "0.2.0" coap = "0.2.1"
``` ```
Then, add this to your crate root: Then, add this to your crate root:
......
...@@ -4,18 +4,8 @@ use coap::packet::*; ...@@ -4,18 +4,8 @@ use coap::packet::*;
use coap::{CoAPServer, CoAPClient}; use coap::{CoAPServer, CoAPClient};
fn request_handler(req: Packet, resp: CoAPClient) { fn request_handler(req: Packet, resp: CoAPClient) {
let uri_path = req.get_option(OptionType::UriPath); let uri_path = req.get_option(OptionType::UriPath).unwrap();
assert!(uri_path.is_some()); resp.reply(&req, uri_path.front().unwrap().clone()).unwrap();
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();
} }
fn main() { fn main() {
......
...@@ -88,6 +88,24 @@ impl CoAPClient { ...@@ -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. /// Execute a request.
pub fn send(&self, packet: &Packet) -> Result<()> { pub fn send(&self, packet: &Packet) -> Result<()> {
match packet.to_bytes() { match packet.to_bytes() {
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
//! //!
//! ```toml //! ```toml
//! [dependencies] //! [dependencies]
//! coap = "0.2.0" //! coap = "0.2.1"
//! ``` //! ```
//! //!
//! Then, add this to your crate root: //! Then, add this to your crate root:
......
...@@ -146,15 +146,8 @@ mod test { ...@@ -146,15 +146,8 @@ mod test {
let uri_path = req.get_option(OptionType::UriPath); let uri_path = req.get_option(OptionType::UriPath);
assert!(uri_path.is_some()); assert!(uri_path.is_some());
let uri_path = uri_path.unwrap(); let uri_path = uri_path.unwrap();
let mut packet = Packet::new();
packet.header.set_version(1); resp.reply(&req, uri_path.front().unwrap().clone()).unwrap();
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();
} }
#[test] #[test]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment