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
// Copyright 2018 Parity Technologies (UK) Ltd.
// 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.

pub mod control;
pub mod floodsub;
pub mod protocol;
pub mod subscription;

use futures::channel::{mpsc, oneshot};
use libp2prs_core::PeerId;
use std::{
    error,
    fmt::{Display, Result},
    io,
};

mod rpc_proto {
    include!(concat!(env!("OUT_DIR"), "/floodsub.pb.rs"));
}

const FLOOD_SUB_ID: &[u8] = b"/floodsub/1.0.0";

/// Configuration options for the Floodsub protocol.
#[derive(Clone)]
pub struct FloodsubConfig {
    /// Peer id of the local node. Used for the source of the messages that we publish.
    pub local_peer_id: PeerId,

    /// `true` if messages published by local node should be propagated as messages received from
    /// the network, `false` by default.
    pub subscribe_local_messages: bool,
}

impl FloodsubConfig {
    pub fn new(local_peer_id: PeerId) -> Self {
        Self {
            local_peer_id,
            subscribe_local_messages: false,
        }
    }
}

/// Built topic.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Topic(String);

impl Topic {
    /// Returns the id of the topic.
    #[inline]
    pub fn id(&self) -> &str {
        &self.0
    }

    pub fn is_empty(&self) -> bool {
        self.0 == ""
    }

    pub fn new<S>(name: S) -> Topic
    where
        S: Into<String>,
    {
        Topic(name.into())
    }
}

impl From<Topic> for String {
    fn from(topic: Topic) -> String {
        topic.0
    }
}

#[derive(Debug)]
pub enum FloodsubError {
    Io(io::Error),
    Closed,
}

impl error::Error for FloodsubError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            FloodsubError::Io(err) => Some(err),
            FloodsubError::Closed => None,
        }
    }
}

impl Display for FloodsubError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
        match self {
            FloodsubError::Io(e) => write!(f, "i/o error: {}", e),
            FloodsubError::Closed => f.write_str("floodsub protocol is closed"),
        }
    }
}

impl From<io::Error> for FloodsubError {
    fn from(e: io::Error) -> Self {
        FloodsubError::Io(e)
    }
}

impl From<mpsc::SendError> for FloodsubError {
    fn from(_: mpsc::SendError) -> Self {
        FloodsubError::Closed
    }
}

impl From<oneshot::Canceled> for FloodsubError {
    fn from(_: oneshot::Canceled) -> Self {
        FloodsubError::Closed
    }
}