forked from AdvancedProgramming1404/HW-10-Socket-Programming
Develop #1
@@ -2,44 +2,80 @@ 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 final UserManager userManager;
|
||||||
|
private final Socket socket;
|
||||||
|
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()
|
this.socket = socket;
|
||||||
// and an ObjectInputStream from socket.getInputStream().
|
this.userManager = userManager;
|
||||||
|
try {
|
||||||
|
out = new ObjectOutputStream(socket.getOutputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
in = new ObjectInputStream(socket.getInputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
System.out.println("Welcome");
|
||||||
|
boolean isLogedIn = false;
|
||||||
|
Object firstObject = in.readObject();
|
||||||
|
if(firstObject instanceof ChatMessage){
|
||||||
|
isLogedIn = ((ChatMessage)firstObject).getType() == MessageType.LOGIN;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Welcome the user (login step)
|
if(!isLogedIn){
|
||||||
// 1. Read the first object sent by the client.
|
socket.close();
|
||||||
// 2. Check it's a ChatMessage with type LOGIN.
|
return;
|
||||||
// 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
|
username = ((ChatMessage) firstObject).getSender();
|
||||||
// In a loop, call in.readObject(), you can separate messages by their type:
|
if(userManager.addUser(username, this)){
|
||||||
// - if it's a ChatMessage -> call handleChatMessage(msg)
|
System.out.println(username + " user name added.");
|
||||||
// - if it's a FileMessage -> call handleFileMessage(fileMsg)
|
FileManager.createUserFolders(username);
|
||||||
// Keep looping until the connection is closed (an exception will be thrown).
|
out.writeObject(MessageType.LOGIN_SUCCESS);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
System.out.println("try another username.");
|
||||||
|
out.writeObject(MessageType.LOGIN_FAILED);
|
||||||
|
out.flush();
|
||||||
|
socket.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(true){
|
||||||
|
Object obj = in.readObject();
|
||||||
|
if(obj instanceof ChatMessage)
|
||||||
|
handleChatMessage((ChatMessage) obj);
|
||||||
|
else if(obj instanceof FileMessage)
|
||||||
|
handleFileMessage((FileMessage) obj);
|
||||||
|
}
|
||||||
|
|
||||||
} 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
|
userManager.removeUser(username);
|
||||||
// receive broadcasts or appear in users list
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,13 +83,32 @@ public class ClientSession implements Runnable {
|
|||||||
private void handleChatMessage(ChatMessage msg) throws IOException {
|
private void handleChatMessage(ChatMessage msg) throws IOException {
|
||||||
switch (msg.getType()) {
|
switch (msg.getType()) {
|
||||||
case PUBLIC_MESSAGE -> {
|
case PUBLIC_MESSAGE -> {
|
||||||
// TODO: Broadcast this message to every connected client.
|
Iterable<ClientSession> users = userManager.getAllSessions();
|
||||||
|
for(ClientSession user : users){
|
||||||
|
user.send(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case PRIVATE_MESSAGE -> {
|
case PRIVATE_MESSAGE -> {
|
||||||
// TODO: Forward this message to the receiver user.
|
ClientSession receiver = userManager.getUser(msg.getReceiver());
|
||||||
|
if(receiver != null){
|
||||||
|
receiver.send(msg);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
send(new ChatMessage(
|
||||||
|
MessageType.PRIVATE_MESSAGE,
|
||||||
|
"server",
|
||||||
|
username,
|
||||||
|
"User doesn't exist: " + msg.getReceiver()
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case USER_LIST -> {
|
case USER_LIST -> {
|
||||||
// TODO: Reply to the requester with the list of online users.
|
send(new ChatMessage(
|
||||||
|
MessageType.USER_LIST,
|
||||||
|
"server",
|
||||||
|
username,
|
||||||
|
userManager.listUsers()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,6 +121,11 @@ 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());
|
||||||
|
|
||||||
// TODO: Forward the received file-message to the destination user.
|
ClientSession receiver = userManager.getUser(fileMsg.getReceiver());
|
||||||
|
receiver.send(fileMsg);
|
||||||
|
}
|
||||||
|
private synchronized void send(Object obj) throws IOException{
|
||||||
|
out.writeObject(obj);
|
||||||
|
out.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user