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