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 {
fn name(&self) -> String;
fn is_active(&self) -> bool;
async fn listen(self: Arc<Self>, success_status_send: oneshot::Sender<Result<()>>,
event_receiver: mpsc::Sender<Event>);
async fn listen(self: Arc<Self>, event_sender: oneshot::Sender<Result<mpsc::Sender<Event>>>,
event_handler: mpsc::Sender<Event>);
}

View File

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

View File

@ -5,7 +5,7 @@ use std::time::Instant;
use std::sync::Arc;
use tokio::sync::oneshot;
use tokio::sync::mpsc;
use crate::event::{EventKind,Event};
use crate::event::{EventKind,Event,EventResponse};
use crate::endpoint::Endpoint;
use crate::irc_endpoint::IrcEndpoint;
@ -50,7 +50,7 @@ impl RircdState {
//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<()>>();
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()));
//TODO color err msg
//TODO paralelize and sync at the end
@ -73,8 +73,12 @@ impl RircdState {
tokio::select! {
event = event_receiver.recv() => {
//TODO don't unwrap() I guess
match event.unwrap().kind {
EventKind::Internal(ev) => {},
let event = event.unwrap();
match event.kind {
EventKind::Internal(ev) => {
//TODO impl
event.result_sender.send(EventResponse::Success);
},
EventKind::Message => {},
_ => {},
};