diff options
author | natrixaeria <janng@gmx.de> | 2019-05-15 23:46:11 +0200 |
---|---|---|
committer | natrixaeria <janng@gmx.de> | 2019-05-15 23:46:11 +0200 |
commit | 2f9e5dfaac05538fdf4513569b4df6872ee85b89 (patch) | |
tree | 605a75fb41a09c892a8c8f3af16e984f38e06297 | |
parent | 73eb78e3ac5eb7d382c1aca98da84cd866d54570 (diff) |
Add web client functionality
additionally added -r feature to build.sh, which reverses build
output.
-rw-r--r-- | game_server/Cargo.toml | 3 | ||||
-rwxr-xr-x | game_server/build.sh | 25 | ||||
-rw-r--r-- | game_server/err | 73 | ||||
-rw-r--r-- | game_server/rbuild.sh | 2 | ||||
-rw-r--r-- | game_server/src/backend_connection.rs | 31 | ||||
-rw-r--r-- | game_server/src/gameserver.rs | 0 | ||||
-rw-r--r-- | game_server/src/group.rs | 22 | ||||
-rw-r--r-- | game_server/src/lobby.rs | 10 | ||||
-rw-r--r-- | game_server/src/main.rs | 13 | ||||
-rw-r--r-- | game_server/src/test_group.rs | 28 |
10 files changed, 173 insertions, 34 deletions
diff --git a/game_server/Cargo.toml b/game_server/Cargo.toml index f3aabed..baf27fa 100644 --- a/game_server/Cargo.toml +++ b/game_server/Cargo.toml @@ -8,4 +8,5 @@ description = "A general game server for connections to web clients. Currently ( [dependencies] log = "0.4" pretty_env_logger = "0.3" -rocket = "0.4" +reqwest = "0.9" +websocket = "0.22" diff --git a/game_server/build.sh b/game_server/build.sh index c42a7ac..5929e29 100755 --- a/game_server/build.sh +++ b/game_server/build.sh @@ -1,8 +1,21 @@ #!/usr/bin/env sh -if rustup run nightly cargo build; then - echo build success! - RUST_LOG=trace target/debug/game-server -else - echo build failed! -fi +case $1 in + ("") + if rustup run nightly cargo --color always build; then + echo build success! + RUST_LOG=info target/debug/game-server + else + echo build failed! + fi + ;; + -r) + sh build.sh &> err && cat err | tac + ;; + -c) + rustup run nightly cargo clean + ;; + *) + echo invalid argument + ;; +esac diff --git a/game_server/err b/game_server/err new file mode 100644 index 0000000..6ca8a6f --- /dev/null +++ b/game_server/err @@ -0,0 +1,73 @@ +[0m[0m[1m[32m Compiling[0m game-server v0.1.0 (/home/jan/projects/DiscoBot/game_server) +[0m[1m[38;5;9merror[E0277][0m[0m[1m: the trait bound `(): futures::future::Future` is not satisfied[0m +[0m [0m[0m[1m[38;5;12m--> [0m[0msrc/backend_connection.rs:32:24[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m[1m[38;5;12m32[0m[0m [0m[0m[1m[38;5;12m| [0m[0m hyper::rt::run(hyper::rt::lazy(|| {[0m +[0m [0m[0m[1m[38;5;12m| [0m[0m [0m[0m[1m[38;5;9m^^^^^^^^^^^^^^^[0m[0m [0m[0m[1m[38;5;9mthe trait `futures::future::Future` is not implemented for `()`[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m [0m[0m[1m[38;5;12m= [0m[0m[1mnote[0m[0m: required because of the requirements on the impl of `futures::future::IntoFuture` for `()`[0m +[0m [0m[0m[1m[38;5;12m= [0m[0m[1mnote[0m[0m: required by `futures::future::lazy::lazy`[0m + +[0m[1m[38;5;9merror[E0599][0m[0m[1m: no method named `wait` found for type `std::result::Result<futures::future::map_err::MapErr<futures::future::map::Map<hyper::client::ResponseFuture, [closure@src/backend_connection.rs:54:34: 54:68]>, [closure@src/backend_connection.rs:55:38: 55:73]>, http::uri::InvalidUri>` in the current scope[0m +[0m [0m[0m[1m[38;5;12m--> [0m[0msrc/backend_connection.rs:56:24[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m[1m[38;5;12m56[0m[0m [0m[0m[1m[38;5;12m| [0m[0m }).wait();[0m +[0m [0m[0m[1m[38;5;12m| [0m[0m [0m[0m[1m[38;5;9m^^^^[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m [0m[0m[1m[38;5;12m= [0m[0m[1mnote[0m[0m: the method `wait` exists but the following trait bounds were not satisfied:[0m +[0m `&mut std::result::Result<futures::future::map_err::MapErr<futures::future::map::Map<hyper::client::ResponseFuture, [closure@src/backend_connection.rs:54:34: 54:68]>, [closure@src/backend_connection.rs:55:38: 55:73]>, http::uri::InvalidUri> : futures::future::Future`[0m + +[0m[1m[38;5;9merror[E0308][0m[0m[1m: mismatched types[0m +[0m [0m[0m[1m[38;5;12m--> [0m[0msrc/backend_connection.rs:58:17[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m[1m[38;5;12m58[0m[0m [0m[0m[1m[38;5;12m| [0m[0m res[0m +[0m [0m[0m[1m[38;5;12m| [0m[0m [0m[0m[1m[38;5;9m^^^[0m[0m [0m[0m[1m[38;5;9mexpected (), found enum `std::result::Result`[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m [0m[0m[1m[38;5;12m= [0m[0m[1mnote[0m[0m: expected type `[0m[0m[1m()[0m[0m`[0m +[0m found type `[0m[0m[1mstd::result::Result<http::response::Response<hyper::body::body::Body>, http::uri::InvalidUri>[0m[0m`[0m + +[0m[1m[38;5;9merror[E0277][0m[0m[1m: the trait bound `(): futures::future::Future` is not satisfied[0m +[0m [0m[0m[1m[38;5;12m--> [0m[0msrc/backend_connection.rs:32:24[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m[1m[38;5;12m32[0m[0m [0m[0m[1m[38;5;12m| [0m[0m [0m[0m hyper::rt::run(hyper::rt::lazy(|| {[0m +[0m [0m[0m[1m[38;5;12m| [0m[0m [0m[0m[1m[38;5;9m________________________^[0m +[0m[1m[38;5;12m33[0m[0m [0m[0m[1m[38;5;12m| [0m[0m[1m[38;5;9m|[0m[0m [0m[0m let client = hyper::Client::builder()[0m +[0m[1m[38;5;12m34[0m[0m [0m[0m[1m[38;5;12m| [0m[0m[1m[38;5;9m|[0m[0m [0m[0m .build::<_, hyper::Body>([0m +[0m[1m[38;5;12m35[0m[0m [0m[0m[1m[38;5;12m| [0m[0m[1m[38;5;9m|[0m[0m [0m[0m HttpsConnector::new(4).unwrap()[0m +[0m[1m[38;5;12m...[0m[0m [0m[0m[1m[38;5;9m|[0m +[0m[1m[38;5;12m59[0m[0m [0m[0m[1m[38;5;12m| [0m[0m[1m[38;5;9m|[0m[0m [0m[0m }[0m +[0m[1m[38;5;12m60[0m[0m [0m[0m[1m[38;5;12m| [0m[0m[1m[38;5;9m|[0m[0m [0m[0m }));[0m +[0m [0m[0m[1m[38;5;12m| [0m[0m[1m[38;5;9m|__________^[0m[0m [0m[0m[1m[38;5;9mthe trait `futures::future::Future` is not implemented for `()`[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m [0m[0m[1m[38;5;12m= [0m[0m[1mnote[0m[0m: required because of the requirements on the impl of `futures::future::IntoFuture` for `()`[0m +[0m [0m[0m[1m[38;5;12m= [0m[0m[1mnote[0m[0m: required by `futures::future::lazy::Lazy`[0m + +[0m[1m[38;5;9merror[E0277][0m[0m[1m: the trait bound `(): futures::future::Future` is not satisfied[0m +[0m [0m[0m[1m[38;5;12m--> [0m[0msrc/backend_connection.rs:32:9[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m[1m[38;5;12m32[0m[0m [0m[0m[1m[38;5;12m| [0m[0m hyper::rt::run(hyper::rt::lazy(|| {[0m +[0m [0m[0m[1m[38;5;12m| [0m[0m [0m[0m[1m[38;5;9m^^^^^^^^^^^^^^[0m[0m [0m[0m[1m[38;5;9mthe trait `futures::future::Future` is not implemented for `()`[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m [0m[0m[1m[38;5;12m= [0m[0m[1mnote[0m[0m: required because of the requirements on the impl of `futures::future::IntoFuture` for `()`[0m +[0m [0m[0m[1m[38;5;12m= [0m[0m[1mnote[0m[0m: required by `hyper::rt::run`[0m + +[0m[1m[38;5;9merror[E0063][0m[0m[1m: missing field `res_receiver` in initializer of `backend_connection::BackendConnection`[0m +[0m [0m[0m[1m[38;5;12m--> [0m[0msrc/backend_connection.rs:62:9[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m[1m[38;5;12m62[0m[0m [0m[0m[1m[38;5;12m| [0m[0m BackendConnection {[0m +[0m [0m[0m[1m[38;5;12m| [0m[0m [0m[0m[1m[38;5;9m^^^^^^^^^^^^^^^^^[0m[0m [0m[0m[1m[38;5;9mmissing `res_receiver`[0m + +[0m[1m[38;5;9merror[E0609][0m[0m[1m: no field `request_sender` on type `&backend_connection::BackendConnection`[0m +[0m [0m[0m[1m[38;5;12m--> [0m[0msrc/backend_connection.rs:69:14[0m +[0m [0m[0m[1m[38;5;12m|[0m +[0m[1m[38;5;12m69[0m[0m [0m[0m[1m[38;5;12m| [0m[0m self.request_sender.send([0m +[0m [0m[0m[1m[38;5;12m| [0m[0m [0m[0m[1m[38;5;9m^^^^^^^^^^^^^^[0m + +[0m[1m[38;5;9merror[0m[0m[1m: aborting due to 7 previous errors[0m + +[0m[1mSome errors have detailed explanations: E0063, E0277, E0308, E0599, E0609.[0m +[0m[1mFor more information about an error, try `rustc --explain E0063`.[0m +[0m[0m[1m[31merror:[0m Could not compile `game-server`. + +To learn more, run the command again with --verbose. +build failed! diff --git a/game_server/rbuild.sh b/game_server/rbuild.sh new file mode 100644 index 0000000..22b10b5 --- /dev/null +++ b/game_server/rbuild.sh @@ -0,0 +1,2 @@ +#!/bin/bash +sh build.sh &> err && cat err | tac diff --git a/game_server/src/backend_connection.rs b/game_server/src/backend_connection.rs new file mode 100644 index 0000000..9307c4a --- /dev/null +++ b/game_server/src/backend_connection.rs @@ -0,0 +1,31 @@ +use reqwest::{Response, Client, Url, UrlError, Error as ReqError}; + +pub struct BackendConnection { + host: String, + client: Client, + last_response: Option<Result<Response, ReqError>> +} + +impl BackendConnection { + pub fn new(host: &str) -> Self { + BackendConnection { + host: host.to_string(), + client: Client::new(), + last_response: None + } + } + + pub fn request(&mut self, location: &str) -> Result<(), UrlError> { + Ok(self.last_response = + Some(self.client.get(Url::parse(&format!("{}{}", self.host, location))?) + .send())) + } + + pub fn get_response(&self) -> &Option<Result<Response, ReqError>> { + &self.last_response + } + + pub fn host_name<'a>(&'a self) -> &'a str { + &self.host + } +} diff --git a/game_server/src/gameserver.rs b/game_server/src/gameserver.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/game_server/src/gameserver.rs diff --git a/game_server/src/group.rs b/game_server/src/group.rs index 9774979..55e4fbf 100644 --- a/game_server/src/group.rs +++ b/game_server/src/group.rs @@ -1,22 +1,8 @@ pub type GroupId = u32; -pub struct Group { - id: GroupId, - name: String, -} - -impl Group { - pub(crate) fn new(id: GroupId, name: String) -> Group { - Group { id, name } - } - - pub(crate) fn get_id(&self) -> GroupId { - self.id - } +pub trait Group { + fn id(&self) -> GroupId; + fn name(&self) -> String; - pub fn run(&self) { - let id = self.id; - std::thread::spawn(move || - loop {println!("group id: {} meldet sich", id)}); - } + fn run(&self); } diff --git a/game_server/src/lobby.rs b/game_server/src/lobby.rs index 8808b7f..fe3bdee 100644 --- a/game_server/src/lobby.rs +++ b/game_server/src/lobby.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use super::group::{Group, GroupId}; pub struct Lobby { - groups: HashMap<GroupId, Group>, + groups: HashMap<GroupId, Box<Group>>, } impl Lobby { @@ -13,8 +13,8 @@ impl Lobby { } } - pub fn add_group(&mut self, group: Group) { - self.groups.insert(group.get_id(), group); + pub fn add_group(&mut self, group: Box<Group>) { + self.groups.insert(group.id(), group); } pub fn iter<'a>(&'a self) -> GroupIterator<'a> { @@ -23,11 +23,11 @@ impl Lobby { } pub struct GroupIterator<'a> { - groups: std::collections::hash_map::Values<'a, GroupId, Group> + groups: std::collections::hash_map::Values<'a, GroupId, Box<Group>> } impl<'a> Iterator for GroupIterator<'a> { - type Item = &'a Group; + type Item = &'a Box<Group>; fn next(&mut self) -> Option<Self::Item> { self.groups.next() diff --git a/game_server/src/main.rs b/game_server/src/main.rs index 7e08b46..f7a1085 100644 --- a/game_server/src/main.rs +++ b/game_server/src/main.rs @@ -1,22 +1,27 @@ mod group; +mod test_group; mod lobby; +mod backend_connection; #[macro_use] extern crate log; use pretty_env_logger; -use group::Group; +use test_group::TestGroup; use lobby::Lobby; +use backend_connection::BackendConnection; fn main() { pretty_env_logger::init(); let mut lobby = Lobby::new(); - lobby.add_group(Group::new(0, "Test".to_string())); - lobby.add_group(Group::new(1, "Very Serious".to_string())); for group in lobby.iter() { group.run() } - loop {} + let mut backend = BackendConnection::new("http://129.13.215.68:5000"); + loop { + backend.request("/scribble").unwrap(); + println!("{:?}", backend.get_response()); + } } diff --git a/game_server/src/test_group.rs b/game_server/src/test_group.rs new file mode 100644 index 0000000..bd570e3 --- /dev/null +++ b/game_server/src/test_group.rs @@ -0,0 +1,28 @@ +use super::group::{Group, GroupId}; + +pub struct TestGroup { + id: GroupId, + name: String, +} + +impl Group for TestGroup { + fn id(&self) -> GroupId { + self.id + } + + fn name(&self) -> String { + self.name.clone() + } + + fn run(&self) { + let id = self.id; + let name = self.name.to_owned(); + std::thread::spawn(move || /*loop { println!("> group nr.{} wishes you: '{}'", id, name) }*/()); + } +} + +impl TestGroup { + pub fn new(id: GroupId, name: String) -> Self { + TestGroup { id, name } + } +} |