-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mycognosist/cargo_fmt
Apply idiomatic formatting with cargo fmt
- Loading branch information
Showing
7 changed files
with
1,432 additions
and
1,167 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,69 @@ | ||
use async_std::{prelude::*,io,task,net}; | ||
use cable::{Cable,MemoryStore,ChannelOptions}; | ||
use async_std::{io, net, prelude::*, task}; | ||
use cable::{Cable, ChannelOptions, MemoryStore}; | ||
|
||
type Error = Box<dyn std::error::Error+Send+Sync+'static>; | ||
type Error = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
|
||
fn main() -> Result<(),Error> { | ||
let (args,argv) = argmap::parse(std::env::args()); | ||
fn main() -> Result<(), Error> { | ||
let (args, argv) = argmap::parse(std::env::args()); | ||
|
||
task::block_on(async move { | ||
let store = MemoryStore::default(); | ||
let cable = Cable::new(store); | ||
{ | ||
let opts = ChannelOptions { | ||
channel: "default".as_bytes().to_vec(), | ||
time_start: 0, | ||
//time_end: now(), | ||
time_end: 0, | ||
limit: 20, | ||
}; | ||
let mut client = cable.clone(); | ||
task::spawn(async move { | ||
let mut msg_stream = client.open_channel(&opts).await.unwrap(); | ||
while let Some(msg) = msg_stream.next().await { | ||
println!["msg={:?}", msg]; | ||
task::block_on(async move { | ||
let store = MemoryStore::default(); | ||
let cable = Cable::new(store); | ||
{ | ||
let opts = ChannelOptions { | ||
channel: "default".as_bytes().to_vec(), | ||
time_start: 0, | ||
//time_end: now(), | ||
time_end: 0, | ||
limit: 20, | ||
}; | ||
let mut client = cable.clone(); | ||
task::spawn(async move { | ||
let mut msg_stream = client.open_channel(&opts).await.unwrap(); | ||
while let Some(msg) = msg_stream.next().await { | ||
println!["msg={:?}", msg]; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
{ | ||
let mut client = cable.clone(); | ||
task::spawn(async move { | ||
let stdin = io::stdin(); | ||
let mut line = String::new(); | ||
loop { | ||
stdin.read_line(&mut line).await.unwrap(); | ||
if line.is_empty() { break } | ||
let channel = "default".as_bytes(); | ||
let text = line.trim_end().as_bytes(); | ||
client.post_text(channel, &text).await.unwrap(); | ||
line.clear(); | ||
{ | ||
let mut client = cable.clone(); | ||
task::spawn(async move { | ||
let stdin = io::stdin(); | ||
let mut line = String::new(); | ||
loop { | ||
stdin.read_line(&mut line).await.unwrap(); | ||
if line.is_empty() { | ||
break; | ||
} | ||
let channel = "default".as_bytes(); | ||
let text = line.trim_end().as_bytes(); | ||
client.post_text(channel, &text).await.unwrap(); | ||
line.clear(); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
if let Some(port) = argv.get("l").and_then(|x| x.first()) { | ||
let listener = net::TcpListener::bind(format!["0.0.0.0:{}",port]).await?; | ||
let mut incoming = listener.incoming(); | ||
while let Some(rstream) = incoming.next().await { | ||
let stream = rstream.unwrap(); | ||
let client = cable.clone(); | ||
task::spawn(async move { | ||
client.listen(stream).await.unwrap(); | ||
}); | ||
} | ||
} else if let Some(addr) = args.get(1) { | ||
let stream = net::TcpStream::connect(addr).await?; | ||
cable.listen(stream).await?; | ||
} | ||
Ok(()) | ||
}) | ||
if let Some(port) = argv.get("l").and_then(|x| x.first()) { | ||
let listener = net::TcpListener::bind(format!["0.0.0.0:{}", port]).await?; | ||
let mut incoming = listener.incoming(); | ||
while let Some(rstream) = incoming.next().await { | ||
let stream = rstream.unwrap(); | ||
let client = cable.clone(); | ||
task::spawn(async move { | ||
client.listen(stream).await.unwrap(); | ||
}); | ||
} | ||
} else if let Some(addr) = args.get(1) { | ||
let stream = net::TcpStream::connect(addr).await?; | ||
cable.listen(stream).await?; | ||
} | ||
Ok(()) | ||
}) | ||
} | ||
|
||
fn _now() -> u64 { | ||
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() | ||
std::time::SystemTime::now() | ||
.duration_since(std::time::UNIX_EPOCH) | ||
.unwrap() | ||
.as_secs() | ||
} |
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
Oops, something went wrong.