From c8246f5c860813f55768da5f4addd098f71a6068 Mon Sep 17 00:00:00 2001 From: michael-bailey Date: Sat, 21 Jan 2023 10:15:50 +0000 Subject: [PATCH] refactored connection initiator into spereate files --- .../src/network/connection_initiator/actor.rs | 171 +++++++++++++++++ .../network/connection_initiator/messages.rs | 15 ++ .../src/network/connection_initiator/mod.rs | 172 +----------------- 3 files changed, 190 insertions(+), 168 deletions(-) create mode 100644 server/src/network/connection_initiator/actor.rs create mode 100644 server/src/network/connection_initiator/messages.rs diff --git a/server/src/network/connection_initiator/actor.rs b/server/src/network/connection_initiator/actor.rs new file mode 100644 index 0000000..cb7e10c --- /dev/null +++ b/server/src/network/connection_initiator/actor.rs @@ -0,0 +1,171 @@ +use std::net::SocketAddr; + +use actix::{ + Actor, + ActorContext, + Addr, + AsyncContext, + Context, + Handler, + WeakAddr, + WeakRecipient, +}; +use foundation::{ + messages::{ + client::{ClientStreamOut, ClientStreamOut::Error}, + network::{NetworkSockIn, NetworkSockOut}, + }, + ClientDetails, +}; +use serde_json::{from_str, to_string}; + +use crate::{ + network::InitiatorOutput, + prelude::{ + actors::Connection, + messages::{ + ConnectionMessage, + ConnectionObservableOutput, + ObservableMessage, + }, + }, +}; + +/// # ConnectionInitiator +/// Handles the initiatin of a new connection. +/// +/// This will do one of two things: +/// - Create a new client and send it to the network manager. +/// - Request the eserver info and send it to the connection. +pub struct ConnectionInitiator { + delegate: WeakRecipient, + connection: Addr, +} + +impl ConnectionInitiator { + pub(crate) fn new( + delegate: WeakRecipient, + connection: Addr, + ) -> Addr { + ConnectionInitiator { + connection, + delegate, + } + .start() + } + + fn handle_request( + &mut self, + sender: WeakAddr, + ctx: &mut ::Context, + _address: SocketAddr, + data: String, + ) { + use InitiatorOutput::{ClientRequest, InfoRequest}; + use NetworkSockIn::{Connect, Info}; + + let msg = from_str::(data.as_str()); + if let Err(e) = msg.as_ref() { + println!("[ConnectionInitiator] error decoding message {}", e); + self.error(ctx, sender); + return; + } + let msg = msg.unwrap(); + + println!("[ConnectionInitiator] matching request"); + if let (Some(delegate), Some(sender)) = + (self.delegate.upgrade(), sender.upgrade()) + { + match msg { + Info => { + delegate.do_send(InfoRequest(ctx.address().downgrade(), sender)) + } + Connect { + uuid, + username, + address, + } => delegate.do_send(ClientRequest( + ctx.address().downgrade(), + sender, + ClientDetails { + uuid, + username, + address, + public_key: None, + }, + )), + }; + ctx.stop(); + } + } + + fn error( + &mut self, + ctx: &mut ::Context, + sender: WeakAddr, + ) { + use ConnectionMessage::{CloseConnection, SendData}; + if let Some(sender) = sender.upgrade() { + sender.do_send(SendData( + to_string::(&Error { + msg: "Error in connection initiator?".to_owned(), + }) + .unwrap(), + )); + sender.do_send(CloseConnection); + } + ctx.stop() + } +} + +impl Actor for ConnectionInitiator { + type Context = Context; + + /// on start initiate the protocol. + /// also add self as a subscriber to the connection. + fn started(&mut self, ctx: &mut Self::Context) { + use ConnectionMessage::SendData; + use NetworkSockOut::Request; + use ObservableMessage::Subscribe; + + println!("[ConnectionInitiator] started"); + + self + .connection + .do_send(Subscribe(ctx.address().recipient().downgrade())); + + self + .connection + .do_send(SendData(to_string(&Request).unwrap())); + } + + /// once stopped remove self from the connection subscribers + fn stopped(&mut self, ctx: &mut Self::Context) { + use ObservableMessage::Unsubscribe; + println!("[ConnectionInitiator] stopped"); + self + .connection + .do_send(Unsubscribe(ctx.address().recipient().downgrade())); + } +} + +impl Handler for ConnectionInitiator { + type Result = (); + fn handle( + &mut self, + msg: ConnectionObservableOutput, + ctx: &mut Self::Context, + ) -> Self::Result { + use ConnectionObservableOutput::RecvData; + + if let RecvData(sender, addr, data) = msg { + self.handle_request(sender, ctx, addr, data) + } + } +} + +impl Drop for ConnectionInitiator { + fn drop(&mut self) { + println!("[ConnectionInitiator] Dropping value") + } +} diff --git a/server/src/network/connection_initiator/messages.rs b/server/src/network/connection_initiator/messages.rs new file mode 100644 index 0000000..561c70b --- /dev/null +++ b/server/src/network/connection_initiator/messages.rs @@ -0,0 +1,15 @@ +use actix::{Addr, Message, WeakAddr}; +use foundation::ClientDetails; + +use crate::prelude::actors::{Connection, ConnectionInitiator}; + +#[derive(Message)] +#[rtype(result = "()")] +pub(crate) enum InitiatorOutput { + InfoRequest(WeakAddr, Addr), + ClientRequest( + WeakAddr, + Addr, + ClientDetails, + ), +} diff --git a/server/src/network/connection_initiator/mod.rs b/server/src/network/connection_initiator/mod.rs index b84b86e..07e1a19 100644 --- a/server/src/network/connection_initiator/mod.rs +++ b/server/src/network/connection_initiator/mod.rs @@ -1,169 +1,5 @@ -use std::net::SocketAddr; +mod actor; +mod messages; -use actix::{ - Actor, - ActorContext, - Addr, - AsyncContext, - Context, - Handler, - Message, - WeakAddr, - WeakRecipient, -}; -use foundation::{ - messages::{ - client::{ClientStreamOut, ClientStreamOut::Error}, - network::{NetworkSockIn, NetworkSockOut}, - }, - ClientDetails, -}; -use serde_json::{from_str, to_string}; - -use crate::{ - network::{connection::ConnectionObservableOutput, Connection, ConnectionMessage}, - prelude::messages::ObservableMessage, -}; - -#[derive(Message)] -#[rtype(result = "()")] -pub(crate) enum InitiatorOutput { - InfoRequest(WeakAddr, Addr), - ClientRequest( - WeakAddr, - Addr, - ClientDetails, - ), -} - -/// # ConnectionInitiator -/// Handles the initiatin of a new connection. -/// -/// This will do one of two things: -/// - Create a new client and send it to the network manager. -/// - Request the eserver info and send it to the connection. -pub struct ConnectionInitiator { - delegate: WeakRecipient, - connection: Addr, -} - -impl ConnectionInitiator { - pub(crate) fn new( - delegate: WeakRecipient, - connection: Addr, - ) -> Addr { - ConnectionInitiator { - connection, - delegate, - } - .start() - } - - fn handle_request( - &mut self, - sender: WeakAddr, - ctx: &mut ::Context, - _address: SocketAddr, - data: String, - ) { - use InitiatorOutput::{ClientRequest, InfoRequest}; - use NetworkSockIn::{Connect, Info}; - - let msg = from_str::(data.as_str()); - if let Err(e) = msg.as_ref() { - println!("[ConnectionInitiator] error decoding message {}", e); - self.error(ctx, sender); - return; - } - let msg = msg.unwrap(); - - println!("[ConnectionInitiator] matching request"); - if let (Some(delegate), Some(sender)) = (self.delegate.upgrade(), sender.upgrade()) { - match msg { - Info => delegate.do_send(InfoRequest(ctx.address().downgrade(), sender)), - Connect { - uuid, - username, - address, - } => delegate.do_send(ClientRequest( - ctx.address().downgrade(), - sender, - ClientDetails { - uuid, - username, - address, - public_key: None, - }, - )), - }; - ctx.stop(); - } - } - - fn error(&mut self, ctx: &mut ::Context, sender: WeakAddr) { - use ConnectionMessage::{CloseConnection, SendData}; - if let Some(sender) = sender.upgrade() { - sender.do_send(SendData( - to_string::(&Error { - msg: "Error in connection initiator?".to_owned(), - }) - .unwrap(), - )); - sender.do_send(CloseConnection); - } - ctx.stop() - } -} - -impl Actor for ConnectionInitiator { - type Context = Context; - - /// on start initiate the protocol. - /// also add self as a subscriber to the connection. - fn started(&mut self, ctx: &mut Self::Context) { - use NetworkSockOut::Request; - use ObservableMessage::Subscribe; - - use super::ConnectionMessage::SendData; - - println!("[ConnectionInitiator] started"); - - self - .connection - .do_send(Subscribe(ctx.address().recipient().downgrade())); - - self - .connection - .do_send(SendData(to_string(&Request).unwrap())); - } - - /// once stopped remove self from the connection subscribers - fn stopped(&mut self, ctx: &mut Self::Context) { - use ObservableMessage::Unsubscribe; - println!("[ConnectionInitiator] stopped"); - self - .connection - .do_send(Unsubscribe(ctx.address().recipient().downgrade())); - } -} - -impl Handler for ConnectionInitiator { - type Result = (); - fn handle( - &mut self, - msg: ConnectionObservableOutput, - ctx: &mut Self::Context, - ) -> Self::Result { - use ConnectionObservableOutput::RecvData; - - if let RecvData(sender, addr, data) = msg { - self.handle_request(sender, ctx, addr, data) - } - } -} - -impl Drop for ConnectionInitiator { - fn drop(&mut self) { - println!("[ConnectionInitiator] Dropping value") - } -} +pub(crate) use actor::ConnectionInitiator; +pub(crate) use messages::InitiatorOutput;