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