-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66d5b42
commit 8b34e45
Showing
14 changed files
with
437 additions
and
603 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
|
||
use std::thread; | ||
use coap::{Server, CoAPClient, CoAPRequest, IsMessage, MessageType, CoAPOption}; | ||
use tokio::runtime::current_thread::Runtime; | ||
|
||
#[bench] | ||
fn bench_server_with_request(b: &mut test::Bencher) { | ||
let mut server = Server::new("127.0.0.1:0").unwrap(); | ||
let server_port = server.socket_addr().unwrap().port(); | ||
|
||
thread::spawn(move || { | ||
Runtime::new().unwrap().block_on(async move { | ||
server.run(move |request| { | ||
let uri_path = request.get_path().to_string(); | ||
|
||
return match request.response { | ||
Some(mut response) => { | ||
response.set_payload(uri_path.as_bytes().to_vec()); | ||
Some(response) | ||
} | ||
_ => None, | ||
}; | ||
}).await.unwrap(); | ||
}); | ||
}); | ||
|
||
let client = CoAPClient::new(format!("127.0.0.1:{}", server_port)).unwrap(); | ||
|
||
let mut request = CoAPRequest::new(); | ||
request.set_version(1); | ||
request.set_type(MessageType::Confirmable); | ||
request.set_code("0.01"); | ||
request.set_message_id(1); | ||
request.set_token(vec!(0x51, 0x55, 0x77, 0xE8)); | ||
request.add_option(CoAPOption::UriPath, "test".to_string().into_bytes()); | ||
|
||
b.iter(|| { | ||
client.send(&request).unwrap(); | ||
let recv_packet = client.receive().unwrap(); | ||
assert_eq!(recv_packet.message.payload, b"test".to_vec()); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,30 @@ | ||
extern crate coap; | ||
|
||
use std::io; | ||
use coap::{CoAPServer, CoAPResponse, CoAPRequest, IsMessage, Method}; | ||
|
||
fn request_handler(request: CoAPRequest) -> Option<CoAPResponse> { | ||
match request.get_method() { | ||
&Method::Get => println!("request by get {}", request.get_path()), | ||
&Method::Post => println!("request by post {}", String::from_utf8(request.message.payload).unwrap()), | ||
&Method::Put => println!("request by put {}", String::from_utf8(request.message.payload).unwrap()), | ||
_ => println!("request by other method"), | ||
}; | ||
|
||
return match request.response { | ||
Some(mut message) => { | ||
message.set_payload(b"OK".to_vec()); | ||
Some(message) | ||
}, | ||
_ => None | ||
}; | ||
} | ||
use coap::{Server, IsMessage, Method}; | ||
use tokio::runtime::current_thread::Runtime; | ||
|
||
fn main() { | ||
let addr = "127.0.0.1:5683"; | ||
|
||
let mut server = CoAPServer::new(addr).unwrap(); | ||
server.handle(request_handler).unwrap(); | ||
|
||
println!("Server up on {}", addr); | ||
println!("Press any key to stop..."); | ||
|
||
io::stdin().read_line(&mut String::new()).unwrap(); | ||
|
||
println!("Server shutdown"); | ||
let mut server = Server::new(addr).unwrap(); | ||
|
||
println!("Server up on {}", addr); | ||
|
||
Runtime::new().unwrap().block_on(async move { | ||
server.run(move |request| { | ||
match request.get_method() { | ||
&Method::Get => println!("request by get {}", request.get_path()), | ||
&Method::Post => println!("request by post {}", String::from_utf8(request.message.payload).unwrap()), | ||
&Method::Put => println!("request by put {}", String::from_utf8(request.message.payload).unwrap()), | ||
_ => println!("request by other method"), | ||
}; | ||
|
||
return match request.response { | ||
Some(mut message) => { | ||
message.set_payload(b"OK".to_vec()); | ||
Some(message) | ||
}, | ||
_ => None | ||
}; | ||
}).await.unwrap(); | ||
}); | ||
} |
Oops, something went wrong.
8b34e45
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
I think we should use a linter for indentation and removing commented past code.