diff --git a/Cargo.lock b/Cargo.lock index 00a32d3..f4a225e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,83 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "bumpalo" +version = "3.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" + [[package]] name = "bytes" version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" +[[package]] +name = "cc" +version = "1.2.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c481bdbf0ed3b892f6f806287d72acd515b352a4ec27a208489b8c1bc839633a" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "chrono" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "find-msvc-tools" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844" + [[package]] name = "futures-core" version = "0.3.31" @@ -57,26 +128,87 @@ dependencies = [ "slab", ] +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", +] + [[package]] name = "hashbrown" version = "0.15.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +[[package]] +name = "iana-time-zone" +version = "0.1.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33e57f83510bb73707521ebaffa789ec8caf86f9657cad665b092b581d40e9fb" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "js-sys" +version = "0.3.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "464a3709c7f55f1f721e5389aa6ea4e3bc6aba669353300af094b29ffbdde1d8" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + [[package]] name = "libc" version = "0.2.178" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + [[package]] name = "mineguard" version = "0.1.0" dependencies = [ + "chrono", + "regex", "thiserror", "tokio", "tokio-stream", "tokio-util", + "uuid", ] [[package]] @@ -90,6 +222,21 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + [[package]] name = "pin-project-lite" version = "0.2.16" @@ -120,6 +267,53 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "regex" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "signal-hook-registry" version = "1.4.7" @@ -238,18 +432,136 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" +[[package]] +name = "uuid" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e054861b4bd027cd373e18e8d8d8e6548085000e41290d95ce0c373a654b4a" +dependencies = [ + "getrandom", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "wasip2" +version = "1.0.1+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d759f433fa64a2d763d1340820e46e111a7a5ab75f993d1852d70b03dbb80fd" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48cb0d2638f8baedbc542ed444afc0644a29166f1595371af4fecf8ce1e7eeb3" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cefb59d5cd5f92d9dcf80e4683949f15ca4b511f4ac0a6e14d4e1ac60c6ecd40" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbc538057e648b67f72a982e708d485b2efa771e1ac05fec311f9f63e5800db4" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-sys" version = "0.60.2" @@ -332,3 +644,9 @@ name = "windows_x86_64_msvc" version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + +[[package]] +name = "wit-bindgen" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" diff --git a/Cargo.toml b/Cargo.toml index 430b48c..e5585f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,15 +15,18 @@ default = ["core", "events", "mc-vanilla"] # Core runtime requirements for the currently implemented functionality. core = ["dep:thiserror", "dep:tokio", "dep:tokio-stream", "dep:tokio-util"] # Placeholder for upcoming event-driven functionality. -events = [] +events = ["dep:uuid"] mc-vanilla = [] # Add new feature groups here; attach their optional dependencies to the relevant feature list. [dependencies] +chrono = "0.4.42" +regex = "1.12.2" thiserror = { version = "2.0.17", optional = true } # Core async runtime and utilities # Add new feature-specific optional dependencies alongside the relevant feature entry above. tokio = { version = "1.48.0", features = ["process", "rt-multi-thread", "macros", "io-std", "io-util"], optional = true } tokio-stream = { version = "0.1.17", features = ["full", "io-util", "signal", "tokio-util"], optional = true } tokio-util = { version = "0.7.17", features = ["full"], optional = true } +uuid = { version = "1.19.0", features = ["v4"], optional = true } diff --git a/src/config/stream.rs b/src/config/stream.rs index 4cecedb..bd27850 100644 --- a/src/config/stream.rs +++ b/src/config/stream.rs @@ -1,7 +1,15 @@ use std::fmt::{self, Display}; +#[cfg(feature = "events")] +use chrono::{DateTime, Utc}; +use chrono::{Local, NaiveTime, TimeZone}; +use regex::Regex; +#[cfg(feature = "events")] +use uuid::Uuid; + #[cfg(feature = "mc-vanilla")] use crate::error::ParserError; +use crate::instance::InstanceStatus; /// Identifies which process stream produced a line of output. #[derive(Debug, Clone, PartialEq, Eq)] @@ -19,12 +27,6 @@ pub struct StreamLine { source: StreamSource, } -#[cfg(feature = "events")] -pub struct InstanceEvent {} - -#[cfg(feature = "events")] -pub enum Events {} - #[cfg(feature = "mc-vanilla")] pub struct LogMeta { time: String, @@ -41,6 +43,28 @@ pub enum LogLevel { Other, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum EventPayload { + #[cfg(feature = "events")] + StateChange { + old: InstanceStatus, + new: InstanceStatus, + }, + + StdLine { + line: StreamLine, + }, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct InstanceEvent { + pub id: Uuid, + + pub timestamp: DateTime, + + pub payload: EventPayload, +} + #[cfg(feature = "mc-vanilla")] impl LogMeta { pub fn new>(line: S) -> Result, ParserError> { @@ -145,6 +169,52 @@ impl StreamLine { pub fn msg(&self) -> String { self.line.clone() } + + pub fn extract_timestamp(&self) -> Option> { + let input = self.line.as_str(); + let re = Regex::new(r"\[(.*?)\]").unwrap(); + let time_s = re.captures(input).map(|v| v[1].to_string()); + if time_s.is_none() { + return None; + } + let time = NaiveTime::parse_from_str(&time_s.unwrap(), "%H:%M:%S").ok()?; + + let today = Local::now().date_naive(); + let naive_dt = today.and_time(time); + + let local_dt = Local.from_local_datetime(&naive_dt).unwrap(); + + let utc_dt = local_dt.with_timezone(&Utc); + + Some(utc_dt) + } +} + +impl InstanceEvent { + pub fn stdout>(line: S) -> Self { + let line = line.into(); + let s_line = StreamLine::stdout(line); + let timestamp = s_line.extract_timestamp().unwrap_or(Utc::now()); + let payload = EventPayload::StdLine { line: s_line }; + + Self { + id: Uuid::new_v4(), + timestamp, + payload, + } + } + pub fn stderr>(line: S) -> Self { + let line = line.into(); + let s_line = StreamLine::stderr(line); + let timestamp = s_line.extract_timestamp().unwrap_or(Utc::now()); + let payload = EventPayload::StdLine { line: s_line }; + + Self { + id: Uuid::new_v4(), + timestamp, + payload, + } + } } impl Display for StreamLine { @@ -152,3 +222,25 @@ impl Display for StreamLine { write!(f, "{}", self.line) } } + +impl Display for InstanceEvent { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let head = format!( + "UUID: {}\nTimestamp:{}\nPayload:\n", + self.id.to_string(), + self.timestamp.to_string() + ); + match self.payload.clone() { + EventPayload::StdLine { line } => { + let full = format!("{}{}", head, line); + write!(f, "{}", full) + } + + #[cfg(feature = "events")] + EventPayload::StateChange { old, new } => { + let full = format!("{}State changed: {:?} -> {:?}", head, old, new); + write!(f, "{}", full) + } + } + } +} diff --git a/src/instance.rs b/src/instance.rs index dd827a0..dccaaa2 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -1,5 +1,6 @@ use std::{path::PathBuf, process::Stdio, sync::Arc}; +use chrono::Utc; use tokio::{ io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter}, process::{self, Child}, @@ -7,9 +8,12 @@ use tokio::{ }; use tokio_stream::wrappers::BroadcastStream; use tokio_util::sync::CancellationToken; +use uuid::Uuid; +#[cfg(feature = "events")] +use crate::config::stream::InstanceEvent; use crate::{ - config::{MinecraftType, MinecraftVersion, StreamLine, StreamSource}, + config::{MinecraftType, MinecraftVersion, StreamLine, StreamSource, stream::EventPayload}, error::{HandleError, ServerError, SubscribeError}, }; @@ -38,10 +42,14 @@ pub enum InstanceStatus { pub struct InstanceHandle { pub data: InstanceData, pub status: Arc>, - stdout_tx: broadcast::Sender, - stderr_tx: Option>, + stdout_tx: broadcast::Sender, + stderr_tx: Option>, #[cfg(feature = "events")] - events_tx: broadcast::Sender, + events_tx: broadcast::Sender, + #[cfg(feature = "events")] + internal_tx: mpsc::Sender, + #[cfg(feature = "events")] + internal_rx: Option>, stdin_tx: mpsc::Sender, stdin_rx: Option>, child: Option>>, @@ -80,6 +88,7 @@ impl InstanceHandle { let status = InstanceStatus::Stopped; let (stdin_tx, stdin_rx) = mpsc::channel(1024); + let (internal_tx, internal_rx) = mpsc::channel(1024); Ok(Self { data, status: Arc::new(RwLock::new(status)), @@ -87,6 +96,10 @@ impl InstanceHandle { stderr_tx: None, #[cfg(feature = "events")] events_tx: broadcast::Sender::new(2048), + #[cfg(feature = "events")] + internal_tx, + #[cfg(feature = "events")] + internal_rx: Some(internal_rx), stdin_tx, stdin_rx: Some(stdin_rx), child: None, @@ -134,9 +147,25 @@ impl InstanceHandle { } async fn transition_status(&self, status: InstanceStatus) { + let r_guard = self.status.read().await; + let old = r_guard.clone(); + drop(r_guard); + + let new = status.clone(); + let mut guard = self.status.write().await; *guard = status; drop(guard); + + let event = InstanceEvent { + id: Uuid::new_v4(), + + timestamp: Utc::now(), + + payload: EventPayload::StateChange { old, new }, + }; + + self.internal_tx.send(event); } fn build_start_command(&self) -> process::Command { @@ -179,7 +208,7 @@ impl InstanceHandle { loop { match stdout_reader.next_line().await { Ok(Some(line)) => { - let _ = stdout_tx.send(StreamLine::stdout(line)); + let _ = stdout_tx.send(InstanceEvent::stdout(line)); } _ => { let status_guard = stdout_status.read().await; @@ -203,7 +232,7 @@ impl InstanceHandle { loop { match stderr_reader.next_line().await { Ok(Some(line)) => { - let _ = stderr_tx.send(StreamLine::stderr(line)); + let _ = stderr_tx.send(InstanceEvent::stderr(line)); } _ => { let status_guard = stderr_status.read().await; @@ -267,12 +296,8 @@ impl InstanceHandle { } line = rx.next() => { if let Some(Ok(val)) = line { - let msg = val.msg(); - let meta = LogMeta::new(msg); - if let Ok(val) = meta - && val.is_some() { - println!("{}", val.unwrap()); - } + + println!("{}", val); } } } @@ -319,7 +344,7 @@ impl InstanceHandle { pub fn subscribe( &self, stream: StreamSource, - ) -> Result, SubscribeError> { + ) -> Result, SubscribeError> { match stream { StreamSource::Stdout => { let rx = self.stdout_tx.subscribe(); diff --git a/src/lib.rs b/src/lib.rs index 213d6ef..82f3e17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod config; pub mod error; pub mod instance; +pub mod utils; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..6f686c3 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,26 @@ +use chrono::{DateTime, Datelike, Local, NaiveTime, TimeZone, Timelike, Utc}; +use regex::Regex; + +pub fn extract_timestamp(input: &str) -> Option> { + let re = Regex::new(r"\[(.*?)\]").unwrap(); + let time_s = re.captures(input).map(|v| v[1].to_string()); + if time_s.is_none() { + return None; + } + let time = NaiveTime::parse_from_str(&time_s.unwrap(), "%H:%M:%S").ok()?; + + let today = Local::now().date_naive(); + + let local_dt = Local + .with_ymd_and_hms( + today.year(), + today.month(), + today.day(), + time.hour(), + time.minute(), + time.second(), + ) + .single()?; + + Some(local_dt.with_timezone(&Utc)) +}