84 lines
2.6 KiB
Rust
84 lines
2.6 KiB
Rust
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<Arc<dyn Endpoint>>,
|
|
}
|
|
|
|
impl RircdState {
|
|
pub fn new() -> Result<Self> {
|
|
Ok( Self {
|
|
endpoints: vec![],
|
|
} )
|
|
}
|
|
|
|
pub async fn activate_endpoint(&mut self, ep: Arc<dyn Endpoint>,
|
|
send_event: mpsc::Sender<RircdEvent>,
|
|
send_msg: mpsc::Sender<RircdMsg>
|
|
) -> 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<Arc<dyn Endpoint>> {
|
|
None
|
|
}
|
|
|
|
pub async fn run(mut self) -> Result<()> {
|
|
//TODO find appropriate channel msg buf size
|
|
let (event_send,mut event_recv) = mpsc::channel::<RircdEvent>(64);
|
|
let (msg_send,mut msg_recv) = mpsc::channel::<RircdMsg>(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<Arc<dyn Endpoint>> = 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<()>;
|
|
}
|