RircdState::run()

This commit is contained in:
some body 2021-09-16 15:40:40 -05:00
parent b28e25eea3
commit aa77d3d2a5
5 changed files with 57 additions and 7 deletions

View File

@ -1,12 +1,14 @@
use eyre::Result;
use async_trait::async_trait;
use tokio::sync::oneshot;
use tokio::sync::{mpsc, oneshot};
use std::sync::Arc;
use crate::event::Event;
#[async_trait]
pub trait Endpoint {
fn name(&self) -> String;
fn is_active(&self) -> bool;
async fn listen(self: Arc<Self>, success_status_send: oneshot::Sender<Result<()>>);
async fn listen(self: Arc<Self>, success_status_send: oneshot::Sender<Result<()>>,
event_receiver: mpsc::Sender<Event>);
}

View File

@ -10,7 +10,7 @@ enum EventKind {
}
struct Event {
pub struct Event {
kind: EventKind,
result_channel: oneshot::Receiver<Result<()>>,
}

25
src/irc_endpoint.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::endpoint::Endpoint;
use async_trait::async_trait;
use tokio::sync::{mpsc,oneshot};
use std::sync::Arc;
use eyre::Result;
use crate::event::Event;
pub struct IrcEndpoint {
}
#[async_trait]
impl Endpoint for IrcEndpoint {
fn name(&self) -> String {
"test irc_endpoint".into()
}
fn is_active(&self) -> bool {
true
}
async fn listen(self: Arc<Self>, success_status_send: oneshot::Sender<Result<()>>,
event_receiver: mpsc::Sender<Event>) {
success_status_send.send(Ok(()));
}
}

View File

@ -4,6 +4,8 @@ mod endpoint;
mod state;
mod event;
mod irc_endpoint;
use state::RircdState;
//TODO maybe spawn the reactor later on with

View File

@ -4,8 +4,11 @@ use tokio::task::spawn_local;
use std::time::Instant;
use std::sync::Arc;
use tokio::sync::oneshot;
use tokio::sync::mpsc;
use crate::event::Event;
use crate::endpoint::Endpoint;
use crate::irc_endpoint::IrcEndpoint;
pub struct RircdState {
endpoints: Vec<Arc<dyn Endpoint>>,
@ -19,9 +22,14 @@ pub struct RircdState {
impl RircdState {
pub fn new() -> Result<Self> {
let eps: Vec<Arc<dyn Endpoint>> = vec![
Arc::new(IrcEndpoint {} ),
];
//TODO impl
Ok(RircdState {
endpoints: vec![],
endpoints: eps,
//endpoints: vec![],
creation_timestamp: Some(Instant::now()),
plainlog: None,
sqlite_log: None,
@ -35,22 +43,35 @@ impl RircdState {
}
pub async fn run(mut self) -> Result<()> {
//TODO choose good default/custom chan size
let (event_sender,mut event_receiver) = mpsc::channel::<Event>(64);
//try to start each endpoint
self.endpoints.iter().filter(|ep| ep.is_active()).for_each(|endpoint| {
let endpoint_name = endpoint.name();
let (success_status_send,success_status_recv) = oneshot::channel::<Result<()>>();
tokio::spawn(endpoint.clone().listen(success_status_send));
tokio::spawn(endpoint.clone().listen(success_status_send, event_sender.to_owned()));
//TODO color err msg
//TODO paralelize and sync at the end
spawn_local(async move {
futures::executor::block_on(async move {
println!("starting {} | {}", endpoint_name,
match success_status_recv.await {
Ok(_) => format!("SUCCEEDED |"),
Err(e) => format!("FAILED| <{}>", e),
Err(e) => format!("FAILED | <{}>", e),
});
});
});
//TODO Err() if not at least one Endpoint is running because
//if none is running there is no way to activate further (user locked out)
//> who needs a daemon that does nothing and where nothing can be configured to be done
tokio::select! {
event = event_receiver.recv() => {
()
},
}
Ok(())
}