forked from AdvancedProgramming1404/HW-10-Socket-Programming
finish
This commit is contained in:
@@ -34,8 +34,8 @@ public class ClientSession implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Object obj = in.readObject() ;
|
||||
if (obj instanceof ChatMessage loginmsg && loginmsg.getType() == MessageType.LOGIN)
|
||||
Object firstobj = in.readObject() ;
|
||||
if (firstobj instanceof ChatMessage loginmsg && loginmsg.getType() == MessageType.LOGIN)
|
||||
{
|
||||
this.username = loginmsg.getSender() ;
|
||||
if (userManager.addUser(username , this))
|
||||
@@ -46,7 +46,30 @@ public class ClientSession implements Runnable {
|
||||
System.out.println("User logged in successfully: " + username);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
out.writeObject(new ChatMessage(MessageType.LOGIN_FAILED , "SERVER" , null, "User Name Already Taken !" ));
|
||||
|
||||
}
|
||||
}
|
||||
else {
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
Object obj = in.readObject() ;
|
||||
if (obj instanceof ChatMessage chatMessage)
|
||||
{
|
||||
handleChatMessage(chatMessage);
|
||||
}
|
||||
else if (obj instanceof FileMessage fileMessage)
|
||||
{
|
||||
handleFileMessage(fileMessage);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// TODO: Welcome the user (login step)
|
||||
// 1. Read the first object sent by the client.
|
||||
@@ -66,8 +89,10 @@ public class ClientSession implements Runnable {
|
||||
} 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,13 +101,29 @@ 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.sendMessage(msg);
|
||||
}
|
||||
}
|
||||
case PRIVATE_MESSAGE -> {
|
||||
// TODO: Forward this message to the receiver user.
|
||||
ClientSession receiverSession = userManager.getUser(msg.getReceiver()) ;
|
||||
if (receiverSession != null)
|
||||
{
|
||||
receiverSession.sendMessage(msg);
|
||||
}
|
||||
else {
|
||||
sendMessage(new ChatMessage(MessageType.PRIVATE_MESSAGE, "SERVER", username, "User " + msg.getReceiver() + " is offline."));
|
||||
}
|
||||
}
|
||||
case USER_LIST -> {
|
||||
// TODO: Reply to the requester with the list of online users.
|
||||
String userList = userManager.listUsers() ;
|
||||
sendMessage(new ChatMessage(MessageType.USER_LIST , "SERVER" , username ,userList ));
|
||||
|
||||
}
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +134,19 @@ public class ClientSession implements Runnable {
|
||||
|
||||
Files.write(sentPath, fileMsg.getData());
|
||||
Files.write(recvPath, fileMsg.getData());
|
||||
ClientSession receiverSession = userManager.getUser(fileMsg.getReceiver());
|
||||
if (receiverSession != null) {
|
||||
receiverSession.sendFile(fileMsg);
|
||||
}
|
||||
|
||||
// TODO: Forward the received file-message to the destination user.
|
||||
}
|
||||
public synchronized void sendMessage(ChatMessage msg) throws IOException {
|
||||
out.writeObject(msg);
|
||||
out.flush();
|
||||
}
|
||||
public synchronized void sendFile(FileMessage fileMsg) throws IOException {
|
||||
out.writeObject(fileMsg);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user