From dce423481ae1c049db1184850d206e1eca0ce20b Mon Sep 17 00:00:00 2001 From: Martin Barksten Date: Fri, 23 Sep 2016 16:15:56 +0200 Subject: [PATCH] Send ClientInfo to the server --- Cargo.toml | 10 ++++++---- log4rs.toml | 5 +++++ src/main.rs | 12 +++++++++++- src/messages.rs | 15 +++++++++++++++ src/snake.rs | 2 +- src/structs.rs | 11 +++++++++++ 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ef87668..03855ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,15 @@ [package] name = "snakebot_rust" -version = "0.1.0" +version = "1.0.0" authors = ["Martin Barksten "] [dependencies] ws = "0.5.2" -serde = "0.8" -serde_json = "0.8" -serde_macros = "0.8" +serde = "= 0.8.8" +serde_json = "= 0.8.0" +serde_macros = "= 0.8.2" quick-error = "1.1.0" log = "0.3.6" log4rs = { version = "0.4.6", features = ["toml"] } +target_info = "0.1.0" +rustc_version = "0.1.0" diff --git a/log4rs.toml b/log4rs.toml index 28b7679..911e93a 100644 --- a/log4rs.toml +++ b/log4rs.toml @@ -20,3 +20,8 @@ appenders = [ "console" ] level = "debug" appenders = [ "client" ] additive = false + +[loggers.snake] +level = "debug" +appenders = [ "snake" ] +additive = false diff --git a/src/main.rs b/src/main.rs index 1176dc8..aec7a94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ extern crate serde; #[macro_use] extern crate quick_error; #[macro_use] extern crate log; extern crate log4rs; +extern crate target_info; +extern crate rustc_version; mod structs; mod messages; @@ -123,8 +125,16 @@ impl ws::Handler for Client { fn on_open(&mut self, _: ws::Handshake) -> ws::Result<()> { debug!(target: LOG_TARGET, "Connection to Websocket opened"); - let parse_msg = messages::create_play_registration_msg(self.snake.get_name()); + let client_info = messages::create_client_info_msg(); + if let Ok(message) = client_info { + info!(target: LOG_TARGET, "Sending client info to server: {:?}", message); + try!(self.out.send(message)); + } else { + error!(target: LOG_TARGET, "Unable to create client info message {:?}", client_info); + try!(self.out.close(ws::CloseCode::Error)); + } + let parse_msg = messages::create_play_registration_msg(self.snake.get_name()); if let Ok(response) = parse_msg { info!(target: LOG_TARGET, "Registering player with message: {:?}", response); self.out.send(response) diff --git a/src/messages.rs b/src/messages.rs index ac44984..738ae8d 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -1,5 +1,7 @@ use structs; use serde_json::{ from_str, to_string, Error }; +use target_info::Target; +use rustc_version; // Inbound pub const GAME_ENDED: &'static str = @@ -30,6 +32,8 @@ const REGISTER_MOVE: &'static str = "se.cygni.snake.api.request.RegisterMove"; const HEART_BEAT_REQUEST: &'static str = "se.cygni.snake.api.request.HeartBeatRequest"; +const CLIENT_INFO: &'static str = + "se.cygni.snake.api.request.ClientInfo"; pub enum Inbound { GameEnded(structs::GameEnded), @@ -103,6 +107,17 @@ pub fn create_heart_beat_msg(id: String) -> Result { }) } +pub fn create_client_info_msg() -> Result { + to_string(&structs::ClientInfo { + type_: String::from(CLIENT_INFO), + language: String::from("rust"), + languageVersion: format!("{}", rustc_version::version()), + operatingSystem: String::from(Target::os()), + operatingSystemVersion: String::from(""), + clientVersion: String::from(option_env!("CARGO_PKG_VERSION").unwrap_or("")) + }) +} + pub fn default_gamesettings() -> structs::GameSettings { structs::GameSettings { maxNoofPlayers: 5, diff --git a/src/snake.rs b/src/snake.rs index 2f34033..bf02a42 100644 --- a/src/snake.rs +++ b/src/snake.rs @@ -3,7 +3,7 @@ use maputil::{ Direction }; use util::{ translate_positions }; const LOG_TARGET: &'static str = "snake"; -pub const TRAINING_VENUE: &'static str = "tournament"; +pub const TRAINING_VENUE: &'static str = "training"; pub struct Snake; diff --git a/src/structs.rs b/src/structs.rs index 5886bc0..a842974 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -28,6 +28,17 @@ pub struct PlayRegistration { pub gameSettings: GameSettings, } +#[derive(Serialize, Deserialize, Debug)] +pub struct ClientInfo { + #[serde(rename="type")] + pub type_: String, + pub language: String, + pub languageVersion: String, + pub operatingSystem: String, + pub operatingSystemVersion: String, + pub clientVersion: String +} + #[derive(Serialize, Deserialize, Debug)] pub struct RegisterMove { #[serde(rename="type")]