libp2p/
transport_ext.rs

1// Copyright 2018 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! Provides the `TransportExt` trait.
22
23use std::sync::Arc;
24
25use libp2p_identity::PeerId;
26
27#[allow(deprecated)]
28use crate::bandwidth::{BandwidthLogging, BandwidthSinks};
29use crate::{
30    core::{
31        muxing::{StreamMuxer, StreamMuxerBox},
32        transport::Boxed,
33    },
34    Transport,
35};
36
37/// Trait automatically implemented on all objects that implement `Transport`. Provides some
38/// additional utilities.
39pub trait TransportExt: Transport {
40    /// Adds a layer on the `Transport` that logs all traffic that passes through the streams
41    /// created by it.
42    ///
43    /// This method returns an `Arc<BandwidthSinks>` that can be used to retrieve the total number
44    /// of bytes transferred through the streams.
45    ///
46    /// # Example
47    ///
48    /// ```
49    /// use libp2p::{core::upgrade, identity, Transport, TransportExt};
50    /// use libp2p_noise as noise;
51    /// use libp2p_tcp as tcp;
52    /// use libp2p_yamux as yamux;
53    ///
54    /// let id_keys = identity::Keypair::generate_ed25519();
55    ///
56    /// let transport = tcp::tokio::Transport::new(tcp::Config::default().nodelay(true))
57    ///     .upgrade(upgrade::Version::V1)
58    ///     .authenticate(
59    ///         noise::Config::new(&id_keys).expect("Signing libp2p-noise static DH keypair failed."),
60    ///     )
61    ///     .multiplex(yamux::Config::default())
62    ///     .boxed();
63    ///
64    /// let (transport, sinks) = transport.with_bandwidth_logging();
65    /// ```
66    #[allow(deprecated)]
67    #[deprecated(
68        note = "Use `libp2p::SwarmBuilder::with_bandwidth_metrics` or `libp2p_metrics::BandwidthTransport` instead."
69    )]
70    fn with_bandwidth_logging<S>(self) -> (Boxed<(PeerId, StreamMuxerBox)>, Arc<BandwidthSinks>)
71    where
72        Self: Sized + Send + Unpin + 'static,
73        Self::Dial: Send + 'static,
74        Self::ListenerUpgrade: Send + 'static,
75        Self::Error: Send + Sync,
76        Self::Output: Into<(PeerId, S)>,
77        S: StreamMuxer + Send + 'static,
78        S::Substream: Send + 'static,
79        S::Error: Send + Sync + 'static,
80    {
81        let sinks = BandwidthSinks::new();
82        let sinks_copy = sinks.clone();
83        let transport = Transport::map(self, |output, _| {
84            let (peer_id, stream_muxer_box) = output.into();
85            (
86                peer_id,
87                StreamMuxerBox::new(BandwidthLogging::new(stream_muxer_box, sinks_copy)),
88            )
89        })
90        .boxed();
91        (transport, sinks)
92    }
93}
94
95impl<TTransport> TransportExt for TTransport where TTransport: Transport {}