RircdState::run()
This commit is contained in:
parent
b28e25eea3
commit
aa77d3d2a5
|
@ -1,12 +1,14 @@
|
||||||
use eyre::Result;
|
use eyre::Result;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::{mpsc, oneshot};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use crate::event::Event;
|
||||||
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait Endpoint {
|
pub trait Endpoint {
|
||||||
fn name(&self) -> String;
|
fn name(&self) -> String;
|
||||||
fn is_active(&self) -> bool;
|
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>);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ enum EventKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct Event {
|
pub struct Event {
|
||||||
kind: EventKind,
|
kind: EventKind,
|
||||||
result_channel: oneshot::Receiver<Result<()>>,
|
result_channel: oneshot::Receiver<Result<()>>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,8 @@ mod endpoint;
|
||||||
mod state;
|
mod state;
|
||||||
mod event;
|
mod event;
|
||||||
|
|
||||||
|
mod irc_endpoint;
|
||||||
|
|
||||||
use state::RircdState;
|
use state::RircdState;
|
||||||
|
|
||||||
//TODO maybe spawn the reactor later on with
|
//TODO maybe spawn the reactor later on with
|
||||||
|
|
29
src/state.rs
29
src/state.rs
|
@ -4,8 +4,11 @@ use tokio::task::spawn_local;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
use crate::event::Event;
|
||||||
|
|
||||||
use crate::endpoint::Endpoint;
|
use crate::endpoint::Endpoint;
|
||||||
|
use crate::irc_endpoint::IrcEndpoint;
|
||||||
|
|
||||||
pub struct RircdState {
|
pub struct RircdState {
|
||||||
endpoints: Vec<Arc<dyn Endpoint>>,
|
endpoints: Vec<Arc<dyn Endpoint>>,
|
||||||
|
@ -19,9 +22,14 @@ pub struct RircdState {
|
||||||
|
|
||||||
impl RircdState {
|
impl RircdState {
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> Result<Self> {
|
||||||
|
let eps: Vec<Arc<dyn Endpoint>> = vec![
|
||||||
|
Arc::new(IrcEndpoint {} ),
|
||||||
|
];
|
||||||
|
|
||||||
//TODO impl
|
//TODO impl
|
||||||
Ok(RircdState {
|
Ok(RircdState {
|
||||||
endpoints: vec![],
|
endpoints: eps,
|
||||||
|
//endpoints: vec![],
|
||||||
creation_timestamp: Some(Instant::now()),
|
creation_timestamp: Some(Instant::now()),
|
||||||
plainlog: None,
|
plainlog: None,
|
||||||
sqlite_log: None,
|
sqlite_log: None,
|
||||||
|
@ -35,22 +43,35 @@ impl RircdState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(mut self) -> Result<()> {
|
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
|
//try to start each endpoint
|
||||||
self.endpoints.iter().filter(|ep| ep.is_active()).for_each(|endpoint| {
|
self.endpoints.iter().filter(|ep| ep.is_active()).for_each(|endpoint| {
|
||||||
let endpoint_name = endpoint.name();
|
let endpoint_name = endpoint.name();
|
||||||
let (success_status_send,success_status_recv) = oneshot::channel::<Result<()>>();
|
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 color err msg
|
||||||
//TODO paralelize and sync at the end
|
//TODO paralelize and sync at the end
|
||||||
spawn_local(async move {
|
futures::executor::block_on(async move {
|
||||||
println!("starting {} | {}", endpoint_name,
|
println!("starting {} | {}", endpoint_name,
|
||||||
match success_status_recv.await {
|
match success_status_recv.await {
|
||||||
Ok(_) => format!("SUCCEEDED |"),
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue