Skip to content
Snippets Groups Projects
Commit bd1dfaf9 authored by Per's avatar Per
Browse files

fistr real commit

parent d4ddb68a
No related branches found
No related tags found
No related merge requests found
{
// 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
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()
);
}
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),
}
}
}
}
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()
);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment