1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2021 Protocol Labs.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::handler::{
    ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, FullyNegotiatedInbound,
    InboundUpgradeSend, KeepAlive, ListenUpgradeError, SubstreamProtocol,
};
use crate::upgrade::SendWrapper;
use either::Either;
use futures::future;
use std::task::{Context, Poll};

impl<LIP, RIP, LIOI, RIOI>
    FullyNegotiatedInbound<Either<SendWrapper<LIP>, SendWrapper<RIP>>, Either<LIOI, RIOI>>
where
    RIP: InboundUpgradeSend,
    LIP: InboundUpgradeSend,
{
    pub(crate) fn transpose(
        self,
    ) -> Either<FullyNegotiatedInbound<LIP, LIOI>, FullyNegotiatedInbound<RIP, RIOI>> {
        match self {
            FullyNegotiatedInbound {
                protocol: future::Either::Left(protocol),
                info: Either::Left(info),
            } => Either::Left(FullyNegotiatedInbound { protocol, info }),
            FullyNegotiatedInbound {
                protocol: future::Either::Right(protocol),
                info: Either::Right(info),
            } => Either::Right(FullyNegotiatedInbound { protocol, info }),
            _ => unreachable!(),
        }
    }
}

impl<LIP, RIP, LIOI, RIOI>
    ListenUpgradeError<Either<LIOI, RIOI>, Either<SendWrapper<LIP>, SendWrapper<RIP>>>
where
    RIP: InboundUpgradeSend,
    LIP: InboundUpgradeSend,
{
    fn transpose(self) -> Either<ListenUpgradeError<LIOI, LIP>, ListenUpgradeError<RIOI, RIP>> {
        match self {
            ListenUpgradeError {
                error: Either::Left(error),
                info: Either::Left(info),
            } => Either::Left(ListenUpgradeError { error, info }),
            ListenUpgradeError {
                error: Either::Right(error),
                info: Either::Right(info),
            } => Either::Right(ListenUpgradeError { error, info }),
            _ => unreachable!(),
        }
    }
}

