forked from AdvancedProgramming1404/HW-10-Socket-Programming
finish
This commit is contained in:
@@ -34,8 +34,8 @@ public class ClientSession implements Runnable {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Object obj = in.readObject() ;
|
Object firstobj = in.readObject() ;
|
||||||
if (obj instanceof ChatMessage loginmsg && loginmsg.getType() == MessageType.LOGIN)
|
if (firstobj instanceof ChatMessage loginmsg && loginmsg.getType() == MessageType.LOGIN)
|
||||||
{
|
{
|
||||||
this.username = loginmsg.getSender() ;
|
this.username = loginmsg.getSender() ;
|
||||||
if (userManager.addUser(username , this))
|
if (userManager.addUser(username , this))
|
||||||
@@ -46,7 +46,30 @@ public class ClientSession implements Runnable {
|
|||||||
System.out.println("User logged in successfully: " + username);
|
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)
|
// TODO: Welcome the user (login step)
|
||||||
// 1. Read the first object sent by the client.
|
// 1. Read the first object sent by the client.
|
||||||
@@ -66,8 +89,10 @@ public class ClientSession implements Runnable {
|
|||||||
} 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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,13 +101,29 @@ public class ClientSession implements Runnable {
|
|||||||
switch (msg.getType()) {
|
switch (msg.getType()) {
|
||||||
case PUBLIC_MESSAGE -> {
|
case PUBLIC_MESSAGE -> {
|
||||||
// TODO: Broadcast this message to every connected client.
|
// TODO: Broadcast this message to every connected client.
|
||||||
|
for (ClientSession clientSession : userManager.getAllSessions())
|
||||||
|
{
|
||||||
|
clientSession.sendMessage(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case PRIVATE_MESSAGE -> {
|
case PRIVATE_MESSAGE -> {
|
||||||
// TODO: Forward this message to the receiver user.
|
// 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 -> {
|
case USER_LIST -> {
|
||||||
// TODO: Reply to the requester with the list of online users.
|
// 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(sentPath, fileMsg.getData());
|
||||||
Files.write(recvPath, 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.
|
// 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