First insert/get workflow
This commit is contained in:
parent
f659022e8c
commit
7e7cf010a0
1 changed files with 38 additions and 6 deletions
|
|
@ -1,15 +1,47 @@
|
|||
use std::io;
|
||||
use tokio::net::UdpSocket;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
use std::{io, net::SocketAddr, str, sync::Arc};
|
||||
use tokio::{net::UdpSocket, sync::mpsc};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
let sock = UdpSocket::bind("0.0.0.0:8080").await?;
|
||||
let sock = UdpSocket::bind("0.0.0.0:8080".parse::<SocketAddr>().unwrap()).await?;
|
||||
let r = Arc::new(sock);
|
||||
let s = r.clone();
|
||||
let (tx, mut rx) = mpsc::channel::<(Vec<u8>, SocketAddr)>(1_000);
|
||||
let storage = Arc::new(Mutex::new(HashMap::<String, String>::new()));
|
||||
|
||||
tokio::spawn(async move {
|
||||
while let Some((bytes, addr)) = rx.recv().await {
|
||||
let len = s.send_to(&bytes, &addr).await.unwrap();
|
||||
println!("{:?} bytes sent", len);
|
||||
}
|
||||
});
|
||||
|
||||
let mut buf = [0; 1024];
|
||||
loop {
|
||||
let (len, addr) = sock.recv_from(&mut buf).await?;
|
||||
let (len, addr) = r.recv_from(&mut buf).await?;
|
||||
println!("{:?} bytes received from {:?}", len, addr);
|
||||
let message = str::from_utf8(&buf[..len]).unwrap().trim_matches('\n');
|
||||
let storage = storage.clone();
|
||||
|
||||
let len = sock.send_to(&buf[..len], addr).await?;
|
||||
println!("{:?} bytes sent", len);
|
||||
if message.contains("=") {
|
||||
println!("Insert request");
|
||||
let (key, value) = message.split_once('=').unwrap();
|
||||
|
||||
println!("Key: {key}");
|
||||
println!("Value: {value}");
|
||||
storage
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(key.to_string(), value.to_string());
|
||||
// continue;
|
||||
} else {
|
||||
println!("Get request: {}", message);
|
||||
let value = storage.lock().unwrap().get(message).unwrap().clone();
|
||||
tx.send((value.as_bytes().to_vec(), addr)).await.unwrap();
|
||||
}
|
||||
|
||||
buf.fill(0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue