-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from ferrumc-rs/feature/systems
feature/systems
- Loading branch information
Showing
15 changed files
with
239 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use ferrumc_net::{GlobalState, NetResult}; | ||
use futures::stream::FuturesUnordered; | ||
use tracing::{debug, debug_span, info, Instrument}; | ||
use async_trait::async_trait; | ||
use crate::systems::keep_alive_system::KeepAliveSystem; | ||
use crate::systems::tcp_listener_system::TcpListenerSystem; | ||
|
||
#[async_trait] | ||
pub trait System: Send + Sync { | ||
async fn start(&self, state: GlobalState); | ||
async fn stop(&self, state: GlobalState); | ||
|
||
fn name(&self) -> &'static str; | ||
} | ||
|
||
pub static ALL_SYSTEMS: &[&dyn System] = &[ | ||
&TcpListenerSystem, | ||
&KeepAliveSystem | ||
]; | ||
|
||
pub async fn start_all_systems(state: GlobalState) -> NetResult<()> { | ||
let handles = FuturesUnordered::new(); | ||
|
||
for system in ALL_SYSTEMS { | ||
let name = system.name(); | ||
|
||
let handle = tokio::spawn( | ||
system | ||
.start(state.clone()) | ||
.instrument(debug_span!("sys", %name)), | ||
); | ||
handles.push(handle); | ||
} | ||
|
||
futures::future::join_all(handles).await; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn stop_all_systems(state: GlobalState) -> NetResult<()> { | ||
info!("Stopping all systems..."); | ||
|
||
for system in ALL_SYSTEMS { | ||
debug!("Stopping system: {}", system.name()); | ||
system.stop(state.clone()).await; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use std::time::Duration; | ||
use async_trait::async_trait; | ||
use ferrumc_core::identity::player_identity::PlayerIdentity; | ||
use ferrumc_net::GlobalState; | ||
use crate::systems::definition::System; | ||
|
||
pub struct KeepAliveSystem; | ||
|
||
static KILLED: AtomicBool = AtomicBool::new(false); | ||
|
||
#[async_trait] | ||
impl System for KeepAliveSystem { | ||
async fn start(&self, state: GlobalState) { | ||
loop { | ||
if KILLED.load(Ordering::Relaxed) { | ||
break; | ||
} | ||
|
||
let online_players = state.universe.query::<&PlayerIdentity>(); | ||
let online_players = online_players.into_iter().map(|player| player.username.clone()).collect::<Vec<String>>(); | ||
tracing::debug!("Online players: {:?}", online_players); | ||
tracing::debug!("Total of {} online players", online_players.len()); | ||
|
||
tokio::time::sleep(Duration::from_secs(5)).await; | ||
} | ||
} | ||
|
||
async fn stop(&self, _state: GlobalState) { | ||
tracing::debug!("Stopping keep alive system..."); | ||
KILLED.store(true, Ordering::Relaxed); | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"keep_alive" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub(crate) mod definition; | ||
|
||
mod tcp_listener_system; | ||
mod keep_alive_system; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::sync::Arc; | ||
use async_trait::async_trait; | ||
use tracing::{debug, error, info, info_span, Instrument}; | ||
use ferrumc_net::connection::handle_connection; | ||
use ferrumc_net::GlobalState; | ||
use crate::systems::definition::System; | ||
use crate::Result; | ||
|
||
pub struct TcpListenerSystem; | ||
|
||
#[async_trait] | ||
impl System for TcpListenerSystem { | ||
async fn start(&self, state: GlobalState) { | ||
if let Err(e) = TcpListenerSystem::initiate_loop(state).await { | ||
error!("TCP listener system failed with error: {:?}", e); | ||
} | ||
} | ||
|
||
async fn stop(&self, _state: GlobalState) { | ||
debug!("Stopping TCP listener system..."); | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"tcp" | ||
} | ||
} | ||
|
||
impl TcpListenerSystem { | ||
async fn initiate_loop(state: GlobalState) -> Result<()> { | ||
let tcp_listener = &state.tcp_listener; | ||
info!("Server is listening on [{}]", tcp_listener.local_addr()?); | ||
|
||
|
||
loop { | ||
let (stream, _) = tcp_listener.accept().await?; | ||
let addy = stream.peer_addr()?; | ||
debug!("Accepted connection from: {}", addy); | ||
tokio::task::spawn( | ||
handle_connection(Arc::clone(&state), stream) | ||
.instrument(info_span!("conn", %addy).or_current()) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.