use eyre::{bail, Result}; use crate::endpoint::Endpoint; use std::sync::Arc; //use async_trait::async_trait; use tokio::sync::mpsc; use crate::TelegramEndpoint::TelegramEndpoint; use crate::msg::RircdMsg; use crate::event::RircdEvent; pub struct RircdState { endpoints: Vec>, } impl RircdState { pub fn new() -> Result { Ok( Self { endpoints: vec![], } ) } pub async fn activate_endpoint(&mut self, ep: Arc, send_event: mpsc::Sender, send_msg: mpsc::Sender ) -> Result<()> { //TODO add joinhandle to a list of joinhandles //TODO ep.active() tokio::spawn(ep.listen(send_msg, send_event)); Ok(()) } pub async fn find_endpoint_by_name(&self, name: String) -> Option> { None } pub async fn run(mut self) -> Result<()> { //TODO find appropriate channel msg buf size let (event_send,mut event_recv) = mpsc::channel::(64); let (msg_send,mut msg_recv) = mpsc::channel::(64); //TOD opnly spawn those already marked as active from the config parsing, //if failed set to inactive for ep in &self.endpoints { let ep = ep.clone(); tokio::spawn(ep.listen(msg_send.clone(), event_send.clone())); } loop { println!("e"); tokio::select! { event = event_recv.recv() => { //TODO should we do this?! let event = event.unwrap(); match event { RircdEvent::Shutdown => break, RircdEvent::ActivateEndpointByName(name) => { let res = self.find_endpoint_by_name(name).await; let res: Option> = Some(Arc::new(TelegramEndpoint::new().unwrap())); //TODO requestor_reply.send( V match res { Some(ep) => self.activate_endpoint( ep, event_send.clone(), msg_send.clone(), ).await, None => Ok(()),//bail!("err no such endpoint XYZ"), }; }, }; }, _ = msg_recv.recv() => { }, } }; Ok(()) } //async fn add_endpoint(&) -> Result<()>; }