Move snake name and venue into configuration file

This commit is contained in:
Martin Barksten
2016-09-26 13:25:13 +02:00
parent 576d4d6975
commit 8ca8392b03
3 changed files with 53 additions and 44 deletions

View File

@ -1,2 +1,5 @@
host = "localhost"; host = "localhost";
port = 8080; port = 8080;
snake_name = "rusty-snake";
venue = "training";

View File

@ -25,15 +25,33 @@ use std::sync::mpsc;
use std::sync::Arc; use std::sync::Arc;
use messages::{ Inbound }; use messages::{ Inbound };
use std::path::Path; use std::path::Path;
use config::error::ConfigError;
use config::reader::from_file; use config::reader::from_file;
const LOG_TARGET: &'static str = "client";
const HEART_BEAT_S: u64 = 20;
const CONFIG_FILE: &'static str = "snake.conf"; const CONFIG_FILE: &'static str = "snake.conf";
const DEFAULT_HOST: &'static str = "snake.cygni.se"; const DEFAULT_HOST: &'static str = "snake.cygni.se";
const DEFAULT_PORT: i32 = 80; const DEFAULT_PORT: i32 = 80;
const DEFAULT_SNAKE_NAME: &'static str = "default-rust-snake-name";
const DEFAULT_VENUE: &'static str = "training";
const HEART_BEAT_S: u64 = 20; #[derive(Clone, Debug)]
const LOG_TARGET: &'static str = "client"; struct Config {
host: String,
port: i32,
snake_name: String,
venue: String
}
fn default_config() -> Config {
Config {
host: String::from(DEFAULT_HOST),
port: DEFAULT_PORT,
snake_name: String::from(DEFAULT_SNAKE_NAME),
venue: String::from(DEFAULT_VENUE)
}
}
quick_error! { quick_error! {
#[derive(Debug)] #[derive(Debug)]
@ -56,13 +74,10 @@ quick_error! {
} }
} }
pub fn is_training_mode() -> bool{
snake::TRAINING_VENUE == snake::get_venue()
}
struct Client { struct Client {
out: Arc<ws::Sender>, out: Arc<ws::Sender>,
snake: Snake, snake: Snake,
config: Config,
out_sender: mpsc::Sender<Arc<ws::Sender>>, out_sender: mpsc::Sender<Arc<ws::Sender>>,
id_sender: mpsc::Sender<String> id_sender: mpsc::Sender<String>
} }
@ -74,7 +89,7 @@ fn route_msg(client: &mut Client, str_msg: &String) -> Result<(), ClientError> {
match inbound_msg { match inbound_msg {
Inbound::GameEnded(msg) => { Inbound::GameEnded(msg) => {
snake.on_game_ended(&msg); snake.on_game_ended(&msg);
if is_training_mode() { if client.config.venue == "training" {
try!(client.out.close(ws::CloseCode::Normal)); try!(client.out.close(ws::CloseCode::Normal));
} }
}, },
@ -139,7 +154,7 @@ impl ws::Handler for Client {
try!(self.out.close(ws::CloseCode::Error)); try!(self.out.close(ws::CloseCode::Error));
} }
let parse_msg = messages::create_play_registration_msg(self.snake.get_name()); let parse_msg = messages::create_play_registration_msg(self.config.snake_name.clone());
if let Ok(response) = parse_msg { if let Ok(response) = parse_msg {
info!(target: LOG_TARGET, "Registering player with message: {:?}", response); info!(target: LOG_TARGET, "Registering player with message: {:?}", response);
self.out.send(response) self.out.send(response)
@ -164,17 +179,39 @@ impl ws::Handler for Client {
} }
} }
fn start_websocket_thread(host: String, port: i32, fn read_conf_file() -> Config {
id_sender: mpsc::Sender<String>, let config_path = Path::new(CONFIG_FILE);
info!(target: LOG_TARGET, "Reading config from file at {:?}", config_path.canonicalize());
let default_config = default_config();
match from_file(config_path) {
Ok(conf) => Config {
host: String::from(conf.lookup_str_or("host", DEFAULT_HOST)),
port: conf.lookup_integer32_or("port", DEFAULT_PORT),
snake_name: String::from(conf.lookup_str_or("venue", DEFAULT_SNAKE_NAME)),
venue: String::from(conf.lookup_str_or("venue", DEFAULT_VENUE))
},
Err(e) => {
error!(target: LOG_TARGET, "Unable to parse config file, got error {:?}", e);
default_config
}
}
}
fn start_websocket_thread(id_sender: mpsc::Sender<String>,
out_sender: mpsc::Sender<Arc<ws::Sender>>) -> thread::JoinHandle<()> { out_sender: mpsc::Sender<Arc<ws::Sender>>) -> thread::JoinHandle<()> {
thread::spawn(move || { thread::spawn(move || {
let connection_url = format!("ws://{}:{}/{}", host, port, snake::get_venue()); let config = read_conf_file();
let connection_url = format!("ws://{}:{}/{}", config.host, config.port, config.venue);
info!(target: LOG_TARGET, "Connecting to {:?}", connection_url); info!(target: LOG_TARGET, "Connecting to {:?}", connection_url);
let result = ws::connect(connection_url, |out| { let result = ws::connect(connection_url, |out| {
Client { Client {
out: Arc::from(out), out: Arc::from(out),
snake: snake::Snake, snake: snake::Snake,
config: config.clone(),
out_sender: out_sender.clone(), out_sender: out_sender.clone(),
id_sender: id_sender.clone() id_sender: id_sender.clone()
} }
@ -237,34 +274,12 @@ fn start_heart_beat_thread(id_receiver: mpsc::Receiver<String>,
}) })
} }
fn read_conf_file() -> Result<(String, i32), ConfigError> {
let config_path = Path::new(CONFIG_FILE);
info!(target: LOG_TARGET, "Reading config from file at {:?}", config_path.canonicalize());
let conf = try!(from_file(config_path));
let host = String::from(conf.lookup_str_or("host", DEFAULT_HOST));
let port = conf.lookup_integer32_or("port", DEFAULT_PORT);
Ok((host, port))
}
fn start_client() { fn start_client() {
let (id_sender,id_receiver) = mpsc::channel(); let (id_sender,id_receiver) = mpsc::channel();
let (out_sender,out_receiver) = mpsc::channel(); let (out_sender,out_receiver) = mpsc::channel();
let (done_sender,done_receiver) = mpsc::channel(); let (done_sender,done_receiver) = mpsc::channel();
let (host, port) = let websocket = start_websocket_thread(id_sender, out_sender);
match read_conf_file() {
Ok(values) => values,
Err(e) => {
error!(target: LOG_TARGET, "Unable to parse config file, got error {:?}", e);
error!(target: LOG_TARGET, "Using default host={} and port={}", DEFAULT_HOST, DEFAULT_PORT);
(String::from(DEFAULT_HOST),
DEFAULT_PORT)
}
};
let websocket = start_websocket_thread(host, port, id_sender, out_sender);
let heartbeat = start_heart_beat_thread(id_receiver, out_receiver, done_receiver); let heartbeat = start_heart_beat_thread(id_receiver, out_receiver, done_receiver);
let websocket_res = websocket.join(); let websocket_res = websocket.join();

View File

@ -3,19 +3,10 @@ use maputil::{ Direction };
use util::{ translate_positions }; use util::{ translate_positions };
const LOG_TARGET: &'static str = "snake"; const LOG_TARGET: &'static str = "snake";
pub const TRAINING_VENUE: &'static str = "training";
pub struct Snake; pub struct Snake;
pub fn get_venue() -> String {
String::from(TRAINING_VENUE)
}
impl Snake { impl Snake {
pub fn get_name(&self) -> String {
String::from("rusty-snake")
}
pub fn get_next_move(&self, msg: &MapUpdate) -> Direction { pub fn get_next_move(&self, msg: &MapUpdate) -> Direction {
debug!(target: LOG_TARGET, "Game map updated, tick: {}", msg.gameTick); debug!(target: LOG_TARGET, "Game map updated, tick: {}", msg.gameTick);