WIP
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
|||||||
|
/target/
|
||||||
|
**/target/
|
||||||
|
**/*.rs.bk
|
||||||
|
*.pdb
|
||||||
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
*.iml
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
|
.env
|
||||||
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hd-bus"
|
||||||
|
version = "0.1.0"
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
[workspace]
|
||||||
|
members = [
|
||||||
|
"crates/*"
|
||||||
|
]
|
||||||
|
resolver = "2"
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "hd-bus"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
use std::{
|
||||||
|
collections::VecDeque, sync::{RwLock, mpsc::{Receiver, Sender}},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub enum HError {
|
||||||
|
BusLockPoisoned(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Event {
|
||||||
|
id: u64,
|
||||||
|
topic_name: String,
|
||||||
|
data_utf8: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EventBus {
|
||||||
|
queue: RwLock<VecDeque<Event>>,
|
||||||
|
subscriptions: RwLock<Vec<Subscription>>,
|
||||||
|
processor: Sender<EventProcessRequest>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait SubscriptionEndpoint {
|
||||||
|
fn deliver(&self, evt: &Event);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ConsoleEndpoint;
|
||||||
|
impl SubscriptionEndpoint for ConsoleEndpoint {
|
||||||
|
fn deliver(&self, evt: &Event) {
|
||||||
|
let data = String::from_utf8_lossy(&evt.data_utf8);
|
||||||
|
println!("({}, {}, {})", evt.id, evt.topic_name, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Subscription {
|
||||||
|
topic_pattern: String,
|
||||||
|
/// from_id is inclusive
|
||||||
|
from_id: u64,
|
||||||
|
endpoint: Box<dyn SubscriptionEndpoint>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventBus {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
queue: RwLock::new(VecDeque::new()),
|
||||||
|
subscriptions: RwLock::new(Vec::new()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn publish(&self, evt: Event) -> Result<(), HError> {
|
||||||
|
let mut queue = self
|
||||||
|
.queue
|
||||||
|
.write()
|
||||||
|
.map_err(|e| HError::BusLockPoisoned(e.to_string()))?;
|
||||||
|
queue.push_back(evt);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn subscribe(&self, request: Subscription) -> Result<(), HError> {
|
||||||
|
let mut subscriptions = self
|
||||||
|
.subscriptions
|
||||||
|
.write()
|
||||||
|
.map_err(|e| HError::BusLockPoisoned(e.to_string()))?;
|
||||||
|
subscriptions.push(request);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct EventProcessRequest {
|
||||||
|
id: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct EventBusProcessor {
|
||||||
|
last_processed_id: u64,
|
||||||
|
work: Receiver<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn it_works() {}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user