FC7J5ASVKIVWYSFE3N5K2HNFZR2F43UZKSEP4QSNW6PTSSGKK2NQC
s.serve_forever(srv);
fn start_ws_server(server: Server) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let listener = TcpListener::bind((Ipv4Addr::UNSPECIFIED, 8888))
.await
.expect("Fail to bind to addr 0.0.0.0:8888");
let (mut conf, _) = server.watch();
let mut poll_timer = server.config().poll_interval.map(interval);
loop {
tokio::select! {
o = listener.accept() => {
match o {
Ok((stream, _)) => {
tokio::spawn(accept_connection(stream, server.clone()));
},
Err(err) => {
println!("[ERROR]: {err}");
return;
}
}
}
() = conf.changed() => {
let poll_interval = server.config().poll_interval;
if poll_timer.as_ref().map(Interval::period) != poll_interval {
poll_timer = poll_interval.map(interval);
}
}
Some(_) = {let t: OptionFuture<_> = poll_timer.as_mut().map(Interval::tick).into(); t} => {
server.send_snapshot()
}
}
}
})
}
fn start_modus_server(server: Server) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let s = tokio_modbus::server::tcp::Server::new(([127, 0, 0, 1], 8000).into());
s.serve(server).await.unwrap();
})
}
#[derive(Debug, Deserialize)]
enum MessageIn {
SetPoll(Option<std::time::Duration>),
SetRegisterConf {
reg_id: u16,
conf: Option<RegisterType>,
},
}
#[derive(Debug, Serialize)]
enum MessageOut {
Configuration(ServerConfig),
History(SystemTime, server::history::Entry),
HistoryLagged(u64),
}
#[derive(Debug, Error)]
enum ClientError {
#[error("io error {0}")]
Io(#[from] std::io::Error),
#[error("websocket error {0}")]
Ws(#[from] async_tungstenite::tungstenite::Error),
#[error("ser/deserialization error {0}")]
Serde(#[from] serde_json::Error),
}
async fn accept_connection(stream: TcpStream, server: Server) -> Result<(), ClientError> {
use async_tungstenite::tungstenite::protocol::Message;
let (mut conf, mut hist) = server.watch();
let mut ws_stream = async_tungstenite::tokio::accept_async(stream).await?;
ws_stream
.send(Message::Text(serde_json::to_string(
&MessageOut::Configuration(server.config()),
)?))
.await?;
loop {
let msg = tokio::select! {
Some(msg) = ws_stream.next() => {
let m = msg?;
if m.is_close() {
return Ok(())
} else if let Ok(m) = m.to_text() {
let i = serde_json::from_str::<MessageIn>(m).unwrap();// TODO dont unwrap
match i {
MessageIn::SetPoll(d) => server.set_poll_interval(d),
MessageIn::SetRegisterConf { reg_id, conf } => server.set_register_conf(reg_id, conf),
}
}
None
}
h = hist.recv() => {
use tokio::sync::broadcast::error::RecvError;
Some(match h {
Ok((time, hist)) => MessageOut::History(time, hist),
Err(RecvError::Lagged(lag)) => MessageOut::HistoryLagged(lag),
// Can't be closed, we still have a handle on the server (that has the sender)
Err(RecvError::Closed) => unreachable!(),
})
}
() = conf.changed() => {
Some(MessageOut::Configuration(server.config()))
}
};
if let Some(msg) = msg {
ws_stream
.send(Message::Text(serde_json::to_string(&msg)?))
.await?;
}
}
]
[[package]]
name = "async-tungstenite"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5682ea0913e5c20780fe5785abacb85a411e7437bf52a1bedb93ddb3972cb8dd"
dependencies = [
"futures-io",
"futures-util",
"log",
"pin-project-lite",
"tokio",
"tungstenite",
name = "cpufeatures"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469"
dependencies = [
"libc",
]
[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
dependencies = [
"generic-array",
]
[[package]]
"wasi",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418d37c8b1d42553c93648be529cb70f920d3baf8ef469b74b9638df426e0b4c"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.10.2+wasi-snapshot-preview1",
"getrandom",
"getrandom 0.1.16",
"libc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc 0.2.0",
]
[[package]]
name = "rand"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.3",
"getrandom",
"getrandom 0.1.16",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom 0.2.4",
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core 0.6.3",
name = "tungstenite"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ad3713a14ae247f22a728a0456a545df14acf3867f905adff84be99e23b3ad1"
dependencies = [
"base64",
"byteorder",
"bytes",
"http",
"httparse",
"log",
"rand 0.8.4",
"sha-1",
"thiserror",
"url",
"utf-8",
]
[[package]]
name = "typenum"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]]
name = "unicode-bidi"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a01404663e3db436ed2746d9fefef640d868edae3cceb81c3b8d5732fda678f"
[[package]]
name = "unicode-normalization"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9"
dependencies = [
"tinyvec",
]
[[package]]
[[package]]
name = "url"
version = "2.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c"
dependencies = [
"form_urlencoded",
"idna",
"matches",
"percent-encoding",
]
[[package]]
name = "utf-8"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"