#![cfg(feature = "pubsub")]
use remdb::pubsub::topics::*;
use remdb::pubsub::{PubSub, PubSubConfig, UdpMode};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
fn main() {
println!("Starting PubSub Test Client...");
let config = PubSubConfig {
udp_mode: UdpMode::Broadcast,
multicast_addr: None,
port: 5555,
max_topics: 32,
max_subscribers_per_topic: 16,
buffer_size: 4096,
enable_nack: true,
retransmit_timeout: Duration::from_millis(100),
max_retransmits: 3,
heartbeat_interval: Duration::from_secs(5),
frame_pool_size: 128,
};
let mut pubsub = PubSub::new(config).expect("Failed to create PubSub instance");
pubsub.init().expect("Failed to initialize PubSub");
let heartbeat_callback = |_topic_id: u16, data: &[u8]| -> bool {
if data == b"heartbeat" {
println!("Received heartbeat from server");
}
true
};
pubsub
.subscribe(0, heartbeat_callback)
.expect("Failed to subscribe to heartbeat topic");
let wal_callback = |topic_id: u16, data: &[u8]| -> bool {
let msg = String::from_utf8_lossy(data);
let topic_name = match topic_id {
1 => "WAL_INSERT",
2 => "WAL_UPDATE",
3 => "WAL_DELETE",
4 => "WAL_TIMESERIES_INSERT",
5 => "WAL_COMMIT",
6 => "WAL_ABORT",
7 => "WAL_CHECKPOINT",
8 => "WAL_ALL",
_ => "UNKNOWN_WAL_TOPIC",
};
println!("Received {}: {}", topic_name, msg);
true
};
pubsub
.subscribe(1, wal_callback.clone())
.expect("Failed to subscribe to WAL_INSERT topic");
pubsub
.subscribe(2, wal_callback.clone())
.expect("Failed to subscribe to WAL_UPDATE topic");
pubsub
.subscribe(3, wal_callback.clone())
.expect("Failed to subscribe to WAL_DELETE topic");
pubsub
.subscribe(4, wal_callback.clone())
.expect("Failed to subscribe to WAL_TIMESERIES_INSERT topic");
pubsub
.subscribe(5, wal_callback.clone())
.expect("Failed to subscribe to WAL_COMMIT topic");
pubsub
.subscribe(6, wal_callback.clone())
.expect("Failed to subscribe to WAL_ABORT topic");
pubsub
.subscribe(7, wal_callback.clone())
.expect("Failed to subscribe to WAL_CHECKPOINT topic");
pubsub
.subscribe(8, wal_callback.clone())
.expect("Failed to subscribe to WAL_ALL topic");
let table_callback = |topic_id: u16, data: &[u8]| -> bool {
let msg = String::from_utf8_lossy(data);
println!("Received TABLE_CONTENT (ID: {}): {}", topic_id, msg);
true
};
pubsub
.subscribe(12, table_callback)
.expect("Failed to subscribe to table.test_table topic");
let tables_callback = |topic_id: u16, data: &[u8]| -> bool {
let msg = String::from_utf8_lossy(data);
println!("Received TABLES (ID: {}): {}", topic_id, msg);
true
};
pubsub
.subscribe(9, tables_callback)
.expect("Failed to subscribe to TABLES topic");
let metrics_callback = |topic_id: u16, data: &[u8]| -> bool {
let msg = String::from_utf8_lossy(data);
println!("Received METRICS (ID: {}): {}", topic_id, msg);
true
};
pubsub
.subscribe(10, metrics_callback)
.expect("Failed to subscribe to METRICS topic");
let health_callback = |topic_id: u16, data: &[u8]| -> bool {
let msg = String::from_utf8_lossy(data);
println!("Received HEALTH_STATUS (ID: {}): {}", topic_id, msg);
true
};
pubsub
.subscribe(11, health_callback)
.expect("Failed to subscribe to HEALTH_STATUS topic");
let actual_port = pubsub.get_actual_port().expect("Failed to get actual port");
let pubsub_clone = Arc::new(Mutex::new(pubsub));
let _receive_thread = thread::spawn(move || {
let mut pubsub = pubsub_clone.lock().unwrap();
pubsub.receive_loop();
});
println!("PubSub test client started successfully!");
println!("Listening for messages on UDP port {}", actual_port);
println!("Subscribed to topics:");
println!("- HEARTBEAT (ID: 0)");
println!("- WAL_INSERT (ID: 1)");
println!("- WAL_UPDATE (ID: 2)");
println!("- WAL_DELETE (ID: 3)");
println!("- WAL_TIMESERIES_INSERT (ID: 4)");
println!("- WAL_COMMIT (ID: 5)");
println!("- WAL_ABORT (ID: 6)");
println!("- WAL_CHECKPOINT (ID: 7)");
println!("- WAL_ALL (ID: 8)");
println!("- TABLES (ID: 9)");
println!("- METRICS (ID: 10)");
println!("- HEALTH_STATUS (ID: 11)");
println!("- table.test_table (ID: 12)");
println!("Client is running and receiving messages...");
thread::sleep(Duration::from_secs(60));
println!("PubSub test client stopped!");
}