only store cid in current

This commit is contained in:
irriden
2023-09-05 21:03:42 +00:00
parent d792db81e8
commit 9f5be6fa1a
2 changed files with 15 additions and 10 deletions

View File

@@ -8,7 +8,7 @@ use std::collections::HashMap;
pub struct Connections {
pub pubkey: Option<String>,
pub clients: HashMap<String, SignerType>,
pub current: Option<(String, SignerType)>,
pub current: Option<String>,
}
impl Connections {
@@ -25,16 +25,16 @@ impl Connections {
pub fn set_pubkey(&mut self, pk: &str) {
self.pubkey = Some(pk.to_string())
}
pub fn set_current(&mut self, cid: String, signer_type: SignerType) {
self.current = Some((cid, signer_type));
pub fn set_current(&mut self, cid: String) {
self.current = Some(cid);
}
pub fn add_client(&mut self, cid: &str, signer_type: SignerType) {
self.clients.insert(cid.to_string(), signer_type);
self.current = Some((cid.to_string(), signer_type));
self.current = Some(cid.to_string());
}
pub fn remove_client(&mut self, cid: &str) {
self.clients.remove(cid);
if let Some((id, _)) = &self.current {
if let Some(id) = &self.current {
if id == cid {
self.current = None;
}

View File

@@ -193,8 +193,13 @@ fn pub_and_wait(
// Try the current connection
// This returns None if 1) signer_type is set, and not equal to the current signer
// 2) If pub_timeout times out
let mut rep = if current.1 == msg.signer_type.unwrap_or(current.1) {
pub_timeout(&current.0, &msg.topic, &msg.message, &msg_rx, link_tx)
let mut rep = if client_list.get(&current).unwrap()
== msg
.signer_type
.as_ref()
.unwrap_or(client_list.get(&current).unwrap())
{
pub_timeout(&current, &msg.topic, &msg.message, &msg_rx, link_tx)
} else {
None
};
@@ -202,14 +207,14 @@ fn pub_and_wait(
// If that failed, try looking for some other signer
if rep.is_none() {
// If signer_type is set, we also filter for only these types
for (cid, signer_type) in client_list.into_iter().filter(|(k, v)| {
k != &current.0 && v == msg.signer_type.as_ref().unwrap_or(v)
for (cid, _) in client_list.into_iter().filter(|(k, v)| {
k != &current && v == msg.signer_type.as_ref().unwrap_or(v)
}) {
rep = pub_timeout(&cid, &msg.topic, &msg.message, &msg_rx, link_tx);
if rep.is_some() {
let mut cs = conns_.lock().unwrap();
log::debug!("got the list lock!");
cs.set_current(cid.to_string(), signer_type);
cs.set_current(cid.to_string());
drop(cs);
break;
}