Skip to content

Instantly share code, notes, and snippets.

@gkbrk
Last active March 4, 2019 05:37
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gkbrk/48fa444360808501e4d1c0bd8f216b9b to your computer and use it in GitHub Desktop.
Save gkbrk/48fa444360808501e4d1c0bd8f216b9b 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ı <webdosusb@gmail.com>"]
[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();
}
@lisp-ceo
Copy link

😍

@lilydjwg
Copy link

It will exit when I strace the process...

@rofrol
Copy link

rofrol commented Aug 19, 2016

@gkbrk
Copy link
Author

gkbrk commented Aug 23, 2016

@rofrol 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment