use async_std::stream::StreamExt; use async_std::sync::{Mutex, MutexGuard}; use futures::{channel::mpsc, SinkExt}; use serde::de::DeserializeOwned; use std::any::{Any, TypeId}; use std::convert::From; use std::sync::Arc; use crate::{connection::*, errors::ProtocolError, log, types::ProtocolMessage}; use std::marker::PhantomData; pub trait BrokerRequest: std::fmt::Debug { fn send(&self) -> ProtocolMessage; } //pub trait BrokerResponse: TryFrom + std::fmt::Debug {} impl TryFrom for () { type Error = ProtocolError; fn try_from(msg: ProtocolMessage) -> Result { Ok(()) } } pub trait IActor: EActor { //fn process_request(&self, req: Box) -> Box {} } #[async_trait::async_trait] pub trait EActor: Send + Sync + std::fmt::Debug { //type T: TryFrom + std::fmt::Debug; //async fn handle(&mut self, msg: ProtocolMessage); async fn respond( &mut self, msg: ProtocolMessage, //stream: Option, fsm: Arc>, ) -> Result<(), ProtocolError>; } #[derive(Debug)] pub struct Actor< 'a, A: BrokerRequest, B: TryFrom + std::fmt::Debug + Sync, > { id: i64, phantom_a: PhantomData<&'a A>, phantom_b: PhantomData<&'a B>, receiver: Receiver, receiver_tx: Sender, initiator: bool, } // #[async_trait::async_trait] // impl< // A: BrokerRequest + std::marker::Sync + 'static, // B: TryFrom // + std::fmt::Debug // + std::marker::Sync // + 'static, // > EActor for Actor<'_, A, B> // { // //type T = B; // // async fn handle(&mut self, msg: ProtocolMessage) { // // if self.initiator && msg.type_id() == TypeId::of::() // // || !self.initiator && msg.type_id() == TypeId::of::() // // { // // let _ = self.receiver_tx.send(ConnectionCommand::Msg(msg)).await; // // } else { // // log!("NOT OK"); // // } // // } // // async fn respond(id: i64, msg: A) -> Result { // // let mut actor = Box::new(Actor::::new(id, false)); // // //actor.process_request // // match self.receiver.next().await { // // Some(msg) => B::receive(msg), // // _ => Err(ProtocolError::ActorError), // // } // // } // } impl< A: BrokerRequest + 'static, B: TryFrom + std::marker::Sync + std::fmt::Debug + 'static, > Actor<'_, A, B> { pub fn new(id: i64, initiator: bool) -> Self { let (mut receiver_tx, receiver) = mpsc::unbounded::(); Self { id, receiver, receiver_tx, phantom_a: PhantomData, phantom_b: PhantomData, initiator, } } // pub fn verify(&self, msg: ProtocolMessage) -> bool { // self.initiator && msg.type_id() == TypeId::of::() // || !self.initiator && msg.type_id() == TypeId::of::() // } pub async fn request( &mut self, msg: ProtocolMessage, stream: Option, fsm: Arc>, ) -> Result { //sender.send(ConnectionCommand::Msg(msg.send())).await; fsm.lock().await.send(msg).await?; match self.receiver.next().await { Some(ConnectionCommand::Msg(msg)) => msg.try_into(), _ => Err(ProtocolError::ActorError), } } pub fn new_responder() -> Box { Box::new(Self::new(0, false)) } pub fn get_receiver_tx(&self) -> Sender { self.receiver_tx.clone() } } mod test { use crate::actor::*; use crate::actors::*; use crate::types::*; #[async_std::test] pub async fn test_actor() { let mut a = Actor::::new(1, true); // a.handle(ProtocolMessage::Start(StartProtocol::Client( // ClientHello::Noise3(Noise::V0(NoiseV0 { data: vec![] })), // ))) // .await; // a.handle(ProtocolMessage::Noise(Noise::V0(NoiseV0 { data: vec![] }))) // .await; } }