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
use crate::identity::Keypair;
use crate::muxing::{IReadWrite, IStreamMuxer, StreamMuxer, StreamMuxerEx};
use crate::secure_io::SecureInfo;
use crate::transport::{ConnectionInfo, TransportError};
use crate::upgrade::{UpgradeInfo, Upgrader};
use crate::{Multiaddr, PeerId, PublicKey};
use async_trait::async_trait;
use futures::future::BoxFuture;
use libp2prs_traits::{ReadEx, WriteEx};
use log::trace;
use std::{fmt, io};
pub struct DummyUpgrader;
pub struct DummyStream<T>(pub(crate) T);
impl<T> Clone for DummyStream<T> {
fn clone(&self) -> Self {
unimplemented!()
}
}
impl<T> fmt::Debug for DummyStream<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("DummyStream")
}
}
impl DummyUpgrader {
pub fn new() -> Self {
DummyUpgrader
}
}
impl Default for DummyUpgrader {
fn default() -> Self {
DummyUpgrader::new()
}
}
impl fmt::Debug for DummyUpgrader {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DummyUpgrader")
}
}
impl Clone for DummyUpgrader {
fn clone(&self) -> Self {
DummyUpgrader
}
}
impl UpgradeInfo for DummyUpgrader {
type Info = &'static [u8];
fn protocol_info(&self) -> Vec<Self::Info> {
vec![b"/dummy/1.0.0"]
}
}
#[async_trait]
impl<T: Send + 'static> Upgrader<T> for DummyUpgrader {
type Output = DummyStream<T>;
async fn upgrade_inbound(self, socket: T, _info: <Self as UpgradeInfo>::Info) -> Result<Self::Output, TransportError> {
trace!("dummy upgrader, upgrade inbound connection");
Ok(DummyStream(socket))
}
async fn upgrade_outbound(self, socket: T, _info: <Self as UpgradeInfo>::Info) -> Result<Self::Output, TransportError> {
trace!("dummy upgrader, upgrade outbound connection");
Ok(DummyStream(socket))
}
}
#[async_trait]
impl<T: Send + ReadEx> ReadEx for DummyStream<T> {
async fn read2(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read2(buf).await
}
}
#[async_trait]
impl<T: Send + WriteEx> WriteEx for DummyStream<T> {
async fn write2(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write2(buf).await
}
async fn flush2(&mut self) -> io::Result<()> {
self.0.flush2().await
}
async fn close2(&mut self) -> io::Result<()> {
self.0.close2().await
}
}
#[async_trait]
impl<T: ConnectionInfo> StreamMuxer for DummyStream<T> {
async fn open_stream(&mut self) -> Result<IReadWrite, TransportError> {
Err(TransportError::Internal)
}
async fn accept_stream(&mut self) -> Result<IReadWrite, TransportError> {
Err(TransportError::Internal)
}
async fn close(&mut self) -> Result<(), TransportError> {
Ok(())
}
fn task(&mut self) -> Option<BoxFuture<'static, ()>> {
None
}
fn box_clone(&self) -> IStreamMuxer {
unimplemented!()
}
}
impl<T: ConnectionInfo> ConnectionInfo for DummyStream<T> {
fn local_multiaddr(&self) -> Multiaddr {
self.0.local_multiaddr()
}
fn remote_multiaddr(&self) -> Multiaddr {
self.0.remote_multiaddr()
}
}
impl<T> SecureInfo for DummyStream<T> {
fn local_peer(&self) -> PeerId {
PeerId::random()
}
fn remote_peer(&self) -> PeerId {
PeerId::random()
}
fn local_priv_key(&self) -> Keypair {
Keypair::generate_ed25519()
}
fn remote_pub_key(&self) -> PublicKey {
Keypair::generate_ed25519().public()
}
}
impl<T: ConnectionInfo> StreamMuxerEx for DummyStream<T> {}