renamed files to match std structure
This commit is contained in:
parent
eb8a512c04
commit
b45fd9a130
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<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!()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue