Skip to main content

rns_net/common/
callbacks.rs

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