Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
05a22f0d39 |
@@ -1,19 +1,35 @@
|
|||||||
package com.university.chat.Client;
|
package com.university.chat.Client;
|
||||||
|
|
||||||
public class ServerListener implements Runnable{
|
import com.university.chat.Common.ChatMessage;
|
||||||
// TODO: store the ObjectInputStream from the user socket
|
import com.university.chat.Common.FileMessage;
|
||||||
// (this should be the same input stream the
|
import java.io.ObjectInputStream;
|
||||||
// chatClient created when connecting)
|
|
||||||
|
public class ServerListener implements Runnable
|
||||||
|
{
|
||||||
|
private final ObjectInputStream in;
|
||||||
|
|
||||||
|
public ServerListener(ObjectInputStream in)
|
||||||
|
{
|
||||||
|
this.in = in;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
|
||||||
// TODO: In an infinite loop read objects from the server
|
try
|
||||||
// - if it's a ChatMessage -> print "<sender>: <content>"
|
{
|
||||||
// - if it's a FileMessage -> print that a file was received
|
while (true)
|
||||||
// (filename + sender), it's already
|
{
|
||||||
// saved to disk by the server.
|
Object obj = in.readObject();
|
||||||
} catch (Exception e){
|
|
||||||
|
if (obj instanceof ChatMessage msg)
|
||||||
|
System.out.println(msg.getSender() + ": " + msg.getContent());
|
||||||
|
else if (obj instanceof FileMessage fileMsg)
|
||||||
|
System.out.println("File received from " + fileMsg.getSender() + " : " + fileMsg.getFilename());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
System.out.println("Disconnected from server");
|
System.out.println("Disconnected from server");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +1,133 @@
|
|||||||
package com.university.chat.Client;
|
package com.university.chat.Client;
|
||||||
|
|
||||||
public class chatClient {
|
import com.university.chat.Common.ChatMessage;
|
||||||
public static void main() {
|
import com.university.chat.Common.FileMessage;
|
||||||
// TODO: Connecting to the server
|
import com.university.chat.Common.MessageType;
|
||||||
// 1. Create a socket and connect to the server
|
import java.io.File;
|
||||||
// 2. Create an ObjectOutputStream (out) and ObjectInputStream (in)
|
import java.io.ObjectInputStream;
|
||||||
// from the socket's streams — output FIRST, then input.
|
import java.io.ObjectOutputStream;
|
||||||
// 2. Get the username, and send a LOGIN ChatMessage with that username
|
import java.net.Socket;
|
||||||
// 3. Start a new Thread running a ServerListener(in) so incoming
|
import java.nio.file.Files;
|
||||||
// messages are handled concurrently.
|
import java.util.Scanner;
|
||||||
|
|
||||||
while (true){
|
|
||||||
try {
|
public class chatClient
|
||||||
// TODO: Program loop — read a line from the console and act on it:
|
{
|
||||||
// - "/msg <user> <text>" -> build & send a PRIVATE_MESSAGE
|
public static void main(String[] args)
|
||||||
// - "/users" -> build & send a USER_LIST request
|
{
|
||||||
// - "/sendfile <user> <path>" -> read the file into a byte[]
|
try
|
||||||
// (you can use TransferProgress
|
{
|
||||||
// to show progress)
|
Socket socket = new Socket("localhost", 5000);
|
||||||
// and send it as a FileMessage
|
|
||||||
// - anything else -> send a PUBLIC_MESSAGE
|
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||||
// Remember to flush() the output stream after writeObject().
|
|
||||||
} catch (Exception e){
|
out.flush();
|
||||||
|
|
||||||
|
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||||
|
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.print("Username: ");
|
||||||
|
String username = scanner.nextLine();
|
||||||
|
|
||||||
|
ChatMessage login = new ChatMessage(MessageType.LOGIN, username, null, "");
|
||||||
|
|
||||||
|
out.writeObject(login);
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
new Thread(new ServerListener(in)).start();
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
String line = scanner.nextLine();
|
||||||
|
|
||||||
|
if (line.startsWith("/msg "))
|
||||||
|
{
|
||||||
|
String[] parts = line.split(" ", 3);
|
||||||
|
|
||||||
|
if (parts.length < 3)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
ChatMessage msg =
|
||||||
|
new ChatMessage(
|
||||||
|
MessageType.PRIVATE_MESSAGE,
|
||||||
|
username,
|
||||||
|
parts[1],
|
||||||
|
parts[2]
|
||||||
|
);
|
||||||
|
|
||||||
|
out.writeObject(msg);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
else if (line.equals("/users"))
|
||||||
|
{
|
||||||
|
ChatMessage msg =
|
||||||
|
new ChatMessage(
|
||||||
|
MessageType.USER_LIST,
|
||||||
|
username,
|
||||||
|
null,
|
||||||
|
""
|
||||||
|
);
|
||||||
|
|
||||||
|
out.writeObject(msg);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
else if (line.startsWith("/sendfile "))
|
||||||
|
{
|
||||||
|
String[] parts = line.split(" ", 3);
|
||||||
|
|
||||||
|
if (parts.length < 3)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
String receiver = parts[1];
|
||||||
|
|
||||||
|
File file = new File(parts[2]);
|
||||||
|
|
||||||
|
byte[] data = Files.readAllBytes(file.toPath());
|
||||||
|
|
||||||
|
TransferProgress progress = new TransferProgress(data.length);
|
||||||
|
|
||||||
|
progress.update(data.length);
|
||||||
|
|
||||||
|
FileMessage fileMessage =
|
||||||
|
new FileMessage(
|
||||||
|
username,
|
||||||
|
receiver,
|
||||||
|
file.getName(),
|
||||||
|
data
|
||||||
|
);
|
||||||
|
|
||||||
|
out.writeObject(fileMessage);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ChatMessage msg =
|
||||||
|
new ChatMessage(
|
||||||
|
MessageType.PUBLIC_MESSAGE,
|
||||||
|
username,
|
||||||
|
null,
|
||||||
|
line
|
||||||
|
);
|
||||||
|
|
||||||
|
out.writeObject(msg);
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
System.out.println("command failed: " + e.getMessage());
|
System.out.println("command failed: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
// just for debugging
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,34 @@
|
|||||||
package com.university.chat.Server;
|
package com.university.chat.Server;
|
||||||
|
|
||||||
public class ChatServer {
|
import java.net.ServerSocket;
|
||||||
// TODO: declare a single shared UserManager instance (static final)
|
import java.net.Socket;
|
||||||
// This MUST be shared by all ClientSession threads so that
|
|
||||||
// broadcasting and private messaging work correctly.
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public class ChatServer
|
||||||
// TODO: Create a ServerSocket
|
{
|
||||||
|
private static final UserManager USER_MANAGER = new UserManager();
|
||||||
|
|
||||||
// TODO: In an infinite loop:
|
public static void main(String[] args)
|
||||||
// accept an incoming client connection
|
{
|
||||||
// make a new thread running ClientSession for each user.
|
int port = 5000;
|
||||||
|
|
||||||
|
try (ServerSocket serverSocket = new ServerSocket(port))
|
||||||
|
{
|
||||||
|
System.out.println("Server started on port " + port);
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
Socket socket = serverSocket.accept();
|
||||||
|
|
||||||
|
ClientSession session = new ClientSession(socket, USER_MANAGER);
|
||||||
|
|
||||||
|
new Thread(session).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
// just for debugging
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,53 +7,133 @@ import java.io.IOException;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
|
import com.university.chat.Common.MessageType;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
|
||||||
public class ClientSession implements Runnable {
|
public class ClientSession implements Runnable {
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
private final Socket socket;
|
||||||
|
private final UserManager userManager;
|
||||||
|
|
||||||
public ClientSession(Socket socket, UserManager userManager) {
|
private ObjectInputStream in;
|
||||||
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
private ObjectOutputStream out;
|
||||||
// and an ObjectInputStream from socket.getInputStream().
|
|
||||||
|
public ClientSession(Socket socket, UserManager userManager)
|
||||||
|
{
|
||||||
|
this.socket = socket;
|
||||||
|
this.userManager = userManager;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
out = new ObjectOutputStream(socket.getOutputStream());
|
||||||
|
out.flush();
|
||||||
|
in = new ObjectInputStream(socket.getInputStream());
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
|
Object firstObject = in.readObject();
|
||||||
|
|
||||||
// TODO: Welcome the user (login step)
|
if (!(firstObject instanceof ChatMessage loginMsg)
|
||||||
// 1. Read the first object sent by the client.
|
|| loginMsg.getType() != MessageType.LOGIN)
|
||||||
// 2. Check it's a ChatMessage with type LOGIN.
|
{
|
||||||
// 3. Extract the username.
|
socket.close();
|
||||||
// 4. Try to register the user via userManager.addUser(...).
|
return;
|
||||||
// 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 = loginMsg.getSender();
|
||||||
// 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).
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
if (!userManager.addUser(username, this))
|
||||||
|
{
|
||||||
|
sendObject(
|
||||||
|
new ChatMessage(
|
||||||
|
MessageType.LOGIN_FAILED,
|
||||||
|
"SERVER",
|
||||||
|
username,
|
||||||
|
"Username already exists"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
socket.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileManager.createUserFolders(username);
|
||||||
|
|
||||||
|
sendObject(
|
||||||
|
new ChatMessage(
|
||||||
|
MessageType.LOGIN_SUCCESS,
|
||||||
|
"SERVER",
|
||||||
|
username,
|
||||||
|
"Login successful"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
System.out.println(username + " connected");
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
Object obj = in.readObject();
|
||||||
|
|
||||||
|
if (obj instanceof ChatMessage chatMsg)
|
||||||
|
handleChatMessage(chatMsg);
|
||||||
|
else if (obj instanceof FileMessage fileMsg)
|
||||||
|
handleFileMessage(fileMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
System.out.println("Disconnected: " + username);
|
System.out.println("Disconnected: " + username);
|
||||||
} finally {
|
}
|
||||||
// TODO: Remove the user from UserManager so they no longer
|
finally
|
||||||
// receive broadcasts or appear in users list
|
{
|
||||||
|
if (username != null) {
|
||||||
|
userManager.removeUser(username);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void handleChatMessage(ChatMessage msg) throws IOException {
|
private void handleChatMessage(ChatMessage msg) throws IOException {
|
||||||
switch (msg.getType()) {
|
|
||||||
case PUBLIC_MESSAGE -> {
|
switch (msg.getType())
|
||||||
// TODO: Broadcast this message to every connected client.
|
{
|
||||||
|
case PUBLIC_MESSAGE ->
|
||||||
|
{
|
||||||
|
for (ClientSession session : userManager.getAllSessions())
|
||||||
|
session.sendObject(msg);
|
||||||
}
|
}
|
||||||
case PRIVATE_MESSAGE -> {
|
|
||||||
// TODO: Forward this message to the receiver user.
|
case PRIVATE_MESSAGE ->
|
||||||
|
{
|
||||||
|
ClientSession receiver = userManager.getUser(msg.getReceiver());
|
||||||
|
|
||||||
|
if (receiver != null)
|
||||||
|
receiver.sendObject(msg);
|
||||||
}
|
}
|
||||||
case USER_LIST -> {
|
|
||||||
// TODO: Reply to the requester with the list of online users.
|
case USER_LIST ->
|
||||||
|
{
|
||||||
|
ChatMessage response =
|
||||||
|
new ChatMessage(
|
||||||
|
MessageType.USER_LIST,
|
||||||
|
"SERVER",
|
||||||
|
username,
|
||||||
|
userManager.listUsers()
|
||||||
|
);
|
||||||
|
|
||||||
|
sendObject(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,6 +146,15 @@ 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());
|
||||||
|
|
||||||
|
if (receiver != null)
|
||||||
|
receiver.sendObject(fileMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void sendObject(Object obj) throws IOException
|
||||||
|
{
|
||||||
|
out.writeObject(obj);
|
||||||
|
out.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user