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