Skip to content

Instantly share code, notes, and snippets.

@gkbrk gkbrk/Cargo.toml
Last active Mar 4, 2019

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Aug 15, 2016

😍

@lilydjwg

This comment has been minimized.

Copy link

commented Aug 18, 2016

It will exit when I strace the process...

@rofrol

This comment has been minimized.

@gkbrk

This comment has been minimized.

Copy link
Owner Author

commented Aug 23, 2016

@rofrol 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.