From aa77d3d2a5192f37d5d59853790de67e0b068696 Mon Sep 17 00:00:00 2001 From: some body Date: Thu, 16 Sep 2021 15:40:40 -0500 Subject: [PATCH] RircdState::run() --- src/endpoint.rs | 6 ++++-- src/event.rs | 2 +- src/irc_endpoint.rs | 25 +++++++++++++++++++++++++ src/main.rs | 2 ++ src/state.rs | 29 +++++++++++++++++++++++++---- 5 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 src/irc_endpoint.rs diff --git a/src/endpoint.rs b/src/endpoint.rs index f35d009..effd3c0 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -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, success_status_send: oneshot::Sender>); + async fn listen(self: Arc, success_status_send: oneshot::Sender>, + event_receiver: mpsc::Sender); } diff --git a/src/event.rs b/src/event.rs index ed4eff8..eaae9c9 100644 --- a/src/event.rs +++ b/src/event.rs @@ -10,7 +10,7 @@ enum EventKind { } -struct Event { +pub struct Event { kind: EventKind, result_channel: oneshot::Receiver>, } diff --git a/src/irc_endpoint.rs b/src/irc_endpoint.rs new file mode 100644 index 0000000..b224476 --- /dev/null +++ b/src/irc_endpoint.rs @@ -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, success_status_send: oneshot::Sender>, + event_receiver: mpsc::Sender) { + success_status_send.send(Ok(())); + } +} diff --git a/src/main.rs b/src/main.rs index 570070f..a82f74a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ mod endpoint; mod state; mod event; +mod irc_endpoint; + use state::RircdState; //TODO maybe spawn the reactor later on with diff --git a/src/state.rs b/src/state.rs index bb829ff..42926a6 100644 --- a/src/state.rs +++ b/src/state.rs @@ -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>, @@ -19,9 +22,14 @@ pub struct RircdState { impl RircdState { pub fn new() -> Result { + let eps: Vec> = 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::(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::>(); - 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(()) }