libp2p/builder/phase/
bandwidth_logging.rs

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