From 33e1df80c3aad98679af5cc8c2139cc7b0990a0f Mon Sep 17 00:00:00 2001 From: Matin Date: Tue, 16 Jun 2026 19:53:02 +0430 Subject: [PATCH] finish --- .../university/chat/Server/ClientSession.java | 61 +++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/university/chat/Server/ClientSession.java b/src/main/java/com/university/chat/Server/ClientSession.java index 643cc1d..3523085 100644 --- a/src/main/java/com/university/chat/Server/ClientSession.java +++ b/src/main/java/com/university/chat/Server/ClientSession.java @@ -34,8 +34,8 @@ public class ClientSession implements Runnable { @Override public void run() { try { - Object obj = in.readObject() ; - if (obj instanceof ChatMessage loginmsg && loginmsg.getType() == MessageType.LOGIN) + Object firstobj = in.readObject() ; + if (firstobj instanceof ChatMessage loginmsg && loginmsg.getType() == MessageType.LOGIN) { this.username = loginmsg.getSender() ; if (userManager.addUser(username , this)) @@ -46,7 +46,30 @@ public class ClientSession implements Runnable { System.out.println("User logged in successfully: " + username); } + else + { + out.writeObject(new ChatMessage(MessageType.LOGIN_FAILED , "SERVER" , null, "User Name Already Taken !" )); + + } } + else { + socket.close(); + return; + } + while (true) + { + Object obj = in.readObject() ; + if (obj instanceof ChatMessage chatMessage) + { + handleChatMessage(chatMessage); + } + else if (obj instanceof FileMessage fileMessage) + { + handleFileMessage(fileMessage); + } + + } + // TODO: Welcome the user (login step) // 1. Read the first object sent by the client. @@ -66,8 +89,10 @@ public class ClientSession implements Runnable { } catch (Exception e) { System.out.println("Disconnected: " + username); } finally { - // TODO: Remove the user from UserManager so they no longer - // receive broadcasts or appear in users list + if (username != null) { + userManager.removeUser(username); + } + } } @@ -76,13 +101,29 @@ public class ClientSession implements Runnable { switch (msg.getType()) { case PUBLIC_MESSAGE -> { // TODO: Broadcast this message to every connected client. + for (ClientSession clientSession : userManager.getAllSessions()) + { + clientSession.sendMessage(msg); + } } case PRIVATE_MESSAGE -> { // TODO: Forward this message to the receiver user. + ClientSession receiverSession = userManager.getUser(msg.getReceiver()) ; + if (receiverSession != null) + { + receiverSession.sendMessage(msg); + } + else { + sendMessage(new ChatMessage(MessageType.PRIVATE_MESSAGE, "SERVER", username, "User " + msg.getReceiver() + " is offline.")); + } } case USER_LIST -> { // TODO: Reply to the requester with the list of online users. + String userList = userManager.listUsers() ; + sendMessage(new ChatMessage(MessageType.USER_LIST , "SERVER" , username ,userList )); + } + default -> {} } } @@ -93,7 +134,19 @@ public class ClientSession implements Runnable { Files.write(sentPath, fileMsg.getData()); Files.write(recvPath, fileMsg.getData()); + ClientSession receiverSession = userManager.getUser(fileMsg.getReceiver()); + if (receiverSession != null) { + receiverSession.sendFile(fileMsg); + } // TODO: Forward the received file-message to the destination user. } + public synchronized void sendMessage(ChatMessage msg) throws IOException { + out.writeObject(msg); + out.flush(); + } + public synchronized void sendFile(FileMessage fileMsg) throws IOException { + out.writeObject(fileMsg); + out.flush(); + } } \ No newline at end of file