|
@@ -1,4 +1,4 @@
|
|
-use crate::{Connection, Error, Subscription};
|
|
|
|
|
|
+use crate::{connection::ConnectionId, Connection, Error, Subscription};
|
|
use futures_util::StreamExt;
|
|
use futures_util::StreamExt;
|
|
use nostr_rs_client::{Error as ClientError, Pool};
|
|
use nostr_rs_client::{Error as ClientError, Pool};
|
|
use nostr_rs_storage_base::Storage;
|
|
use nostr_rs_storage_base::Storage;
|
|
@@ -20,8 +20,7 @@ use tokio::{
|
|
task::JoinHandle,
|
|
task::JoinHandle,
|
|
};
|
|
};
|
|
|
|
|
|
-type SubId = u128;
|
|
|
|
-
|
|
|
|
|
|
+type SubId = ConnectionId;
|
|
type Subscriptions = HashMap<SubId, (SubscriptionId, Sender<Response>)>;
|
|
type Subscriptions = HashMap<SubId, (SubscriptionId, Sender<Response>)>;
|
|
|
|
|
|
/// Relayer struct
|
|
/// Relayer struct
|
|
@@ -45,11 +44,11 @@ pub struct Relayer<T: Storage + Send + Sync + 'static> {
|
|
/// when it is translated in OR filters. It is designed this way to allow a
|
|
/// when it is translated in OR filters. It is designed this way to allow a
|
|
/// fast iteration and match quickly filters.
|
|
/// fast iteration and match quickly filters.
|
|
subscriptions: RwLock<HashMap<Subscription, RwLock<Subscriptions>>>,
|
|
subscriptions: RwLock<HashMap<Subscription, RwLock<Subscriptions>>>,
|
|
- clients: RwLock<HashMap<u128, Connection>>,
|
|
|
|
|
|
+ clients: RwLock<HashMap<ConnectionId, Connection>>,
|
|
/// This Sender can be used to send requests from anywhere to the relayer.
|
|
/// This Sender can be used to send requests from anywhere to the relayer.
|
|
- send_to_relayer: Sender<(u128, Request)>,
|
|
|
|
|
|
+ send_to_relayer: Sender<(ConnectionId, Request)>,
|
|
/// This Receiver is the relayer the way the relayer receives messages
|
|
/// This Receiver is the relayer the way the relayer receives messages
|
|
- relayer_receiver: Option<Receiver<(u128, Request)>>,
|
|
|
|
|
|
+ relayer_receiver: Option<Receiver<(ConnectionId, Request)>>,
|
|
/// Client pool
|
|
/// Client pool
|
|
///
|
|
///
|
|
/// A relayer can optionally be connected to a pool of clients to get foreign events.
|
|
/// A relayer can optionally be connected to a pool of clients to get foreign events.
|
|
@@ -91,7 +90,7 @@ impl<T: Storage + Send + Sync + 'static> Relayer<T> {
|
|
}
|
|
}
|
|
|
|
|
|
/// Splits the relayer object and extract their receiver.
|
|
/// Splits the relayer object and extract their receiver.
|
|
- pub fn split(mut self) -> Result<(Self, Receiver<(u128, Request)>), Error> {
|
|
|
|
|
|
+ pub fn split(mut self) -> Result<(Self, Receiver<(ConnectionId, Request)>), Error> {
|
|
let receiver = self.relayer_receiver.take().ok_or(Error::AlreadySplitted)?;
|
|
let receiver = self.relayer_receiver.take().ok_or(Error::AlreadySplitted)?;
|
|
Ok((self, receiver))
|
|
Ok((self, receiver))
|
|
}
|
|
}
|
|
@@ -111,7 +110,7 @@ impl<T: Storage + Send + Sync + 'static> Relayer<T> {
|
|
},
|
|
},
|
|
Some((conn_id, request)) = receiver.recv() => {
|
|
Some((conn_id, request)) = receiver.recv() => {
|
|
// receive messages from the connection pool
|
|
// receive messages from the connection pool
|
|
- if conn_id == 0 {
|
|
|
|
|
|
+ if conn_id.is_empty() {
|
|
// connection pool
|
|
// connection pool
|
|
if let Request::Event(event) = request {
|
|
if let Request::Event(event) = request {
|
|
if let Some(storage) = this.storage.as_ref() {
|
|
if let Some(storage) = this.storage.as_ref() {
|
|
@@ -142,7 +141,7 @@ impl<T: Storage + Send + Sync + 'static> Relayer<T> {
|
|
|
|
|
|
fn handle_client_pool(
|
|
fn handle_client_pool(
|
|
client_pool: Pool,
|
|
client_pool: Pool,
|
|
- sender: Sender<(u128, Request)>,
|
|
|
|
|
|
+ sender: Sender<(ConnectionId, Request)>,
|
|
) -> Result<(Pool, JoinHandle<()>), ClientError> {
|
|
) -> Result<(Pool, JoinHandle<()>), ClientError> {
|
|
let (mut receiver, client_pool) = client_pool.split()?;
|
|
let (mut receiver, client_pool) = client_pool.split()?;
|
|
|
|
|
|
@@ -151,7 +150,12 @@ impl<T: Storage + Send + Sync + 'static> Relayer<T> {
|
|
if let Some((response, _)) = receiver.recv().await {
|
|
if let Some((response, _)) = receiver.recv().await {
|
|
match response {
|
|
match response {
|
|
Response::Event(event) => {
|
|
Response::Event(event) => {
|
|
- let _ = sender.send((0, Request::Event(event.event.into()))).await;
|
|
|
|
|
|
+ let _ = sender
|
|
|
|
+ .send((
|
|
|
|
+ ConnectionId::new_empty(),
|
|
|
|
+ Request::Event(event.event.into()),
|
|
|
|
+ ))
|
|
|
|
+ .await;
|
|
}
|
|
}
|
|
x => {
|
|
x => {
|
|
println!("x => {:?}", x);
|
|
println!("x => {:?}", x);
|
|
@@ -174,9 +178,9 @@ impl<T: Storage + Send + Sync + 'static> Relayer<T> {
|
|
/// This function will spawn the client's loop to receive incoming messages and send those messages
|
|
/// This function will spawn the client's loop to receive incoming messages and send those messages
|
|
pub async fn add_connection(
|
|
pub async fn add_connection(
|
|
&self,
|
|
&self,
|
|
- disconnection_notify: Option<mpsc::Sender<u128>>,
|
|
|
|
|
|
+ disconnection_notify: Option<mpsc::Sender<ConnectionId>>,
|
|
stream: TcpStream,
|
|
stream: TcpStream,
|
|
- ) -> Result<u128, Error> {
|
|
|
|
|
|
+ ) -> Result<ConnectionId, Error> {
|
|
let client =
|
|
let client =
|
|
Connection::new_connection(self.send_to_relayer.clone(), disconnection_notify, stream)
|
|
Connection::new_connection(self.send_to_relayer.clone(), disconnection_notify, stream)
|
|
.await?;
|
|
.await?;
|