From 099fd3fe11d4bef3d634bb3f2fbbeadb680fac8c Mon Sep 17 00:00:00 2001 From: irriden Date: Tue, 6 Jun 2023 03:10:33 +0000 Subject: [PATCH] solve reconnections --- sphinx-key/Cargo.lock | 1 + sphinx-key/Cargo.toml | 1 + sphinx-key/src/main.rs | 16 ++++++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/sphinx-key/Cargo.lock b/sphinx-key/Cargo.lock index 2830a7c..3282e7c 100644 --- a/sphinx-key/Cargo.lock +++ b/sphinx-key/Cargo.lock @@ -2013,6 +2013,7 @@ dependencies = [ "hex", "log", "lss-connector", + "rand", "rmp-serde", "serde", "serde_json", diff --git a/sphinx-key/Cargo.toml b/sphinx-key/Cargo.toml index e6e3347..035f993 100644 --- a/sphinx-key/Cargo.toml +++ b/sphinx-key/Cargo.toml @@ -37,6 +37,7 @@ serde = { version = "1.0.137", default-features = false } serde_json = { version = "1.0.81", default-features = false } hex = "0.4.3" rmp-serde = "1.1.0" +rand = "0.8.5" [build-dependencies] embuild = "0.29" diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index 4d18b42..9401b57 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -24,6 +24,9 @@ use esp_idf_svc::nvs::*; use sphinx_signer::lightning_signer::bitcoin::Network; use sphinx_signer::sphinx_glyph::control::{Config, ControlPersist, Policy}; +use rand::distributions::Alphanumeric; +use rand::Rng; + #[cfg(not(feature = "pingpong"))] const CLIENT_ID: &str = "sphinx-1"; @@ -151,7 +154,8 @@ fn make_and_launch_client( let token = ctrlr.make_auth_token().expect("couldnt make auth token"); log::info!("PUBKEY {} TOKEN {}", &pubkey, &token); - let mqtt_client = conn::mqtt::make_client(&config.broker, CLIENT_ID, &pubkey, &token, tx)?; + let client_id = random_word(8); + let mqtt_client = conn::mqtt::make_client(&config.broker, &client_id, &pubkey, &token, tx)?; // let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?; // this blocks forever... the "main thread" @@ -170,7 +174,15 @@ fn make_and_launch_client( seed, policy, ctrlr, - CLIENT_ID, + &client_id, )?; Ok(()) } + +pub fn random_word(n: usize) -> String { + rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(n) + .map(char::from) + .collect() +}