Refactors

This commit is contained in:
2026-10-03 15:56:29 +05:30
parent a3385e7702
commit c9438d00a6
14 changed files with 358 additions and 401 deletions
+1 -2
View File
@@ -5,5 +5,4 @@ edition = "2024"
[dependencies]
tracing = { workspace = true }
hd-lib = { path = "../hd-lib" }
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
hd-lib = {path = "../hd-lib"}
-11
View File
@@ -1,11 +0,0 @@
use hd_client::{EventBusClient, EventBusClientError};
fn main() -> Result<(), EventBusClientError> {
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
let addr = "0.0.0.0:21368";
let mut client = EventBusClient::start(addr)?;
client.subscribe()
}
+17 -18
View File
@@ -1,43 +1,42 @@
use std::net::TcpStream;
use hd_lib::{
bus::packet::{self, Packet, PacketType},
error::HError,
bus::packet::{self, Packet},
error::HdError,
hd_tcp,
};
use tracing::{debug, info};
#[derive(Debug)]
pub enum EventBusClientError {
pub enum HdClientError {
TcpStreamError(std::io::Error),
HError(HError),
HError(HdError),
}
impl From<HError> for EventBusClientError {
fn from(value: HError) -> Self {
impl From<HdError> for HdClientError {
fn from(value: HdError) -> Self {
Self::HError(value)
}
}
pub struct EventBusClient {
pub struct HdClient {
tcp_stream: TcpStream,
}
impl EventBusClient {
pub fn start(addr: &'static str) -> Result<Self, EventBusClientError> {
let mut stream = TcpStream::connect(addr).map_err(EventBusClientError::TcpStreamError)?;
hd_tcp::write_packet(&mut stream, &Packet::create_connect_packet())?;
hd_tcp::read_ack(&mut stream, &[PacketType::Connect])?;
impl HdClient {
pub fn start(addr: &'static str) -> Result<Self, HdClientError> {
let stream = TcpStream::connect(addr).map_err(HdClientError::TcpStreamError)?;
info!("Event bus client connected to {}", addr);
Ok(Self { tcp_stream: stream })
}
pub fn subscribe(&mut self) -> Result<(), EventBusClientError> {
pub fn subscribe(&mut self) -> Result<(), HdClientError> {
debug!("Going to subscribe");
hd_tcp::write_packet(&mut self.tcp_stream, &Packet::create_subscribe_packet(0))?;
hd_tcp::read_ack(&mut self.tcp_stream, &[PacketType::Subscribe])?;
hd_tcp::write_packet(
&mut self.tcp_stream,
&Packet::create_connect_packet(packet::ConnectMode::Subscribe),
)?;
loop {
let packet = hd_tcp::read_packet(&mut self.tcp_stream)?;
if let Packet::Disconnect = packet {
@@ -46,7 +45,7 @@ impl EventBusClient {
}
debug!("Received packet ... {}", packet::get_packet_type(&packet));
hd_tcp::write_ack(&mut self.tcp_stream, PacketType::SendEvent)?;
hd_tcp::write_packet(&mut self.tcp_stream, &Packet::create_settle_packet(1))?;
}
}
}