Skip to content

Commit

Permalink
feat: start on runner
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Aug 24, 2022
1 parent 486dfa4 commit 05bb59c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions hop_leap/src/lib.rs
@@ -1,4 +1,5 @@
#![warn(clippy::pedantic)]

mod errors;
mod runner;
mod shard;
Empty file added hop_leap/src/manager/mod.rs
Empty file.
64 changes: 64 additions & 0 deletions hop_leap/src/runner/mod.rs
@@ -0,0 +1,64 @@
use futures::channel::mpsc::UnboundedReceiver;
use tokio::sync::mpsc::UnboundedSender;

use crate::shard::socket::SenderExt;

use crate::{
errors::Result,
shard::{types::InterMessage, Shard},
};

pub struct ShardRunner {
manager_tx: UnboundedSender<String>,
runner_rx: UnboundedReceiver<InterMessage>,
runner_tx: UnboundedSender<InterMessage>,
pub shard: Shard,
}

impl ShardRunner {
pub fn new(
manager_tx: UnboundedSender<String>,
runner_rx: UnboundedReceiver<InterMessage>,
runner_tx: UnboundedSender<InterMessage>,
shard: Shard,
) -> Self {
Self {
manager_tx,
runner_rx,
runner_tx,
shard,
}
}

pub async fn run(&mut self) -> Result<()> {
loop {
if !self.recieve().await? {
return Ok(());
}
}
}

async fn recieve(&mut self) -> Result<bool> {
loop {
match self.runner_rx.try_next() {
Ok(Some(message)) => {
if !self.handle_rx_message(message).await {
return Ok(false);
}
}

Ok(None) => return Ok(false),

Err(_) => break,
}
}

Ok(true)
}

async fn handle_rx_message(&mut self, message: InterMessage) -> bool {
match message {
InterMessage::Json(json) => self.shard.client.send_json(&json).await.is_ok(),
}
}
}
2 changes: 1 addition & 1 deletion hop_leap/src/shard/mod.rs
@@ -1,5 +1,5 @@
pub mod error;
mod socket;
pub mod socket;
pub mod types;

use std::sync::Arc;
Expand Down
7 changes: 7 additions & 0 deletions hop_leap/src/shard/types.rs
Expand Up @@ -3,6 +3,13 @@ use std::fmt;
use serde::Deserialize;
use serde_json::Value;

#[derive(Clone, Debug)]
pub enum InterMessage {
#[cfg(feature = "client")]
Client(Box<ShardClientMessage>),
Json(Value),
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ConnectionStage {
Expand Down

0 comments on commit 05bb59c

Please sign in to comment.