Start local server on client run

This commit is contained in:
Pierre HUBERT 2022-10-01 20:44:01 +02:00
parent 14c5820ed2
commit 7a06feb7cf
18 changed files with 88 additions and 18 deletions

8
rust/Cargo.lock generated
View File

@ -465,8 +465,10 @@ dependencies = [
"clap", "clap",
"crossterm", "crossterm",
"env_logger", "env_logger",
"lazy_static",
"log", "log",
"sea_battle_backend", "sea_battle_backend",
"tokio",
"tui", "tui",
] ]
@ -925,6 +927,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388" checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.134" version = "0.2.134"

View File

@ -11,4 +11,6 @@ clap = { version = "3.2.17", features = ["derive"] }
log = "0.4.17" log = "0.4.17"
env_logger = "0.9.0" env_logger = "0.9.0"
tui = "0.19.0" tui = "0.19.0"
crossterm = "0.25.0" crossterm = "0.25.0"
lazy_static = "1.4.0"
tokio = "1.21.2"

View File

@ -10,5 +10,32 @@ pub struct CliArgs {
value_parser, value_parser,
default_value = "https://fixme.communiquons.org" default_value = "https://fixme.communiquons.org"
)] )]
server_uri: String, pub server_uri: String,
/// Local server listen address
#[clap(short, long, default_value = "127.0.0.1:5679")]
pub listen_address: String,
}
impl CliArgs {
/// Get local listen port
pub fn listen_port(&self) -> u16 {
self.listen_address
.rsplit(':')
.next()
.expect("Failed to split listen address!")
.parse::<u16>()
.expect("Failed to parse listen port!")
}
}
lazy_static::lazy_static! {
static ref ARGS: CliArgs = {
CliArgs::parse()
};
}
/// Get parsed command line arguments
pub fn cli_args() -> &'static CliArgs {
&ARGS
} }

View File

@ -1,2 +1,3 @@
pub mod cli_args; pub mod cli_args;
pub mod server;
pub mod ui_screens; pub mod ui_screens;

View File

