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(&mut self, announced: crate::common::destination::AnnouncedIdentity);
7
8    fn on_path_updated(&mut self, dest_hash: rns_core::types::DestHash, hops: u8);
9
10    fn on_local_delivery(
11        &mut self,
12        dest_hash: rns_core::types::DestHash,
13        raw: Vec<u8>,
14        packet_hash: rns_core::types::PacketHash,
15    );
16
17    /// Called when an interface comes online.
18    fn on_interface_up(&mut self, _id: InterfaceId) {}
19
20    /// Called when an interface goes offline.
21    fn on_interface_down(&mut self, _id: InterfaceId) {}
22
23    /// Called when a link is fully established.
24    fn on_link_established(
25        &mut self,
26        _link_id: rns_core::types::LinkId,
27        _dest_hash: rns_core::types::DestHash,
28        _rtt: f64,
29        _is_initiator: bool,
30    ) {
31    }
32
33    /// Called when a link is closed.
34    fn on_link_closed(
35        &mut self,
36        _link_id: rns_core::types::LinkId,
37        _reason: Option<rns_core::link::TeardownReason>,
38    ) {
39    }
40
41    /// Called when a remote peer identifies on a link.
42    fn on_remote_identified(
43        &mut self,
44        _link_id: rns_core::types::LinkId,
45        _identity_hash: rns_core::types::IdentityHash,
46        _public_key: [u8; 64],
47    ) {
48    }
49
50    /// Called when a resource transfer delivers data.
51    fn on_resource_received(
52        &mut self,
53        _link_id: rns_core::types::LinkId,
54        _data: Vec<u8>,
55        _metadata: Option<Vec<u8>>,
56    ) {
57    }
58
59    /// Called when a resource transfer completes (sender-side proof validated).
60    fn on_resource_completed(&mut self, _link_id: rns_core::types::LinkId) {}
61
62    /// Called when a resource transfer fails.
63    fn on_resource_failed(&mut self, _link_id: rns_core::types::LinkId, _error: String) {}
64
65    /// Called with resource transfer progress updates.
66    fn on_resource_progress(
67        &mut self,
68        _link_id: rns_core::types::LinkId,
69        _received: usize,
70        _total: usize,
71    ) {
72    }
73
74    /// Called to ask whether to accept an incoming resource (for AcceptApp strategy).
75    /// Return true to accept, false to reject.
76    fn on_resource_accept_query(
77        &mut self,
78        _link_id: rns_core::types::LinkId,
79        _resource_hash: Vec<u8>,
80        _transfer_size: u64,
81        _has_metadata: bool,
82    ) -> bool {
83        false
84    }
85
86    /// Called when a channel message is received on a link.
87    fn on_channel_message(
88        &mut self,
89        _link_id: rns_core::types::LinkId,
90        _msgtype: u16,
91        _payload: Vec<u8>,
92    ) {
93    }
94
95    /// Called when generic link data is received.
96    fn on_link_data(&mut self, _link_id: rns_core::types::LinkId, _context: u8, _data: Vec<u8>) {}
97
98    /// Called when a response is received on a link.
99    fn on_response(
100        &mut self,
101        _link_id: rns_core::types::LinkId,
102        _request_id: [u8; 16],
103        _data: Vec<u8>,
104    ) {
105    }
106
107    /// Called when a delivery proof is received for a packet we sent.
108    /// `rtt` is the round-trip time in seconds.
109    fn on_proof(
110        &mut self,
111        _dest_hash: rns_core::types::DestHash,
112        _packet_hash: rns_core::types::PacketHash,
113        _rtt: f64,
114    ) {
115    }
116
117    /// Called for ProveApp strategy: should we prove this incoming packet?
118    /// Return true to generate and send a proof, false to skip.
119    fn on_proof_requested(
120        &mut self,
121        _dest_hash: rns_core::types::DestHash,
122        _packet_hash: rns_core::types::PacketHash,
123    ) -> bool {
124        true
125    }
126
127    /// Called when a direct connection is proposed by a peer (for AskApp policy).
128    /// Return true to accept, false to reject.
129    fn on_direct_connect_proposed(
130        &mut self,
131        _link_id: rns_core::types::LinkId,
132        _peer_identity: Option<rns_core::types::IdentityHash>,
133    ) -> bool {
134        false
135    }
136
137    /// Called when a direct P2P connection is established via hole punching.
138    fn on_direct_connect_established(
139        &mut self,
140        _link_id: rns_core::types::LinkId,
141        _interface_id: InterfaceId,
142    ) {
143    }
144
145    /// Called when a direct connection attempt fails.
146    fn on_direct_connect_failed(&mut self, _link_id: rns_core::types::LinkId, _reason: u8) {}
147}