cs_mwc_libp2p_swarm/protocols_handler/
map_out.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
21use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend};
22use crate::protocols_handler::{
23    KeepAlive,
24    SubstreamProtocol,
25    ProtocolsHandler,
26    ProtocolsHandlerEvent,
27    ProtocolsHandlerUpgrErr
28};
29use mwc_libp2p_core::Multiaddr;
30use std::task::{Context, Poll};
31
32/// Wrapper around a protocol handler that turns the output event into something else.
33pub struct MapOutEvent<TProtoHandler, TMap> {
34    inner: TProtoHandler,
35    map: TMap,
36}
37
38impl<TProtoHandler, TMap> MapOutEvent<TProtoHandler, TMap> {
39    /// Creates a `MapOutEvent`.
40    pub(crate) fn new(inner: TProtoHandler, map: TMap) -> Self {
41        MapOutEvent {
42            inner,
43            map,
44        }
45    }
46}
47
48impl<TProtoHandler, TMap, TNewOut> ProtocolsHandler for MapOutEvent<TProtoHandler, TMap>
49where
50    TProtoHandler: ProtocolsHandler,
51    TMap: FnMut(TProtoHandler::OutEvent) -> TNewOut,
52    TNewOut: Send + 'static,
53    TMap: Send + 'static,
54{
55    type InEvent = TProtoHandler::InEvent;
56    type OutEvent = TNewOut;
57    type Error = TProtoHandler::Error;
58    type InboundProtocol = TProtoHandler::InboundProtocol;
59    type OutboundProtocol = TProtoHandler::OutboundProtocol;
60    type InboundOpenInfo = TProtoHandler::InboundOpenInfo;
61    type OutboundOpenInfo = TProtoHandler::OutboundOpenInfo;
62
63    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
64        self.inner.listen_protocol()
65    }
66
67    fn inject_fully_negotiated_inbound(
68        &mut self,
69        protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output,
70        info: Self::InboundOpenInfo
71    ) {
72        self.inner.inject_fully_negotiated_inbound(protocol, info)
73    }
74
75    fn inject_fully_negotiated_outbound(
76        &mut self,
77        protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
78        info: Self::OutboundOpenInfo
79    ) {
80        self.inner.inject_fully_negotiated_outbound(protocol, info)
81    }
82
83    fn inject_event(&mut self, event: Self::InEvent) {
84        self.inner.inject_event(event)
85    }
86
87    fn inject_address_change(&mut self, addr: &Multiaddr) {
88        self.inner.inject_address_change(addr)
89    }
90
91    fn inject_dial_upgrade_error(&mut self, info: Self::OutboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>) {
92        self.inner.inject_dial_upgrade_error(info, error)
93    }
94
95    fn inject_listen_upgrade_error(&mut self, info: Self::InboundOpenInfo, error: ProtocolsHandlerUpgrErr<<Self::InboundProtocol as InboundUpgradeSend>::Error>) {
96        self.inner.inject_listen_upgrade_error(info, error)
97    }
98
99    fn connection_keep_alive(&self) -> KeepAlive {
100        self.inner.connection_keep_alive()
101    }
102
103    fn poll(
104        &mut self,
105        cx: &mut Context<'_>,
106    ) -> Poll<
107        ProtocolsHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::OutEvent, Self::Error>,
108    > {
109        self.inner.poll(cx).map(|ev| {
110            match ev {
111                ProtocolsHandlerEvent::Custom(ev) => ProtocolsHandlerEvent::Custom((self.map)(ev)),
112                ProtocolsHandlerEvent::Close(err) => ProtocolsHandlerEvent::Close(err),
113                ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol } => {
114                    ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol }
115                }
116            }
117        })
118    }
119}