@ -12,9 +12,13 @@ use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use env_logger::Env; use env_logger::Env;
use tui::backend::CrosstermBackend; use tui::backend::CrosstermBackend;
use tui::Terminal; use tui::Terminal;
use cli_player::server::start_server_if_missing;
pub fn main() -> Result<(), Box<dyn Error>> { #[tokio::main]
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); pub async fn main() -> Result<(), Box<dyn Error>> {
env_logger::Builder::from_env(Env::default()).init();
start_server_if_missing().await;
// setup terminal // setup terminal
enable_raw_mode()?; enable_raw_mode()?;

View File

@ -0,0 +1,30 @@
use tokio::runtime::Builder;
use tokio::task;
use sea_battle_backend::args::Args;
use sea_battle_backend::utils::network_utils;
use crate::cli_args::cli_args;
pub async fn start_server_if_missing() {
if !network_utils::is_port_open(cli_args().listen_port()).await {
log::info!(
"Local server will start on {}...",
cli_args().listen_address
);
std::thread::spawn(move || {
let rt = Builder::new_current_thread().enable_all().build().unwrap();
let local_set = task::LocalSet::new();
rt.block_on(local_set.run_until(async move {
sea_battle_backend::server::start_server(Args {
listen_address: cli_args().listen_address.clone(),
cors: None,
})
.await
.expect("Failed to run local server!")
}));
});
}
}

View File

@ -19,9 +19,9 @@ actix-rt = "2.7.0"
uuid = { version = "1.1.2", features = ["v4"] } uuid = { version = "1.1.2", features = ["v4"] }
rand = "0.8.5" rand = "0.8.5"
serde_with = "2.0.1" serde_with = "2.0.1"
tokio = { version = "1", features = ["full"] }
[dev-dependencies] [dev-dependencies]
tokio = { version = "1", features = ["full"] }
#reqwest = { version = "0.11.11", default-features = false, features = ["json", "rustls-tls"] } #reqwest = { version = "0.11.11", default-features = false, features = ["json", "rustls-tls"] }
tokio-tungstenite = "0.17.2" tokio-tungstenite = "0.17.2"
serde_urlencoded = "0.7.1" serde_urlencoded = "0.7.1"

View File

@ -11,7 +11,7 @@ use crate::consts::INVITE_CODE_LENGTH;
use crate::data::GameRules; use crate::data::GameRules;
use crate::game::Game; use crate::game::Game;
use crate::human_player_ws::{CloseConnection, HumanPlayerWS, ServerMessage, SetGame}; use crate::human_player_ws::{CloseConnection, HumanPlayerWS, ServerMessage, SetGame};
use crate::utils::rand_str; use crate::utils::string_utils::rand_str;
/// How often garbage collector is run /// How often garbage collector is run
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(60); const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(60);

View File

@ -5,8 +5,8 @@ use crate::data::{BotType, CurrentGameStatus, GameRules};
use crate::human_player_ws::ServerMessage; use crate::human_player_ws::ServerMessage;
use crate::server::start_server; use crate::server::start_server;
use crate::test::bot_client::ClientEndResult; use crate::test::bot_client::ClientEndResult;
use crate::test::network_utils::wait_for_port;
use crate::test::{bot_client, TestPort}; use crate::test::{bot_client, TestPort};
use crate::utils::network_utils::wait_for_port;
#[tokio::test] #[tokio::test]
async fn full_game() { async fn full_game() {

View File

@ -5,9 +5,9 @@ use crate::data::{BoatsLayout, BotType, Coordinates, GameRules};
use crate::human_player_ws::ServerMessage; use crate::human_player_ws::ServerMessage;
use crate::server::start_server; use crate::server::start_server;
use crate::test::bot_client::ClientEndResult; use crate::test::bot_client::ClientEndResult;
use crate::test::network_utils::wait_for_port;
use crate::test::play_utils::check_no_replay_on_hit; use crate::test::play_utils::check_no_replay_on_hit;
use crate::test::{bot_client, TestPort}; use crate::test::{bot_client, TestPort};
use crate::utils::network_utils::wait_for_port;
fn check_strikes_are_linear(msg: &ServerMessage) { fn check_strikes_are_linear(msg: &ServerMessage) {
if let ServerMessage::RequestFire { status } = msg { if let ServerMessage::RequestFire { status } = msg {

View File

@ -5,9 +5,9 @@ use crate::data::{BoatsLayout, GameRules};
use crate::server::start_server; use crate::server::start_server;
use crate::test::bot_client; use crate::test::bot_client;
use crate::test::bot_client::ClientEndResult; use crate::test::bot_client::ClientEndResult;
use crate::test::network_utils::wait_for_port;
use crate::test::play_utils::check_no_replay_on_hit; use crate::test::play_utils::check_no_replay_on_hit;
use crate::test::TestPort; use crate::test::TestPort;
use crate::utils::network_utils::wait_for_port;
#[tokio::test] #[tokio::test]
async fn invalid_port() { async fn invalid_port() {

View File

@ -5,8 +5,8 @@ use crate::data::{BotType, CurrentGameStatus, GameRules};
use crate::human_player_ws::ServerMessage; use crate::human_player_ws::ServerMessage;
use crate::server::start_server; use crate::server::start_server;
use crate::test::bot_client::ClientEndResult; use crate::test::bot_client::ClientEndResult;
use crate::test::network_utils::wait_for_port;
use crate::test::{bot_client, TestPort}; use crate::test::{bot_client, TestPort};
use crate::utils::network_utils::wait_for_port;
#[tokio::test] #[tokio::test]
async fn full_game() { async fn full_game() {

View File

@ -9,8 +9,8 @@ use crate::data::BotType;
use crate::human_player_ws::ServerMessage; use crate::human_player_ws::ServerMessage;
use crate::server::start_server; use crate::server::start_server;
use crate::test::bot_client::{ClientEndResult, RunMode}; use crate::test::bot_client::{ClientEndResult, RunMode};
use crate::test::network_utils::wait_for_port;
use crate::test::{bot_client, TestPort}; use crate::test::{bot_client, TestPort};
use crate::utils::network_utils::wait_for_port;
#[tokio::test] #[tokio::test]
async fn invalid_accept_code() { async fn invalid_accept_code() {

View File

@ -46,6 +46,5 @@ mod bot_linear_play;
mod bot_random_play; mod bot_random_play;
mod bot_smart_play; mod bot_smart_play;
mod invite_mode; mod invite_mode;
mod network_utils;
mod play_utils; mod play_utils;
mod random_mode; mod random_mode;

View File

@ -1,8 +1,8 @@
use crate::args::Args; use crate::args::Args;
use crate::server::start_server; use crate::server::start_server;
use crate::test::bot_client::{ClientEndResult, RunMode}; use crate::test::bot_client::{ClientEndResult, RunMode};
use crate::test::network_utils::wait_for_port;
use crate::test::{bot_client, TestPort}; use crate::test::{bot_client, TestPort};
use crate::utils::network_utils::wait_for_port;
use std::error::Error; use std::error::Error;
use tokio::task; use tokio::task;

View File

@ -0,0 +1,2 @@
pub mod network_utils;
pub mod string_utils;

View File

@ -5,10 +5,7 @@ use tokio::time;
/// Check whether a given port is open or not /// Check whether a given port is open or not
pub async fn is_port_open(port: u16) -> bool { pub async fn is_port_open(port: u16) -> bool {
match TcpStream::connect(("127.0.0.1", port)).await { TcpStream::connect(("127.0.0.1", port)).await.is_ok()
Ok(_) => true,
Err(_) => false,
}
} }
/// Wait for a port to become available /// Wait for a port to become available

View File

@ -12,7 +12,7 @@ pub fn rand_str(len: usize) -> String {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::utils::rand_str; use crate::utils::string_utils::rand_str;
#[test] #[test]
fn test_rand_str() { fn test_rand_str() {