Develop #1
@@ -2,18 +2,28 @@ 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.IOException;
|
||||
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) {
|
||||
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
|
||||
@@ -29,17 +39,49 @@ public class ClientSession implements Runnable {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,12 +90,21 @@ 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.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,5 +118,8 @@ public class ClientSession implements Runnable {
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user