extern crate flugrost; use std::env; use std::net::TcpListener; use flugrost::message::Message; use flugrost::message::Response; use flugrost::FlugrostProtocol; use flugrost::connection::Connection; use flugrost::connection::FetchResult; fn main() { let args: Vec<_> = env::args().collect(); if args.len() != 3 { println!("Usage: {} ip port", args[0]); return; } let host = format!("{}:{}", args[1], args[2]); let socket = TcpListener::bind(host).unwrap(); let proto = FlugrostProtocol::new(); 'outer: loop { let (sock, _) = socket.accept().unwrap(); let mut conn = Connection::from_blocking_socket(&sock, &proto); let mut responses = Vec::new(); for fr in conn.fetch() { match fr.unwrap() { FetchResult::EOF => break, FetchResult::Message(msg) => { let req = msg.unwrap_request(); println!("Client called method: {}", req.method); let res = match req.method.as_ref() { "add" => { let (p0, p1): (u32, u32) = req.deserialize_params().unwrap(); let sum = p0 + p1; Message::Response(Response::new(req.req_id, &sum).unwrap()) } "sub" => { let (p0, p1): (u32, u32) = req.deserialize_params().unwrap(); let dif = p0 - p1; Message::Response(Response::new(req.req_id, &dif).unwrap()) } _ => continue 'outer, }; responses.push(res); } } } for res in responses { conn.send(&res).unwrap(); } conn.notify_writable().unwrap(); } }