diff --git a/examples/register_and_serve_idiot.rs b/examples/register_and_serve_idiot.rs new file mode 100644 index 0000000000000000000000000000000000000000..537726a87b17b468ce38cc74f69617fea3606004 --- /dev/null +++ b/examples/register_and_serve_idiot.rs @@ -0,0 +1,87 @@ + +extern crate coap; + +use std::io::ErrorKind; +use coap::{CoAPClient, CoAPOption, CoAPRequest, CoAPResponse, CoAPServer}; +use coap::IsMessage; + +fn request_handler(request: CoAPRequest) -> Option<CoAPResponse> { + let uri_path = request.get_option(CoAPOption::UriPath).unwrap(); + + return match request.response { + Some(mut response) => { + response.set_payload(uri_path.front().unwrap().clone()); + Some(response) + } + _ => None, + }; +} + +fn register() { + let url = "127.0.0.1:9683"; + let endpoint = "servicediscovery/publish"; + + let client = CoAPClient::new(url).unwrap(); + let mut request = CoAPRequest::new(); + request.set_code("0.02"); + request.set_version(1); + + let service_description = " + { + \"name\": \"crazy-827635ef\", + \"type\": \"kalle._coap-json._udp\", + \"host\": \"127.0.0.1\", + \"port\": \"3400\", + \"domain\": \"unknown\", + \"properties\": { + \"property\": [ + { + \"name\": \"version\", + \"value\": \"1.0\" + }, + { + \"name\": \"path\", + \"value\": \"/palletAvailable\", + \"loc\": \"Station-01\" + } + ] + } + }"; + + request.set_payload(service_description.to_string().into_bytes()); + request.add_option(CoAPOption::UriPath, endpoint.to_string().into_bytes()); + println!("{:?}", request); + + client.send(&request).unwrap(); + println!("Client request: coap://{}{}", url, endpoint); + + match client.receive() { + Ok(response) => { + println!( + "Server reply: {}", + String::from_utf8(response.message.payload).unwrap() + ); + } + Err(e) => { + match e.kind() { + ErrorKind::WouldBlock => println!("Request timeout"), // Unix + ErrorKind::TimedOut => println!("Request timeout"), // Windows + _ => println!("Request error: {:?}", e), + } + } + } +} + +fn main() { + register(); + let mut server = CoAPServer::new("127.0.0.1:3400").unwrap(); + server.handle(request_handler).unwrap(); + let url = "coap://127.0.0.1:9683/servicediscovery/service/cra"; + println!("Client request: {}", url); + + let response: CoAPResponse = CoAPClient::request(url).unwrap(); + println!( + "Server reply: {}", + String::from_utf8(response.message.payload).unwrap() + ); +} diff --git a/examples/register_idiot.rs b/examples/register_idiot.rs new file mode 100644 index 0000000000000000000000000000000000000000..48210145bbce7df670ed364e123e3a3b3e69f0b0 --- /dev/null +++ b/examples/register_idiot.rs @@ -0,0 +1,67 @@ +extern crate coap; + +use std::io::ErrorKind; +use coap::{CoAPClient, CoAPOption, CoAPRequest, IsMessage, MessageType}; +use coap::message::header::Requests; + +fn main() { + // let addr = "127.0.0.1:5683"; + let url = "127.0.0.1:9683"; + let endpoint = "servicediscovery/publish"; + + let client = CoAPClient::new(url).unwrap(); + let mut request = CoAPRequest::new(); + request.set_code("0.02"); + // let mut header = request.get_mut_header(); + // header = Requests::Post; + request.set_version(1); + //request.set_type(Requests::Post); + // request.set_code("0.01"); + // request.set_message_id(1); + // request.set_token(vec![0x51, 0x55, 0x77, 0xE8]); + + // + let service_description = " + { + \"name\": \"crazy-827635ef\", + \"type\": \"kalle._coap-json._udp\", + \"host\": \"[fdfd::ff]\", + \"port\": \"5683\", + \"domain\": \"unknown\", + \"properties\": { + \"property\": [ + { + \"name\": \"version\", + \"value\": \"1.0\" + }, + { + \"name\": \"path\", + \"value\": \"/palletAvailable\", + \"loc\": \"Station-01\" + } + ] + } + }"; + request.set_payload(service_description.to_string().into_bytes()); + request.add_option(CoAPOption::UriPath, endpoint.to_string().into_bytes()); + println!("{:?}", request); + + client.send(&request).unwrap(); + println!("Client request: coap://{}{}", url, endpoint); + + match client.receive() { + Ok(response) => { + println!( + "Server reply: {}", + String::from_utf8(response.message.payload).unwrap() + ); + } + Err(e) => { + match e.kind() { + ErrorKind::WouldBlock => println!("Request timeout"), // Unix + ErrorKind::TimedOut => println!("Request timeout"), // Windows + _ => println!("Request error: {:?}", e), + } + } + } +} diff --git a/examples/search_idiot.rs b/examples/search_idiot.rs new file mode 100644 index 0000000000000000000000000000000000000000..b392d761d963368b20a1ce6b718a327dc5137d69 --- /dev/null +++ b/examples/search_idiot.rs @@ -0,0 +1,15 @@ +extern crate coap; + +use coap::{CoAPClient, CoAPOption, CoAPRequest, CoAPResponse, CoAPServer}; +use coap::IsMessage; + +fn main() { + let url = "coap://127.0.0.1:9683/servicediscovery/service/cra"; + println!("Client request: {}", url); + + let response: CoAPResponse = CoAPClient::request(url).unwrap(); + println!( + "Server reply: {}", + String::from_utf8(response.message.payload).unwrap() + ); +}