use emerwen_protocol::{message::payload::MessagePayload, ProtocolActorHandle};
use tokio::{
net::TcpStream,
sync::mpsc::{Receiver, Sender},
};
use tracing::info;
pub enum ClientMessage {
SendPayload { payload: MessagePayload },
}
pub struct Client {
addr: String,
worker_tx: Sender<ClientMessage>,
payload_rx: Receiver<MessagePayload>,
}
impl Client {
pub fn new(
addr: impl Into<String>,
worker_tx: Sender<ClientMessage>,
payload_rx: Receiver<MessagePayload>,
) -> Client {
Client {
addr: addr.into(),
worker_tx,
payload_rx,
}
}
pub async fn run(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let socket = TcpStream::connect(&self.addr).await?;
let handle = ProtocolActorHandle::new(socket);
let handle_read = handle.clone();
tokio::task::spawn(async move {
let handle = handle_read;
loop {
let message = handle.read_message().await;
info!("Received message: {:?}", message);
// match payload bla bla
// write something back if appliccable
// use worker_tx to send data to main worker thread
}
});
info!("before prx");
while let Some(payload) = self.payload_rx.recv().await {
info!("prx");
let _ = handle.send_message(payload).await;
}
info!("after prx");
// read some channel that comes passed from main to this server struct
// and send messages yeah.
//let message = handle.send_message(Message).await;
Ok(())
}
}