integrate SubsDebug into RelayPool
Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -13,6 +13,8 @@ use ewebsock::{WsEvent, WsMessage};
|
|||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
|
|
||||||
|
use super::subs_debug::SubsDebug;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PoolEvent<'a> {
|
pub struct PoolEvent<'a> {
|
||||||
pub relay: &'a str,
|
pub relay: &'a str,
|
||||||
@@ -58,6 +60,7 @@ impl PoolRelay {
|
|||||||
pub struct RelayPool {
|
pub struct RelayPool {
|
||||||
pub relays: Vec<PoolRelay>,
|
pub relays: Vec<PoolRelay>,
|
||||||
pub ping_rate: Duration,
|
pub ping_rate: Duration,
|
||||||
|
pub debug: Option<SubsDebug>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for RelayPool {
|
impl Default for RelayPool {
|
||||||
@@ -72,9 +75,14 @@ impl RelayPool {
|
|||||||
RelayPool {
|
RelayPool {
|
||||||
relays: vec![],
|
relays: vec![],
|
||||||
ping_rate: Duration::from_secs(25),
|
ping_rate: Duration::from_secs(25),
|
||||||
|
debug: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn use_debug(&mut self) {
|
||||||
|
self.debug = Some(SubsDebug::default());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ping_rate(&mut self, duration: Duration) -> &mut Self {
|
pub fn ping_rate(&mut self, duration: Duration) -> &mut Self {
|
||||||
self.ping_rate = duration;
|
self.ping_rate = duration;
|
||||||
self
|
self
|
||||||
@@ -99,18 +107,32 @@ impl RelayPool {
|
|||||||
|
|
||||||
pub fn send(&mut self, cmd: &ClientMessage) {
|
pub fn send(&mut self, cmd: &ClientMessage) {
|
||||||
for relay in &mut self.relays {
|
for relay in &mut self.relays {
|
||||||
|
if let Some(debug) = &mut self.debug {
|
||||||
|
debug.send_cmd(relay.relay.url.clone(), cmd);
|
||||||
|
}
|
||||||
relay.relay.send(cmd);
|
relay.relay.send(cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unsubscribe(&mut self, subid: String) {
|
pub fn unsubscribe(&mut self, subid: String) {
|
||||||
for relay in &mut self.relays {
|
for relay in &mut self.relays {
|
||||||
relay.relay.send(&ClientMessage::close(subid.clone()));
|
let cmd = &ClientMessage::close(subid.clone());
|
||||||
|
if let Some(debug) = &mut self.debug {
|
||||||
|
debug.send_cmd(relay.relay.url.clone(), cmd);
|
||||||
|
}
|
||||||
|
relay.relay.send(cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe(&mut self, subid: String, filter: Vec<Filter>) {
|
pub fn subscribe(&mut self, subid: String, filter: Vec<Filter>) {
|
||||||
for relay in &mut self.relays {
|
for relay in &mut self.relays {
|
||||||
|
if let Some(debug) = &mut self.debug {
|
||||||
|
debug.send_cmd(
|
||||||
|
relay.relay.url.clone(),
|
||||||
|
&ClientMessage::req(subid.clone(), filter.clone()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
relay.relay.subscribe(subid.clone(), filter.clone());
|
relay.relay.subscribe(subid.clone(), filter.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,6 +186,9 @@ impl RelayPool {
|
|||||||
for relay in &mut self.relays {
|
for relay in &mut self.relays {
|
||||||
let relay = &mut relay.relay;
|
let relay = &mut relay.relay;
|
||||||
if relay.url == relay_url {
|
if relay.url == relay_url {
|
||||||
|
if let Some(debug) = &mut self.debug {
|
||||||
|
debug.send_cmd(relay.url.clone(), cmd);
|
||||||
|
}
|
||||||
relay.send(cmd);
|
relay.send(cmd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -242,10 +267,17 @@ impl RelayPool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Some(PoolEvent {
|
|
||||||
|
if let Some(debug) = &mut self.debug {
|
||||||
|
debug.receive_cmd(relay.url.clone(), (&event).into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let pool_event = PoolEvent {
|
||||||
event,
|
event,
|
||||||
relay: &relay.url,
|
relay: &relay.url,
|
||||||
});
|
};
|
||||||
|
|
||||||
|
return Some(pool_event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user