ndn_strategy/strategy.rs
1use crate::context::StrategyContext;
2use ndn_packet::Name;
3use ndn_transport::ForwardingAction;
4
5/// The forwarding strategy trait.
6///
7/// A strategy is a pure decision function — it reads state through
8/// `StrategyContext` but cannot modify forwarding tables directly.
9/// Autonomous behaviour (probing, discovery) is done via the `Probe` channel
10/// on `StrategyContext`.
11pub trait Strategy: Send + Sync + 'static {
12 /// Canonical name identifying this strategy.
13 fn name(&self) -> &Name;
14
15 /// Synchronous fast path for forwarding decisions.
16 ///
17 /// Strategies whose `after_receive_interest` is fully synchronous should
18 /// override this to return `Some(actions)`, avoiding the `Box::pin` heap
19 /// allocation in the `ErasedStrategy` wrapper.
20 ///
21 /// Returns `None` (default) to fall through to the async path.
22 fn decide(&self, _ctx: &StrategyContext) -> Option<smallvec::SmallVec<[ForwardingAction; 2]>> {
23 None
24 }
25
26 /// Called when an Interest arrives and needs a forwarding decision.
27 fn after_receive_interest(
28 &self,
29 ctx: &StrategyContext,
30 ) -> impl std::future::Future<Output = smallvec::SmallVec<[ForwardingAction; 2]>> + Send;
31
32 /// Called when Data arrives and needs to be forwarded to consumers.
33 fn after_receive_data(
34 &self,
35 ctx: &StrategyContext,
36 ) -> impl std::future::Future<Output = smallvec::SmallVec<[ForwardingAction; 2]>> + Send;
37
38 /// Called when a PIT entry times out. Default: suppress (let the entry die).
39 fn on_interest_timeout(
40 &self,
41 _ctx: &StrategyContext,
42 ) -> impl std::future::Future<Output = ForwardingAction> + Send {
43 async { ForwardingAction::Suppress }
44 }
45
46 /// Called when a Nack arrives on an out-record face.
47 fn on_nack(
48 &self,
49 _ctx: &StrategyContext,
50 _reason: ndn_transport::NackReason,
51 ) -> impl std::future::Future<Output = ForwardingAction> + Send {
52 async { ForwardingAction::Suppress }
53 }
54}