libp2p/builder/phase/
bandwidth_logging.rs

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