2024-08-23 22:56:27 +01:00
|
|
|
use log::{debug, info, warn};
|
2024-08-23 23:23:57 +01:00
|
|
|
use scx_stats::{Meta, ScxStatsServer, ScxStatsServerData, ToJson};
|
2024-08-15 16:31:34 +01:00
|
|
|
use scx_stats_derive::Stats;
|
2024-08-14 18:52:39 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::env::args;
|
|
|
|
use std::io::Read;
|
2024-08-17 05:15:19 +01:00
|
|
|
use std::thread::{current, spawn, ThreadId};
|
2024-08-14 18:52:39 +01:00
|
|
|
|
2024-08-16 08:16:53 +01:00
|
|
|
// Hacky definition sharing. See stats_def.rs.h.
|
|
|
|
include!("stats_defs.rs.h");
|
2024-08-14 18:52:39 +01:00
|
|
|
|
|
|
|
fn main() {
|
2024-08-17 05:15:19 +01:00
|
|
|
simple_logger::SimpleLogger::new()
|
|
|
|
.with_level(log::LevelFilter::Info)
|
|
|
|
.env()
|
|
|
|
.init()
|
|
|
|
.unwrap();
|
|
|
|
|
2024-08-15 17:24:56 +01:00
|
|
|
let stats = ClusterStats {
|
2024-08-15 23:17:30 +01:00
|
|
|
name: "test cluster".into(),
|
|
|
|
at: 12345,
|
2024-08-17 05:15:19 +01:00
|
|
|
bitmap: vec![0xdeadbeef, 0xbeefdead],
|
2024-08-14 18:52:39 +01:00
|
|
|
doms_dict: BTreeMap::from([
|
|
|
|
(
|
|
|
|
0,
|
2024-08-15 16:31:34 +01:00
|
|
|
DomainStats {
|
2024-08-15 23:17:30 +01:00
|
|
|
name: "domain 0".into(),
|
2024-08-17 05:15:19 +01:00
|
|
|
events: 1234,
|
|
|
|
pressure: 1.234,
|
2024-08-14 18:52:39 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
3,
|
2024-08-15 16:31:34 +01:00
|
|
|
DomainStats {
|
2024-08-15 23:17:30 +01:00
|
|
|
name: "domain 3".into(),
|
2024-08-17 05:15:19 +01:00
|
|
|
events: 5678,
|
|
|
|
pressure: 5.678,
|
2024-08-14 18:52:39 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
};
|
|
|
|
|
|
|
|
std::assert_eq!(args().len(), 2, "Usage: server UNIX_SOCKET_PATH");
|
|
|
|
let path = args().nth(1).unwrap();
|
|
|
|
|
2024-08-17 05:15:19 +01:00
|
|
|
// If communication from the stats generating closure is not necessary,
|
|
|
|
// ScxStatsServer::<(), ()> can be used. This example sends thread ID
|
|
|
|
// and receives the formatted string just for demonstration.
|
2024-08-23 23:23:57 +01:00
|
|
|
let sdata = ScxStatsServerData::<ThreadId, String>::new()
|
2024-08-23 19:10:20 +01:00
|
|
|
.add_meta(ClusterStats::meta())
|
|
|
|
.add_meta(DomainStats::meta())
|
2024-08-17 05:15:19 +01:00
|
|
|
.add_stats(
|
|
|
|
"top",
|
|
|
|
Box::new(move |_args, (tx, rx)| {
|
|
|
|
let id = current().id();
|
|
|
|
let res = tx.send(id);
|
|
|
|
debug!("Sendt {:?} {:?}", id, &res);
|
|
|
|
let res = rx.recv();
|
|
|
|
debug!("Recevied {:?}", res);
|
|
|
|
stats.to_json()
|
|
|
|
}),
|
2024-08-23 23:23:57 +01:00
|
|
|
);
|
2024-08-14 18:52:39 +01:00
|
|
|
|
2024-08-23 22:56:27 +01:00
|
|
|
info!("stats_meta:");
|
2024-08-23 23:23:57 +01:00
|
|
|
sdata.describe_meta(&mut std::io::stderr(), None).unwrap();
|
|
|
|
|
|
|
|
let server = ScxStatsServer::<ThreadId, String>::new(sdata)
|
|
|
|
.set_path(&path)
|
|
|
|
.launch()
|
|
|
|
.unwrap();
|
2024-08-23 22:56:27 +01:00
|
|
|
|
2024-08-17 05:15:19 +01:00
|
|
|
debug!("Doing unnecessary server channel handling");
|
|
|
|
let (tx, rx) = server.channels();
|
|
|
|
spawn(move || {
|
|
|
|
while let Ok(id) = rx.recv() {
|
|
|
|
if let Err(e) = tx.send(format!("hello {:?}", &id)) {
|
|
|
|
warn!("Server channel errored ({:?})", &e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-08-23 22:56:27 +01:00
|
|
|
info!("Server listening. Run `client {:?}`.", &path);
|
|
|
|
info!("Use `socat - UNIX-CONNECT:{:?}` for raw connection.", &path);
|
|
|
|
info!("Press any key to exit.");
|
2024-08-14 18:52:39 +01:00
|
|
|
|
|
|
|
let mut buf: [u8; 1] = [0];
|
|
|
|
let _ = std::io::stdin().read(&mut buf);
|
|
|
|
}
|