125 lines
5.1 KiB
Java
125 lines
5.1 KiB
Java
package com.university.chat.Server;
|
|
|
|
import com.university.chat.Common.ChatMessage;
|
|
import com.university.chat.Common.FileMessage;
|
|
import com.university.chat.Common.MessageType;
|
|
|
|
import java.io.*;
|
|
import java.net.Socket;
|
|
import java.nio.file.Files;
|
|
|
|
public class ClientSession implements Runnable {
|
|
|
|
private String username;
|
|
UserManager userManager = null;
|
|
Socket socket = null;
|
|
ObjectOutputStream outputStream = null;
|
|
ObjectInputStream inputStream = null;
|
|
|
|
public ClientSession(Socket socket, UserManager userManager) throws IOException {
|
|
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
|
// and an ObjectInputStream from socket.getInputStream().
|
|
this.userManager = userManager;
|
|
this.socket = socket;
|
|
outputStream = new ObjectOutputStream(socket.getOutputStream());
|
|
outputStream.flush();
|
|
inputStream = new ObjectInputStream(socket.getInputStream());
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
|
|
// TODO: Welcome the user (login step)
|
|
// 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.
|
|
|
|
System.out.println("Welcome");
|
|
Object object = inputStream.readObject();
|
|
|
|
if(object instanceof ChatMessage) {
|
|
ChatMessage chatMessage = (ChatMessage) object;
|
|
|
|
if(chatMessage.getType().equals(MessageType.LOGIN)) {
|
|
username = chatMessage.getSender();
|
|
|
|
if (userManager.addUser(username, this)) {
|
|
FileManager.createUserFolders(username);
|
|
outputStream.writeObject(MessageType.LOGIN_SUCCESS);
|
|
outputStream.flush();
|
|
}
|
|
else {
|
|
outputStream.writeObject(MessageType.LOGIN_FAILED);
|
|
outputStream.flush();
|
|
socket.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO: Main message loop
|
|
// 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).
|
|
|
|
while (!socket.isClosed()) {
|
|
object = inputStream.readObject();
|
|
|
|
if(object instanceof ChatMessage)
|
|
handleChatMessage((ChatMessage) object);
|
|
else if(object instanceof FileMessage)
|
|
handleFileMessage((FileMessage) object);
|
|
}
|
|
|
|
} 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
|
|
userManager.removeUser(username);
|
|
}
|
|
}
|
|
|
|
|
|
private void handleChatMessage(ChatMessage msg) throws IOException {
|
|
switch (msg.getType()) {
|
|
case PUBLIC_MESSAGE -> {
|
|
// TODO: Broadcast this message to every connected client.
|
|
for (ClientSession clientSession : userManager.getAllSessions()) {
|
|
clientSession.outputStream.writeObject(msg);
|
|
clientSession.outputStream.flush();
|
|
}
|
|
}
|
|
case PRIVATE_MESSAGE -> {
|
|
// TODO: Forward this message to the receiver user.
|
|
userManager.getUser(msg.getReceiver()).outputStream.writeObject(msg);
|
|
userManager.getUser(msg.getReceiver()).outputStream.flush();
|
|
}
|
|
case USER_LIST -> {
|
|
// TODO: Reply to the requester with the list of online users.
|
|
ChatMessage chatMessage = new ChatMessage(MessageType.USER_LIST, "UserManager", username, userManager.listUsers());
|
|
outputStream.writeObject(chatMessage);
|
|
outputStream.flush();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void handleFileMessage(FileMessage fileMsg) throws IOException {
|
|
// Storing the file
|
|
var sentPath = FileManager.getSentPath(fileMsg.getSender(), fileMsg.getFilename());
|
|
var recvPath = FileManager.getReceivedPath(fileMsg.getReceiver(), fileMsg.getFilename());
|
|
|
|
Files.write(sentPath, fileMsg.getData());
|
|
Files.write(recvPath, fileMsg.getData());
|
|
|
|
// TODO: Forward the received file-message to the destination user.
|
|
ClientSession recieverSession = userManager.getUser(fileMsg.getReceiver());
|
|
recieverSession.outputStream.writeObject(fileMsg);
|
|
recieverSession.outputStream.flush();
|
|
}
|
|
} |