From b45fd9a130e9f9dced2c4e4bb5cd47daa0d5c346 Mon Sep 17 00:00:00 2001 From: michael-bailey Date: Wed, 15 Jun 2022 18:20:38 +0200 Subject: [PATCH] renamed files to match std structure --- server/src/main.rs | 20 +++++++ server/src/server.rs | 138 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 server/src/main.rs create mode 100644 server/src/server.rs diff --git a/server/src/main.rs b/server/src/main.rs new file mode 100644 index 0000000..9d81738 --- /dev/null +++ b/server/src/main.rs @@ -0,0 +1,20 @@ +//! # actor +//! This is the main module of the actix server. +//! It starts the actor runtime and then sleeps +//! for the duration of the program. + +pub(crate) mod client_management; +pub(crate) mod network; +pub(crate) mod prelude; +pub(crate) mod server; + +use server::ServerActor; +use tokio::time::{sleep, Duration}; + +#[actix::main()] +async fn main() { + let _server = ServerActor::new(); + loop { + sleep(Duration::from_millis(1000)).await; + } +} diff --git a/server/src/server.rs b/server/src/server.rs new file mode 100644 index 0000000..79c6d4f --- /dev/null +++ b/server/src/server.rs @@ -0,0 +1,138 @@ +//! # actix_server +//! this holds the server actor +//! the server acts as teh main actor +//! and supervisor to the actor system. + +use actix::{ + fut::wrap_future, + Actor, + ActorFutureExt, + Addr, + AsyncContext, + Context, + Handler, +}; +use foundation::{messages::network::NetworkSockOut, ClientDetails}; + +use crate::{ + client_management::{ + Client, + ClientManager, + ClientManagerMessage, + ClientManagerOutput, + }, + network::{ + Connection, + ConnectionInitiator, + ConnectionMessage, + NetworkManager, + NetworkMessage, + NetworkOutput, + }, +}; + +/// This struct is the main actor of the server. +/// all other actors are ran through here. +pub struct ServerActor { + network_manager: Option>, + client_management: Option>, +} + +impl ServerActor { + pub(crate) fn new() -> Addr { + ServerActor { + network_manager: None, + client_management: None, + } + .start() + } + + pub(crate) fn client_request( + &mut self, + _ctx: &mut ::Context, + addr: Addr, + details: ClientDetails, + ) { + use ClientManagerMessage::AddClient; + if let Some(mgr) = self.client_management.as_ref() { + let client = Client::new(addr, details.clone()); + mgr.do_send(AddClient(details.uuid, client)); + } + } + + pub(crate) fn info_request( + &mut self, + ctx: &mut ::Context, + sender: Addr, + ) { + use ConnectionMessage::{CloseConnection, SendData}; + use NetworkSockOut::GotInfo; + let fut = wrap_future( + sender.send(SendData( + serde_json::to_string(&GotInfo { + server_name: "String".to_owned(), + server_owner: "String".to_owned(), + }) + .expect("Failed to serialise"), + )), + ) + // equivalent to using .then() in js + .map(move |_out, _act: &mut Self, _ctx| { + sender.do_send(CloseConnection); + }); + ctx.spawn(fut); + } +} + +impl Actor for ServerActor { + type Context = Context; + + fn started(&mut self, ctx: &mut Self::Context) { + let addr = ctx.address(); + + self.network_manager + .replace(NetworkManager::new(addr.clone().recipient().downgrade())); + + self.client_management.replace(ClientManager::new( + addr.clone().recipient::().downgrade(), + )); + + if let Some(net_mgr) = self.network_manager.as_ref() { + net_mgr.do_send(NetworkMessage::StartListening); + } + } +} + +impl Handler for ServerActor { + type Result = (); + fn handle( + &mut self, + msg: NetworkOutput, + ctx: &mut Self::Context, + ) -> Self::Result { + use ConnectionMessage::{CloseConnection, SendData}; + use NetworkOutput::{InfoRequested, NewClient}; + use NetworkSockOut::GotInfo; + println!("[ServerActor] received message"); + match msg { + // This uses promise like funcionality to queue + // a set of async operations, + // so they occur in the right order + InfoRequested(sender) => self.info_request(ctx, sender), + // A new client is to be added + NewClient(addr, details) => self.client_request(ctx, addr, details), + }; + } +} + +impl Handler for ServerActor { + type Result = (); + + fn handle( + &mut self, + msg: ClientManagerOutput, + ctx: &mut Self::Context, + ) -> Self::Result { + todo!() + } +}