@@ -18,6 +18,9 @@ use std::collections::HashMap;
18
18
use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr } ;
19
19
20
20
use std:: sync:: { Arc , Mutex } ;
21
+ //use tokio::sync::Mutex;
22
+ //use std::sync::Arc;
23
+
21
24
use std:: str:: FromStr ;
22
25
23
26
@@ -62,14 +65,17 @@ pub async fn start(arc_map: Arc<std::sync::Mutex<HashMap<IpAddr,super::Target>>>
62
65
while let Ok ( ( stream, _) ) = listener. accept ( ) . await {
63
66
let peer = stream. peer_addr ( ) . expect ( "connected streams should have a peer address" ) ;
64
67
//tokio::spawn(accept_connection(arc_map.clone(), stream));
65
- tokio:: spawn ( accept_connection ( arc_map. clone ( ) , peer, stream) ) ;
68
+ //tokio::spawn(accept_connection(arc_map.clone(), peer, stream));
69
+ let handle = tokio:: spawn ( handle_connection ( arc_map. clone ( ) , peer, stream) ) ;
70
+ //let _ = handle.await.unwrap();
66
71
67
72
}
68
73
69
74
Ok ( ( ) )
70
75
}
71
76
72
- async fn accept_connection ( arc_map : Arc < std:: sync:: Mutex < HashMap < IpAddr , super :: Target > > > , peer : SocketAddr , stream : TcpStream ) {
77
+ async fn accept_connection ( arc_map : Arc < std:: sync:: Mutex < HashMap < IpAddr , super :: Target > > > , peer : SocketAddr , stream : TcpStream )
78
+ -> Result < ( ) > {
73
79
/*
74
80
let addr = stream.peer_addr().expect("connected streams should have a peer address");
75
81
//info!("Peer address: {}", addr);
@@ -84,12 +90,15 @@ async fn accept_connection(arc_map: Arc<std::sync::Mutex<HashMap<IpAddr,super::T
84
90
read.forward(write).await.expect("Failed to forward message");
85
91
*/
86
92
87
- if let Err ( e) = handle_connection ( peer, stream) . await {
93
+
94
+ if let Err ( e) = handle_connection ( arc_map, peer, stream) . await {
88
95
match e {
89
96
Error :: ConnectionClosed | Error :: Protocol ( _) | Error :: Utf8 => ( ) ,
90
97
err => error ! ( "Error processing connection: {}" , err) ,
91
98
}
92
99
}
100
+ Ok ( ( ) )
101
+
93
102
/*
94
103
let ipaddr = IpAddr::V4(Ipv4Addr::new(10, 1, 0, 2));
95
104
let mut map = arc_map.lock().unwrap();
@@ -108,7 +117,78 @@ async fn accept_connection(arc_map: Arc<std::sync::Mutex<HashMap<IpAddr,super::T
108
117
}
109
118
110
119
111
- async fn handle_connection ( peer : SocketAddr , stream : TcpStream ) -> Result < ( ) > {
120
+ //async fn parser(arc_map: &Arc<std::sync::Mutex<HashMap<IpAddr,super::Target>>>, msg :&Message) -> Result<()> {
121
+ async fn parser ( arc_map : & Arc < std:: sync:: Mutex < HashMap < IpAddr , super :: Target > > > , msg : & Message ) {
122
+
123
+ println ! ( "SERVER: MSG" ) ;
124
+
125
+ let ipaddr = IpAddr :: V4 ( Ipv4Addr :: new ( 10 , 1 , 0 , 2 ) ) ;
126
+ //let mut map = arc_map.lock().unwrap();
127
+ let map = arc_map. lock ( ) . unwrap ( ) ;
128
+ if map. contains_key ( & ipaddr) {
129
+ //Box::pin(async move {
130
+ match map. get ( & ipaddr) {
131
+ Some ( target) => {
132
+ //&target.tx,
133
+
134
+ println ! ( "map ok " ) ;
135
+ //&target.tx,
136
+ //let cmd = super::Command::Cmd {
137
+ // key: "*command".to_string(),
138
+ //};
139
+ //let txc = target.tx.clone();
140
+ //target.tx.send(cmd).await.unwrap();
141
+ //target.tx.send(cmd);
142
+
143
+ //txc.send(cmd).await.unwrap();
144
+ //txc.send(cmd).await;
145
+ //txc.send(cmd);
146
+ } ,
147
+ None => {
148
+ println ! ( "map ng " ) ;
149
+ }
150
+ }
151
+ //});
152
+ }
153
+ //Ok(())
154
+
155
+ }
156
+ /*
157
+ async fn parser(arc_map: &Arc<std::sync::Mutex<HashMap<IpAddr,super::Target>>>, msg :&Message) -> Result<()> {
158
+
159
+ println!("SERVER: MSG");
160
+
161
+ let ipaddr = IpAddr::V4(Ipv4Addr::new(10, 1, 0, 2));
162
+
163
+ let mut map = arc_map.lock().unwrap();
164
+ if map.contains_key(&ipaddr) {
165
+ match map.get(&ipaddr) {
166
+ Some(target) => {
167
+ //&target.tx,
168
+ println!("map ok ");
169
+ let cmd = super::Command::Cmd {
170
+ key: "*command".to_string(),
171
+ };
172
+ let txc = target.tx.clone();
173
+ target.tx.send(cmd).await.unwrap();
174
+ //target.tx.send(cmd);
175
+
176
+ //txc.send(cmd).await.unwrap();
177
+ //txc.send(cmd);
178
+
179
+ },
180
+ None => {
181
+ println!("map ng ");
182
+ }
183
+ }
184
+ };
185
+
186
+ Ok(())
187
+
188
+ }
189
+
190
+ */
191
+ async fn handle_connection ( arc_map : Arc < std:: sync:: Mutex < HashMap < IpAddr , super :: Target > > > , peer : SocketAddr , stream : TcpStream ) -> Result < ( ) > {
112
192
let ws_stream = accept_async ( stream) . await . expect ( "Failed to accept" ) ;
113
193
info ! ( "New WebSocket connection: {}" , peer) ;
114
194
let ( mut ws_sender, mut ws_receiver) = ws_stream. split ( ) ;
@@ -123,7 +203,44 @@ async fn handle_connection(peer: SocketAddr, stream: TcpStream) -> Result<()> {
123
203
Some ( msg) => {
124
204
let msg = msg?;
125
205
if msg. is_text( ) ||msg. is_binary( ) {
126
- ws_sender. send( msg) . await ?;
206
+
207
+ // parser(&arc_map, &msg).await;
208
+ ws_sender. send( msg) . await ?;
209
+
210
+ let ipaddr = IpAddr :: V4 ( Ipv4Addr :: new( 10 , 1 , 0 , 2 ) ) ;
211
+ //let map = arc_map.lock().unwrap();
212
+ let map_lock = Arc :: clone( & arc_map) ;
213
+ let map = map_lock. lock( ) . unwrap( ) ;
214
+ let rtx_ = if map. contains_key( & ipaddr) {
215
+ match map. get( & ipaddr) {
216
+ Some ( target) => {
217
+ println!( "map ok " ) ;
218
+ Some ( target. tx. clone( ) )
219
+ } ,
220
+ None => {
221
+ println!( "map ng " ) ;
222
+ None
223
+ }
224
+ }
225
+ } else {
226
+ None
227
+ } ;
228
+
229
+ match rtx_ {
230
+
231
+ Some ( tx) => {
232
+ let cmd = super :: Command :: Cmd {
233
+ key: "*command" . to_string( ) ,
234
+ } ;
235
+ //tx.send(cmd).await.unwrap();
236
+
237
+ } ,
238
+ None => {
239
+ continue ;
240
+ } ,
241
+
242
+ } ;
243
+
127
244
} else if msg. is_close( ) {
128
245
break ;
129
246
}
0 commit comments