libp2p/builder/phase/
bandwidth_logging.rs

1use super::*;
2use crate::bandwidth::BandwidthSinks;
3use crate::transport_ext::TransportExt;
4use crate::SwarmBuilder;
5use std::marker::PhantomData;
6use std::sync::Arc;
7
8pub struct BandwidthLoggingPhase<T, R> {
9    pub(crate) relay_behaviour: R,
10    pub(crate) transport: T,
11}
12
13impl<T: AuthenticatedMultiplexedTransport, Provider, R>
14    SwarmBuilder<Provider, BandwidthLoggingPhase<T, R>>
15{
16    pub fn with_bandwidth_logging(
17        self,
18    ) -> (
19        SwarmBuilder<Provider, BehaviourPhase<impl AuthenticatedMultiplexedTransport, R>>,
20        Arc<BandwidthSinks>,
21    ) {
22        let (transport, sinks) = self.phase.transport.with_bandwidth_logging();
23        (
24            SwarmBuilder {
25                phase: BehaviourPhase {
26                    relay_behaviour: self.phase.relay_behaviour,
27                    transport,
28                },
29                keypair: self.keypair,
30                phantom: PhantomData,
31            },
32            sinks,
33        )
34    }
35
36    pub fn without_bandwidth_logging(self) -> SwarmBuilder<Provider, BehaviourPhase<T, R>> {
37        SwarmBuilder {
38            phase: BehaviourPhase {
39                relay_behaviour: self.phase.relay_behaviour,
40                transport: self.phase.transport,
41            },
42            keypair: self.keypair,
43            phantom: PhantomData,
44        }
45    }
46}
47
48// Shortcuts
49#[cfg(feature = "relay")]
50impl<Provider, T: AuthenticatedMultiplexedTransport>
51    SwarmBuilder<Provider, BandwidthLoggingPhase<T, libp2p_relay::client::Behaviour>>
52{
53    pub fn with_behaviour<B, R: TryIntoBehaviour<B>>(
54        self,
55        constructor: impl FnOnce(&libp2p_identity::Keypair, libp2p_relay::client::Behaviour) -> R,
56    ) -> Result<SwarmBuilder<Provider, SwarmPhase<T, B>>, R::Error> {
57        self.without_bandwidth_logging().with_behaviour(constructor)
58    }
59}
60
61impl<Provider, T: AuthenticatedMultiplexedTransport>
62    SwarmBuilder<Provider, BandwidthLoggingPhase<T, NoRelayBehaviour>>
63{
64    pub fn with_behaviour<B, R: TryIntoBehaviour<B>>(
65        self,
66        constructor: impl FnOnce(&libp2p_identity::Keypair) -> R,
67    ) -> Result<SwarmBuilder<Provider, SwarmPhase<T, B>>, R::Error> {
68        self.without_bandwidth_logging().with_behaviour(constructor)
69    }
70}