implement clientSession class
This commit is contained in:
@@ -9,8 +9,8 @@
|
|||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>25</maven.compiler.source>
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
<maven.compiler.target>25</maven.compiler.target>
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|||||||
@@ -2,18 +2,33 @@ 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 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.socket = socket;
|
||||||
|
this.userManager = userManager;
|
||||||
|
try {
|
||||||
|
this.out = new ObjectOutputStream(socket.getOutputStream());
|
||||||
|
this.in = new ObjectInputStream(socket.getInputStream());
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
System.out.println("Failed to create streams: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,28 +36,49 @@ public class ClientSession implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// TODO: Welcome the user (login step)
|
Object firstObject = in.readObject();
|
||||||
// 1. Read the first object sent by the client.
|
if (!(firstObject instanceof ChatMessage loginMsg) || loginMsg.getType() != MessageType.LOGIN) {
|
||||||
// 2. Check it's a ChatMessage with type LOGIN.
|
socket.close();
|
||||||
// 3. Extract the username.
|
return;
|
||||||
// 4. Try to register the user via userManager.addUser(...).
|
}
|
||||||
// 5. If the username is taken, send back LOGIN_FAILED and close the socket.
|
username = loginMsg.getSender();
|
||||||
// 6. Otherwise, create the user's folders with FileManager.createUserFolders(...)
|
|
||||||
// and send back LOGIN_SUCCESS.
|
|
||||||
|
|
||||||
// TODO: Main message loop
|
boolean registered = userManager.addUser(username,this);
|
||||||
// In a loop, call in.readObject(), you can separate messages by their type:
|
|
||||||
// - if it's a ChatMessage -> call handleChatMessage(msg)
|
if (!registered) {
|
||||||
// - if it's a FileMessage -> call handleFileMessage(fileMsg)
|
sendMessage(new ChatMessage(MessageType.LOGIN_FAILED, "Server", username, "Username already taken."));
|
||||||
// Keep looping until the connection is closed (an exception will be thrown).
|
socket.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FileManager.createUserFolders(username);
|
||||||
|
sendMessage(new ChatMessage(MessageType.LOGIN_SUCCESS, "Server", username, "Welcome " + username + "!"));
|
||||||
|
System.out.println(username + " logged in successfully.");
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
Object message = in.readObject();
|
||||||
|
if (message instanceof FileMessage msg) {
|
||||||
|
handleFileMessage(msg);
|
||||||
|
}
|
||||||
|
else if (message instanceof ChatMessage msg) {
|
||||||
|
handleChatMessage(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} 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);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
socket.close();
|
||||||
|
}catch (IOException e) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public synchronized void sendMessage(Object obj) throws IOException {
|
||||||
|
out.writeObject(obj);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void handleChatMessage(ChatMessage msg) throws IOException {
|
private void handleChatMessage(ChatMessage msg) throws IOException {
|
||||||
|
|||||||
Reference in New Issue
Block a user