From da6b366397aa15e2686840c1d9ffa5dae5d49d2a Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 16 Nov 2024 02:16:16 +0530 Subject: First Commit --- src/Server/JSONMessage.java | 19 +++++++++++++++ src/Server/SocketIOMessage.java | 17 +++++++++++++ src/Server/SocketServer.java | 53 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/Server/JSONMessage.java create mode 100644 src/Server/SocketIOMessage.java create mode 100644 src/Server/SocketServer.java (limited to 'src/Server') diff --git a/src/Server/JSONMessage.java b/src/Server/JSONMessage.java new file mode 100644 index 0000000..c3f9712 --- /dev/null +++ b/src/Server/JSONMessage.java @@ -0,0 +1,19 @@ +package Server; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.util.Map; + +public class JSONMessage { + private Map data; + + public JSONMessage(String json) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + data = mapper.readValue(json, Map.class); + } + + public Map getData() { + return data; + } +} diff --git a/src/Server/SocketIOMessage.java b/src/Server/SocketIOMessage.java new file mode 100644 index 0000000..8e6167a --- /dev/null +++ b/src/Server/SocketIOMessage.java @@ -0,0 +1,17 @@ +package Server; + +public class SocketIOMessage { + private String message; + + public SocketIOMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/Server/SocketServer.java b/src/Server/SocketServer.java new file mode 100644 index 0000000..f3a8f2e --- /dev/null +++ b/src/Server/SocketServer.java @@ -0,0 +1,53 @@ +package Server; + +import Store.Book; +import Store.User; +import com.corundumstudio.socketio.SocketIOServer; +import com.corundumstudio.socketio.listener.ConnectListener; +import com.corundumstudio.socketio.listener.DisconnectListener; +import com.corundumstudio.socketio.Configuration; + +public class SocketServer { + public static SocketIOServer server; + + public SocketServer(Configuration config) { + server = new SocketIOServer(config); + + server.addConnectListener(onConnected()); + server.addDisconnectListener(onDisconnected()); + + server.addEventListener("login", JSONMessage.class, User.loginHandler()); + server.addEventListener("signUp", JSONMessage.class, User.signUpHandler()); + + server.addEventListener("issue", JSONMessage.class, Book.issueHandler()); + server.addEventListener("return", JSONMessage.class, Book.returnHandler()); + server.addEventListener("getAllBooks", JSONMessage.class, Book.getListHandler(false)); + server.addEventListener("getAvailableBooks", JSONMessage.class, Book.getListHandler(true)); + } + + private ConnectListener onConnected() { + return (client -> { + System.out.println("New connection!"); + }); + } + + private DisconnectListener onDisconnected() { + return (client -> { + System.out.println("Disconnected!"); + }); + } + + public void start() { + server.start(); + + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + public void run() { + server.stop(); + } + }, "Shutdown-thread")); + } + + public void stop() { + server.stop(); + } +} -- cgit v1.2.3