diff --git a/crates/ais/Cargo.toml b/crates/ais/Cargo.toml index c00b058..adefb0a 100644 --- a/crates/ais/Cargo.toml +++ b/crates/ais/Cargo.toml @@ -3,6 +3,13 @@ name = "ais" version = "0.1.0" edition = "2021" +[[bin]] +name = "ais" +path = "src/main.rs" + +[profile.dev] +debug = false + [dependencies] tokio = { version = "1.0", features = ["full"] } tokio-tungstenite = { version = "0.20", features = ["native-tls"] } diff --git a/crates/ais/src/ais.rs b/crates/ais/src/ais.rs index 8255672..9d0e713 100644 --- a/crates/ais/src/ais.rs +++ b/crates/ais/src/ais.rs @@ -1,12 +1,12 @@ use axum::{ - extract::{ws::{WebSocket, Message as WsMessage}, Query, State, WebSocketUpgrade}, + extract::{ws::{Message as WsMessage, WebSocket}, Query, State, WebSocketUpgrade}, http::StatusCode, - response::{Json, Response}, - routing::get, - Router, + response::{Json, Response} + + , }; use base64::{engine::general_purpose::STANDARD, Engine as _}; -use futures_util::{stream::SplitSink, SinkExt, StreamExt}; +use futures_util::{SinkExt, StreamExt}; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::sync::Arc; @@ -14,10 +14,9 @@ use tokio::{ sync::{broadcast, Mutex}, task::JoinHandle, }; -use tokio_util::sync::CancellationToken; -use tower_http::cors::CorsLayer; -use url::Url; use tokio_tungstenite::{connect_async, tungstenite::protocol::Message}; +use tokio_util::sync::CancellationToken; +use url::Url; #[derive(Serialize, Deserialize, Debug)] @@ -716,9 +715,9 @@ pub async fn shutdown_signal() { #[cfg(test)] mod tests { use super::*; + use crate::create_router; use axum_test::TestServer; use serde_json::json; - use tokio::sync::broadcast; #[test] fn test_get_ship_type_description() { diff --git a/crates/yachtpit/src/main.rs b/crates/yachtpit/src/main.rs index bf65fee..d343eda 100644 --- a/crates/yachtpit/src/main.rs +++ b/crates/yachtpit/src/main.rs @@ -2,27 +2,37 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] use bevy::asset::AssetMetaCheck; +use bevy::ecs::spawn::SpawnableList; use bevy::prelude::*; use bevy::window::PrimaryWindow; use bevy::winit::WinitWindows; use bevy::DefaultPlugins; -use yachtpit::GamePlugin; use std::io::Cursor; -use winit::window::Icon; use tokio::process::Command; +use winit::window::Icon; +use yachtpit::GamePlugin; #[cfg(not(target_arch = "wasm32"))] -use bevy_webview_wry::WebviewWryPlugin; - - -#[cfg(not(target_arch = "wasm32"))] -#[tokio::main(flavor = "multi_thread")] +#[tokio::main] async fn main() { - + // Start AIS server in background + tokio::spawn(async { + info!("Starting AIS server..."); + let mut cmd = Command::new("target/release/ais").spawn().unwrap(); + match cmd.wait().await { + Ok(status) => info!("AIS server process exited with status: {}", status), + Err(e) => error!("Error waiting for AIS server process: {}", e), + } + }); + launch_bevy(); } + +#[cfg(not(target_arch = "wasm32"))] +use bevy_webview_wry::WebviewWryPlugin; + #[cfg(not(target_arch = "wasm32"))] fn launch_bevy() { App::new() @@ -45,12 +55,15 @@ fn launch_bevy() { }), ) .add_plugins(GamePlugin) - .add_systems(Startup, set_window_icon) - .add_systems(Update, start_ais_server) + .add_systems(Startup, set_window_icon) // Changed here .add_plugins(WebviewWryPlugin::default()) .run(); } +#[cfg(target_arch = "wasm32")] +async fn main() { + launch_bevy(); +} #[cfg(target_arch = "wasm32")] fn launch_bevy() { @@ -87,18 +100,53 @@ fn launch_bevy() { .run(); } } +// +// fn start_ais_server() { +// static mut SERVER_STARTED: bool = false; +// +// unsafe { +// if SERVER_STARTED { +// return; +// } +// SERVER_STARTED = true; +// } +// +// let rt = tokio::runtime::Runtime::new().unwrap(); +// rt.block_on(async { +// info!("Starting AIS server..."); +// if let Ok(mut cmd) = Command::new("cargo") +// .current_dir("../ais") +// .arg("run").arg("--release") +// .spawn() { +// info!("AIS server process spawned"); +// let status = cmd.wait().await; +// match status { +// Ok(exit_status) => match exit_status.code() { +// Some(code) => info!("AIS server exited with status code: {}", code), +// None => info!("AIS server terminated by signal"), +// }, +// Err(e) => error!("AIS server failed: {}", e), +// } +// } else { +// error!("Failed to start AIS server - unable to spawn process"); +// } +// }); +// } -fn start_ais_server() { - let rt = tokio::runtime::Runtime::new().unwrap(); - rt.block_on(async { - if let Ok(mut cmd) = Command::new("cargo") - .current_dir("../ais-server") - .arg("run").arg("--release").spawn() { - let _ = cmd.wait().await; - } - }); -} - +// fn start_ais_server() { +// // This task will run on the Tokio runtime's thread pool without blocking Bevy +// tokio::spawn(async { +// info!("Starting AIS server in the background..."); +// +// +// +// // This now waits on the background task, not the main Bevy thread +// match cmd.wait().await { +// Ok(status) => info!("AIS server process exited with status: {}", status), +// Err(e) => error!("Error waiting for AIS server process: {}", e), +// } +// }); +// } // Sets the icon on windows and X11 fn set_window_icon( diff --git a/yachtpit-og.png b/yachtpit-og.png deleted file mode 100644 index f81e585..0000000 Binary files a/yachtpit-og.png and /dev/null differ diff --git a/yachtpit-x.png b/yachtpit-x.png index 7cb739f..1bc257b 100644 Binary files a/yachtpit-x.png and b/yachtpit-x.png differ