RircdState::run() result channel

This commit is contained in:
some body 2021-09-16 16:47:52 -05:00
parent 6ce23674ee
commit b38615b5dd
4 changed files with 50 additions and 19 deletions

View File

@ -9,6 +9,6 @@ use crate::event::Event;
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>, event_sender: oneshot::Sender<Result<mpsc::Sender<Event>>>,
event_receiver: mpsc::Sender<Event>); event_handler: mpsc::Sender<Event>);
} }

View File

@ -2,6 +2,7 @@ use tokio::sync::oneshot;
use eyre::Result; use eyre::Result;
pub enum InternalEvent { pub enum InternalEvent {
Test,
} }
pub enum EventKind { pub enum EventKind {
@ -12,5 +13,12 @@ pub enum EventKind {
pub struct Event { pub struct Event {
pub kind: EventKind, pub kind: EventKind,
pub result_sender: oneshot::Sender<Result<()>>, pub result_sender: oneshot::Sender<EventResponse>,
}
pub enum EventResponse {
Success,
SuccessQuery(String),
//use Error type
Failed(String),
} }

View File

@ -1,13 +1,20 @@
use crate::endpoint::Endpoint; use crate::{endpoint::Endpoint, event::InternalEvent};
use async_trait::async_trait; use async_trait::async_trait;
use tokio::sync::{mpsc,oneshot}; use tokio::sync::{mpsc,oneshot,Mutex};
use std::sync::Arc; use std::sync::Arc;
use eyre::Result; use eyre::Result;
use crate::event::{EventKind,Event}; use crate::event::{EventKind,Event, EventResponse};
pub struct IrcEndpoint { pub struct IrcEndpoint {
} }
impl IrcEndpoint {
fn new() -> Result<Self> {
Ok( IrcEndpoint {
})
}
}
#[async_trait] #[async_trait]
impl Endpoint for IrcEndpoint { impl Endpoint for IrcEndpoint {
fn name(&self) -> String { fn name(&self) -> String {
@ -18,15 +25,27 @@ impl Endpoint for IrcEndpoint {
true true
} }
async fn listen(self: Arc<Self>, success_status_send: oneshot::Sender<Result<()>>, async fn listen(self: Arc<Self>, event_sink_sender: oneshot::Sender<Result<mpsc::Sender<Event>>>,
event_receiver: mpsc::Sender<Event>) { event_handler: mpsc::Sender<Event>) {
success_status_send.send(Ok(())); //TODO find apropiate defeault or size
let (s,r) = mpsc::channel(64);
event_sink_sender.send(Ok(s));
let (s,r) = oneshot::channel();
event_receiver.send( loop {
let (event_response_send, event_response_recv) = oneshot::channel();
event_handler.send(
Event { Event {
kind: EventKind::Message, kind: EventKind::Internal(InternalEvent::Test),
event.result_sender.send(Ok(()), result_sender: event_response_send,
}).await; }).await;
//TODO don't unwrap
match event_response_recv.await.unwrap() {
EventResponse::Success => {},
_ => {},
};
}
} }
} }

View File

@ -5,7 +5,7 @@ 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 tokio::sync::mpsc;
use crate::event::{EventKind,Event}; use crate::event::{EventKind,Event,EventResponse};
use crate::endpoint::Endpoint; use crate::endpoint::Endpoint;
use crate::irc_endpoint::IrcEndpoint; use crate::irc_endpoint::IrcEndpoint;
@ -50,7 +50,7 @@ impl RircdState {
//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<mpsc::Sender<Event>>>();
tokio::spawn(endpoint.clone().listen(success_status_send, event_sender.to_owned())); 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
@ -73,8 +73,12 @@ impl RircdState {
tokio::select! { tokio::select! {
event = event_receiver.recv() => { event = event_receiver.recv() => {
//TODO don't unwrap() I guess //TODO don't unwrap() I guess
match event.unwrap().kind { let event = event.unwrap();
EventKind::Internal(ev) => {}, match event.kind {
EventKind::Internal(ev) => {
//TODO impl
event.result_sender.send(EventResponse::Success);
},
EventKind::Message => {}, EventKind::Message => {},
_ => {}, _ => {},
}; };