use crate::comm::types::message::{MessageState, MessageType};
use pyo3::prelude::*;
use super::IngestedTokens;
pub trait ChannelInterface {
fn receive_tokens_in_python(&mut self) -> Option<IngestedTokens>;
fn send_tokens_in_rust(&mut self, tokens: IngestedTokens);
fn receive_in_python(&mut self) -> Option<MessageState>;
fn send_in_rust(&mut self, message_type: MessageType, message_data: MessageState);
}
#[derive(Clone, Debug)]
#[pyclass]
pub struct ChannelHandler {
pub token_receiver: Option<crossbeam_channel::Receiver<IngestedTokens>>,
pub token_sender: Option<crossbeam_channel::Sender<IngestedTokens>>,
pub py_message_receiver: Option<crossbeam_channel::Receiver<(MessageType, MessageState)>>,
pub message_sender: Option<crossbeam_channel::Sender<(MessageType, MessageState)>>,
}
impl ChannelHandler {
pub fn new(
token_sender: Option<crossbeam_channel::Sender<IngestedTokens>>,
token_receiver: Option<crossbeam_channel::Receiver<IngestedTokens>>,
py_message_receiver: Option<crossbeam_channel::Receiver<(MessageType, MessageState)>>,
message_sender: Option<crossbeam_channel::Sender<(MessageType, MessageState)>>,
) -> Self {
ChannelHandler { py_message_receiver, token_sender, token_receiver, message_sender }
}
}
#[derive(Clone, Debug)]
#[pyclass]
pub struct PyMessageInterface {
channel_handler: ChannelHandler,
}
#[pymethods]
impl PyMessageInterface {
#[new]
pub fn new(channel: ChannelHandler) -> Self {
PyMessageInterface { channel_handler: channel }
}
pub fn receive_tokens_in_python(&mut self) -> Option<IngestedTokens> {
self.channel_handler.receive_tokens_in_python()
}
pub fn send_tokens_in_rust(&mut self, tokens: IngestedTokens) {
self.channel_handler.send_tokens_in_rust(tokens)
}
pub fn receive_in_python(&mut self) -> Option<MessageState> {
self.channel_handler.receive_in_python()
}
pub fn send_in_rust(&mut self, message_type: MessageType, message_data: MessageState) {
self.channel_handler.send_in_rust(message_type, message_data)
}
}
impl ChannelInterface for ChannelHandler {
fn receive_tokens_in_python(&mut self) -> Option<IngestedTokens> {
if self.token_receiver.is_some() {
let token = self.token_receiver.as_ref();
match token {
Some(token) => {
let token = token.try_recv();
match token {
Ok(token) => Some(token),
Err(_) => None,
}
},
None => None,
}
} else {
None
}
}
fn send_tokens_in_rust(&mut self, tokens: IngestedTokens) {
if self.token_sender.is_some() {
let token = self.token_sender.as_ref();
match token {
Some(token) => {
let token = token.send(tokens);
match token {
Ok(_) => (),
Err(_) => (),
}
},
None => (),
}
} else {
()
}
}
fn receive_in_python(&mut self) -> Option<MessageState> {
if self.py_message_receiver.is_some() {
let message = self.py_message_receiver.as_ref();
match message {
Some(message) => {
let message = message.try_recv();
match message {
Ok(message) => Some(message.1),
Err(_) => None,
}
},
None => None,
}
} else {
None
}
}
fn send_in_rust(&mut self, message_type: MessageType, message_data: MessageState) {
if self.message_sender.is_some() {
let message = self.message_sender.as_ref();
match message {
Some(message) => {
let message = message.send((message_type, message_data));
match message {
Ok(_) => (),
Err(_) => (),
}
},
None => (),
}
} else {
()
}
}
}