feat: implement message handling and add utility methods to ClientSession
This commit is contained in:
@@ -2,62 +2,145 @@ package com.university.chat.Server;
|
|||||||
|
|
||||||
import com.university.chat.Common.ChatMessage;
|
import com.university.chat.Common.ChatMessage;
|
||||||
import com.university.chat.Common.FileMessage;
|
import com.university.chat.Common.FileMessage;
|
||||||
|
import com.university.chat.Common.MessageType;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
public class ClientSession implements Runnable {
|
public class ClientSession implements Runnable {
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
private Socket socket;
|
||||||
|
private final UserManager userManager;
|
||||||
|
private ObjectOutputStream out;
|
||||||
|
private ObjectInputStream in;
|
||||||
|
|
||||||
public ClientSession(Socket socket, UserManager userManager) {
|
public ClientSession(Socket socket, UserManager userManager) {
|
||||||
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
|
||||||
// and an ObjectInputStream from socket.getInputStream().
|
this.userManager = userManager;
|
||||||
|
this.socket = socket;
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.out = new ObjectOutputStream(socket.getOutputStream());
|
||||||
|
out.flush();
|
||||||
|
this.in = new ObjectInputStream(socket.getInputStream());
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
System.out.println("Error initializing stream: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
System.out.println("welcome");
|
||||||
|
|
||||||
// TODO: Welcome the user (login step)
|
Object object = in.readObject();
|
||||||
// 1. Read the first object sent by the client.
|
|
||||||
// 2. Check it's a ChatMessage with type LOGIN.
|
|
||||||
// 3. Extract the username.
|
|
||||||
// 4. Try to register the user via userManager.addUser(...).
|
|
||||||
// 5. If the username is taken, send back LOGIN_FAILED and close the socket.
|
|
||||||
// 6. Otherwise, create the user's folders with FileManager.createUserFolders(...)
|
|
||||||
// and send back LOGIN_SUCCESS.
|
|
||||||
|
|
||||||
// TODO: Main message loop
|
if (object instanceof ChatMessage login && login.getType() == MessageType.LOGIN) {
|
||||||
// In a loop, call in.readObject(), you can separate messages by their type:
|
|
||||||
// - if it's a ChatMessage -> call handleChatMessage(msg)
|
|
||||||
// - if it's a FileMessage -> call handleFileMessage(fileMsg)
|
|
||||||
// Keep looping until the connection is closed (an exception will be thrown).
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
username = login.getSender();
|
||||||
|
|
||||||
|
if (userManager.addUser(username, this)) {
|
||||||
|
System.out.printf("Successfully registered: %s\n", username);
|
||||||
|
|
||||||
|
FileManager.createUserFolders(username);
|
||||||
|
|
||||||
|
out.writeObject(
|
||||||
|
new ChatMessage(MessageType.LOGIN_SUCCESS,
|
||||||
|
"[server]",
|
||||||
|
username,
|
||||||
|
null
|
||||||
|
));
|
||||||
|
|
||||||
|
out.flush();
|
||||||
|
broadcastUserList();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
out.writeObject(
|
||||||
|
new ChatMessage(MessageType.LOGIN_FAILED,
|
||||||
|
"[server]",
|
||||||
|
username,
|
||||||
|
null
|
||||||
|
));
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
socket.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
socket.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
Object msg = in.readObject();
|
||||||
|
|
||||||
|
if (msg instanceof ChatMessage chatMsg) {
|
||||||
|
handleChatMessage(chatMsg);
|
||||||
|
}
|
||||||
|
else if (msg instanceof FileMessage fileMsg) {
|
||||||
|
handleFileMessage(fileMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
System.out.println("Disconnected: " + username);
|
System.out.println("Disconnected: " + username);
|
||||||
} finally {
|
}
|
||||||
// TODO: Remove the user from UserManager so they no longer
|
finally {
|
||||||
// receive broadcasts or appear in users list
|
if (username != null) {
|
||||||
|
userManager.removeUser(username);
|
||||||
|
broadcastUserList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void handleChatMessage(ChatMessage msg) throws IOException {
|
private void handleChatMessage(ChatMessage msg) throws IOException {
|
||||||
switch (msg.getType()) {
|
switch (msg.getType()) {
|
||||||
case PUBLIC_MESSAGE -> {
|
case PUBLIC_MESSAGE -> {
|
||||||
// TODO: Broadcast this message to every connected client.
|
Iterable<ClientSession> users = userManager.getAllSessions();
|
||||||
|
for (ClientSession user : users) {
|
||||||
|
if (user != this) {
|
||||||
|
user.sendObject(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case PRIVATE_MESSAGE -> {
|
case PRIVATE_MESSAGE -> {
|
||||||
// TODO: Forward this message to the receiver user.
|
|
||||||
|
ClientSession receiver = userManager.getUser(msg.getReceiver());
|
||||||
|
if (receiver != null) {
|
||||||
|
receiver.sendObject(msg);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sendObject(new ChatMessage(
|
||||||
|
MessageType.PRIVATE_MESSAGE,
|
||||||
|
"[server]",
|
||||||
|
username,
|
||||||
|
"User not found: " + msg.getReceiver()
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case USER_LIST -> {
|
case USER_LIST -> {
|
||||||
// TODO: Reply to the requester with the list of online users.
|
|
||||||
|
sendObject(new ChatMessage(MessageType.USER_LIST,
|
||||||
|
"[server]",
|
||||||
|
username,
|
||||||
|
userManager.listUsers())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private synchronized void sendObject(Object obj) throws IOException {
|
||||||
|
out.writeObject(obj);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
private void handleFileMessage(FileMessage fileMsg) throws IOException {
|
private void handleFileMessage(FileMessage fileMsg) throws IOException {
|
||||||
// Storing the file
|
// Storing the file
|
||||||
var sentPath = FileManager.getSentPath(fileMsg.getSender(), fileMsg.getFilename());
|
var sentPath = FileManager.getSentPath(fileMsg.getSender(), fileMsg.getFilename());
|
||||||
@@ -66,6 +149,26 @@ public class ClientSession implements Runnable {
|
|||||||
Files.write(sentPath, fileMsg.getData());
|
Files.write(sentPath, fileMsg.getData());
|
||||||
Files.write(recvPath, fileMsg.getData());
|
Files.write(recvPath, fileMsg.getData());
|
||||||
|
|
||||||
// TODO: Forward the received file-message to the destination user.
|
ClientSession receiver = userManager.getUser(fileMsg.getReceiver());
|
||||||
|
receiver.sendObject(fileMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void broadcastUserList() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
String activeUsers = userManager.listUsers();
|
||||||
|
ChatMessage updateMsg = new ChatMessage(
|
||||||
|
MessageType.USER_LIST,
|
||||||
|
"[server]",
|
||||||
|
null,
|
||||||
|
activeUsers
|
||||||
|
);
|
||||||
|
for (ClientSession session : userManager.getAllSessions()) {
|
||||||
|
session.sendObject(updateMsg);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error broadcasting user list: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user