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
23#[allow(deprecated)]
24use crate::bandwidth::{BandwidthLogging, BandwidthSinks};
25use crate::core::{
26 muxing::{StreamMuxer, StreamMuxerBox},
27 transport::Boxed,
28};
29use crate::Transport;
30use libp2p_identity::PeerId;
31use std::sync::Arc;
32
33/// Trait automatically implemented on all objects that implement `Transport`. Provides some
34/// additional utilities.
35pub trait TransportExt: Transport {
36 /// Adds a layer on the `Transport` that logs all trafic that passes through the streams
37 /// created by it.
38 ///
39 /// This method returns an `Arc<BandwidthSinks>` that can be used to retrieve the total number
40 /// of bytes transferred through the streams.
41 ///
42 /// # Example
43 ///
44 /// ```
45 /// use libp2p_yamux as yamux;
46 /// use libp2p_noise as noise;
47 /// use libp2p_tcp as tcp;
48 /// use libp2p::{
49 /// core::upgrade,
50 /// identity,
51 /// TransportExt,
52 /// Transport,
53 /// };
54 ///
55 /// let id_keys = identity::Keypair::generate_ed25519();
56 ///
57 /// let transport = tcp::tokio::Transport::new(tcp::Config::default().nodelay(true))
58 /// .upgrade(upgrade::Version::V1)
59 /// .authenticate(
60 /// noise::Config::new(&id_keys)
61 /// .expect("Signing libp2p-noise static DH keypair failed."),
62 /// )
63 /// .multiplex(yamux::Config::default())
64 /// .boxed();
65 ///
66 /// let (transport, sinks) = transport.with_bandwidth_logging();
67 /// ```
68 #[allow(deprecated)]
69 #[deprecated(
70 note = "Use `libp2p::SwarmBuilder::with_bandwidth_metrics` or `libp2p_metrics::BandwidthTransport` instead."
71 )]
72 fn with_bandwidth_logging<S>(self) -> (Boxed<(PeerId, StreamMuxerBox)>, Arc<BandwidthSinks>)
73 where
74 Self: Sized + Send + Unpin + 'static,
75 Self::Dial: Send + 'static,
76 Self::ListenerUpgrade: Send + 'static,
77 Self::Error: Send + Sync,
78 Self::Output: Into<(PeerId, S)>,
79 S: StreamMuxer + Send + 'static,
80 S::Substream: Send + 'static,
81 S::Error: Send + Sync + 'static,
82 {
83 let sinks = BandwidthSinks::new();
84 let sinks_copy = sinks.clone();
85 let transport = Transport::map(self, |output, _| {
86 let (peer_id, stream_muxer_box) = output.into();
87 (
88 peer_id,
89 StreamMuxerBox::new(BandwidthLogging::new(stream_muxer_box, sinks_copy)),
90 )
91 })
92 .boxed();
93 (transport, sinks)
94 }
95}
96
97impl<TTransport> TransportExt for TTransport where TTransport: Transport {}