Develop #1
@@ -18,7 +18,7 @@ public class ServerListener implements Runnable{
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// TODO: In an infinite loop read objects from the server
|
||||
// In an infinite loop read objects from the server
|
||||
// - if it's a ChatMessage -> print "<sender>: <content>"
|
||||
// - if it's a FileMessage -> print that a file was received
|
||||
// (filename + sender), it's already
|
||||
@@ -34,7 +34,7 @@ public class ServerListener implements Runnable{
|
||||
}
|
||||
}
|
||||
} catch (Exception e){
|
||||
System.out.println("Disconnected from server");
|
||||
System.out.println("Disconnected from server" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.Scanner;
|
||||
|
||||
public class chatClient {
|
||||
public static void main() throws IOException {
|
||||
// TODO: Connecting to the server
|
||||
// Connecting to the server
|
||||
// 1. Create a socket and connect to the server
|
||||
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in)
|
||||
// from the socket's streams — output FIRST, then input.
|
||||
@@ -39,7 +39,7 @@ public class chatClient {
|
||||
|
||||
while (true){
|
||||
try {
|
||||
// TODO: Program loop — read a line from the console and act on it:
|
||||
//Program loop — read a line from the console and act on it:
|
||||
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
|
||||
// - "/users" -> build & send a USER_LIST request
|
||||
// - "/sendfile <user> <path>" -> read the file into a byte[]
|
||||
|
||||
@@ -4,21 +4,22 @@ import java.io.IOException;
|
||||
import java.net.*;
|
||||
|
||||
public class ChatServer {
|
||||
// TODO: declare a single shared UserManager instance (static final)
|
||||
// declare a single shared UserManager instance (static final)
|
||||
// This MUST be shared by all ClientSession threads so that
|
||||
// broadcasting and private messaging work correctly.
|
||||
|
||||
static final UserManager userManager = new UserManager();
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
// TODO: Create a ServerSocket
|
||||
// Create a ServerSocket
|
||||
ServerSocket serverSocket = new ServerSocket(5000);
|
||||
|
||||
// TODO: In an infinite loop:
|
||||
// In an infinite loop:
|
||||
// accept an incoming client connection
|
||||
// make a new thread running ClientSession for each user.
|
||||
while (true) {
|
||||
Socket socket = serverSocket.accept();
|
||||
System.out.println("New client accepted");
|
||||
ClientSession clientSession = new ClientSession(socket, userManager);
|
||||
Thread thread = new Thread(clientSession);
|
||||
thread.start();
|
||||
|
||||
@@ -17,7 +17,7 @@ public class ClientSession implements Runnable {
|
||||
ObjectInputStream inputStream = null;
|
||||
|
||||
public ClientSession(Socket socket, UserManager userManager) throws IOException {
|
||||
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
||||
// Create an ObjectOutputStream from socket.getOutputStream()
|
||||
// and an ObjectInputStream from socket.getInputStream().
|
||||
this.userManager = userManager;
|
||||
this.socket = socket;
|
||||
@@ -30,7 +30,7 @@ public class ClientSession implements Runnable {
|
||||
public void run() {
|
||||
try {
|
||||
|
||||
// TODO: Welcome the user (login step)
|
||||
// Welcome the user (login step)
|
||||
// 1. Read the first object sent by the client.
|
||||
// 2. Check it's a ChatMessage with type LOGIN.
|
||||
// 3. Extract the username.
|
||||
@@ -39,7 +39,6 @@ public class ClientSession implements Runnable {
|
||||
// 6. Otherwise, create the user's folders with FileManager.createUserFolders(...)
|
||||
// and send back LOGIN_SUCCESS.
|
||||
|
||||
System.out.println("Welcome");
|
||||
Object object = inputStream.readObject();
|
||||
|
||||
if(object instanceof ChatMessage) {
|
||||
@@ -50,18 +49,20 @@ public class ClientSession implements Runnable {
|
||||
|
||||
if (userManager.addUser(username, this)) {
|
||||
FileManager.createUserFolders(username);
|
||||
outputStream.writeObject(MessageType.LOGIN_SUCCESS);
|
||||
ChatMessage response = new ChatMessage(MessageType.LOGIN_SUCCESS, "Server", username, "Login successfully");
|
||||
outputStream.writeObject(response);
|
||||
outputStream.flush();
|
||||
}
|
||||
else {
|
||||
outputStream.writeObject(MessageType.LOGIN_FAILED);
|
||||
ChatMessage response = new ChatMessage(MessageType.LOGIN_FAILED, "Server", username, "Username already exists");
|
||||
outputStream.writeObject(response);
|
||||
outputStream.flush();
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Main message loop
|
||||
// Main message loop
|
||||
// In a loop, call in.readObject(), you can separate messages by their type:
|
||||
// - if it's a ChatMessage -> call handleChatMessage(msg)
|
||||
// - if it's a FileMessage -> call handleFileMessage(fileMsg)
|
||||
@@ -79,9 +80,9 @@ public class ClientSession implements Runnable {
|
||||
} catch (Exception e) {
|
||||
System.out.println("Disconnected: " + username);
|
||||
} finally {
|
||||
// TODO: Remove the user from UserManager so they no longer
|
||||
// Remove the user from UserManager so they no longer
|
||||
// receive broadcasts or appear in users list
|
||||
userManager.removeUser(username);
|
||||
if(!socket.isClosed()) userManager.removeUser(username);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,19 +90,19 @@ public class ClientSession implements Runnable {
|
||||
private void handleChatMessage(ChatMessage msg) throws IOException {
|
||||
switch (msg.getType()) {
|
||||
case PUBLIC_MESSAGE -> {
|
||||
// TODO: Broadcast this message to every connected client.
|
||||
// Broadcast this message to every connected client.
|
||||
for (ClientSession clientSession : userManager.getAllSessions()) {
|
||||
clientSession.outputStream.writeObject(msg);
|
||||
clientSession.outputStream.flush();
|
||||
}
|
||||
}
|
||||
case PRIVATE_MESSAGE -> {
|
||||
// TODO: Forward this message to the receiver user.
|
||||
// Forward this message to the receiver user.
|
||||
userManager.getUser(msg.getReceiver()).outputStream.writeObject(msg);
|
||||
userManager.getUser(msg.getReceiver()).outputStream.flush();
|
||||
}
|
||||
case USER_LIST -> {
|
||||
// TODO: Reply to the requester with the list of online users.
|
||||
// Reply to the requester with the list of online users.
|
||||
ChatMessage chatMessage = new ChatMessage(MessageType.USER_LIST, "UserManager", username, userManager.listUsers());
|
||||
outputStream.writeObject(chatMessage);
|
||||
outputStream.flush();
|
||||
@@ -117,7 +118,7 @@ public class ClientSession implements Runnable {
|
||||
Files.write(sentPath, fileMsg.getData());
|
||||
Files.write(recvPath, fileMsg.getData());
|
||||
|
||||
// TODO: Forward the received file-message to the destination user.
|
||||
// Forward the received file-message to the destination user.
|
||||
ClientSession recieverSession = userManager.getUser(fileMsg.getReceiver());
|
||||
recieverSession.outputStream.writeObject(fileMsg);
|
||||
recieverSession.outputStream.flush();
|
||||
|
||||
Reference in New Issue
Block a user