diff --git a/src/chat_pinnd.rs b/src/chat_pinnd.rs index 9a5d8fe..3fc15c5 100644 --- a/src/chat_pinnd.rs +++ b/src/chat_pinnd.rs @@ -2,9 +2,11 @@ use std::collections::HashMap; -use actix::{Actor, Addr, Context, Handler, Message}; +use actix::prelude::*; +use actix_broker::BrokerSubscribe; use crate::ws_sansad; +use crate::messages as ms; #[allow(dead_code)] pub struct ChatPinnd { @@ -18,41 +20,21 @@ pub struct Grih { clients: Vec> } -// Handler Messages -pub struct Join { - pub grih: JoinType, - pub length: Option, - pub addr: Addr -} - -#[allow(dead_code)] -pub enum JoinType { - Name(String), - Kunjika(i32) -} - -pub struct Text { - pub grih_kunjika: i32, - pub sender_name: String, - pub text: String -} - -pub struct Delete { - pub grih_kunjika: i32, - pub addr: Addr -} - impl Actor for ChatPinnd { type Context = Context; + + fn started(&mut self, ctx: &mut Self::Context) { + self.subscribe_system_async::(ctx); + self.subscribe_system_async::(ctx); + } } -impl Handler for ChatPinnd { +impl Handler for ChatPinnd { type Result = Option; - fn handle(&mut self, msg: Join, _: &mut Self::Context) -> Self::Result { - println!("Came to join"); + fn handle(&mut self, msg: ms::JoinUser, _: &mut Self::Context) -> Self::Result { match msg.grih { - JoinType::Name(name) => { + ms::JoinUserGrihType::Name(name) => { let mat = Some(name.clone()); if let Some((kunjika, grih)) = self.grih.iter_mut().find(|(_, g)| g.name == mat) { @@ -69,7 +51,6 @@ impl Handler for ChatPinnd { while self.grih.contains_key(&kunjika) { kunjika = rand::random::(); } - println!("Creating {}", name); self.grih.insert(kunjika, Grih { name: Some(name), length: msg.length, @@ -77,7 +58,7 @@ impl Handler for ChatPinnd { }); return Some(kunjika); - }, JoinType::Kunjika(kunjika) => { + }, ms::JoinUserGrihType::Kunjika(kunjika) => { match self.grih.get_mut(&kunjika) { Some(grih) => { if let Some(val) = grih.length { @@ -100,14 +81,14 @@ impl Handler for ChatPinnd { } } -impl Handler for ChatPinnd { +impl Handler for ChatPinnd { type Result = (); - fn handle(&mut self, msg: Text, _: &mut Self::Context) -> Self::Result { + fn handle(&mut self, msg: ms::ReciveText, _: &mut Self::Context) -> Self::Result { println!("Here to text"); if let Some(grih) = self.grih.get(&msg.grih_kunjika) { for client in grih.clients.iter() { - client.do_send(ws_sansad::Text { + client.do_send(ms::WsMessage { sender: msg.sender_name.clone(), text: msg.text.clone(), }); @@ -116,10 +97,10 @@ impl Handler for ChatPinnd { } } -impl Handler for ChatPinnd { +impl Handler for ChatPinnd { type Result = (); - fn handle(&mut self, msg: Delete, _: &mut Self::Context) -> Self::Result { + fn handle(&mut self, msg: ms::LeaveUser, _: &mut Self::Context) -> Self::Result { if let Some(grih) = self.grih.get_mut(&msg.grih_kunjika) { if let Some(i) = grih.clients.iter().position(|x| x == &msg.addr) { grih.clients.remove(i); @@ -132,19 +113,14 @@ impl Handler for ChatPinnd { } } -impl ChatPinnd { - pub fn new() -> Self { +impl Default for ChatPinnd { + fn default() -> Self { ChatPinnd { grih: HashMap::new(), vyaktigat_waitlist: Vec::new() } } - - pub fn start() -> Addr { - ChatPinnd::new().start() - } } -impl Message for Join { type Result = Option; } -impl Message for Text { type Result = (); } -impl Message for Delete { type Result = (); } \ No newline at end of file +impl SystemService for ChatPinnd {} +impl Supervised for ChatPinnd {} diff --git a/src/main.rs b/src/main.rs index 48b849f..007ffa0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,19 @@ -use actix::Addr; use actix_web::{App, Error, HttpRequest, HttpResponse, HttpServer, web}; use actix_files as fs; use actix_web_actors::ws; -use chat_pinnd::ChatPinnd; use ws_sansad::WsSansad; mod config; +mod messages; mod ws_sansad; mod chat_pinnd; #[actix_web::main] async fn main() -> std::io::Result<()> { let config = config::Config::new(); - let addr = web::Data::new(ChatPinnd::start()); let static_path = config.static_path; HttpServer::new(move || { App::new() - .app_data(addr.clone()) .service(web::resource("/ws/").route(web::get().to(ws_index))) .service(fs::Files::new("/", &static_path).index_file("index.html")) }) @@ -25,8 +22,6 @@ async fn main() -> std::io::Result<()> { .await } -async fn ws_index(req: HttpRequest, stream: web::Payload, pinnd: web::Data>) -> Result { - let (addr, resp) = ws::start_with_addr(WsSansad::new(pinnd), &req, stream)?; - addr.do_send(ws_sansad::SelfAddr(addr.clone())); - Ok(resp) +async fn ws_index(req: HttpRequest, stream: web::Payload) -> Result { + ws::start(WsSansad::new(), &req, stream) } diff --git a/src/messages.rs b/src/messages.rs new file mode 100644 index 0000000..cd66407 --- /dev/null +++ b/src/messages.rs @@ -0,0 +1,43 @@ +//! Messages to be sent between Actors +use actix::prelude::*; + +use crate::ws_sansad::WsSansad; + +// For ChatPinnd +#[derive(Clone, Message)] +#[rtype(result = "Option")] +pub struct JoinUser { + pub grih: JoinUserGrihType, + pub length: Option, + pub addr: Addr +} + +#[allow(dead_code)] +#[derive(Clone)] +pub enum JoinUserGrihType { + Name(String), + Kunjika(i32) +} + +#[derive(Clone, Message)] +#[rtype(result = "()")] +pub struct ReciveText { + pub grih_kunjika: i32, + pub sender_name: String, + pub text: String +} + +#[derive(Clone, Message)] +#[rtype(result = "()")] +pub struct LeaveUser { + pub grih_kunjika: i32, + pub addr: Addr +} + +// For WsSansad +#[derive(Clone, Message)] +#[rtype(result = "()")] +pub struct WsMessage { + pub text: String, + pub sender: String +} \ No newline at end of file diff --git a/src/ws_sansad.rs b/src/ws_sansad.rs index 99683e6..c8a4d9a 100644 --- a/src/ws_sansad.rs +++ b/src/ws_sansad.rs @@ -1,16 +1,15 @@ //! Ws Sansad manage websocket of each client -use actix::{Actor, Addr, Handler, Message, Running, StreamHandler}; -use actix_web::web; +use actix::prelude::*; +use actix_broker::{Broker, SystemBroker}; use actix_web_actors::ws; use serde_json::{json, Value}; use crate::chat_pinnd::ChatPinnd; -use crate::chat_pinnd as pd; +use crate::messages as ms; pub struct WsSansad { name: String, isthiti: Isthiti, - pinnd: web::Data>, addr: Option> } @@ -26,16 +25,12 @@ pub struct Grih { // name: String } -// Handler Messages -pub struct Text { - pub text: String, - pub sender: String -} - -pub struct SelfAddr(pub Addr); - impl Actor for WsSansad { type Context = ws::WebsocketContext; + + fn started(&mut self, ctx: &mut Self::Context) { + self.addr = Some(ctx.address().clone()); + } fn stopping(&mut self, _: &mut Self::Context) -> Running { futures::executor::block_on(self.end()); @@ -58,9 +53,9 @@ impl StreamHandler> for WsSansad { } } -impl Handler for WsSansad { +impl Handler for WsSansad { type Result = (); - fn handle(&mut self, msg: Text, ctx: &mut Self::Context) -> Self::Result { + fn handle(&mut self, msg: ms::WsMessage, ctx: &mut Self::Context) -> Self::Result { let json = json!({ "cmd": "text", "text": msg.text, @@ -70,19 +65,11 @@ impl Handler for WsSansad { } } -impl Handler for WsSansad { - type Result = (); - fn handle(&mut self, msg: SelfAddr, _: &mut Self::Context) -> Self::Result { - self.addr = Some(msg.0); - } -} - impl WsSansad { - pub fn new(pinnd: web::Data>) -> Self { + pub fn new() -> Self { WsSansad { name: "()".to_owned(), isthiti: Isthiti::None, - pinnd, addr: None } } @@ -113,7 +100,7 @@ impl WsSansad { } }; - self.pinnd.do_send(pd::Text { + Broker::::issue_async(ms::ReciveText { grih_kunjika, sender_name: self.name.clone(), text @@ -127,8 +114,8 @@ impl WsSansad { None => None }; - let kunjika = self.pinnd.send(pd::Join{ - grih: pd::JoinType::Name(name.clone()), + let kunjika = ChatPinnd::from_registry().send(ms::JoinUser{ + grih: ms::JoinUserGrihType::Name(name.clone()), length, addr: self.addr.clone().unwrap() }).await.unwrap().unwrap(); @@ -141,13 +128,10 @@ impl WsSansad { async fn end(&mut self) { if let Isthiti::Grih(val) = &mut self.isthiti { - self.pinnd.do_send(pd::Delete { + Broker::::issue_async(ms::LeaveUser { grih_kunjika: val.kunjika, addr: self.addr.clone().unwrap() }); } } } - -impl Message for Text { type Result = (); } -impl Message for SelfAddr { type Result = (); }