diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000000000000000000000000000000000000..552242a48e53ee6f5a6e2f99b19bf581378e0235 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,48 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "type": "shell", + "label": "cargo build", + "command": "cargo", + "args": [ + "build" + ], + "problemMatcher": [ + "$rustc" + ] + }, + { + "type": "shell", + "label": "cargo build --example client_test", + "command": "cargo", + "args": [ + "build --example client_test" + ], + "problemMatcher": [ + "$rustc" + ], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "type": "shell", + "label": "cargo build --example idiot", + "command": "cargo", + "args": [ + "build --example idiot" + ], + "problemMatcher": [ + "$rustc" + ], + "group": { + "kind": "build", + "isDefault": true + } + } + ] +} \ No newline at end of file diff --git a/examples/client_and_server.rs b/examples/client_and_server.rs index 0e474bdfca83e7e5fa7e4e832e7241752e496f82..e2dc1f1c133abc7597916d110e35534ebeeda680 100644 --- a/examples/client_and_server.rs +++ b/examples/client_and_server.rs @@ -1,6 +1,6 @@ extern crate coap; -use coap::{CoAPServer, CoAPClient, CoAPRequest, CoAPResponse, CoAPOption}; +use coap::{CoAPClient, CoAPOption, CoAPRequest, CoAPResponse, CoAPServer}; use coap::IsMessage; fn request_handler(request: CoAPRequest) -> Option<CoAPResponse> { @@ -23,6 +23,8 @@ fn main() { println!("Client request: {}", url); let response: CoAPResponse = CoAPClient::request(url).unwrap(); - println!("Server reply: {}", - String::from_utf8(response.message.payload).unwrap()); + println!( + "Server reply: {}", + String::from_utf8(response.message.payload).unwrap() + ); } diff --git a/examples/client_test.rs b/examples/client_test.rs new file mode 100644 index 0000000000000000000000000000000000000000..48210145bbce7df670ed364e123e3a3b3e69f0b0 --- /dev/null +++ b/examples/client_test.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/idiot.rs b/examples/idiot.rs new file mode 100644 index 0000000000000000000000000000000000000000..537726a87b17b468ce38cc74f69617fea3606004 --- /dev/null +++ b/examples/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() + ); +}