]> git.seodisparate.com - EN605.607.81.SP22_ASDM_Project/commitdiff
Update backend protocol, skeleton backend project
authorStephen Seo <seo.disparate@gmail.com>
Wed, 16 Mar 2022 03:26:18 +0000 (12:26 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 16 Mar 2022 03:26:18 +0000 (12:26 +0900)
back_end/Cargo.lock
back_end/Cargo.toml
back_end/src/json_handlers.rs [new file with mode: 0644]
back_end/src/main.rs
backend_protocol_specification.md

index b6bfcc719d7ff4589034af7926eff6ffd39aac2a..93786c0be07f3d0bf076d0156d0865847f10cc25 100644 (file)
@@ -133,6 +133,7 @@ dependencies = [
 name = "four_line_dropper_backend"
 version = "0.1.0"
 dependencies = [
+ "serde_json",
  "tokio",
  "warp",
 ]
index 3dd1016eeabb9f8f0422bd8f5d20f547476f40f4..dc40e30fe1838a8df1b3717e63f3dbda9f352132 100644 (file)
@@ -8,3 +8,4 @@ edition = "2021"
 [dependencies]
 tokio = { version = "1", features = ["full"] }
 warp = "0.3"
+serde_json = "1.0"
diff --git a/back_end/src/json_handlers.rs b/back_end/src/json_handlers.rs
new file mode 100644 (file)
index 0000000..4a3ee17
--- /dev/null
@@ -0,0 +1,48 @@
+use serde_json::Value;
+
+pub fn handle_json(root: Value) -> Result<String, String> {
+    if let Some(Value::String(type_str)) = root.get("type") {
+        match type_str.as_str() {
+            "pairing_request" => handle_pairing_request(root),
+            "check_pairing" => handle_check_pairing(root),
+            "place_token" => handle_place_token(root),
+            "whose_turn" => handle_whose_turn(root),
+            "disconnect" => handle_disconnect(root),
+            "request_board_state" => handle_request_board_state(root),
+            "game_state" => handle_game_state(root),
+            _ => {
+                Err("{\"type\":\"invalid_type\"}".into())
+            }
+        }
+    } else {
+        Err("{\"type\":\"invalid_json\"}".into())
+    }
+}
+
+fn handle_pairing_request(root: Value) -> Result<String, String> {
+    Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_check_pairing(root: Value) -> Result<String, String> {
+    Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_place_token(root: Value) -> Result<String, String> {
+    Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_whose_turn(root: Value) -> Result<String, String> {
+    Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_disconnect(root: Value) -> Result<String, String> {
+    Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_request_board_state(root: Value) -> Result<String, String> {
+    Err("{\"type\":\"unimplemented\"}".into())
+}
+
+fn handle_game_state(root: Value) -> Result<String, String> {
+    Err("{\"type\":\"unimplemented\"}".into())
+}
index e7a11a969c037e00a796aafeff6258501ec15e9a..ee63d036647f134ee531f7b05e0d7c5311e645b8 100644 (file)
@@ -1,3 +1,24 @@
-fn main() {
-    println!("Hello, world!");
+mod json_handlers;
+
+use serde_json::Value;
+use warp::Filter;
+
+#[tokio::main]
+async fn main() {
+    let route = warp::body::content_length_limit(1024 * 32)
+        .and(warp::body::json())
+        .map(|json_value: Value| {
+            let result = json_handlers::handle_json(json_value);
+            if let Ok(result_str) = result {
+                result_str
+            } else if let Err(error_str) = result {
+                error_str
+            } else {
+                unreachable!()
+            }
+        });
+
+    warp::serve(route)
+        .run(([0,0,0,0], 1237))
+        .await;
 }
index b431433090ae27f1844816d3fb9b7c80737cd0b5..1516eb1292a0cc6a48f1c1fe53a4f72a0a7fc1a7 100644 (file)
@@ -199,3 +199,28 @@ of "opponent\_disconnected". Future requests will return "unknown\_id".
         "status": "opponent_disconnected", // or "unknown_id"
     }
 ```
+
+8. Failure Response
+
+When request "type" is not handled by the back-end, it returns with
+"invalid\_type".
+```
+    {
+        "type": "invalid_type"
+    }
+```
+
+When JSON is missing a required value, it returns with "invalid\_json".
+```
+    {
+        "type": "invalid_json"
+    }
+```
+
+When the back-end hasn't yet implemented handling a specific type, it returns
+"unimplemented".
+```
+    {
+        "type": "unimplemented"
+    }
+```