forked from AdvancedProgramming1404/HW-10-Socket-Programming
ClientSession completed.
This commit is contained in:
@@ -2,58 +2,161 @@ 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.*;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
|
import static java.lang.System.in;
|
||||||
|
import static java.lang.System.out;
|
||||||
|
|
||||||
public class ClientSession implements Runnable {
|
public class ClientSession implements Runnable {
|
||||||
|
private Socket socket;
|
||||||
|
|
||||||
|
public ClientSession(Socket socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
}
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
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().
|
try (ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream())) {
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
//Read the first object sent by the client
|
||||||
|
try (ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||||
|
ObjectInputStream in = new ObjectInputStream(socket.getInputStream())) {
|
||||||
|
|
||||||
// TODO: Welcome the user (login step)
|
out.flush();
|
||||||
// 1. Read the first object sent by the client.
|
Object firstObject = in.readObject();
|
||||||
// 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.
|
|
||||||
|
|
||||||
// TODO: Main message loop
|
if (!(firstObject instanceof ChatMessage)) {
|
||||||
// In a loop, call in.readObject(), you can separate messages by their type:
|
return;
|
||||||
// - 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).
|
|
||||||
|
|
||||||
|
ChatMessage message = (ChatMessage) firstObject;
|
||||||
|
|
||||||
|
//Check it's a ChatMessage with type LOGIN
|
||||||
|
if (message.getType() != MessageType.LOGIN) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Extract the username
|
||||||
|
username = message.getSender();
|
||||||
|
|
||||||
|
//Try to register the user via userManager.addUser(...)
|
||||||
|
boolean isRegistered = ChatServer.userManager.addUser(username, this);
|
||||||
|
|
||||||
|
//If the username is taken, send back LOGIN_FAILED and close the socket
|
||||||
|
if (!isRegistered) {
|
||||||
|
out.writeObject(new ChatMessage(MessageType.LOGIN_FAILED, "ClientSession", username, "Username is already taken."));
|
||||||
|
out.flush();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//create the user's folders with FileManager.createUserFolders(username)
|
||||||
|
FileManager.createUserFolders(username);
|
||||||
|
|
||||||
|
//send back LOGIN_SUCCESS.
|
||||||
|
out.writeObject(new ChatMessage(MessageType.LOGIN_SUCCESS, "ClientSession", username, "Username is registered successfully!"));
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
System.out.println(username + " has been successfully registered!");
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
Object receivedObject = in.readObject();
|
||||||
|
|
||||||
|
if (receivedObject instanceof ChatMessage) {
|
||||||
|
handleChatMessage((ChatMessage) receivedObject);
|
||||||
|
} else if (receivedObject instanceof FileMessage) {
|
||||||
|
handleFileMessage((FileMessage) receivedObject);
|
||||||
|
}
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Disconnected: " + username);
|
out.println("Disconnected: " + username);
|
||||||
} finally {
|
} finally {
|
||||||
// TODO: Remove the user from UserManager so they no longer
|
if (!username.equals("unknown")) {
|
||||||
// receive broadcasts or appear in users list
|
ChatServer.userManager.removeUser(username);
|
||||||
|
out.println(username + " removed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
socket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void handleChatMessage(ChatMessage msg) throws IOException {
|
private void handleChatMessage(ChatMessage msg) throws IOException {
|
||||||
switch (msg.getType()) {
|
switch (msg.getType()) {
|
||||||
|
|
||||||
|
//to Broadcast this message to every connected client.
|
||||||
case PUBLIC_MESSAGE -> {
|
case PUBLIC_MESSAGE -> {
|
||||||
// TODO: Broadcast this message to every connected client.
|
String sender = msg.getSender();
|
||||||
|
String content = msg.getContent();
|
||||||
|
|
||||||
|
for (ClientSession session : ChatServer.userManager.getAllSessions()) {
|
||||||
|
try {
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(session.socket.getOutputStream());
|
||||||
|
out.writeObject(session);
|
||||||
|
out.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("[PUBLIC] " + msg.getSender() + ": " + msg.getContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//to Forward this message to the receiver user.
|
||||||
case PRIVATE_MESSAGE -> {
|
case PRIVATE_MESSAGE -> {
|
||||||
// TODO: Forward this message to the receiver user.
|
String receiver = msg.getReceiver();
|
||||||
|
ClientSession receiverSession = ChatServer.userManager.getUser(receiver);
|
||||||
|
|
||||||
|
if (receiverSession != null) {
|
||||||
|
try {
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(receiverSession.socket.getOutputStream());
|
||||||
|
out.writeObject(receiverSession);
|
||||||
|
out.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ChatMessage errorMessage = new ChatMessage(MessageType.PRIVATE_MESSAGE, "Server", msg.getSender(), "User" + receiver + " not found.");
|
||||||
|
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(this.socket.getOutputStream());
|
||||||
|
out.writeObject(errorMessage);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//to Reply to the requester with the list of online users.
|
||||||
case USER_LIST -> {
|
case USER_LIST -> {
|
||||||
// TODO: Reply to the requester with the list of online users.
|
String onlineUsers = ChatServer.userManager.listUsers();
|
||||||
|
ChatMessage message = new ChatMessage(MessageType.USER_LIST, "Server", msg.getSender(), onlineUsers);
|
||||||
|
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(this.socket.getOutputStream());
|
||||||
|
out.writeObject(message);
|
||||||
|
out.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,5 +170,21 @@ public class ClientSession implements Runnable {
|
|||||||
Files.write(recvPath, fileMsg.getData());
|
Files.write(recvPath, fileMsg.getData());
|
||||||
|
|
||||||
// TODO: Forward the received file-message to the destination user.
|
// TODO: Forward the received file-message to the destination user.
|
||||||
|
String receiverName = fileMsg.getReceiver();
|
||||||
|
ClientSession receiverSession = ChatServer.userManager.getUser(receiverName);
|
||||||
|
|
||||||
|
if (receiverSession != null) {
|
||||||
|
try {
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(receiverSession.socket.getOutputStream());
|
||||||
|
out.writeObject(receiverSession);
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
System.out.println("[FILE] " + fileMsg.getFilename() + " from " + fileMsg.getSender() + " to " + receiverName + " sent.");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println("[FILE] user " + receiverName + " is not online. File saved on server but not sent.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user