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 to ask whether to accept an incoming resource (for AcceptApp strategy).
59    /// Return true to accept, false to reject.
60    fn on_resource_accept_query(
61        &mut self,
62        _link_id: LinkId,
63        _resource_hash: Vec<u8>,
64        _transfer_size: u64,
65        _has_metadata: bool,
66    ) -> bool {
67        false
68    }
69
70    /// Called when a channel message is received on a link.
71    fn on_channel_message(&mut self, _link_id: LinkId, _msgtype: u16, _payload: Vec<u8>) {}
72
73    /// Called when generic link data is received.
74    fn on_link_data(&mut self, _link_id: LinkId, _context: u8, _data: Vec<u8>) {}
75
76    /// Called when a response is received on a link.
77    fn on_response(&mut self, _link_id: LinkId, _request_id: [u8; 16], _data: Vec<u8>) {}
78
79    /// Called when a response is received on a link, including resource metadata when present.
80    fn on_response_with_metadata(
81        &mut self,
82        link_id: LinkId,
83        request_id: [u8; 16],
84        data: Vec<u8>,
85        _metadata: Option<Vec<u8>>,
86    ) {
87        self.on_response(link_id, request_id, data);
88    }
89
90    /// Called when a delivery proof is received for a packet we sent.
91    /// `rtt` is the round-trip time in seconds.
92    fn on_proof(&mut self, _dest_hash: DestHash, _packet_hash: PacketHash, _rtt: f64) {}
93
94    /// Called for ProveApp strategy: should we prove this incoming packet?
95    /// Return true to generate and send a proof, false to skip.
96    fn on_proof_requested(&mut self, _dest_hash: DestHash, _packet_hash: PacketHash) -> bool {
97        true
98    }
99
100    /// Called when a direct connection is proposed by a peer (for AskApp policy).
101    /// Return true to accept, false to reject.
102    fn on_direct_connect_proposed(
103        &mut self,
104        _link_id: LinkId,
105        _peer_identity: Option<IdentityHash>,
106    ) -> bool {
107        false
108    }
109
110    /// Called when a direct P2P connection is established via hole punching.
111    fn on_direct_connect_established(&mut self, _link_id: LinkId, _interface_id: InterfaceId) {}
112
113    /// Called when a direct connection attempt fails.
114    fn on_direct_connect_failed(&mut self, _link_id: LinkId, _reason: u8) {}
115}