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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Copyright 2020 Netwarps Ltd.
//
// 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.

//! Communication channel to the remote peer.
//!
//! The Swarm [`Connection`] is the multiplexed connection, which can be used to open or accept
//! new substreams. Furthermore, a raw substream opened by the StreamMuxer has to be upgraded to
//! the Swarm [`Substream`] via multistream select procedure.
//!

use smallvec::SmallVec;
use std::hash::Hash;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use std::{error::Error, fmt};

use futures::channel::{mpsc, oneshot};
use futures::prelude::*;

use async_std::task;
use async_std::task::JoinHandle;

use libp2prs_core::identity::Keypair;
use libp2prs_core::multistream::Negotiator;
use libp2prs_core::muxing::IStreamMuxer;
use libp2prs_core::transport::TransportError;
use libp2prs_core::PublicKey;

use crate::connection::Direction::Outbound;
use crate::control::SwarmControlCmd;
use crate::identify::{IDENTIFY_PROTOCOL, IDENTIFY_PUSH_PROTOCOL};
use crate::metrics::metric::Metric;
use crate::ping::PING_PROTOCOL;
use crate::substream::{ConnectInfo, StreamId, Substream, SubstreamView};
use crate::{identify, ping, Multiaddr, PeerId, ProtocolId, SwarmError, SwarmEvent};

/// The direction of a peer-to-peer communication channel.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Direction {
    /// The socket comes from a dialer.
    Outbound,
    /// The socket comes from a listener.
    Inbound,
}

impl fmt::Display for Direction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", if self == &Outbound { "Out" } else { "In " })
    }
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ConnectionId(usize);

impl fmt::Display for ConnectionId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:<5}", self.0)
    }
}

/// A multiplexed connection to a peer with associated `Substream`s.
#[allow(dead_code)]
pub struct Connection {
    /// The unique ID for a connection
    id: ConnectionId,
    /// Node that handles the stream_muxer.
    stream_muxer: IStreamMuxer,
    /// The tx channel, to send Connection events to Swarm
    tx: mpsc::UnboundedSender<SwarmEvent>,
    /// The ctrl tx channel.
    ctrl: mpsc::Sender<SwarmControlCmd>,
    /// All sub-streams belonged to this connection.
    substreams: SmallVec<[SubstreamView; 8]>,
    /// Direction of this connection
    dir: Direction,
    /// Indicates if Ping task is running.
    ping_running: Arc<AtomicBool>,
    /// Ping failure count.
    ping_failures: u32,
    /// Identity service
    identity: Option<()>,
    /// The task handle of this connection, returned by task::Spawn
    /// handle.await() when closing a connection
    handle: Option<JoinHandle<()>>,
    /// The task handle of the Ping service of this connection
    ping_handle: Option<JoinHandle<()>>,
    /// The task handle of the Identify service of this connection
    identify_handle: Option<JoinHandle<()>>,
    /// The task handle of the Identify Push service of this connection
    identify_push_handle: Option<JoinHandle<()>>,
    /// Global metrics.
    metric: Arc<Metric>,
}

impl PartialEq for Connection {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl fmt::Debug for Connection {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Connection")
            .field("id", &self.id)
            .field("muxer", &self.stream_muxer)
            .field("dir", &self.dir)
            .field("subs", &self.substreams)
            .finish()
    }
}

//impl Unpin for Connection where TMuxer: StreamMuxer {}

#[allow(dead_code)]
impl Connection {
    /// Builds a new `Connection` from the given substream multiplexer
    /// and a tx channel which will used to send events to Swarm.
    pub(crate) fn new(
        id: usize,
        stream_muxer: IStreamMuxer,
        dir: Direction,
        tx: mpsc::UnboundedSender<SwarmEvent>,
        ctrl: mpsc::Sender<SwarmControlCmd>,
        metric: Arc<Metric>,
    ) -> Self {
        Connection {
            id: ConnectionId(id),
            stream_muxer,
            tx,
            ctrl,
            dir,
            substreams: Default::default(),
            handle: None,
            ping_running: Arc::new(AtomicBool::new(false)),
            ping_failures: 0,
            ping_handle: None,
            identity: None,
            identify_handle: None,
            identify_push_handle: None,
            metric,
        }
    }

    pub(crate) fn to_view(&self) -> ConnectionView {
        ConnectionView {
            id: self.id,
            dir: self.dir,
            info: self.info(),
            substreams: self.substreams.clone(),
        }
    }

