|
@@ -7,6 +7,7 @@ use nostr_rs_types::{
|
|
|
};
|
|
|
use parking_lot::{RwLock, RwLockReadGuard};
|
|
|
use std::{collections::HashMap, ops::Deref, sync::Arc};
|
|
|
+use tokio::sync::mpsc;
|
|
|
#[allow(unused_imports)]
|
|
|
use tokio::{
|
|
|
net::TcpStream,
|
|
@@ -57,9 +58,12 @@ impl Relayer {
|
|
|
&self.storage
|
|
|
}
|
|
|
|
|
|
- #[cfg(not(test))]
|
|
|
- pub async fn add_connection(&self, stream: TcpStream) -> Result<(), Error> {
|
|
|
- let client = Connection::new(self.sender.clone(), stream).await?;
|
|
|
+ pub async fn add_connection(
|
|
|
+ &self,
|
|
|
+ disconnection_notify: Option<mpsc::Sender<u128>>,
|
|
|
+ stream: TcpStream,
|
|
|
+ ) -> Result<(), Error> {
|
|
|
+ let client = Connection::new(self.sender.clone(), disconnection_notify, stream).await?;
|
|
|
let mut clients = self.clients.write();
|
|
|
clients.insert(client.conn_id, client);
|
|
|
|
|
@@ -164,6 +168,15 @@ impl Relayer {
|
|
|
self.recv_request_from_client(connection, request)
|
|
|
}
|
|
|
|
|
|
+ pub fn send_to_conn(&self, conn_id: u128, response: Response) -> Result<(), Error> {
|
|
|
+ let connections = self.clients.read();
|
|
|
+ let connection = connections
|
|
|
+ .get(&conn_id)
|
|
|
+ .ok_or(Error::UnknownConnection(conn_id))?;
|
|
|
+
|
|
|
+ connection.send(response)
|
|
|
+ }
|
|
|
+
|
|
|
#[inline]
|
|
|
fn broadcast_to_subscribers(subscriptions: RwLockReadGuard<Subscriptions>, event: &Event) {
|
|
|
for (_, receiver) in subscriptions.iter() {
|
|
@@ -233,7 +246,7 @@ mod test {
|
|
|
async fn serve_listener_from_local_db() {
|
|
|
let request: Request = serde_json::from_str("[\"REQ\",\"1298169700973717\",{\"authors\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"since\":1681939304},{\"#p\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"kinds\":[1,3,6,7,9735],\"since\":1681939304},{\"#p\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"kinds\":[4]},{\"authors\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"kinds\":[4]},{\"#e\":[\"2e72250d80e9b3fd30230b3db3ed7d22f15d266ed345c36700b01ec153c9e28a\",\"a5e3369c43daf2675ecbce18831e5f4e07db0d4dde0ef4f5698e645e4c46eed1\"],\"kinds\":[1,6,7,9735]}]").expect("valid object");
|
|
|
let (relayer, _) = Relayer::new(get_db(true));
|
|
|
- let (connection, mut recv) = Connection::new();
|
|
|
+ let (connection, mut recv) = Connection::new_for_test();
|
|
|
let _ = relayer.recv_request_from_client(&connection, request);
|
|
|
// ev1
|
|
|
assert_eq!(
|
|
@@ -282,7 +295,7 @@ mod test {
|
|
|
async fn server_listener_real_time() {
|
|
|
let request: Request = serde_json::from_str("[\"REQ\",\"1298169700973717\",{\"authors\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"since\":1681939304},{\"#p\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"kinds\":[1,3,6,7,9735],\"since\":1681939304},{\"#p\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"kinds\":[4]},{\"authors\":[\"39a7d06e824c0c2523bedb93f0cef84245e4401fee03b6257a1c6dfd18b57efb\"],\"kinds\":[4]},{\"#e\":[\"2e72250d80e9b3fd30230b3db3ed7d22f15d266ed345c36700b01ec153c9e28a\",\"a5e3369c43daf2675ecbce18831e5f4e07db0d4dde0ef4f5698e645e4c46eed1\"],\"kinds\":[1,6,7,9735]}]").expect("valid object");
|
|
|
let (relayer, _) = Relayer::new(get_db(false));
|
|
|
- let (connection, mut recv) = Connection::new();
|
|
|
+ let (connection, mut recv) = Connection::new_for_test();
|
|
|
let _ = relayer.recv_request_from_client(&connection, request);
|
|
|
// eod
|
|
|
assert!(recv
|