Merge pull request 'Develop' (#1) from develop into main
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
@@ -1,20 +1,40 @@
|
||||
package com.university.chat.Client;
|
||||
|
||||
public class ServerListener implements Runnable{
|
||||
// TODO: store the ObjectInputStream from the user socket
|
||||
// (this should be the same input stream the
|
||||
// chatClient created when connecting)
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Common.FileMessage;
|
||||
|
||||
import java.io.ObjectInputStream;
|
||||
|
||||
public class ServerListener implements Runnable {
|
||||
|
||||
private ObjectInputStream in;
|
||||
|
||||
public ServerListener(ObjectInputStream in) {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// TODO: 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
|
||||
// saved to disk by the server.
|
||||
} catch (Exception e){
|
||||
while (true) {
|
||||
Object object = in.readObject();
|
||||
|
||||
if (object instanceof ChatMessage msg) {
|
||||
System.out.println(msg.getSender() + ": " + msg.getContent());
|
||||
}
|
||||
|
||||
if (object instanceof FileMessage fileMsg) {
|
||||
System.out.println(
|
||||
"File received: " +
|
||||
fileMsg.getFilename() +
|
||||
" from " +
|
||||
fileMsg.getSender()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Disconnected from server");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,106 @@
|
||||
package com.university.chat.Client;
|
||||
|
||||
public class chatClient {
|
||||
public static void main() {
|
||||
// TODO: 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.
|
||||
// 2. Get the username, and send a LOGIN ChatMessage with that username
|
||||
// 3. Start a new Thread running a ServerListener(in) so incoming
|
||||
// messages are handled concurrently.
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Common.FileMessage;
|
||||
import com.university.chat.Common.MessageType;
|
||||
|
||||
while (true){
|
||||
try {
|
||||
// TODO: 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[]
|
||||
// (you can use TransferProgress
|
||||
// to show progress)
|
||||
// and send it as a FileMessage
|
||||
// - anything else -> send a PUBLIC_MESSAGE
|
||||
// Remember to flush() the output stream after writeObject().
|
||||
} catch (Exception e){
|
||||
System.out.println("command failed: " + e.getMessage());
|
||||
import java.io.File;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class chatClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Socket socket = new Socket("localhost", 5000);
|
||||
|
||||
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
System.out.print("Enter username: ");
|
||||
String username = scanner.nextLine();
|
||||
|
||||
ChatMessage loginMessage = new ChatMessage(
|
||||
MessageType.LOGIN,
|
||||
username,
|
||||
null,
|
||||
""
|
||||
);
|
||||
|
||||
out.writeObject(loginMessage);
|
||||
out.flush();
|
||||
|
||||
new Thread(new ServerListener(in)).start();
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
String line = scanner.nextLine();
|
||||
|
||||
if (line.startsWith("/msg ")) {
|
||||
String[] parts = line.split(" ", 3);
|
||||
|
||||
ChatMessage privateMessage = new ChatMessage(
|
||||
MessageType.PRIVATE_MESSAGE,
|
||||
username,
|
||||
parts[1],
|
||||
parts[2]
|
||||
);
|
||||
|
||||
out.writeObject(privateMessage);
|
||||
}
|
||||
|
||||
else if (line.equals("/users")) {
|
||||
ChatMessage userListMessage = new ChatMessage(
|
||||
MessageType.USER_LIST,
|
||||
username,
|
||||
null,
|
||||
""
|
||||
);
|
||||
|
||||
out.writeObject(userListMessage);
|
||||
}
|
||||
|
||||
else if (line.startsWith("/sendfile ")) {
|
||||
String[] parts = line.split(" ", 3);
|
||||
|
||||
File file = new File(parts[2]);
|
||||
byte[] data = Files.readAllBytes(file.toPath());
|
||||
|
||||
FileMessage fileMessage = new FileMessage(
|
||||
username,
|
||||
parts[1],
|
||||
file.getName(),
|
||||
data
|
||||
);
|
||||
|
||||
out.writeObject(fileMessage);
|
||||
}
|
||||
|
||||
else {
|
||||
ChatMessage publicMessage = new ChatMessage(
|
||||
MessageType.PUBLIC_MESSAGE,
|
||||
username,
|
||||
null,
|
||||
line
|
||||
);
|
||||
|
||||
out.writeObject(publicMessage);
|
||||
}
|
||||
|
||||
out.flush();
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("command failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Could not connect to server");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,24 @@
|
||||
package com.university.chat.Server;
|
||||
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ChatServer {
|
||||
// TODO: declare a single shared UserManager instance (static final)
|
||||
// This MUST be shared by all ClientSession threads so that
|
||||
// broadcasting and private messaging work correctly.
|
||||
|
||||
private static final UserManager userManager = new UserManager();
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO: Create a ServerSocket
|
||||
try {
|
||||
ServerSocket serverSocket = new ServerSocket(5000);
|
||||
|
||||
// TODO: In an infinite loop:
|
||||
// accept an incoming client connection
|
||||
// make a new thread running ClientSession for each user.
|
||||
while (true) {
|
||||
Socket socket = serverSocket.accept();
|
||||
ClientSession session = new ClientSession(socket, userManager);
|
||||
new Thread(session).start();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,70 +2,160 @@ package com.university.chat.Server;
|
||||
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Common.FileMessage;
|
||||
import com.university.chat.Common.MessageType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class ClientSession implements Runnable {
|
||||
|
||||
private String username;
|
||||
private Socket socket;
|
||||
private UserManager userManager;
|
||||
private ObjectInputStream in;
|
||||
private ObjectOutputStream out;
|
||||
|
||||
public ClientSession(Socket socket, UserManager userManager) {
|
||||
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
||||
// and an ObjectInputStream from socket.getInputStream().
|
||||
try {
|
||||
this.socket = socket;
|
||||
this.userManager = userManager;
|
||||
|
||||
out = new ObjectOutputStream(socket.getOutputStream());
|
||||
in = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Object firstObject = in.readObject();
|
||||
|
||||
// TODO: 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.
|
||||
// 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.
|
||||
if (!(firstObject instanceof ChatMessage loginMessage)) {
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: 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)
|
||||
// Keep looping until the connection is closed (an exception will be thrown).
|
||||
if (loginMessage.getType() != MessageType.LOGIN) {
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
username = loginMessage.getSender();
|
||||
|
||||
boolean added = userManager.addUser(username, this);
|
||||
|
||||
if (!added) {
|
||||
ChatMessage failedMessage = new ChatMessage(
|
||||
MessageType.LOGIN_FAILED,
|
||||
"SERVER",
|
||||
username,
|
||||
"Username already exists"
|
||||
);
|
||||
|
||||
send(failedMessage);
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
FileManager.createUserFolders(username);
|
||||
|
||||
ChatMessage successMessage = new ChatMessage(
|
||||
MessageType.LOGIN_SUCCESS,
|
||||
"SERVER",
|
||||
username,
|
||||
"Login successful"
|
||||
);
|
||||
|
||||
send(successMessage);
|
||||
|
||||
while (true) {
|
||||
Object object = in.readObject();
|
||||
|
||||
if (object instanceof ChatMessage msg) {
|
||||
handleChatMessage(msg);
|
||||
}
|
||||
|
||||
if (object instanceof FileMessage fileMsg) {
|
||||
handleFileMessage(fileMsg);
|
||||
}
|
||||
}
|
||||
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void handleChatMessage(ChatMessage msg) throws IOException {
|
||||
switch (msg.getType()) {
|
||||
|
||||
case PUBLIC_MESSAGE -> {
|
||||
// TODO: Broadcast this message to every connected client.
|
||||
for (ClientSession session : userManager.getAllSessions()) {
|
||||
session.send(msg);
|
||||
}
|
||||
}
|
||||
|
||||
case PRIVATE_MESSAGE -> {
|
||||
// TODO: Forward this message to the receiver user.
|
||||
ClientSession receiver = userManager.getUser(msg.getReceiver());
|
||||
|
||||
if (receiver != null) {
|
||||
receiver.send(msg);
|
||||
}
|
||||
}
|
||||
|
||||
case USER_LIST -> {
|
||||
// TODO: Reply to the requester with the list of online users.
|
||||
ChatMessage userListMessage = new ChatMessage(
|
||||
MessageType.USER_LIST,
|
||||
"SERVER",
|
||||
username,
|
||||
userManager.listUsers()
|
||||
);
|
||||
|
||||
send(userListMessage);
|
||||
}
|
||||
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleFileMessage(FileMessage fileMsg) throws IOException {
|
||||
// Storing the file
|
||||
var sentPath = FileManager.getSentPath(fileMsg.getSender(), fileMsg.getFilename());
|
||||
var recvPath = FileManager.getReceivedPath(fileMsg.getReceiver(), fileMsg.getFilename());
|
||||
var sentPath = FileManager.getSentPath(
|
||||
fileMsg.getSender(),
|
||||
fileMsg.getFilename()
|
||||
);
|
||||
|
||||
var recvPath = FileManager.getReceivedPath(
|
||||
fileMsg.getReceiver(),
|
||||
fileMsg.getFilename()
|
||||
);
|
||||
|
||||
Files.write(sentPath, fileMsg.getData());
|
||||
Files.write(recvPath, fileMsg.getData());
|
||||
|
||||
// TODO: Forward the received file-message to the destination user.
|
||||
ClientSession receiver = userManager.getUser(fileMsg.getReceiver());
|
||||
|
||||
if (receiver != null) {
|
||||
receiver.send(fileMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public void send(ChatMessage msg) throws IOException {
|
||||
out.writeObject(msg);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
public void send(FileMessage msg) throws IOException {
|
||||
out.writeObject(msg);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user