/// Implementation of a [`ConnectionHandler`] that represents either of two [`ConnectionHandler`]
/// implementations.
impl<L, R> ConnectionHandler for Either<L, R>
where
    L: ConnectionHandler,
    R: ConnectionHandler,
{
    type FromBehaviour = Either<L::FromBehaviour, R::FromBehaviour>;
    type ToBehaviour = Either<L::ToBehaviour, R::ToBehaviour>;
    #[allow(deprecated)]
    type Error = Either<L::Error, R::Error>;
    type InboundProtocol = Either<SendWrapper<L::InboundProtocol>, SendWrapper<R::InboundProtocol>>;
    type OutboundProtocol =
        Either<SendWrapper<L::OutboundProtocol>, SendWrapper<R::OutboundProtocol>>;
    type InboundOpenInfo = Either<L::InboundOpenInfo, R::InboundOpenInfo>;
    type OutboundOpenInfo = Either<L::OutboundOpenInfo, R::OutboundOpenInfo>;

    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
        match self {
            Either::Left(a) => a
                .listen_protocol()
                .map_upgrade(|u| Either::Left(SendWrapper(u)))
                .map_info(Either::Left),
            Either::Right(b) => b
                .listen_protocol()
                .map_upgrade(|u| Either::Right(SendWrapper(u)))
                .map_info(Either::Right),
        }
    }

    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
        match (self, event) {
            (Either::Left(handler), Either::Left(event)) => handler.on_behaviour_event(event),
            (Either::Right(handler), Either::Right(event)) => handler.on_behaviour_event(event),
            _ => unreachable!(),
        }
    }

    fn connection_keep_alive(&self) -> KeepAlive {
        match self {
            Either::Left(handler) => handler.connection_keep_alive(),
            Either::Right(handler) => handler.connection_keep_alive(),
        }
    }

    #[allow(deprecated)]
    fn poll(
        &mut self,
        cx: &mut Context<'_>,
    ) -> Poll<
        ConnectionHandlerEvent<
            Self::OutboundProtocol,
            Self::OutboundOpenInfo,
            Self::ToBehaviour,
            Self::Error,
        >,
    > {
        let event = match self {
            Either::Left(handler) => futures::ready!(handler.poll(cx))
                .map_custom(Either::Left)
                .map_close(Either::Left)
                .map_protocol(|p| Either::Left(SendWrapper(p)))
                .map_outbound_open_info(Either::Left),
            Either::Right(handler) => futures::ready!(handler.poll(cx))
                .map_custom(Either::Right)
                .map_close(Either::Right)
                .map_protocol(|p| Either::Right(SendWrapper(p)))
                .map_outbound_open_info(Either::Right),
        };

        Poll::Ready(event)
    }

    fn on_connection_event(
        &mut self,
        event: ConnectionEvent<
            Self::InboundProtocol,
            Self::OutboundProtocol,
            Self::InboundOpenInfo,
            Self::OutboundOpenInfo,
        >,
    ) {
        match event {
            ConnectionEvent::FullyNegotiatedInbound(fully_negotiated_inbound) => {
                match (fully_negotiated_inbound.transpose(), self) {
                    (Either::Left(fully_negotiated_inbound), Either::Left(handler)) => handler
                        .on_connection_event(ConnectionEvent::FullyNegotiatedInbound(
                            fully_negotiated_inbound,
                        )),
                    (Either::Right(fully_negotiated_inbound), Either::Right(handler)) => handler
                        .on_connection_event(ConnectionEvent::FullyNegotiatedInbound(
                            fully_negotiated_inbound,
                        )),
                    _ => unreachable!(),
                }
            }
            ConnectionEvent::FullyNegotiatedOutbound(fully_negotiated_outbound) => {
                match (fully_negotiated_outbound.transpose(), self) {
                    (Either::Left(fully_negotiated_outbound), Either::Left(handler)) => handler
                        .on_connection_event(ConnectionEvent::FullyNegotiatedOutbound(
                            fully_negotiated_outbound,
                        )),
                    (Either::Right(fully_negotiated_outbound), Either::Right(handler)) => handler
                        .on_connection_event(ConnectionEvent::FullyNegotiatedOutbound(
                            fully_negotiated_outbound,
                        )),
                    _ => unreachable!(),
                }
            }
            ConnectionEvent::DialUpgradeError(dial_upgrade_error) => {
                match (dial_upgrade_error.transpose(), self) {
                    (Either::Left(dial_upgrade_error), Either::Left(handler)) => handler
                        .on_connection_event(ConnectionEvent::DialUpgradeError(dial_upgrade_error)),
                    (Either::Right(dial_upgrade_error), Either::Right(handler)) => handler
                        .on_connection_event(ConnectionEvent::DialUpgradeError(dial_upgrade_error)),
                    _ => unreachable!(),
                }
            }
            ConnectionEvent::ListenUpgradeError(listen_upgrade_error) => {
                match (listen_upgrade_error.transpose(), self) {
                    (Either::Left(listen_upgrade_error), Either::Left(handler)) => handler
                        .on_connection_event(ConnectionEvent::ListenUpgradeError(
                            listen_upgrade_error,
                        )),
                    (Either::Right(listen_upgrade_error), Either::Right(handler)) => handler
                        .on_connection_event(ConnectionEvent::ListenUpgradeError(
                            listen_upgrade_error,
                        )),
                    _ => unreachable!(),
                }
            }
            ConnectionEvent::AddressChange(address_change) => match self {
                Either::Left(handler) => {
                    handler.on_connection_event(ConnectionEvent::AddressChange(address_change))
                }
                Either::Right(handler) => {
                    handler.on_connection_event(ConnectionEvent::AddressChange(address_change))
                }
            },
            ConnectionEvent::LocalProtocolsChange(supported_protocols) => match self {
                Either::Left(handler) => handler.on_connection_event(
                    ConnectionEvent::LocalProtocolsChange(supported_protocols),
                ),
                Either::Right(handler) => handler.on_connection_event(
                    ConnectionEvent::LocalProtocolsChange(supported_protocols),
                ),
            },
            ConnectionEvent::RemoteProtocolsChange(supported_protocols) => match self {
                Either::Left(handler) => handler.on_connection_event(
                    ConnectionEvent::RemoteProtocolsChange(supported_protocols),
                ),
                Either::Right(handler) => handler.on_connection_event(
                    ConnectionEvent::RemoteProtocolsChange(supported_protocols),
                ),
            },
        }
    }
}