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
extern crate bytes;
extern crate futures;
extern crate libp2p_core;
extern crate tokio_codec;
extern crate tokio_io;
extern crate unsigned_varint;
use bytes::Bytes;
use futures::{future, prelude::*};
use libp2p_core::{Multiaddr, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}};
use std::{io, iter};
use tokio_codec::{FramedRead, FramedWrite};
use tokio_io::{AsyncRead, AsyncWrite};
use unsigned_varint::codec::UviBytes;
pub struct Observed {}
impl Observed {
pub fn new() -> Self {
Observed {}
}
}
impl UpgradeInfo for Observed {
type Info = &'static [u8];
type InfoIter = iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {
iter::once(b"/paritytech/observed-address/0.1.0")
}
}
impl<C> InboundUpgrade<C> for Observed
where
C: AsyncRead + AsyncWrite + Send + 'static
{
type Output = Sender<C>;
type Error = io::Error;
type Future = Box<dyn Future<Item=Self::Output, Error=Self::Error> + Send>;
fn upgrade_inbound(self, conn: C, _: Self::Info) -> Self::Future {
let io = FramedWrite::new(conn, UviBytes::default());
Box::new(future::ok(Sender { io }))
}
}
impl<C> OutboundUpgrade<C> for Observed
where
C: AsyncRead + AsyncWrite + Send + 'static
{
type Output = Multiaddr;
type Error = io::Error;
type Future = Box<dyn Future<Item=Self::Output, Error=Self::Error> + Send>;
fn upgrade_outbound(self, conn: C, _: Self::Info) -> Self::Future {
let io = FramedRead::new(conn, UviBytes::default());
let future = io.into_future()
.map_err(|(e, _): (io::Error, FramedRead<C, UviBytes>)| e)
.and_then(move |(bytes, _)| {
if let Some(b) = bytes {
let ma = Multiaddr::from_bytes(b.to_vec())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
Ok(ma)
} else {
Err(io::ErrorKind::InvalidData.into())
}
});
Box::new(future)
}
}
pub struct Sender<C> {
io: FramedWrite<C, UviBytes>
}
impl<C: AsyncWrite> Sender<C> {
pub fn send_address(self, a: Multiaddr) -> impl Future<Item=(), Error=io::Error> {
self.io.send(Bytes::from(a.into_bytes())).map(|_io| ())
}
}
#[cfg(test)]
mod tests {
extern crate tokio;
use libp2p_core::{Multiaddr, upgrade::{InboundUpgrade, OutboundUpgrade}};
use self::tokio::runtime::current_thread;
use self::tokio::net::{TcpListener, TcpStream};
use super::*;
#[test]
fn observed_address() {
let server = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
let server_addr = server.local_addr().unwrap();
let observed_addr1: Multiaddr = "/ip4/127.0.0.1/tcp/10000".parse().unwrap();
let observed_addr2 = observed_addr1.clone();
let server = server.incoming()
.into_future()
.map_err(|(e, _)| e.into())
.and_then(move |(conn, _)| {
Observed::new().upgrade_inbound(conn.unwrap(), b"/paritytech/observed-address/0.1.0")
})
.and_then(move |sender| sender.send_address(observed_addr1));
let client = TcpStream::connect(&server_addr)
.map_err(|e| e.into())
.and_then(|conn| {
Observed::new().upgrade_outbound(conn, b"/paritytech/observed-address/0.1.0")
})
.map(move |addr| {
eprintln!("{} {}", addr, observed_addr2);
assert_eq!(addr, observed_addr2)
});
current_thread::block_on_all(future::lazy(move || {
current_thread::spawn(server.map_err(|e| panic!("server error: {}", e)).map(|_| ()));
client.map_err(|e| panic!("client error: {}", e))
}))
.unwrap();
}
}