From db5eeb3bbd7a42fca8b0ab35904ddaa977b5a2e6 Mon Sep 17 00:00:00 2001 From: davikstone Date: Thu, 17 Oct 2024 20:09:13 +0100 Subject: [PATCH] update event interface --- saito-core/src/core/consensus/peers/peer.rs | 10 +++++----- saito-core/src/core/io/network_event.rs | 5 +++-- saito-core/src/core/routing_thread.rs | 21 +++++++++------------ saito-e2e/src/rust.node.ts | 1 - saito-js/saito.ts | 1 + saito-wasm/src/saitowasm.rs | 9 ++++----- 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/saito-core/src/core/consensus/peers/peer.rs b/saito-core/src/core/consensus/peers/peer.rs index 625f5c50..12ef65d7 100644 --- a/saito-core/src/core/consensus/peers/peer.rs +++ b/saito-core/src/core/consensus/peers/peer.rs @@ -21,8 +21,8 @@ use tokio::sync::RwLock; #[derive(Clone, Debug)] pub enum PeerType { - DEFAULT, - STUN, + Default, + Stun, } #[derive(Clone, Debug)] @@ -83,7 +83,7 @@ impl Peer { Duration::from_secs(600), ), public_key: None, - peer_type: PeerType::DEFAULT, + peer_type: PeerType::Default, } } @@ -93,7 +93,7 @@ impl Peer { io_handler: &(dyn InterfaceIO + Send + Sync), ) -> Peer { let mut peer = Peer::new(peer_index); - peer.peer_type = PeerType::STUN; + peer.peer_type = PeerType::Stun; peer.public_key = Some(public_key); peer.peer_status = PeerStatus::Connected; peer.services = io_handler.get_my_services(); @@ -101,7 +101,7 @@ impl Peer { } pub fn is_stun_peer(&self) -> bool { - matches!(self.peer_type, PeerType::STUN) + matches!(self.peer_type, PeerType::Stun) } pub fn has_key_list_limit_exceeded(&mut self, current_time: Timestamp) -> bool { diff --git a/saito-core/src/core/io/network_event.rs b/saito-core/src/core/io/network_event.rs index 7d92ec34..e3ebd0a2 100644 --- a/saito-core/src/core/io/network_event.rs +++ b/saito-core/src/core/io/network_event.rs @@ -26,10 +26,11 @@ pub enum NetworkEvent { result: Result, }, AddStunPeer { - result: Result<(u64, SaitoPublicKey), std::io::Error>, + peer_index: u64, + public_key: SaitoPublicKey, }, RemoveStunPeer { - result: Result, + peer_index: u64, }, PeerDisconnected { peer_index: u64, diff --git a/saito-core/src/core/routing_thread.rs b/saito-core/src/core/routing_thread.rs index da225e2c..683f181c 100644 --- a/saito-core/src/core/routing_thread.rs +++ b/saito-core/src/core/routing_thread.rs @@ -618,19 +618,16 @@ impl ProcessEvent for RoutingThread { } } - NetworkEvent::AddStunPeer { result } => { - if result.is_ok() { - let (peer_index, public_key) = result.unwrap(); - self.handle_new_stun_peer(peer_index, public_key).await; - return Some(()); - } + NetworkEvent::AddStunPeer { + peer_index, + public_key, + } => { + self.handle_new_stun_peer(peer_index, public_key).await; + return Some(()); } - NetworkEvent::RemoveStunPeer { result } => { - if result.is_ok() { - let peer_index = result.unwrap(); - self.remove_stun_peer(peer_index).await; - return Some(()); - } + NetworkEvent::RemoveStunPeer { peer_index } => { + self.remove_stun_peer(peer_index).await; + return Some(()); } NetworkEvent::PeerDisconnected { peer_index, diff --git a/saito-e2e/src/rust.node.ts b/saito-e2e/src/rust.node.ts index 2958cc87..1aa79723 100644 --- a/saito-e2e/src/rust.node.ts +++ b/saito-e2e/src/rust.node.ts @@ -1,4 +1,3 @@ -import { type Page, type Locator } from "@playwright/test"; import SaitoNode from "./saito_node"; export default class RustNode extends SaitoNode { diff --git a/saito-js/saito.ts b/saito-js/saito.ts index 33dc2237..cd29799e 100644 --- a/saito-js/saito.ts +++ b/saito-js/saito.ts @@ -217,6 +217,7 @@ export default class Saito { console.log("adding socket : " + peer_index + ". total sockets : " + this.sockets.size); } + public async addStunPeer(publicKey: string, peerConnection: RTCPeerConnection) { await this.stunManager.addStunPeer(publicKey, peerConnection); diff --git a/saito-wasm/src/saitowasm.rs b/saito-wasm/src/saitowasm.rs index 8254c766..8cce24b6 100644 --- a/saito-wasm/src/saitowasm.rs +++ b/saito-wasm/src/saitowasm.rs @@ -508,7 +508,7 @@ pub async fn process_stun_peer(peer_index: PeerIndex, public_key: JsString) -> R peer_index, public_key ); let mut saito = SAITO.lock().await; - let key: [u8; 33] = string_to_key(public_key.into()) + let key: SaitoPublicKey = string_to_key(public_key.into()) .map_err(|e| JsValue::from_str(&format!("Failed to parse public key: {}", e)))?; saito @@ -516,7 +516,8 @@ pub async fn process_stun_peer(peer_index: PeerIndex, public_key: JsString) -> R .unwrap() .routing_thread .process_network_event(NetworkEvent::AddStunPeer { - result: Ok((peer_index, key)), + peer_index, + public_key: key, }) .await; Ok(()) @@ -533,9 +534,7 @@ pub async fn remove_stun_peer(peer_index: PeerIndex) { .as_mut() .unwrap() .routing_thread - .process_network_event(NetworkEvent::RemoveStunPeer { - result: Ok(peer_index), - }) + .process_network_event(NetworkEvent::RemoveStunPeer { peer_index }) .await; }