1
1
use std:: net:: IpAddr ;
2
2
use std:: net:: SocketAddr ;
3
3
4
+ use futures:: Stream ;
4
5
use futures:: StreamExt ;
5
6
use litep2p:: config:: ConfigBuilder ;
6
7
use litep2p:: crypto:: ed25519:: SecretKey ;
8
+ use litep2p:: protocol:: libp2p:: ping:: Config as PingConfig ;
9
+ use litep2p:: protocol:: libp2p:: ping:: PingEvent ;
7
10
use litep2p:: protocol:: request_response:: Config as RequestResponseConfig ;
8
11
use litep2p:: protocol:: request_response:: ConfigBuilder as RequestResponseConfigBuilder ;
9
12
use litep2p:: protocol:: request_response:: RequestResponseEvent ;
@@ -16,18 +19,21 @@ use litep2p::Litep2pEvent;
16
19
#[ derive( Debug ) ]
17
20
pub enum P2pServerEvent {
18
21
Litep2p ( Litep2pEvent ) ,
22
+ PingEvent ( PingEvent ) ,
19
23
ControlEvent ( RequestResponseEvent ) ,
20
24
TunnelEvent ( RequestResponseEvent ) ,
21
25
}
22
26
23
27
pub struct P2pServer {
24
28
pub ( crate ) litep2p : Litep2p ,
29
+ pub ( crate ) ping_stream : Box < dyn Stream < Item = PingEvent > + Send + Unpin > ,
25
30
pub ( crate ) control_handle : RequestResponseHandle ,
26
31
pub ( crate ) tunnel_handle : RequestResponseHandle ,
27
32
}
28
33
29
34
impl P2pServer {
30
35
pub fn new ( secret_key : SecretKey , server_addr : SocketAddr ) -> Self {
36
+ let ( ping_config, ping_stream) = PingConfig :: default ( ) ;
31
37
let ( control_config, control_handle) = Self :: init_control ( ) ;
32
38
let ( tunnel_config, tunnel_handle) = Self :: init_tunnel ( ) ;
33
39
@@ -42,6 +48,7 @@ impl P2pServer {
42
48
listen_addresses : vec ! [ format!( "/{ip_type}/{ip}/tcp/{port}" ) . parse( ) . unwrap( ) ] ,
43
49
..Default :: default ( )
44
50
} )
51
+ . with_libp2p_ping ( ping_config)
45
52
. with_request_response_protocol ( control_config)
46
53
. with_request_response_protocol ( tunnel_config)
47
54
. build ( ) ;
@@ -52,6 +59,7 @@ impl P2pServer {
52
59
53
60
Self {
54
61
litep2p,
62
+ ping_stream,
55
63
control_handle,
56
64
tunnel_handle,
57
65
}
@@ -77,6 +85,9 @@ impl P2pServer {
77
85
ev = self . litep2p. next_event( ) => {
78
86
ev. map( P2pServerEvent :: Litep2p )
79
87
}
88
+ ev = self . ping_stream. next( ) => {
89
+ ev. map( P2pServerEvent :: PingEvent )
90
+ }
80
91
ev = self . control_handle. next( ) => {
81
92
ev. map( P2pServerEvent :: ControlEvent )
82
93
}
0 commit comments