|
@@ -1,8 +1,7 @@
|
|
|
-use crate::Error;
|
|
|
+use crate::{relayers::Event, Error};
|
|
|
use futures::Future;
|
|
|
use futures_util::{SinkExt, StreamExt};
|
|
|
use nostr_rs_types::{Request, Response};
|
|
|
-use parking_lot::RwLock;
|
|
|
use std::{
|
|
|
pin::Pin,
|
|
|
sync::{
|
|
@@ -34,14 +33,13 @@ const NO_ACTIVITY_TIMEOUT_SECS: u64 = 120;
|
|
|
impl Relayer {
|
|
|
/// Creates a new relayer
|
|
|
pub fn new<F>(
|
|
|
- broadcast_to_listeners: mpsc::Sender<(Response, String)>,
|
|
|
- sent_messages: Arc<RwLock<Vec<Request>>>,
|
|
|
+ broadcast_to_listeners: mpsc::Sender<(Event, String)>,
|
|
|
max_connections_attempts: u16,
|
|
|
url: &str,
|
|
|
on_connection: Option<F>,
|
|
|
) -> Result<Self, Error>
|
|
|
where
|
|
|
- F: (Fn(Vec<Request>, mpsc::Sender<Request>) -> Pin<Box<dyn Future<Output = ()> + Send>>)
|
|
|
+ F: (Fn(&str, mpsc::Sender<Request>) -> Pin<Box<dyn Future<Output = ()> + Send>>)
|
|
|
+ Send
|
|
|
+ Sync
|
|
|
+ 'static,
|
|
@@ -50,7 +48,6 @@ impl Relayer {
|
|
|
let is_connected = Arc::new(AtomicBool::new(false));
|
|
|
let stop_service = Self::spawn_background_client(
|
|
|
broadcast_to_listeners,
|
|
|
- sent_messages,
|
|
|
send_to_socket.clone(),
|
|
|
receiver,
|
|
|
url,
|
|
@@ -68,8 +65,7 @@ impl Relayer {
|
|
|
}
|
|
|
|
|
|
fn spawn_background_client<F>(
|
|
|
- broadcast_to_listeners: mpsc::Sender<(Response, String)>,
|
|
|
- sent_messages: Arc<RwLock<Vec<Request>>>,
|
|
|
+ broadcast_to_listeners: mpsc::Sender<(Event, String)>,
|
|
|
send_to_socket: mpsc::Sender<Request>,
|
|
|
mut receiver: mpsc::Receiver<Request>,
|
|
|
url_str: &str,
|
|
@@ -78,7 +74,7 @@ impl Relayer {
|
|
|
on_connection: Option<F>,
|
|
|
) -> Result<oneshot::Sender<()>, Error>
|
|
|
where
|
|
|
- F: (Fn(Vec<Request>, mpsc::Sender<Request>) -> Pin<Box<dyn Future<Output = ()> + Send>>)
|
|
|
+ F: (Fn(&str, mpsc::Sender<Request>) -> Pin<Box<dyn Future<Output = ()> + Send>>)
|
|
|
+ Send
|
|
|
+ Sync
|
|
|
+ 'static,
|
|
@@ -107,8 +103,7 @@ impl Relayer {
|
|
|
log::info!("Connected to {}", url);
|
|
|
|
|
|
if let Some(on_connection) = &on_connection {
|
|
|
- let sent_messages = sent_messages.read().to_owned();
|
|
|
- on_connection(sent_messages, send_to_socket.clone()).await;
|
|
|
+ on_connection(&url, send_to_socket.clone()).await;
|
|
|
}
|
|
|
|
|
|
loop {
|
|
@@ -158,7 +153,7 @@ impl Relayer {
|
|
|
let msg: Result<Response, _> = serde_json::from_str(&msg);
|
|
|
|
|
|
if let Ok(msg) = msg {
|
|
|
- if let Err(error) = broadcast_to_listeners.try_send((msg, url.to_owned())) {
|
|
|
+ if let Err(error) = broadcast_to_listeners.try_send((Event::Response(msg), url.to_owned())) {
|
|
|
log::error!("{}: Reconnecting client because of {}", url, error);
|
|
|
break;
|
|
|
}
|
|
@@ -172,11 +167,12 @@ impl Relayer {
|
|
|
}
|
|
|
|
|
|
is_connected.store(false, Relaxed);
|
|
|
-
|
|
|
// Throttle down to not spam the server with reconnections
|
|
|
sleep(Duration::from_millis(500)).await;
|
|
|
}
|
|
|
|
|
|
+ let _ = broadcast_to_listeners.try_send((Event::Disconnected, "".to_owned()));
|
|
|
+
|
|
|
log::warn!("{}: Disconnected", url);
|
|
|
});
|
|
|
|