]> piware.de Git - learn-rust.git/commitdiff
tokio-tutorial-jbarszczewski: Implement actual balance data handling
authorMartin Pitt <martin@piware.de>
Fri, 16 Sep 2022 09:41:27 +0000 (11:41 +0200)
committerMartin Pitt <martin@piware.de>
Fri, 16 Sep 2022 09:54:11 +0000 (11:54 +0200)
tokio-tutorial-jbarszczewski/src/main.rs

index 57af76b2c9c46435d30fadb3c85671a2d29249ca..f998acc288997a887307ef57b4eb129b85e7b540 100644 (file)
@@ -1,19 +1,22 @@
 use std::str;
 use std::str;
+use std::sync::{ Arc, Mutex };
 
 use tokio::io::{ AsyncReadExt, AsyncWriteExt };
 
 use tokio::io::{ AsyncReadExt, AsyncWriteExt };
-use tokio::net::{TcpListener, TcpStream};
+use tokio::net::{ TcpListener, TcpStream };
 
 #[tokio::main]
 async fn main() {
 
 #[tokio::main]
 async fn main() {
+    let balance = Arc::new(Mutex::new(0.0f64));
     let listener = TcpListener::bind("127.0.0.1:8181").await.unwrap();
 
     loop {
         let (stream, _) = listener.accept().await.unwrap();
     let listener = TcpListener::bind("127.0.0.1:8181").await.unwrap();
 
     loop {
         let (stream, _) = listener.accept().await.unwrap();
-        tokio::spawn(async move { handle_connection(stream).await });
+        let balance_ref = balance.clone();
+        tokio::spawn(async move { handle_connection(stream, balance_ref).await });
     }
 }
 
     }
 }
 
-async fn handle_connection(mut stream: TcpStream) {
+async fn handle_connection(mut stream: TcpStream, balance: Arc<Mutex<f64>>) {
     // Read the first 16 characters from the incoming stream
     let mut buffer = [0; 16];
     assert!(stream.read(&mut buffer).await.unwrap() >= 16);
     // Read the first 16 characters from the incoming stream
     let mut buffer = [0; 16];
     assert!(stream.read(&mut buffer).await.unwrap() >= 16);
@@ -25,8 +28,7 @@ async fn handle_connection(mut stream: TcpStream) {
 
     let contents = match method_type {
         "GET " => {
 
     let contents = match method_type {
         "GET " => {
-            // todo: return real balance
-            format!("{{\"balance\": {}}}", 0.0)
+            format!("{{\"balance\": {}}}", balance.lock().unwrap())
         }
         "POST" => {
             // Take characters after 'POST /' until whitespace is detected.
         }
         "POST" => {
             // Take characters after 'POST /' until whitespace is detected.
@@ -35,9 +37,11 @@ async fn handle_connection(mut stream: TcpStream) {
                 .take_while(|x| **x != 32u8)
                 .map(|x| *x as char)
                 .collect();
                 .take_while(|x| **x != 32u8)
                 .map(|x| *x as char)
                 .collect();
-            let balance_update = input.parse::<f32>().unwrap();
-            // todo: add balance update handling
-            format!("{{\"balance\": {}}}", balance_update)
+            let balance_update = input.parse::<f64>().unwrap();
+            println!("got POST request to update by {}", balance_update);
+            let mut locked_balance = balance.lock().unwrap();
+            *locked_balance += balance_update;
+            format!("{{\"balance\": {}}}", locked_balance)
         }
         _ => {
             panic!("Invalid HTTP method!")
         }
         _ => {
             panic!("Invalid HTTP method!")