Skip to content

Commit

Permalink
update event interface
Browse files Browse the repository at this point in the history
  • Loading branch information
davikstone2 committed Oct 17, 2024
1 parent b6216fc commit db5eeb3
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 25 deletions.
10 changes: 5 additions & 5 deletions saito-core/src/core/consensus/peers/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use tokio::sync::RwLock;

#[derive(Clone, Debug)]
pub enum PeerType {
DEFAULT,
STUN,
Default,
Stun,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -83,7 +83,7 @@ impl Peer {
Duration::from_secs(600),
),
public_key: None,
peer_type: PeerType::DEFAULT,
peer_type: PeerType::Default,
}
}

Expand All @@ -93,15 +93,15 @@ 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();
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 {
Expand Down
5 changes: 3 additions & 2 deletions saito-core/src/core/io/network_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ pub enum NetworkEvent {
result: Result<u64, std::io::Error>,
},
AddStunPeer {
result: Result<(u64, SaitoPublicKey), std::io::Error>,
peer_index: u64,
public_key: SaitoPublicKey,
},
RemoveStunPeer {
result: Result<u64, std::io::Error>,
peer_index: u64,
},
PeerDisconnected {
peer_index: u64,
Expand Down
21 changes: 9 additions & 12 deletions saito-core/src/core/routing_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,19 +618,16 @@ impl ProcessEvent<RoutingEvent> 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,
Expand Down
1 change: 0 additions & 1 deletion saito-e2e/src/rust.node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { type Page, type Locator } from "@playwright/test";
import SaitoNode from "./saito_node";

export default class RustNode extends SaitoNode {
Expand Down
1 change: 1 addition & 0 deletions saito-js/saito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 4 additions & 5 deletions saito-wasm/src/saitowasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,16 @@ 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
.as_mut()
.unwrap()
.routing_thread
.process_network_event(NetworkEvent::AddStunPeer {
result: Ok((peer_index, key)),
peer_index,
public_key: key,
})
.await;
Ok(())
Expand All @@ -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;
}

Expand Down

0 comments on commit db5eeb3

Please sign in to comment.