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
//! A JSONRPC response.
use std::sync::Arc;

use serde::Deserialize;
use serde::Serialize;
use serde_json::Value as JsonValue;

use crate::error::Error;
use crate::error::Result;
use crate::prelude::rings_core::dht::Did;
use crate::prelude::rings_core::message::Encoded;
use crate::prelude::rings_core::prelude::web3::contract::tokens::Tokenizable;
use crate::prelude::rings_core::transports::Transport;
use crate::processor;

/// Peer contains transport address and state information.
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Peer {
    /// a processor' address
    pub did: String,
    /// a transport protocol using in swarm instance
    pub transport_id: String,
    /// transport ice connection state
    pub state: String,
}

impl Peer {
    pub fn to_json_vec(&self) -> Result<Vec<u8>> {
        serde_json::to_vec(self).map_err(|_| Error::JsonSerializeError)
    }

    pub fn to_json_obj(&self) -> Result<JsonValue> {
        serde_json::to_value(self).map_err(|_| Error::JsonSerializeError)
    }

    #[cfg(feature = "node")]
    pub fn base64_encode(&self) -> Result<String> {
        Ok(base64::encode(self.to_json_vec()?))
    }
}

impl From<(Did, &Arc<Transport>, Option<String>)> for Peer {
    fn from((did, transport, state): (Did, &Arc<Transport>, Option<String>)) -> Self {
        Self {
            did: did.to_string(),
            transport_id: transport.id.to_string(),
            state: state.unwrap_or_else(|| "Unknown".to_owned()),
        }
    }
}

impl From<(&processor::Peer, Option<String>)> for Peer {
    fn from((p, state): (&processor::Peer, Option<String>)) -> Self {
        Self {
            did: p.did.clone().into_token().to_string(),
            transport_id: p.transport.id.to_string(),
            state: state.unwrap_or_else(|| "Unknown".to_owned()),
        }
    }
}

/// Base Transport Info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransportInfo {
    pub transport_id: String,
    pub state: String,
}

impl TransportInfo {
    pub fn new(transport_id: String, state: Option<String>) -> Self {
        Self {
            transport_id,
            state: state.unwrap_or_else(|| "Unknown".to_owned()),
        }
    }
}

impl From<(&Arc<Transport>, Option<String>)> for TransportInfo {
    fn from((transport, state): (&Arc<Transport>, Option<String>)) -> Self {
        Self::new(transport.id.to_string(), state)
    }
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct TransportAndIce {
    pub transport_id: String,
    pub ice: String,
}

impl TransportAndIce {
    pub fn new(transport_id: &str, ice: &str) -> Self {
        Self {
            transport_id: transport_id.to_owned(),
            ice: ice.to_owned(),
        }
    }

    pub fn to_json_vec(&self) -> Result<Vec<u8>> {
        serde_json::to_vec(self).map_err(|_| Error::JsonSerializeError)
    }

    pub fn to_json_obj(&self) -> Result<JsonValue> {
        serde_json::to_value(self).map_err(|_| Error::JsonSerializeError)
    }

    #[cfg(feature = "node")]
    pub fn base64_encode(&self) -> Result<String> {
        Ok(base64::encode(self.to_json_vec()?))
    }
}

impl From<(Arc<Transport>, Encoded)> for TransportAndIce {
    fn from((transport, handshake_info): (Arc<Transport>, Encoded)) -> Self {
        Self {
            transport_id: transport.id.to_string(),
            ice: handshake_info.to_string(),
        }
    }
}