    pub(crate) fn substream_view(&self) -> Vec<SubstreamView> {
        self.substreams.to_vec()
    }

    /// Returns the unique Id of the connection.
    pub(crate) fn id(&self) -> ConnectionId {
        self.id
    }

    /// Returns a reference of the stream_muxer.
    pub(crate) fn stream_muxer(&self) -> &IStreamMuxer {
        &self.stream_muxer
    }

    /// Sets the task handle of the connection.
    pub(crate) fn set_handle(&mut self, handle: JoinHandle<()>) {
        self.handle = Some(handle);
    }

    /// Opens a sub stream with the protocols specified
    pub fn open_stream<T: Send + 'static>(
        &mut self,
        pids: Vec<ProtocolId>,
        f: impl FnOnce(Result<Substream, TransportError>) -> T + Send + 'static,
    ) -> JoinHandle<T> {
        let cid = self.id();
        let stream_muxer = self.stream_muxer().clone();
        let mut tx = self.tx.clone();
        let ctrl = self.ctrl.clone();
        let metric = self.metric.clone();

        task::spawn(async move {
            let result = open_stream_internal(cid, stream_muxer, pids, ctrl, metric).await;

            // TODO: how to extract the error from TransportError, ??? it doesn't implement 'Clone'
            // So, at this moment, make a new 'TransportError::Internal'
            let event = match result.as_ref() {
                Ok(sub_stream) => {
                    let view = sub_stream.to_view();
                    SwarmEvent::StreamOpened { view }
                }
                Err(_) => SwarmEvent::StreamError {
                    cid,
                    error: TransportError::Internal,
                },
            };

            let _ = tx.send(event).await;

            f(result)
        })
    }

    /// Closes the inner stream_muxer. Spawn a task to avoid blocking.
    pub fn close(&self) {
        log::debug!("closing {:?}", self);

        let mut stream_muxer = self.stream_muxer.clone();
        // spawns a task to close the stream_muxer, later connection will cleaned up
        // in 'handle_connection_closed'
        task::spawn(async move {
            let _ = stream_muxer.close().await;
        });
    }

    /// Waits for bg-task & accept-task.
    pub(crate) async fn wait(&mut self) -> Result<(), SwarmError> {
        // wait for accept-task and bg-task to exit
        if let Some(h) = self.handle.take() {
            h.await;
        }
        Ok(())
    }
    /// local_addr is the multiaddr on our side of the connection.
    pub(crate) fn local_addr(&self) -> Multiaddr {
        self.stream_muxer.local_multiaddr()
    }

    /// remote_addr is the multiaddr on the remote side of the connection.
    pub(crate) fn remote_addr(&self) -> Multiaddr {
        self.stream_muxer.remote_multiaddr()
    }

    /// local_peer is the Peer on our side of the connection.
    pub(crate) fn local_peer(&self) -> PeerId {
        self.stream_muxer.local_peer()
    }

    /// remote_peer is the Peer on the remote side.
    pub fn remote_peer(&self) -> PeerId {
        self.stream_muxer.remote_peer()
    }

    /// local_priv_key is the public key of the peer on this side.
    pub(crate) fn local_priv_key(&self) -> Keypair {
        self.stream_muxer.local_priv_key()
    }

    /// remote_pub_key is the public key of the peer on the remote side.
    pub(crate) fn remote_pub_key(&self) -> PublicKey {
        self.stream_muxer.remote_pub_key()
    }

    /// Adds a substream id to the list.
    pub(crate) fn add_stream(&mut self, view: SubstreamView) {
        log::debug!("adding sub {:?} to connection", view);
        self.substreams.push(view);
    }
    /// Removes a substream id from the list.
    pub(crate) fn del_stream(&mut self, sid: StreamId) {
        log::debug!("removing sub {:?} from connection", sid);
        self.substreams.retain(|s| s.id != sid);
    }

    /// Returns how many substreams in the list.
    pub(crate) fn num_streams(&self) -> usize {
        self.substreams.len()
    }

    /// Starts the Ping service on this connection. The task handle will be tracked
    /// by the connection for later closing the Ping service
    ///
    /// Note that we don't generate StreamOpened/Closed event for Ping/Identify outbound
    /// simply because it doesn't make much sense doing so for a transient outgoing
    /// stream.
    pub(crate) fn start_ping(&mut self, timeout: Duration, interval: Duration, max_failures: u32) {
        self.ping_running.store(true, Ordering::Relaxed);

        let cid = self.id();
        let stream_muxer = self.stream_muxer.clone();
        let mut tx = self.tx.clone();
        let flag = self.ping_running.clone();
        let pids = vec![PING_PROTOCOL.into()];
        let ctrl = self.ctrl.clone();
        let metric = self.metric.clone();

        let handle = task::spawn(async move {
            let mut fail_cnt: u32 = 0;
            loop {
                if !flag.load(Ordering::Relaxed) {
                    break;
                }

                // sleep for the interval
                task::sleep(interval).await;

                //recheck, in case ping service has been terminated already
                if !flag.load(Ordering::Relaxed) {
                    break;
                }

                let stream_muxer = stream_muxer.clone();
                let pids = pids.clone();

                let ctrl2 = ctrl.clone();
                let r = open_stream_internal(cid, stream_muxer, pids, ctrl2, metric.clone()).await;
                let r = match r {
                    Ok(stream) => {
                        let view = stream.to_view();
                        let _ = tx.send(SwarmEvent::StreamOpened { view }).await;
                        let res = ping::ping(stream, timeout).await;
                        if res.is_ok() {
                            fail_cnt = 0;
                        } else {
                            fail_cnt += 1;
                        }
                        res
                    }
                    Err(err) => {
                        // looks like the peer doesn't support the protocol
                        log::info!("Ping protocol not supported: {:?}", err);
                        Err(err)
                    }
                };

                if fail_cnt >= max_failures {
                    let _ = tx
                        .send(SwarmEvent::PingResult {
                            cid,
                            result: r.map_err(|e| e.into()),
                        })
                        .await;

                    break;
                }
            }

            log::debug!("ping task exiting...");
        });

        self.ping_handle = Some(handle);
    }

    /// Stops the Ping service on this connection
    pub(crate) async fn stop_ping(&mut self) {
        if let Some(h) = self.ping_handle.take() {
            log::debug!("stopping Ping service for {:?}...", self.id);
            self.ping_running.store(false, Ordering::Relaxed);
            h.await;
            //h.cancel().await;
        }
    }

    /// Starts the Identify service on this connection.
    pub(crate) fn start_identify(&mut self) {
        let cid = self.id();
        let stream_muxer = self.stream_muxer.clone();
        let mut tx = self.tx.clone();
        let ctrl = self.ctrl.clone();
        let pids = vec![IDENTIFY_PROTOCOL.into()];
        let metric = self.metric.clone();

        let handle = task::spawn(async move {
            let r = open_stream_internal(cid, stream_muxer, pids, ctrl, metric).await;
            let r = match r {
                Ok(stream) => {
                    let view = stream.to_view();
                    let _ = tx.send(SwarmEvent::StreamOpened { view }).await;
                    identify::process_message(stream).await
                }
                Err(err) => {
                    // looks like the peer doesn't support the protocol
                    log::info!("Identify protocol not supported: {:?}", err);
                    Err(err)
                }
            };
            let _ = tx
                .send(SwarmEvent::IdentifyResult {
                    cid,
                    result: r.map_err(TransportError::into),
                })
                .await;

            log::debug!("identify task exiting...");
        });

        self.identify_handle = Some(handle);
    }

    pub(crate) async fn stop_identify(&mut self) {
        if let Some(h) = self.identify_handle.take() {
            log::debug!("stopping Identify service for {:?}...", self.id);
            h.cancel().await;
        }
    }

    /// Starts the Identify service on this connection.
    pub(crate) fn start_identify_push(&mut self) {
        let cid = self.id();
        let stream_muxer = self.stream_muxer.clone();
        let pids = vec![IDENTIFY_PUSH_PROTOCOL.into()];
        let metric = self.metric.clone();

        let mut ctrl = self.ctrl.clone();

        let mut tx = self.tx.clone();

        let handle = task::spawn(async move {
            let (swrm_tx, swrm_rx) = oneshot::channel();
            if ctrl.send(SwarmControlCmd::IdentifyInfo(swrm_tx)).await.is_err() {
                // this might happen, when swarm is exiting...
                return;
            }
            let info = swrm_rx.await.expect("get identify info");

            let r = open_stream_internal(cid, stream_muxer, pids, ctrl, metric).await;
            match r {
                Ok(stream) => {
                    let view = stream.to_view();
                    let _ = tx.send(SwarmEvent::StreamOpened { view }).await;
                    // ignore the error
                    let _ = identify::produce_message(stream, info).await;
                }
                Err(err) => {
                    // looks like the peer doesn't support the protocol
                    log::info!("Identify push protocol not supported: {:?}", err);
                    //Err(err)
                }
            }

            log::debug!("identify push task exiting...");
        });

        self.identify_push_handle = Some(handle);
    }
    pub(crate) async fn stop_identify_push(&mut self) {
        if let Some(h) = self.identify_push_handle.take() {
            log::debug!("stopping Identify Push service for {:?}...", self.id);
            h.cancel().await;
        }
    }

    pub(crate) fn info(&self) -> ConnectionInfo {
        // calculate inbound
        let num_inbound_streams = self.substreams.iter().fold(0usize, |mut acc, s| {
            if s.dir == Direction::Inbound {
                acc += 1;
            }
            acc
        });
        let num_outbound_streams = self.substreams.len() - num_inbound_streams;
        ConnectionInfo {
            la: self.local_addr(),
            ra: self.remote_addr(),
            local_peer_id: self.local_peer(),
            remote_peer_id: self.remote_peer(),
            num_inbound_streams,
            num_outbound_streams,
        }
    }
}

