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