backend: Use enum to handle multiple db requests

This commit is contained in:
Stephen Seo 2022-03-28 16:31:53 +09:00
parent ca2b74eb03
commit fcd20af9bd
4 changed files with 60 additions and 46 deletions

2
back_end/.gitignore vendored
View file

@ -1 +1,3 @@
/target
fourLineDropper.db
.dump

View file

@ -4,6 +4,11 @@ use std::thread;
use rand::{thread_rng, Rng};
use rusqlite::Connection;
#[derive(Clone, Debug)]
pub enum DBHandlerRequest {
GetID(SyncSender<u32>),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum DBFirstRun {
FirstRun,
@ -58,7 +63,7 @@ fn init_conn(sqlite_path: &str, first_run: DBFirstRun) -> Result<Connection, Str
}
pub fn start_db_handler_thread(
rx: Receiver<SyncSender<u32>>,
rx: Receiver<DBHandlerRequest>,
sqlite_path: String,
shutdown_tx: SyncSender<()>,
) {
@ -74,12 +79,13 @@ pub fn start_db_handler_thread(
'outer: loop {
let rx_recv_result = rx.recv();
if let Err(e) = rx_recv_result {
println!("Failed to get player_tx: {:?}", e);
println!("Failed to get DBHandlerRequest: {:?}", e);
shutdown_tx.send(()).ok();
break;
}
let player_tx = rx_recv_result.unwrap();
let db_request = rx_recv_result.unwrap();
match db_request {
DBHandlerRequest::GetID(player_tx) => {
// got request to create new player, create new player
let mut player_id: u32 = thread_rng().gen();
let conn_result = init_conn(&sqlite_path, DBFirstRun::NotFirstRun);
@ -120,6 +126,8 @@ pub fn start_db_handler_thread(
break 'outer;
}
send_result.unwrap();
}
}
// Pair up players
// TODO

View file

@ -1,3 +1,5 @@
use crate::db_handler::DBHandlerRequest;
use std::{
sync::mpsc::{sync_channel, SyncSender},
time::Duration,
@ -7,7 +9,7 @@ use serde_json::Value;
pub fn handle_json(
root: Value,
tx: SyncSender<SyncSender<u32>>,
tx: SyncSender<DBHandlerRequest>,
_shutdown_tx: SyncSender<()>, // maybe used here, not sure if it will be
) -> Result<String, String> {
if let Some(Value::String(type_str)) = root.get("type") {
@ -24,9 +26,9 @@ pub fn handle_json(
}
}
fn handle_pairing_request(tx: SyncSender<SyncSender<u32>>) -> Result<String, String> {
fn handle_pairing_request(tx: SyncSender<DBHandlerRequest>) -> Result<String, String> {
let (player_tx, player_rx) = sync_channel::<u32>(1);
if tx.send(player_tx).is_err() {
if tx.send(DBHandlerRequest::GetID(player_tx)).is_err() {
return Err("{\"type\":\"pairing_response\", \"status\":\"internal_error\"}".into());
}
if let Ok(pid) = player_rx.recv_timeout(Duration::from_secs(5)) {

View file

@ -3,7 +3,9 @@ mod json_handlers;
const SQLITE_DB_PATH: &str = "./fourLineDropper.db";
use std::sync::mpsc::{sync_channel, SyncSender};
use db_handler::DBHandlerRequest;
use std::sync::mpsc::sync_channel;
use db_handler::start_db_handler_thread;
use tokio::sync::oneshot;
@ -11,7 +13,7 @@ use warp::{Filter, Rejection};
#[tokio::main]
async fn main() {
let (db_tx, db_rx) = sync_channel::<SyncSender<u32>>(32);
let (db_tx, db_rx) = sync_channel::<DBHandlerRequest>(32);
let db_tx_clone = db_tx.clone();
let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();