develop
This commit is contained in:
@@ -2,18 +2,38 @@ 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.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static com.university.chat.Client.chatClient.userManager;
|
||||
|
||||
public class ClientSession implements Runnable {
|
||||
|
||||
private String username;
|
||||
private final Socket socket;
|
||||
private final UserManager userManager;
|
||||
|
||||
private ObjectInputStream in;
|
||||
private ObjectOutputStream out;
|
||||
|
||||
public ClientSession(Socket socket, UserManager userManager) {
|
||||
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
||||
// and an ObjectInputStream from socket.getInputStream().
|
||||
this.socket=socket;
|
||||
this.userManager=userManager;
|
||||
try {
|
||||
out = new ObjectOutputStream(socket.getOutputStream());
|
||||
out.flush();
|
||||
|
||||
in = new ObjectInputStream(socket.getInputStream());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -28,18 +48,65 @@ public class ClientSession implements Runnable {
|
||||
// 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.
|
||||
Object first = in.readObject();
|
||||
if (!(first instanceof ChatMessage msg)
|
||||
|| msg.getType() != MessageType.LOGIN) {
|
||||
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
username = msg.getSender();
|
||||
if (!userManager.addUser(username, this)) {
|
||||
|
||||
send(new ChatMessage(
|
||||
MessageType.LOGIN_FAILED,
|
||||
"SERVER",
|
||||
username,
|
||||
"Username already exists"
|
||||
));
|
||||
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
FileManager.createUserFolders(username);
|
||||
|
||||
send(new ChatMessage(
|
||||
MessageType.LOGIN_SUCCESS,
|
||||
"SERVER",
|
||||
username,
|
||||
"Login successful"
|
||||
));
|
||||
|
||||
// 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(true){
|
||||
|
||||
Object obj = in.readObject();
|
||||
|
||||
if(obj instanceof ChatMessage mesg){
|
||||
handleChatMessage(mesg);
|
||||
}
|
||||
|
||||
else if(obj instanceof FileMessage file){
|
||||
handleFileMessage(file);
|
||||
}
|
||||
}
|
||||
|
||||
} 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);
|
||||
}
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,12 +115,27 @@ public class ClientSession implements Runnable {
|
||||
switch (msg.getType()) {
|
||||
case PUBLIC_MESSAGE -> {
|
||||
// TODO: Broadcast this message to every connected client.
|
||||
for(ClientSession session :
|
||||
userManager.getAllSessions()){
|
||||
session.send(msg);
|
||||
}
|
||||
}
|
||||
case PRIVATE_MESSAGE -> {
|
||||
// TODO: Forward this message to the receiver user.
|
||||
ClientSession target =
|
||||
userManager.getUser(msg.getReceiver());
|
||||
|
||||
if(target != null){
|
||||
target.send(msg);
|
||||
}
|
||||
}
|
||||
case USER_LIST -> {
|
||||
// TODO: Reply to the requester with the list of online users.
|
||||
send(new ChatMessage(
|
||||
MessageType.USER_LIST,
|
||||
"SERVER",
|
||||
username,
|
||||
userManager.listUsers()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,5 +149,20 @@ public class ClientSession implements Runnable {
|
||||
Files.write(recvPath, fileMsg.getData());
|
||||
|
||||
// TODO: Forward the received file-message to the destination user.
|
||||
ClientSession target =
|
||||
userManager.getUser(
|
||||
fileMsg.getReceiver()
|
||||
);
|
||||
|
||||
if(target != null){
|
||||
target.send(fileMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void send(Object obj)
|
||||
throws IOException {
|
||||
|
||||
out.writeObject(obj);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user