Skip to content

Commit

Permalink
async io with tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
Covertness committed Oct 18, 2019
1 parent 66d5b42 commit 8b34e45
Show file tree
Hide file tree
Showing 14 changed files with 437 additions and 603 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ addons:
rust:
- nightly
- beta
- stable

# load travis-cargo
before_script:
Expand All @@ -32,8 +31,6 @@ script:
after_success:
# measure code coverage and upload to coveralls.io
- cargo coveralls
# upload documentation to github.io (gh-pages branch)
- cargo doc-upload

env:
global:
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ edition = "2018"
[dependencies]
bincode = "1.1.2"
serde = { version= "1.0.88", features= [ "derive" ] }
mio = "0.5"
url = "1.7.2"
num = "0.2.0"
num-derive = "0.2.4"
num-traits = "0.2.6"
rand = "0.3"
log = "0.4.6"
threadpool = "1.7.1"
regex = "1.1.0"
tokio = "0.2.0-alpha.2"
tokio-net = "0.2.0-alpha.2"
futures-preview = { version = "=0.3.0-alpha.18", features = ["nightly", "async-await"] }
bytes = "0.4.12"

[dev-dependencies]
quickcheck = "0.8.2"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014-2016 Yang Zhang
Copyright (c) 2014-2019 Yang Zhang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
48 changes: 25 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Features:
- CoAP Observe option [RFC 7641](https://tools.ietf.org/rfc/rfc7641.txt)
- *Too Many Requests* Response Code [RFC 8516](https://tools.ietf.org/html/rfc8516)

[Documentation](http://covertness.github.io/coap-rs/master/coap/index.html)
[Documentation](https://docs.rs/coap/0.7.5/coap/)

## Installation

Expand All @@ -37,31 +37,33 @@ extern crate coap;
```rust
extern crate coap;

use std::io;
use coap::{CoAPServer, CoAPResponse, CoAPRequest, 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()),
_ => println!("request by other method"),
};

// Return the auto-generated response
request.response
}
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();
});
}
```

Expand Down
27 changes: 0 additions & 27 deletions benches/client.rs

This file was deleted.

45 changes: 45 additions & 0 deletions benches/server.rs
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());
});
}
33 changes: 19 additions & 14 deletions examples/client_and_server.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
extern crate coap;

use coap::{CoAPServer, CoAPClient, CoAPRequest, CoAPResponse};
use std::thread;
use coap::{Server, CoAPClient};
use coap::IsMessage;
use tokio::runtime::current_thread::Runtime;

fn request_handler(request: CoAPRequest) -> Option<CoAPResponse> {
let uri_path = request.get_path().to_string();
fn main() {
let mut server = Server::new("127.0.0.1:5683").unwrap();

return match request.response {
Some(mut response) => {
response.set_payload(uri_path.as_bytes().to_vec());
Some(response)
}
_ => None,
};
}
thread::spawn(move || {
Runtime::new().unwrap().block_on(async move {
server.run(move |request| {
let uri_path = request.get_path().to_string();

fn main() {
let mut server = CoAPServer::new("127.0.0.1:5683").unwrap();
server.handle(request_handler).unwrap();
return match request.response {
Some(mut response) => {
response.set_payload(uri_path.as_bytes().to_vec());
Some(response)
}
_ => None,
};
}).await.unwrap();
});
});

let url = "coap://127.0.0.1:5683/Rust";
println!("Client request: {}", url);
Expand Down
53 changes: 24 additions & 29 deletions examples/server.rs
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();
});
}
Loading

1 comment on commit 8b34e45

@StEvUgnIn
Copy link

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.

Please sign in to comment.