Develop #1

Merged
Aeen merged 4 commits from develop into main 2026-07-30 10:17:52 +00:00
Showing only changes of commit 59edb79db4 - Show all commits
@@ -2,70 +2,160 @@ 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 UserManager userManager;
private ObjectInputStream in;
private ObjectOutputStream out;
public ClientSession(Socket socket, UserManager userManager) { public ClientSession(Socket socket, UserManager userManager) {
// TODO : Create an ObjectOutputStream from socket.getOutputStream() try {
// and an ObjectInputStream from socket.getInputStream(). this.socket = socket;
this.userManager = userManager;
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
} }
@Override @Override
public void run() { public void run() {
try { try {
Object firstObject = in.readObject();
// TODO: Welcome the user (login step) if (!(firstObject instanceof ChatMessage loginMessage)) {
// 1. Read the first object sent by the client. socket.close();
// 2. Check it's a ChatMessage with type LOGIN. return;
// 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 (loginMessage.getType() != MessageType.LOGIN) {
// In a loop, call in.readObject(), you can separate messages by their type: socket.close();
// - if it's a ChatMessage -> call handleChatMessage(msg) return;
// - if it's a FileMessage -> call handleFileMessage(fileMsg) }
// Keep looping until the connection is closed (an exception will be thrown).
username = loginMessage.getSender();
boolean added = userManager.addUser(username, this);
if (!added) {
ChatMessage failedMessage = new ChatMessage(
MessageType.LOGIN_FAILED,
"SERVER",
username,
"Username already exists"
);
send(failedMessage);
socket.close();
return;
}
FileManager.createUserFolders(username);
ChatMessage successMessage = new ChatMessage(
MessageType.LOGIN_SUCCESS,
"SERVER",
username,
"Login successful"
);
send(successMessage);
while (true) {
Object object = in.readObject();
if (object instanceof ChatMessage msg) {
handleChatMessage(msg);
}
if (object instanceof FileMessage fileMsg) {
handleFileMessage(fileMsg);
}
}
} catch (Exception e) { } catch (Exception e) {
System.out.println("Disconnected: " + username); System.out.println("Disconnected: " + username);
} finally { } finally {
// TODO: Remove the user from UserManager so they no longer if (username != null) {
// receive broadcasts or appear in users list userManager.removeUser(username);
}
} }
} }
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. for (ClientSession session : userManager.getAllSessions()) {
session.send(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.send(msg);
} }
}
case USER_LIST -> { case USER_LIST -> {
// TODO: Reply to the requester with the list of online users. ChatMessage userListMessage = new ChatMessage(
MessageType.USER_LIST,
"SERVER",
username,
userManager.listUsers()
);
send(userListMessage);
}
default -> {
} }
} }
} }
private void handleFileMessage(FileMessage fileMsg) throws IOException { private void handleFileMessage(FileMessage fileMsg) throws IOException {
// Storing the file var sentPath = FileManager.getSentPath(
var sentPath = FileManager.getSentPath(fileMsg.getSender(), fileMsg.getFilename()); fileMsg.getSender(),
var recvPath = FileManager.getReceivedPath(fileMsg.getReceiver(), fileMsg.getFilename()); fileMsg.getFilename()
);
var recvPath = FileManager.getReceivedPath(
fileMsg.getReceiver(),
fileMsg.getFilename()
);
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());
if (receiver != null) {
receiver.send(fileMsg);
}
}
public void send(ChatMessage msg) throws IOException {
out.writeObject(msg);
out.flush();
}
public void send(FileMessage msg) throws IOException {
out.writeObject(msg);
out.flush();
} }
} }