libp2p_request_response/handler/
protocol.rs

1// Copyright 2020 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//! The definition of a request/response protocol via inbound
22//! and outbound substream upgrades. The inbound upgrade
23//! receives a request and sends a response, whereas the
24//! outbound upgrade send a request and receives a response.
25
26use std::convert::Infallible;
27
28use futures::future::{ready, Ready};
29use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
30use libp2p_swarm::Stream;
31use smallvec::SmallVec;
32
33/// The level of support for a particular protocol.
34#[derive(Debug, Clone)]
35pub enum ProtocolSupport {
36    /// The protocol is only supported for inbound requests.
37    Inbound,
38    /// The protocol is only supported for outbound requests.
39    Outbound,
40    /// The protocol is supported for inbound and outbound requests.
41    Full,
42}
43
44impl ProtocolSupport {
45    /// Whether inbound requests are supported.
46    pub fn inbound(&self) -> bool {
47        match self {
48            ProtocolSupport::Inbound | ProtocolSupport::Full => true,
49            ProtocolSupport::Outbound => false,
50        }
51    }
52
53    /// Whether outbound requests are supported.
54    pub fn outbound(&self) -> bool {
55        match self {
56            ProtocolSupport::Outbound | ProtocolSupport::Full => true,
57            ProtocolSupport::Inbound => false,
58        }
59    }
60}
61
62/// Response substream upgrade protocol.
63///
64/// Receives a request and sends a response.
65#[derive(Debug)]
66pub struct Protocol<P> {
67    pub(crate) protocols: SmallVec<[P; 2]>,
68}
69
70impl<P> UpgradeInfo for Protocol<P>
71where
72    P: AsRef<str> + Clone,
73{
74    type Info = P;
75    type InfoIter = smallvec::IntoIter<[Self::Info; 2]>;
76
77    fn protocol_info(&self) -> Self::InfoIter {
78        self.protocols.clone().into_iter()
79    }
80}
81
82impl<P> InboundUpgrade<Stream> for Protocol<P>
83where
84    P: AsRef<str> + Clone,
85{
86    type Output = (Stream, P);
87    type Error = Infallible;
88    type Future = Ready<Result<Self::Output, Self::Error>>;
89
90    fn upgrade_inbound(self, io: Stream, protocol: Self::Info) -> Self::Future {
91        ready(Ok((io, protocol)))
92    }
93}
94
95impl<P> OutboundUpgrade<Stream> for Protocol<P>
96where
97    P: AsRef<str> + Clone,
98{
99    type Output = (Stream, P);
100    type Error = Infallible;
101    type Future = Ready<Result<Self::Output, Self::Error>>;
102
103    fn upgrade_outbound(self, io: Stream, protocol: Self::Info) -> Self::Future {
104        ready(Ok((io, protocol)))
105    }
106}