backend: Use enum to handle multiple db requests
This commit is contained in:
parent
ca2b74eb03
commit
fcd20af9bd
4 changed files with 60 additions and 46 deletions
2
back_end/.gitignore
vendored
2
back_end/.gitignore
vendored
|
@ -1 +1,3 @@
|
||||||
/target
|
/target
|
||||||
|
fourLineDropper.db
|
||||||
|
.dump
|
||||||
|
|
|
@ -4,6 +4,11 @@ use std::thread;
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum DBHandlerRequest {
|
||||||
|
GetID(SyncSender<u32>),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
enum DBFirstRun {
|
enum DBFirstRun {
|
||||||
FirstRun,
|
FirstRun,
|
||||||
|
@ -58,7 +63,7 @@ fn init_conn(sqlite_path: &str, first_run: DBFirstRun) -> Result<Connection, Str
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_db_handler_thread(
|
pub fn start_db_handler_thread(
|
||||||
rx: Receiver<SyncSender<u32>>,
|
rx: Receiver<DBHandlerRequest>,
|
||||||
sqlite_path: String,
|
sqlite_path: String,
|
||||||
shutdown_tx: SyncSender<()>,
|
shutdown_tx: SyncSender<()>,
|
||||||
) {
|
) {
|
||||||
|
@ -74,12 +79,13 @@ pub fn start_db_handler_thread(
|
||||||
'outer: loop {
|
'outer: loop {
|
||||||
let rx_recv_result = rx.recv();
|
let rx_recv_result = rx.recv();
|
||||||
if let Err(e) = rx_recv_result {
|
if let Err(e) = rx_recv_result {
|
||||||
println!("Failed to get player_tx: {:?}", e);
|
println!("Failed to get DBHandlerRequest: {:?}", e);
|
||||||
shutdown_tx.send(()).ok();
|
shutdown_tx.send(()).ok();
|
||||||
break;
|
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
|
// got request to create new player, create new player
|
||||||
let mut player_id: u32 = thread_rng().gen();
|
let mut player_id: u32 = thread_rng().gen();
|
||||||
let conn_result = init_conn(&sqlite_path, DBFirstRun::NotFirstRun);
|
let conn_result = init_conn(&sqlite_path, DBFirstRun::NotFirstRun);
|
||||||
|
@ -120,6 +126,8 @@ pub fn start_db_handler_thread(
|
||||||
break 'outer;
|
break 'outer;
|
||||||
}
|
}
|
||||||
send_result.unwrap();
|
send_result.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Pair up players
|
// Pair up players
|
||||||
// TODO
|
// TODO
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use crate::db_handler::DBHandlerRequest;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
sync::mpsc::{sync_channel, SyncSender},
|
sync::mpsc::{sync_channel, SyncSender},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
|
@ -7,7 +9,7 @@ use serde_json::Value;
|
||||||
|
|
||||||
pub fn handle_json(
|
pub fn handle_json(
|
||||||
root: Value,
|
root: Value,
|
||||||
tx: SyncSender<SyncSender<u32>>,
|
tx: SyncSender<DBHandlerRequest>,
|
||||||
_shutdown_tx: SyncSender<()>, // maybe used here, not sure if it will be
|
_shutdown_tx: SyncSender<()>, // maybe used here, not sure if it will be
|
||||||
) -> Result<String, String> {
|
) -> Result<String, String> {
|
||||||
if let Some(Value::String(type_str)) = root.get("type") {
|
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);
|
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());
|
return Err("{\"type\":\"pairing_response\", \"status\":\"internal_error\"}".into());
|
||||||
}
|
}
|
||||||
if let Ok(pid) = player_rx.recv_timeout(Duration::from_secs(5)) {
|
if let Ok(pid) = player_rx.recv_timeout(Duration::from_secs(5)) {
|
||||||
|
|
|
@ -3,7 +3,9 @@ mod json_handlers;
|
||||||
|
|
||||||
const SQLITE_DB_PATH: &str = "./fourLineDropper.db";
|
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 db_handler::start_db_handler_thread;
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
|
@ -11,7 +13,7 @@ use warp::{Filter, Rejection};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn 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 db_tx_clone = db_tx.clone();
|
||||||
|
|
||||||
let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
|
let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
|
||||||
|
|
Loading…
Reference in a new issue