WIP
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "hd-server"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
tracing = { workspace = true }
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
|
||||
hd-lib = { path = "../hd-lib" }
|
||||
@@ -0,0 +1,27 @@
|
||||
use std::{sync::Arc, thread, time::Duration};
|
||||
|
||||
use hd_lib::{bus::EventData, error::HError};
|
||||
use hd_server::TcpEventBus;
|
||||
use tracing::info_span;
|
||||
|
||||
fn main() -> Result<(), HError> {
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
||||
.init();
|
||||
|
||||
let addr = "0.0.0.0:21368";
|
||||
let _scope = info_span!("example_server").entered();
|
||||
let server = Arc::new(TcpEventBus::new());
|
||||
server.publish(EventData::new(1, "Hello"))?;
|
||||
server.publish(EventData::new(1, "World"))?;
|
||||
|
||||
let server_clone = server.clone();
|
||||
thread::spawn(move || server.start(addr));
|
||||
|
||||
for i in 0..255 {
|
||||
thread::sleep(Duration::from_secs(5));
|
||||
server_clone.publish(EventData::new(i, "asd"))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
use std::{
|
||||
io::{Read, Write},
|
||||
net::TcpStream,
|
||||
sync::{Arc, mpsc::Receiver},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use hd_lib::{
|
||||
bus::{Event, EventBus, EventData}, error::HError, thread_pool::ThreadPool,
|
||||
};
|
||||
|
||||
use tracing::{Span, info, info_span};
|
||||
|
||||
use std::net::TcpListener;
|
||||
|
||||
pub struct TcpEventBus {
|
||||
bus: EventBus,
|
||||
pool: ThreadPool,
|
||||
}
|
||||
|
||||
impl TcpEventBus {
|
||||
pub fn new() -> Self {
|
||||
let pool = ThreadPool::new(10);
|
||||
Self {
|
||||
bus: EventBus::new(),
|
||||
pool,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn publish(&self, evt: EventData) -> Result<(), HError> {
|
||||
self.bus.publish(evt)
|
||||
}
|
||||
}
|
||||
|
||||
impl TcpEventBus {
|
||||
pub fn start(&self, addr: &'static str) -> Result<(), HError> {
|
||||
let listener = TcpListener::bind(addr).map_err(HError::TcpSocketBindError)?;
|
||||
info!("Heimdall listening at {}", addr);
|
||||
for stream in listener.incoming() {
|
||||
match stream {
|
||||
Ok(stream) => handle_event_bus_client(&self.pool, self.bus.clone(), stream)?,
|
||||
Err(e) => tracing::error!("TCP connection failed {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
enum EventBusClientConnectionState {
|
||||
Connect,
|
||||
ReadPrelude,
|
||||
ReadCursor,
|
||||
SendEvent,
|
||||
ReceiveAck,
|
||||
Disconnect,
|
||||
}
|
||||
|
||||
struct EventBusClientConnection {
|
||||
stream: TcpStream,
|
||||
pub addr: String,
|
||||
state: EventBusClientConnectionState,
|
||||
subscription: Option<Receiver<Arc<Event>>>,
|
||||
bus: EventBus,
|
||||
}
|
||||
|
||||
impl EventBusClientConnection {
|
||||
fn new(bus: EventBus, stream: TcpStream) -> Result<Self, HError> {
|
||||
let addr = stream
|
||||
.peer_addr()
|
||||
.map(|a| a.to_string())
|
||||
.unwrap_or("unknown".to_string());
|
||||
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(5)))
|
||||
.map_err(HError::TcpPeerError)?;
|
||||
|
||||
let state = EventBusClientConnectionState::Connect;
|
||||
|
||||
Ok(Self {
|
||||
stream,
|
||||
addr,
|
||||
state,
|
||||
bus,
|
||||
subscription: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_event_bus_client(thread_pool: &ThreadPool, bus: EventBus, stream: TcpStream) -> Result<(), HError> {
|
||||
thread_pool.execute(move || {
|
||||
let mut client = EventBusClientConnection::new(bus, stream)?;
|
||||
|
||||
let _scope = info_span!(
|
||||
"handle_event_bus_client",
|
||||
addr = client.addr,
|
||||
state = %client.state
|
||||
)
|
||||
.entered();
|
||||
|
||||
loop {
|
||||
if let EventBusClientConnectionState::Disconnect = client.state {
|
||||
info!("Closing connection due to invalid input");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let prev_state = client.state;
|
||||
handle_event_bus_client_state(&mut client)
|
||||
.inspect_err(|e| info!("Closing connection due to error {}", e))?;
|
||||
if prev_state != client.state {
|
||||
info!("State change {} -> {}", prev_state, client.state);
|
||||
}
|
||||
|
||||
Span::current().record("state", format!("{}", client.state));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn handle_event_bus_client_state(client: &mut EventBusClientConnection) -> Result<(), HError> {
|
||||
let next_state = match client.state {
|
||||
EventBusClientConnectionState::Connect => handle_state_connect(client),
|
||||
EventBusClientConnectionState::ReadPrelude => todo!(),
|
||||
EventBusClientConnectionState::ReadCursor => todo!(),
|
||||
EventBusClientConnectionState::SendEvent => handle_state_send_event(client),
|
||||
EventBusClientConnectionState::ReceiveAck => todo!(),
|
||||
_ => Ok(EventBusClientConnectionState::Disconnect),
|
||||
}?;
|
||||
|
||||
client.state = next_state;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_state_connect(client: &mut EventBusClientConnection) -> Result<EventBusClientConnectionState, HError> {
|
||||
let mut buf = [0u8; 1];
|
||||
client.stream.read_exact(&mut buf).map_err(HError::TcpPeerError)?;
|
||||
if buf[0] == 1 {
|
||||
let subscription = client.bus.subscribe()?;
|
||||
client.subscription = Some(subscription);
|
||||
return Ok(EventBusClientConnectionState::SendEvent);
|
||||
}
|
||||
|
||||
Ok(EventBusClientConnectionState::Disconnect)
|
||||
}
|
||||
|
||||
fn handle_state_send_event(client: &mut EventBusClientConnection) -> Result<EventBusClientConnectionState, HError> {
|
||||
if let Some(subscription) = &mut client.subscription {
|
||||
loop {
|
||||
let Ok(evt) = subscription.recv() else {
|
||||
return Ok(EventBusClientConnectionState::Disconnect);
|
||||
};
|
||||
|
||||
let data_len = evt.data.data_utf8.len();
|
||||
// [ ID ] [TYPE] [ LEN ] [ DATA ]
|
||||
// 0 to 7 8 9 to 15 . . . .
|
||||
let buf_length = data_len + 17;
|
||||
let mut buf = vec![0u8; buf_length];
|
||||
write_le_bytes(evt.id, &mut buf[0..8]);
|
||||
write_le_bytes(data_len as u64, &mut buf[9..17]);
|
||||
buf[8] = evt.data.event_type;
|
||||
|
||||
for i in 17..buf_length {
|
||||
buf[i] = evt.data.data_utf8[i - 17];
|
||||
}
|
||||
|
||||
info!(
|
||||
"SEND => ID: {} TYPE: {} LEN: {} <=",
|
||||
evt.id, evt.data.event_type, data_len
|
||||
);
|
||||
|
||||
let stream = &mut client.stream;
|
||||
stream.write(&mut buf).map_err(HError::TcpPeerError)?;
|
||||
stream.flush().map_err(HError::TcpPeerError)?;
|
||||
|
||||
let mut ack = vec![0];
|
||||
stream.read(&mut ack).map_err(HError::TcpPeerError)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(EventBusClientConnectionState::Disconnect)
|
||||
}
|
||||
|
||||
fn write_le_bytes(v64: u64, buf: &mut [u8]) {
|
||||
let le_bytes = v64.to_le_bytes();
|
||||
buf[0] = le_bytes[0];
|
||||
buf[1] = le_bytes[1];
|
||||
buf[2] = le_bytes[2];
|
||||
buf[3] = le_bytes[3];
|
||||
buf[4] = le_bytes[4];
|
||||
buf[5] = le_bytes[5];
|
||||
buf[6] = le_bytes[6];
|
||||
buf[7] = le_bytes[7];
|
||||
}
|
||||
|
||||
impl std::fmt::Display for EventBusClientConnectionState {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let to_write = match self {
|
||||
EventBusClientConnectionState::Connect => "CONNECT",
|
||||
EventBusClientConnectionState::ReadPrelude => "READ_PRELUDE",
|
||||
EventBusClientConnectionState::ReadCursor => "READ_CURSOR",
|
||||
EventBusClientConnectionState::SendEvent => "SEND_EVENT",
|
||||
EventBusClientConnectionState::ReceiveAck => "RECEIVE_ACK",
|
||||
EventBusClientConnectionState::Disconnect => "DISCONNECT",
|
||||
};
|
||||
|
||||
write!(f, "{}", to_write)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user