merge develop into master #20

Merged
michael-bailey merged 181 commits from develop into master 2023-12-01 21:48:28 +00:00
4 changed files with 2 additions and 160 deletions
Showing only changes of commit 58df6bc7a8 - Show all commits

View File

@ -16,7 +16,7 @@ edition = "2018"
[[bin]]
name = "server"
path = "src/actor.rs"
path = "src/main.rs"
[dependencies]
clap = "2.33.3"

View File

@ -1,138 +0,0 @@
//! # 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<Addr<NetworkManager>>,
client_management: Option<Addr<ClientManager>>,
}
impl ServerActor {
pub(crate) fn new() -> Addr<Self> {
ServerActor {
network_manager: None,
client_management: None,
}
.start()
}
pub(crate) fn client_request(
&mut self,
_ctx: &mut <Self as Actor>::Context,
addr: Addr<Connection>,
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 <Self as Actor>::Context,
sender: Addr<Connection>,
) {
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<Self>;
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::<ClientManagerOutput>().downgrade(),
));
if let Some(net_mgr) = self.network_manager.as_ref() {
net_mgr.do_send(NetworkMessage::StartListening);
}
}
}
impl Handler<NetworkOutput> 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<ClientManagerOutput> for ServerActor {
type Result = ();
fn handle(
&mut self,
msg: ClientManagerOutput,
ctx: &mut Self::Context,
) -> Self::Result {
todo!()
}
}

View File

@ -1,20 +0,0 @@
//! # 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 actix_server;
pub(crate) mod client_management;
pub(crate) mod network;
pub(crate) mod prelude;
use actix_server::ServerActor;
use tokio::time::{sleep, Duration};
#[actix::main()]
async fn main() {
let _server = ServerActor::new();
loop {
sleep(Duration::from_millis(1000)).await;
}
}

View File

@ -3,10 +3,10 @@
//! It starts the actor runtime and then sleeps
//! for the duration of the program.
pub(crate) mod server;
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};