mirror of
https://github.com/stakwork/sphinx-key.git
synced 2025-12-18 15:54:31 +01:00
refactor broker metrics conn, single threaded virtual esp, hardcoded config, start auther module
This commit is contained in:
@@ -20,97 +20,116 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
.setting(AppSettings::NoAutoVersion)
|
||||
.about("CLN:mqtt-tester - MQTT client signer")
|
||||
.arg(Arg::from("--test run a test against the embedded device"));
|
||||
|
||||
let mut try_i = 0;
|
||||
let (client, mut eventloop) = loop {
|
||||
let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
|
||||
mqttoptions.set_credentials(USERNAME, PASSWORD);
|
||||
mqttoptions.set_keep_alive(Duration::from_secs(5));
|
||||
let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
|
||||
match eventloop.poll().await {
|
||||
Ok(event) => {
|
||||
if let Some(_) = incoming_conn_ack(event) {
|
||||
println!("==========> MQTT connected!");
|
||||
break (client, eventloop);
|
||||
let matches = app.get_matches();
|
||||
let is_test = matches.is_present("test");
|
||||
// main loop - alternate between "reconnection" and "handler"
|
||||
loop {
|
||||
let mut try_i = 0;
|
||||
let (client, mut eventloop) = loop {
|
||||
let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
|
||||
mqttoptions.set_credentials(USERNAME, PASSWORD);
|
||||
mqttoptions.set_keep_alive(Duration::from_secs(5));
|
||||
let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
|
||||
match eventloop.poll().await {
|
||||
Ok(event) => {
|
||||
if let Some(_) = incoming_conn_ack(event) {
|
||||
println!("==========> MQTT connected!");
|
||||
break (client, eventloop);
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
try_i = try_i + 1;
|
||||
println!("reconnect.... {}", try_i);
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
try_i = try_i + 1;
|
||||
println!("reconnect.... {}", try_i);
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
client
|
||||
.subscribe(SUB_TOPIC, QoS::AtMostOnce)
|
||||
.await
|
||||
.expect("could not mqtt subscribe");
|
||||
|
||||
// client
|
||||
// .publish(
|
||||
// PUB_TOPIC,
|
||||
// QoS::AtMostOnce,
|
||||
// false,
|
||||
// "READY".as_bytes().to_vec(),
|
||||
// )
|
||||
// .await
|
||||
// .expect("could not pub");
|
||||
|
||||
let matches = app.get_matches();
|
||||
if matches.is_present("test") {
|
||||
loop {
|
||||
let event = eventloop.poll().await.expect("failed to unwrap event");
|
||||
// println!("{:?}", event);
|
||||
if let Some(ping_bytes) = incoming_bytes(event) {
|
||||
let (ping, sequence, dbid): (msgs::Ping, u16, u64) =
|
||||
parser::request_from_bytes(ping_bytes).expect("read ping header");
|
||||
println!("sequence {}", sequence);
|
||||
println!("dbid {}", dbid);
|
||||
println!("INCOMING: {:?}", ping);
|
||||
let pong = msgs::Pong {
|
||||
id: ping.id,
|
||||
message: ping.message,
|
||||
};
|
||||
let bytes = parser::raw_response_from_msg(pong, sequence)
|
||||
.expect("couldnt parse raw response");
|
||||
client
|
||||
.publish(PUB_TOPIC, QoS::AtMostOnce, false, bytes)
|
||||
.await
|
||||
.expect("could not mqtt publish");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// once the init loop is done, the root_handler is returned
|
||||
let root_handler = loop {
|
||||
let init_event = eventloop.poll().await.expect("failed to unwrap event");
|
||||
// this may be another kind of message like MQTT ConnAck
|
||||
// loop around again and wait for the init
|
||||
if let Some(init_msg_bytes) = incoming_bytes(init_event) {
|
||||
let InitResponse {
|
||||
root_handler,
|
||||
init_reply,
|
||||
} = sphinx_key_signer::init(init_msg_bytes).expect("failed to init signer");
|
||||
client
|
||||
.publish(PUB_TOPIC, QoS::AtMostOnce, false, init_reply)
|
||||
.await
|
||||
.expect("could not publish init response");
|
||||
// return the root_handler and finish the init loop
|
||||
break root_handler;
|
||||
}
|
||||
};
|
||||
// the actual loop
|
||||
loop {
|
||||
let event = eventloop.poll().await.expect("failed to unwrap event");
|
||||
let dummy_peer = PubKey([0; 33]);
|
||||
if let Some(msg_bytes) = incoming_bytes(event) {
|
||||
match sphinx_key_signer::handle(&root_handler, msg_bytes, dummy_peer.clone()) {
|
||||
Ok(b) => client
|
||||
.publish(PUB_TOPIC, QoS::AtMostOnce, false, b)
|
||||
.await
|
||||
.expect("could not publish init response"),
|
||||
Err(e) => panic!("HANDLE FAILED {:?}", e),
|
||||
};
|
||||
|
||||
client
|
||||
.subscribe(SUB_TOPIC, QoS::AtMostOnce)
|
||||
.await
|
||||
.expect("could not mqtt subscribe");
|
||||
|
||||
if is_test {
|
||||
// test handler loop
|
||||
loop {
|
||||
match eventloop.poll().await {
|
||||
Ok(event) => {
|
||||
// println!("{:?}", event);
|
||||
if let Some(ping_bytes) = incoming_bytes(event) {
|
||||
let (ping, sequence, dbid): (msgs::Ping, u16, u64) =
|
||||
parser::request_from_bytes(ping_bytes).expect("read ping header");
|
||||
println!("sequence {}", sequence);
|
||||
println!("dbid {}", dbid);
|
||||
println!("INCOMING: {:?}", ping);
|
||||
let pong = msgs::Pong {
|
||||
id: ping.id,
|
||||
message: ping.message,
|
||||
};
|
||||
let bytes = parser::raw_response_from_msg(pong, sequence)
|
||||
.expect("couldnt parse raw response");
|
||||
client
|
||||
.publish(PUB_TOPIC, QoS::AtMostOnce, false, bytes)
|
||||
.await
|
||||
.expect("could not mqtt publish");
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("diconnected {:?}", e);
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
break; // break out of this loop to reconnect
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// once the init loop is done, the root_handler is returned
|
||||
let root_handler = loop {
|
||||
if let Ok(init_event) = eventloop.poll().await {
|
||||
// this may be another kind of message like MQTT ConnAck
|
||||
// loop around again and wait for the init
|
||||
if let Some(init_msg_bytes) = incoming_bytes(init_event) {
|
||||
let InitResponse {
|
||||
root_handler,
|
||||
init_reply,
|
||||
} = sphinx_key_signer::init(init_msg_bytes).expect("failed to init signer");
|
||||
client
|
||||
.publish(PUB_TOPIC, QoS::AtMostOnce, false, init_reply)
|
||||
.await
|
||||
.expect("could not publish init response");
|
||||
// return the root_handler and finish the init loop
|
||||
break Some(root_handler);
|
||||
}
|
||||
} else {
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
log::warn!("failed to initialize! Lost connection");
|
||||
break None;
|
||||
}
|
||||
};
|
||||
// the actual handler loop
|
||||
loop {
|
||||
if let Some(rh) = &root_handler {
|
||||
match eventloop.poll().await {
|
||||
Ok(event) => {
|
||||
let dummy_peer = PubKey([0; 33]);
|
||||
if let Some(msg_bytes) = incoming_bytes(event) {
|
||||
match sphinx_key_signer::handle(rh, msg_bytes, dummy_peer.clone()) {
|
||||
Ok(b) => client
|
||||
.publish(PUB_TOPIC, QoS::AtMostOnce, false, b)
|
||||
.await
|
||||
.expect("could not publish init response"),
|
||||
Err(e) => panic!("HANDLE FAILED {:?}", e),
|
||||
};
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("diconnected {:?}", e);
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
break; // break out of this loop to reconnect
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user