Skip to content

Instantly share code, notes, and snippets.

@kazcw
Forked from gkbrk/Cargo.toml
Created August 19, 2016 19:09
Show Gist options
  • Save kazcw/f6b8ae33e410cc2df863b6390daf1e31 to your computer and use it in GitHub Desktop.
Save kazcw/f6b8ae33e410cc2df863b6390daf1e31 to your computer and use it in GitHub Desktop.
Asynchronous server example in Rust
[package]
name = "rust-async-qotd"
version = "0.1.0"
authors = ["Gökberk Yaltıraklı <[email protected]>"]
[dependencies]
tokio = { git = "https://github.com/tokio-rs/tokio" }
rand = "0.3"
Simplicity is the ultimate sophistication.
-- Leonardo Da Vinci
%
Learning never exhausts the mind.
-- Leonardo Da Vinci
%
Tears come from the heart and not from the brain.
-- Leonardo Da Vinci
%
Why does the eye see a thing more clearly in
dreams than the imagination when awake?
-- Leonardo Da Vinci
%
Nothing strengthens authority so much as silence.
-- Leonardo Da Vinci
%
Art is never finished, only abandoned.
-- Leonardo Da Vinci
%
A beautiful body perishes, but a work of art dies not.
-- Leonardo Da Vinci
%
While I thought that I was learning how to live, I have been learning how to die.
-- Leonardo Da Vinci
%
I have been impressed with the urgency of doing. Knowing is not enough;
we must apply. Being willing is not enough; we must do.
-- Leonardo Da Vinci
%
Time abides long enough for those who make use of it.
-- Leonardo Da Vinci
%
Beware the barrenness of a busy life.
-- Socrates
%
It is not living that matters, but living rightly.
-- Socrates
%
Life is really simple, but we insist on making it complicated.
-- Confucius
%
It does not matter how slowly you go as long as you do not stop.
-- Confucius
%
Everything has beauty, but not everyone sees it.
-- Confucius
%
Choose a job you love, and you will never have to work a day in your life.
-- Confucius
extern crate tokio;
extern crate rand;
use tokio::reactor;
use tokio::reactor::{Reactor, Task, Tick};
use tokio::tcp::TcpListener;
use rand::{Rng, thread_rng};
use std::io::{self, Write, BufReader, BufRead};
use std::fs::File;
struct Listener {
socket: TcpListener,
quotes: Vec<String>
}
impl Task for Listener {
fn tick(&mut self) -> io::Result<Tick> {
while let Some(mut conn) = try!(self.socket.accept()) {
let quote = thread_rng().choose(&self.quotes).unwrap();
try!(conn.write_all(quote.as_bytes()));
}
Ok(Tick::WouldBlock)
}
}
fn read_quotes(file: &str) -> Vec<String> {
let mut quotes = Vec::new();
let quote_file = File::open(file).unwrap();
let mut quote = String::new();
for line in BufReader::new(quote_file).lines() {
if let Ok(line) = line {
if line == "%" {
quotes.push(quote.clone());
quote.clear();
}else {
quote.push_str(&line);
quote.push('\n');
}
}
}
quotes.push(quote);
return quotes;
}
fn main() {
println!("Reading quotes");
let quotes = read_quotes("quotes.txt");
println!("Starting asyncronous Quote of the Day server...");
let reactor = Reactor::default().unwrap();
reactor.handle().oneshot(|| {
let addr = "0.0.0.0:1234".parse().unwrap();
let listener = match TcpListener::bind(&addr) {
Ok(l) => l,
Err(e) => {
println!("Error creating listener: {}", e);
std::process::exit(1);
}
};
reactor::schedule(Listener {socket: listener, quotes: quotes});
Ok(())
});
reactor.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment