Start local server on client run
This commit is contained in:
parent
14c5820ed2
commit
7a06feb7cf
8
rust/Cargo.lock
generated
8
rust/Cargo.lock
generated
@ -465,8 +465,10 @@ dependencies = [
|
||||
"clap",
|
||||
"crossterm",
|
||||
"env_logger",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"sea_battle_backend",
|
||||
"tokio",
|
||||
"tui",
|
||||
]
|
||||
|
||||
@ -925,6 +927,12 @@ version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.134"
|
||||
|
@ -12,3 +12,5 @@ log = "0.4.17"
|
||||
env_logger = "0.9.0"
|
||||
tui = "0.19.0"
|
||||
crossterm = "0.25.0"
|
||||
lazy_static = "1.4.0"
|
||||
tokio = "1.21.2"
|
@ -10,5 +10,32 @@ pub struct CliArgs {
|
||||
value_parser,
|
||||
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
|
||||
}
|
||||
|
@ -1,2 +1,3 @@
|
||||
pub mod cli_args;
|
||||
pub mod server;
|
||||
pub mod ui_screens;
|
||||
|
@ -12,9 +12,13 @@ use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
|
||||
use env_logger::Env;
|
||||
use tui::backend::CrosstermBackend;
|
||||
use tui::Terminal;
|
||||
use cli_player::server::start_server_if_missing;
|
||||
|
||||
pub fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||
#[tokio::main]
|
||||
pub async fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::Builder::from_env(Env::default()).init();
|
||||
|
||||
start_server_if_missing().await;
|
||||
|
||||
// setup terminal
|
||||
enable_raw_mode()?;
|
||||
|
30
rust/cli_player/src/server.rs
Normal file
30
rust/cli_player/src/server.rs
Normal 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!")
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
@ -19,9 +19,9 @@ actix-rt = "2.7.0"
|
||||
uuid = { version = "1.1.2", features = ["v4"] }
|
||||
rand = "0.8.5"
|
||||
serde_with = "2.0.1"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
#reqwest = { version = "0.11.11", default-features = false, features = ["json", "rustls-tls"] }
|
||||
tokio-tungstenite = "0.17.2"
|
||||
serde_urlencoded = "0.7.1"
|
||||
|
@ -11,7 +11,7 @@ use crate::consts::INVITE_CODE_LENGTH;
|
||||
use crate::data::GameRules;
|
||||
use crate::game::Game;
|
||||
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
|
||||
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(60);
|
||||
|
@ -5,8 +5,8 @@ use crate::data::{BotType, CurrentGameStatus, GameRules};
|
||||
use crate::human_player_ws::ServerMessage;
|
||||
use crate::server::start_server;
|
||||
use crate::test::bot_client::ClientEndResult;
|
||||
use crate::test::network_utils::wait_for_port;
|
||||
use crate::test::{bot_client, TestPort};
|
||||
use crate::utils::network_utils::wait_for_port;
|
||||
|
||||
#[tokio::test]
|
||||
async fn full_game() {
|
||||
|
@ -5,9 +5,9 @@ use crate::data::{BoatsLayout, BotType, Coordinates, GameRules};
|
||||
use crate::human_player_ws::ServerMessage;
|
||||
use crate::server::start_server;
|
||||
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::{bot_client, TestPort};
|
||||
use crate::utils::network_utils::wait_for_port;
|
||||
|
||||
fn check_strikes_are_linear(msg: &ServerMessage) {
|
||||
if let ServerMessage::RequestFire { status } = msg {
|
||||
|
@ -5,9 +5,9 @@ use crate::data::{BoatsLayout, GameRules};
|
||||
use crate::server::start_server;
|
||||
use crate::test::bot_client;
|
||||
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::TestPort;
|
||||
use crate::utils::network_utils::wait_for_port;
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_port() {
|
||||
|
@ -5,8 +5,8 @@ use crate::data::{BotType, CurrentGameStatus, GameRules};
|
||||
use crate::human_player_ws::ServerMessage;
|
||||
use crate::server::start_server;
|
||||
use crate::test::bot_client::ClientEndResult;
|
||||
use crate::test::network_utils::wait_for_port;
|
||||
use crate::test::{bot_client, TestPort};
|
||||
use crate::utils::network_utils::wait_for_port;
|
||||
|
||||
#[tokio::test]
|
||||
async fn full_game() {
|
||||
|
@ -9,8 +9,8 @@ use crate::data::BotType;
|
||||
use crate::human_player_ws::ServerMessage;
|
||||
use crate::server::start_server;
|
||||
use crate::test::bot_client::{ClientEndResult, RunMode};
|
||||
use crate::test::network_utils::wait_for_port;
|
||||
use crate::test::{bot_client, TestPort};
|
||||
use crate::utils::network_utils::wait_for_port;
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_accept_code() {
|
||||
|
@ -46,6 +46,5 @@ mod bot_linear_play;
|
||||
mod bot_random_play;
|
||||
mod bot_smart_play;
|
||||
mod invite_mode;
|
||||
mod network_utils;
|
||||
mod play_utils;
|
||||
mod random_mode;
|
||||
|
@ -1,8 +1,8 @@
|
||||
use crate::args::Args;
|
||||
use crate::server::start_server;
|
||||
use crate::test::bot_client::{ClientEndResult, RunMode};
|
||||
use crate::test::network_utils::wait_for_port;
|
||||
use crate::test::{bot_client, TestPort};
|
||||
use crate::utils::network_utils::wait_for_port;
|
||||
use std::error::Error;
|
||||
use tokio::task;
|
||||
|
||||
|
2
rust/sea_battle_backend/src/utils/mod.rs
Normal file
2
rust/sea_battle_backend/src/utils/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod network_utils;
|
||||
pub mod string_utils;
|
@ -5,10 +5,7 @@ use tokio::time;
|
||||
|
||||
/// Check whether a given port is open or not
|
||||
pub async fn is_port_open(port: u16) -> bool {
|
||||
match TcpStream::connect(("127.0.0.1", port)).await {
|
||||
Ok(_) => true,
|
||||
Err(_) => false,
|
||||
}
|
||||
TcpStream::connect(("127.0.0.1", port)).await.is_ok()
|
||||
}
|
||||
|
||||
/// Wait for a port to become available
|
@ -12,7 +12,7 @@ pub fn rand_str(len: usize) -> String {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::utils::rand_str;
|
||||
use crate::utils::string_utils::rand_str;
|
||||
|
||||
#[test]
|
||||
fn test_rand_str() {
|
Loading…
Reference in New Issue
Block a user