Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
incorporating changes from the review
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Szabo committed Jul 23, 2018
1 parent 0b6a7ad commit f3d8cda
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 112 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -5,6 +5,7 @@ members = [
"home-node",
"storage",
"test",
"examples/TheButton"
]

[patch.crates-io]
Expand Down
4 changes: 2 additions & 2 deletions examples/TheButton/src/client.rs
Expand Up @@ -15,7 +15,7 @@ impl Client{

pub fn run(&self)->i32{
match self.on_fail {
OnFail::RETRY => {
OnFail::Retry => {
let mut i : u8 = 1;
while i<33{
if self.connect(){
Expand All @@ -31,7 +31,7 @@ impl Client{
warn!("Could not connect after repeated tries, exiting app");
return EX_TEMPFAIL;
},
OnFail::TERMINATE => {
OnFail::Terminate => {
if self.connect(){
Self::on_event();
return EX_OK;
Expand Down
111 changes: 47 additions & 64 deletions examples/TheButton/src/config.rs
@@ -1,63 +1,52 @@
use super::*;


pub const default_addr : String = "127.0.0.1:7070".into();

pub struct ServerConfig{
pub event_file : String,
pub event_timer : u64,
pub event_count : u32,
pub event_file : Option<String>,
pub event_timer : Option<u64>,
pub event_count : Option<u32>,
}

impl ServerConfig{
pub fn new_from_args(args: ArgMatches)->Self{
let file_name = match args.value_of("event-file"){
Some(name) => {
name.to_string()
}
None => {
"".into()
}
};
pub fn new_from_args(args: ArgMatches)-> Result<Self, std::io::Error> {
let file_name = args.value_of("event-file").map(|s| s.into());

let timer = match args.value_of("event-timer"){
Some(time) => {
match time.parse::<u64>(){
Ok(int)=>{
int
}
Err(_e)=>{
0
}
}
}
None => {
0
}
};
let timer = match args.value_of("event-timer") {
Some(s) =>
s.parse::<u64>()
.map(|i| Some(i))
.map_err(|err|
std::io::Error::new(std::io::ErrorKind::InvalidInput, "failed to parse --event-timer")),
_ =>
Result::Ok(Option::None)
}?;



let count = match args.value_of("stop-after"){
Some(times) => {
match times.parse::<u32>(){
Ok(int)=>{
int
}
Err(_e)=>{
0
}
}
}
None => {
0
}
};
Some(s) => {
s.parse::<u32>()
.map(|i|
Some(i))
.map_err(|err|
std::io::Error::new(std::io::ErrorKind::InvalidInput, "failed to parse --stop-after"))
},
_ =>
Result::Ok(Option::None)

}?;

info!("File descriptor: {:?}", file_name);
info!("Event loop timer: {:?}", timer);
info!("Event count: {:?}", count);

Self{
Ok(Self{
event_file: file_name,
event_timer: timer,
event_count: count
}
})
}
}

Expand All @@ -67,37 +56,31 @@ pub struct ClientConfig{
}

impl ClientConfig{
pub fn new_from_args(args: ArgMatches)->Self{
let connect_address = match args.value_of("connect"){
Some(addr)=>{
addr
}
None=>{
"127.0.0.1:7007".into()
}
};
let on_fail = match args.value_of("on-fail"){
pub fn new_from_args(args: ArgMatches)->Result<Self, std::io::Error> {
let connect_address = args.value_of("connect").map(|s| s.into()).unwrap_or(default_addr);

let on_fail = match args.value_of("on-fail") {
Some(fail) => {
match fail{
"retry" => {
OnFail::RETRY
}
_ => {
OnFail::TERMINATE
}
match fail {
"retry" =>
OnFail::Retry,
"terminate" =>
OnFail::Terminate,
_ =>
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "failed to parse --on-fail value"))
}
}
},
None => {
OnFail::TERMINATE
OnFail::Terminate
}
};

info!("Connect address: {:?}", connect_address);
info!("On fail: {:?}",on_fail);

Self{
Ok(Self{
addr: connect_address.to_string(),
on_fail: on_fail
}
})
}
}
77 changes: 32 additions & 45 deletions examples/TheButton/src/main.rs
Expand Up @@ -36,14 +36,14 @@ use tokio_signal::unix::{SIGINT, SIGUSR1, SIGUSR2};

#[derive(Debug)]
pub enum OnFail {
TERMINATE,
RETRY,
Terminate,
Retry,
}

enum Mode{
SERVER(Server),
CLIENT(Client),
ERROR(i32),
Server(Server),
Client(Client),
Error(i32),
}

fn application_code()->i32{
Expand Down Expand Up @@ -79,27 +79,31 @@ fn application_code()->i32{

//SERVER MODE HANDLING
let (sub_name, sub_args) = matches.subcommand();
let app_mode : Mode;
match sub_args {

let app_mode = match sub_args {
Some(args)=>{
match sub_name{
"server"=>{
let cfg = ServerConfig::new_from_args(args.to_owned());
app_mode = Mode::SERVER(Server::new(cfg));
ServerConfig::new_from_args(args.to_owned())
.map( |cfg|
Mode::Server(Server::new(cfg))
)


},
"client"=>{
let cfg = ClientConfig::new_from_args(args.to_owned());
app_mode = Mode::CLIENT(Client::new(cfg));
Mode::Client(Client::new(cfg))
},
_=>{
warn!("Subcommand could not be parsed");
app_mode = Mode::ERROR(EX_USAGE);
Mode::Error(EX_USAGE)
}
}
}
None=>{
warn!("No subcommand given, starting in server mode");
app_mode = Mode::SERVER(Server::default());
app_mode = Mode::Server(Server::default());
}
};

Expand Down Expand Up @@ -127,31 +131,17 @@ fn application_code()->i32{

let core_fut = Future::join3(c,u1,u2);

if let Mode::SERVER(ser) = app_mode {
match reactor.run(
core_fut.join(ser)
.map(|_|return EX_OK)
.map_err(|err| {
match err.kind(){
std::io::ErrorKind::Interrupted => {
warn!("exiting on SIGINT");
return EX_OK;
}std::io::ErrorKind::NotConnected => {
return EX_UNAVAILABLE;
}std::io::ErrorKind::TimedOut => {
return EX_TEMPFAIL;
}_=>{
return EX_SOFTWARE;
}
}
})
){
Ok(code)=>code,
Err(code)=>code
let app_fut = match app_mode {
Mode::Client(client_fut) => Box::new(client_fut) as Box<Future<Item=i32, Error=std::io::Error>>,
Mode::Server(server_fut) => Box::new(server_fut) as Box<Future<Item=i32, Error=std::io::Error>>,
Mode::Error(err) => {
error!("failed to initialize application: {}", err);
return EX_SOFTWARE;
}
}else if let Mode::CLIENT(cli) = app_mode{
match reactor.run(
core_fut.join(cli)
};

match reactor.run({
core_fut.join(app_fut)
.map(|_|return EX_OK)
.map_err(|err| {
match err.kind(){
Expand All @@ -167,15 +157,12 @@ fn application_code()->i32{
}
}
})
){
Ok(code)=>code,
Err(code)=>code
}
}else if let Mode::ERROR(e) = app_mode{
return e
}else{
return EX_SOFTWARE;
}
}){
Ok(code)=>code,
Err(code)=>code
};

EX_OK
}

fn main() {
Expand Down
1 change: 0 additions & 1 deletion storage/src/filesys/mod.rs
Expand Up @@ -97,7 +97,6 @@ impl AsyncFileHandler{
-> Box< Future< Item = String, Error = StorageError> > {
let (tx, rx) = oneshot::channel::<Result<String,StorageError>>();
if !&self.get_path(&file_path).exists(){
warn!("opening file failed: {}", file_path);
return Box::new(future::err(StorageError::InvalidKey));
}
self.pool.spawn(
Expand Down

0 comments on commit f3d8cda

Please sign in to comment.