Skip to main content

rns_net/common/
callbacks.rs

1//! Application callback trait for driver events.
2
3use rns_core::transport::types::InterfaceId;
4
5pub trait Callbacks: Send {
6    fn on_announce(
7        &mut self,
8        announced: crate::common::destination::AnnouncedIdentity,
9    );
10
11    fn on_path_updated(&mut self, dest_hash: rns_core::types::DestHash, hops: u8);
12
13    fn on_local_delivery(&mut self, dest_hash: rns_core::types::DestHash, raw: Vec<u8>, packet_hash: rns_core::types::PacketHash);
14
15    /// Called when an interface comes online.
16    fn on_interface_up(&mut self, _id: InterfaceId) {}
17
18    /// Called when an interface goes offline.
19    fn on_interface_down(&mut self, _id: InterfaceId) {}
20
21    /// Called when a link is fully established.
22    fn on_link_established(&mut self, _link_id: rns_core::types::LinkId, _dest_hash: rns_core::types::DestHash, _rtt: f64, _is_initiator: bool) {}
23
24    /// Called when a link is closed.
25    fn on_link_closed(&mut self, _link_id: rns_core::types::LinkId, _reason: Option<rns_core::link::TeardownReason>) {}
26
27    /// Called when a remote peer identifies on a link.
28    fn on_remote_identified(&mut self, _link_id: rns_core::types::LinkId, _identity_hash: rns_core::types::IdentityHash, _public_key: [u8; 64]) {}
29
30    /// Called when a resource transfer delivers data.
31    fn on_resource_received(&mut self, _link_id: rns_core::types::LinkId, _data: Vec<u8>, _metadata: Option<Vec<u8>>) {}
32
33    /// Called when a resource transfer completes (sender-side proof validated).
34    fn on_resource_completed(&mut self, _link_id: rns_core::types::LinkId) {}
35
36    /// Called when a resource transfer fails.
37    fn on_resource_failed(&mut self, _link_id: rns_core::types::LinkId, _error: String) {}
38
39    /// Called with resource transfer progress updates.
40    fn on_resource_progress(&mut self, _link_id: rns_core::types::LinkId, _received: usize, _total: usize) {}
41
42    /// Called to ask whether to accept an incoming resource (for AcceptApp strategy).
43    /// Return true to accept, false to reject.
44    fn on_resource_accept_query(&mut self, _link_id: rns_core::types::LinkId, _resource_hash: Vec<u8>, _transfer_size: u64, _has_metadata: bool) -> bool {
45        false
46    }
47
48    /// Called when a channel message is received on a link.
49    fn on_channel_message(&mut self, _link_id: rns_core::types::LinkId, _msgtype: u16, _payload: Vec<u8>) {}
50
51    /// Called when generic link data is received.
52    fn on_link_data(&mut self, _link_id: rns_core::types::LinkId, _context: u8, _data: Vec<u8>) {}
53
54    /// Called when a response is received on a link.
55    fn on_response(&mut self, _link_id: rns_core::types::LinkId, _request_id: [u8; 16], _data: Vec<u8>) {}
56
57    /// Called when a delivery proof is received for a packet we sent.
58    /// `rtt` is the round-trip time in seconds.
59    fn on_proof(&mut self, _dest_hash: rns_core::types::DestHash, _packet_hash: rns_core::types::PacketHash, _rtt: f64) {}
60
61    /// Called for ProveApp strategy: should we prove this incoming packet?
62    /// Return true to generate and send a proof, false to skip.
63    fn on_proof_requested(&mut self, _dest_hash: rns_core::types::DestHash, _packet_hash: rns_core::types::PacketHash) -> bool {
64        true
65    }
66
67    /// Called when a direct connection is proposed by a peer (for AskApp policy).
68    /// Return true to accept, false to reject.
69    fn on_direct_connect_proposed(&mut self, _link_id: rns_core::types::LinkId, _peer_identity: Option<rns_core::types::IdentityHash>) -> bool {
70        false
71    }
72
73    /// Called when a direct P2P connection is established via hole punching.
74    fn on_direct_connect_established(&mut self, _link_id: rns_core::types::LinkId, _interface_id: InterfaceId) {}
75
76    /// Called when a direct connection attempt fails.
77    fn on_direct_connect_failed(&mut self, _link_id: rns_core::types::LinkId, _reason: u8) {}
78}