1
+ use std:: collections:: HashMap ;
1
2
use std:: net:: SocketAddr ;
2
3
3
4
use litep2p:: protocol:: request_response:: DialOptions ;
5
+ use litep2p:: protocol:: request_response:: RequestResponseEvent ;
6
+ use litep2p:: types:: RequestId ;
4
7
use litep2p:: PeerId ;
5
8
use multiaddr:: Multiaddr ;
6
9
use multiaddr:: Protocol ;
7
10
use tokio:: sync:: mpsc;
8
11
use tokio:: sync:: oneshot;
9
- use tracing:: trace;
10
12
use tracing:: warn;
11
13
12
14
use crate :: command:: proto:: AddPeerRequest ;
@@ -29,6 +31,9 @@ const DEFAULT_CHANNEL_SIZE: usize = 4096;
29
31
/// Public result type used by the crate.
30
32
pub type Result < T > = std:: result:: Result < T , error:: Error > ;
31
33
34
+ type CommandNotification = Result < PProxyCommandResponse > ;
35
+ type CommandNotifier = oneshot:: Sender < CommandNotification > ;
36
+
32
37
#[ derive( Debug ) ]
33
38
pub enum PProxyCommand {
34
39
AddPeer ( AddPeerRequest ) ,
@@ -41,12 +46,13 @@ pub enum PProxyCommandResponse {
41
46
}
42
47
43
48
pub struct PProxy {
44
- command_rx : mpsc:: Receiver < ( PProxyCommand , oneshot:: Sender < PProxyCommandResponse > ) > ,
49
+ command_rx : mpsc:: Receiver < ( PProxyCommand , CommandNotifier ) > ,
50
+ tunnel_notifier : HashMap < RequestId , CommandNotifier > ,
45
51
p2p_server : P2pServer ,
46
52
}
47
53
48
54
pub struct PProxyHandle {
49
- command_tx : mpsc:: Sender < ( PProxyCommand , oneshot :: Sender < PProxyCommandResponse > ) > ,
55
+ command_tx : mpsc:: Sender < ( PProxyCommand , CommandNotifier ) > ,
50
56
}
51
57
52
58
impl PProxy {
@@ -56,6 +62,7 @@ impl PProxy {
56
62
(
57
63
Self {
58
64
command_rx,
65
+ tunnel_notifier : HashMap :: new ( ) ,
59
66
p2p_server : P2pServer :: new ( server_addr) ,
60
67
} ,
61
68
PProxyHandle { command_tx } ,
@@ -70,41 +77,75 @@ impl PProxy {
70
77
71
78
event = self . p2p_server. next_event( ) => match event {
72
79
None => return ,
73
- Some ( ref event) => if let Err ( error) = self . handle_p2p_server_event( event) . await {
80
+ Some ( ref event) => if let Err ( error) = self . handle_p2p_server_event( event) . await {
74
81
warn!( "failed to handle event {:?}: {:?}" , event, error) ;
75
82
76
83
}
77
84
} ,
78
85
79
86
command = self . command_rx. recv( ) => match command {
80
87
None => return ,
81
- Some ( ( ref command, ref tx) ) => match command {
82
- PProxyCommand :: AddPeer ( request) => {
83
- if let Err ( error) = self . on_add_peer( request, tx) . await {
84
- warn!( "failed to handle command {:?}: {:?}" , command, error) ;
85
- }
86
- }
87
- PProxyCommand :: RequestHttpServer ( request) => {
88
- if let Err ( error) = self . on_request_http_server( request, tx) . await {
89
- warn!( "failed to handle command {:?}: {:?}" , command, error) ;
90
- }
91
- }
88
+ Some ( ( ref command, tx) ) => if let Err ( error) = self . handle_command( command, tx) . await {
89
+ warn!( "failed to handle command {:?}: {:?}" , command, error) ;
92
90
}
93
91
}
94
92
}
95
93
}
96
94
}
97
95
98
96
async fn handle_p2p_server_event ( & mut self , event : & P2pServerEvent ) -> Result < ( ) > {
99
- trace ! ( "handle P2pServerEvent: {:?}" , event) ;
97
+ match event {
98
+ P2pServerEvent :: TunnelEvent ( RequestResponseEvent :: RequestReceived {
99
+ request_id,
100
+ ..
101
+ } ) => self
102
+ . p2p_server
103
+ . tunnel_handle
104
+ . send_response ( * request_id, vec ! [ 1 , 2 , 3 , 4 ] ) ,
105
+
106
+ P2pServerEvent :: TunnelEvent ( RequestResponseEvent :: RequestFailed {
107
+ request_id,
108
+ error,
109
+ ..
110
+ } ) => {
111
+ let Some ( tx) = self . tunnel_notifier . remove ( request_id) else {
112
+ todo ! ( ) ;
113
+ } ;
114
+ tx. send ( Err ( Error :: Litep2pRequestResponseError ( error. clone ( ) ) ) )
115
+ . map_err ( |_| Error :: EssentialTaskClosed ) ?;
116
+ }
117
+
118
+ P2pServerEvent :: TunnelEvent ( RequestResponseEvent :: ResponseReceived {
119
+ request_id,
120
+ response,
121
+ ..
122
+ } ) => {
123
+ let Some ( tx) = self . tunnel_notifier . remove ( request_id) else {
124
+ todo ! ( ) ;
125
+ } ;
126
+ tx. send ( Ok ( PProxyCommandResponse :: RequestHttpServer (
127
+ RequestHttpServerResponse {
128
+ data : response. clone ( ) ,
129
+ } ,
130
+ ) ) )
131
+ . map_err ( |_| Error :: EssentialTaskClosed ) ?;
132
+ }
133
+ _ => { }
134
+ }
135
+
100
136
Ok ( ( ) )
101
137
}
102
138
103
- async fn on_add_peer (
104
- & mut self ,
105
- request : & AddPeerRequest ,
106
- _rx : & oneshot:: Sender < PProxyCommandResponse > ,
107
- ) -> Result < AddPeerResponse > {
139
+ async fn handle_command ( & mut self , command : & PProxyCommand , tx : CommandNotifier ) -> Result < ( ) > {
140
+ match command {
141
+ PProxyCommand :: AddPeer ( request) => self . on_add_peer ( request. clone ( ) , tx) . await ,
142
+ PProxyCommand :: RequestHttpServer ( request) => {
143
+ self . on_request_http_server ( request. clone ( ) , tx) . await
144
+ }
145
+ }
146
+ }
147
+
148
+ async fn on_add_peer ( & mut self , request : AddPeerRequest , tx : CommandNotifier ) -> Result < ( ) > {
108
149
let addr: Multiaddr = request
109
150
. address
110
151
. parse ( )
@@ -123,27 +164,30 @@ impl PProxy {
123
164
. litep2p
124
165
. add_known_address ( peer_id, vec ! [ addr] . into_iter ( ) ) ;
125
166
126
- Ok ( AddPeerResponse {
167
+ tx . send ( Ok ( PProxyCommandResponse :: AddPeer ( AddPeerResponse {
127
168
peer_id : peer_id. to_string ( ) ,
128
- } )
169
+ } ) ) )
170
+ . map_err ( |_| Error :: EssentialTaskClosed )
129
171
}
130
172
131
173
async fn on_request_http_server (
132
174
& mut self ,
133
- request : & RequestHttpServerRequest ,
134
- _rx : & oneshot :: Sender < PProxyCommandResponse > ,
175
+ request : RequestHttpServerRequest ,
176
+ rx : CommandNotifier ,
135
177
) -> Result < ( ) > {
136
178
let peer_id = request
137
179
. peer_id
138
180
. parse ( )
139
181
. map_err ( |_| Error :: PeerIdParseError ( request. peer_id . clone ( ) ) ) ?;
140
182
141
- let _request_id = self
183
+ let request_id = self
142
184
. p2p_server
143
185
. tunnel_handle
144
186
. send_request ( peer_id, request. data . clone ( ) , DialOptions :: Dial )
145
187
. await ?;
146
188
189
+ self . tunnel_notifier . insert ( request_id, rx) ;
190
+
147
191
Ok ( ( ) )
148
192
}
149
193
}
@@ -156,7 +200,7 @@ impl PProxyHandle {
156
200
. send ( ( PProxyCommand :: AddPeer ( request) , tx) )
157
201
. await ?;
158
202
159
- let response = rx. await ?;
203
+ let response = rx. await ?? ;
160
204
161
205
match response {
162
206
PProxyCommandResponse :: AddPeer ( response) => Ok ( response) ,
@@ -174,7 +218,7 @@ impl PProxyHandle {
174
218
. send ( ( PProxyCommand :: RequestHttpServer ( request) , tx) )
175
219
. await ?;
176
220
177
- let response = rx. await ?;
221
+ let response = rx. await ?? ;
178
222
179
223
match response {
180
224
PProxyCommandResponse :: RequestHttpServer ( response) => Ok ( response) ,
0 commit comments