#[derive(Debug)]
/// ConnectionView is used for debugging purpose.
pub struct ConnectionView {
    /// The unique ID for a connection.
    pub id: ConnectionId,
    /// Direction of this connection.
    pub dir: Direction,
    /// Detailed information of this connection.
    pub info: ConnectionInfo,
    /// Handler that processes substreams.
    pub substreams: SmallVec<[SubstreamView; 8]>,
}

impl fmt::Display for ConnectionView {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {} RPID({:52}) I/O({}{}) RA({})",
            self.id, self.dir, self.info.remote_peer_id, self.info.num_inbound_streams, self.info.num_outbound_streams, self.info.ra
        )
    }
}

async fn open_stream_internal(
    cid: ConnectionId,
    mut stream_muxer: IStreamMuxer,
    pids: Vec<ProtocolId>,
    ctrl: mpsc::Sender<SwarmControlCmd>,
    metric: Arc<Metric>,
) -> Result<Substream, TransportError> {
    log::debug!("opening substream on {:?} {:?}", cid, pids);

    let raw_stream = stream_muxer.open_stream().await?;
    let la = stream_muxer.local_multiaddr();
    let ra = stream_muxer.remote_multiaddr();
    let rpid = stream_muxer.remote_peer();

    // now it's time to do protocol multiplexing for sub stream
    let negotiator = Negotiator::new_with_protocols(pids);
    let result = negotiator.select_one(raw_stream).await;

    match result {
        Ok((proto, raw_stream)) => {
            log::debug!("selected outbound {:?} {:?}", cid, proto);

            let ci = ConnectInfo { la, ra, rpid };
            let stream = Substream::new(raw_stream, metric.clone(), Direction::Outbound, proto, cid, ci, ctrl);
            Ok(stream)
        }
        Err(err) => {
            log::debug!("failed outbound protocol selection {:?} {:?}", cid, err);
            Err(TransportError::NegotiationError(err))
        }
    }
}

/// Information about a connection limit.
#[derive(Debug, Clone)]
pub struct ConnectionLimit {
    /// The maximum number of connections.
    pub limit: usize,
    /// The current number of connections.
    pub current: usize,
}

impl fmt::Display for ConnectionLimit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}/{}", self.current, self.limit)
    }
}

/// A `ConnectionLimit` can represent an error if it has been exceeded.
impl Error for ConnectionLimit {}

/// Information about the network obtained by [`Network::info()`].
#[derive(Debug)]
pub struct ConnectionInfo {
    /// The local multiaddr of this connection.
    pub la: Multiaddr,
    /// The remote multiaddr of this connection.
    pub ra: Multiaddr,
    /// The local peer ID.
    pub local_peer_id: PeerId,
    /// The remote peer ID.
    pub remote_peer_id: PeerId,
    /// The total number of inbound sub streams.
    pub num_inbound_streams: usize,
    /// The total number of outbound sub streams.
    pub num_outbound_streams: usize,
    // /// The Sub-streams.
    // pub streams: Vec<StreamStats>,
}