libp2p_core/transport/
dummy.rs

1// Copyright 2019 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
21use crate::transport::{ListenerId, Transport, TransportError, TransportEvent};
22use crate::Multiaddr;
23use futures::{prelude::*, task::Context, task::Poll};
24use std::{fmt, io, marker::PhantomData, pin::Pin};
25
26/// Implementation of `Transport` that doesn't support any multiaddr.
27///
28/// Useful for testing purposes, or as a fallback implementation when no protocol is available.
29pub struct DummyTransport<TOut = DummyStream>(PhantomData<TOut>);
30
31impl<TOut> DummyTransport<TOut> {
32    /// Builds a new `DummyTransport`.
33    pub fn new() -> Self {
34        DummyTransport(PhantomData)
35    }
36}
37
38impl<TOut> Default for DummyTransport<TOut> {
39    fn default() -> Self {
40        DummyTransport::new()
41    }
42}
43
44impl<TOut> fmt::Debug for DummyTransport<TOut> {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(f, "DummyTransport")
47    }
48}
49
50impl<TOut> Clone for DummyTransport<TOut> {
51    fn clone(&self) -> Self {
52        DummyTransport(PhantomData)
53    }
54}
55
56impl<TOut> Transport for DummyTransport<TOut> {
57    type Output = TOut;
58    type Error = io::Error;
59    type ListenerUpgrade = futures::future::Pending<Result<Self::Output, io::Error>>;
60    type Dial = futures::future::Pending<Result<Self::Output, io::Error>>;
61
62    fn listen_on(
63        &mut self,
64        _id: ListenerId,
65        addr: Multiaddr,
66    ) -> Result<(), TransportError<Self::Error>> {
67        Err(TransportError::MultiaddrNotSupported(addr))
68    }
69
70    fn remove_listener(&mut self, _id: ListenerId) -> bool {
71        false
72    }
73
74    fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
75        Err(TransportError::MultiaddrNotSupported(addr))
76    }
77
78    fn dial_as_listener(
79        &mut self,
80        addr: Multiaddr,
81    ) -> Result<Self::Dial, TransportError<Self::Error>> {
82        Err(TransportError::MultiaddrNotSupported(addr))
83    }
84
85    fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option<Multiaddr> {
86        None
87    }
88
89    fn poll(
90        self: Pin<&mut Self>,
91        _: &mut Context<'_>,
92    ) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>> {
93        Poll::Pending
94    }
95}
96
97/// Implementation of `AsyncRead` and `AsyncWrite`. Not meant to be instanciated.
98pub struct DummyStream(());
99
100impl fmt::Debug for DummyStream {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        write!(f, "DummyStream")
103    }
104}
105
106impl AsyncRead for DummyStream {
107    fn poll_read(
108        self: Pin<&mut Self>,
109        _: &mut Context<'_>,
110        _: &mut [u8],
111    ) -> Poll<Result<usize, io::Error>> {
112        Poll::Ready(Err(io::ErrorKind::Other.into()))
113    }
114}
115
116impl AsyncWrite for DummyStream {
117    fn poll_write(
118        self: Pin<&mut Self>,
119        _: &mut Context<'_>,
120        _: &[u8],
121    ) -> Poll<Result<usize, io::Error>> {
122        Poll::Ready(Err(io::ErrorKind::Other.into()))
123    }
124
125    fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
126        Poll::Ready(Err(io::ErrorKind::Other.into()))
127    }
128
129    fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
130        Poll::Ready(Err(io::ErrorKind::Other.into()))
131    }
132}