From 3b16f36d497151ea1628491a057da292855a46f1 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Thu, 21 Nov 2024 23:54:32 +0530 Subject: added multithreading because... yes --- src/Store/Book.java | 72 +++++++++++++++++++++++++++++++++++++---------------- src/Store/User.java | 66 +++++++++++++++++++++++++++++------------------- 2 files changed, 90 insertions(+), 48 deletions(-) (limited to 'src/Store') diff --git a/src/Store/Book.java b/src/Store/Book.java index 4232d77..fe2d63e 100644 --- a/src/Store/Book.java +++ b/src/Store/Book.java @@ -164,38 +164,66 @@ public class Book { public static DataListener getListHandler(boolean availableOnly) { return ((client, data, ackSender) -> { - ArrayList books = getAll(availableOnly); - client.sendEvent(availableOnly ? "availableBooksList" : "allBooksList", books); + Thread t = new Thread(() -> { + ArrayList books = null; + try { + books = getAll(availableOnly); + } catch (SQLException e) { + throw new RuntimeException(e); + } + client.sendEvent(availableOnly ? "availableBooksList" : "allBooksList", books); + }); + + t.start(); + t.join(); }); } public static DataListener issueHandler() { return ((client, data, ackSender) -> { - Map d = data.getData(); - - int userId = (int) d.get("id"); - Book b = new Book((int) d.get("bookId")); - - if (b.issue(userId)) { - client.sendEvent("issued"); - } else { - client.sendEvent("alreadyIssued"); - } + Thread t = new Thread(() -> { + Map d = data.getData(); + + int userId = (int) d.get("id"); + Book b = new Book((int) d.get("bookId")); + + try { + if (b.issue(userId)) { + client.sendEvent("issued"); + } else { + client.sendEvent("alreadyIssued"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + }); + + t.start(); + t.join(); }); } public static DataListener returnHandler() { return ((client, data, ackSender) -> { - Map d = data.getData(); - - int userId = (int) d.get("id"); - Book b = new Book((int) d.get("bookId")); - - if (b.returnBook(userId)) { - client.sendEvent("returned"); - } else { - client.sendEvent("returnFailed"); - } + Thread t = new Thread(() -> { + Map d = data.getData(); + + int userId = (int) d.get("id"); + Book b = new Book((int) d.get("bookId")); + + try { + if (b.returnBook(userId)) { + client.sendEvent("returned"); + } else { + client.sendEvent("returnFailed"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + }); + + t.start(); + t.join(); }); } diff --git a/src/Store/User.java b/src/Store/User.java index e414520..c253421 100644 --- a/src/Store/User.java +++ b/src/Store/User.java @@ -74,39 +74,53 @@ public class User { public static DataListener signUpHandler() { return ((client, data, ackSender) -> { - Map d = data.getData(); - - User user = new User(d.get("userName"), d.get("password")); - try { - if (user.save()) { - client.sendEvent("signedUp"); - } else { - client.sendEvent("usernameTaken"); + Thread t = new Thread(() -> { + Map d = data.getData(); + + User user = new User(d.get("userName"), d.get("password")); + try { + if (user.save()) { + client.sendEvent("signedUp"); + } else { + client.sendEvent("usernameTaken"); + client.sendEvent("signUpFailed"); + } + } catch (SQLException e) { client.sendEvent("signUpFailed"); + System.out.println(e.getMessage()); } - } catch (SQLException e) { - client.sendEvent("signUpFailed"); - System.out.println(e.getMessage()); - } + }); + + t.start(); + t.join(); }); } public static DataListener loginHandler() { return ((client, data, ackSender) -> { - Map d = data.getData(); - - User user = new User(d.get("userName"), d.get("password")); - try { - user.login(); - } catch (SQLException e) { - System.out.println(e.getMessage()); - } - - if (user.login()) { - client.sendEvent("loggedIn", user.id); - } else { - client.sendEvent("loginFailed"); - } + Thread t = new Thread(() -> { + Map d = data.getData(); + + User user = new User(d.get("userName"), d.get("password")); + try { + user.login(); + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + + try { + if (user.login()) { + client.sendEvent("loggedIn", user.id); + } else { + client.sendEvent("loginFailed"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + }); + + t.start(); + t.join(); }); } -- cgit v1.2.3