Sharing chat System
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
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)
|
||||
|
||||
@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){
|
||||
System.out.println("Disconnected from server");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
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.
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.university.chat.Server;
|
||||
|
||||
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.
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO: Create a ServerSocket
|
||||
|
||||
// TODO: In an infinite loop:
|
||||
// accept an incoming client connection
|
||||
// make a new thread running ClientSession for each user.
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package com.university.chat.Server;
|
||||
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Common.FileMessage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class ClientSession implements Runnable {
|
||||
|
||||
private String username;
|
||||
|
||||
public ClientSession(Socket socket, UserManager userManager) {
|
||||
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
||||
// and an ObjectInputStream from socket.getInputStream().
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
|
||||
// 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.
|
||||
|
||||
// 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).
|
||||
|
||||
} 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void handleChatMessage(ChatMessage msg) throws IOException {
|
||||
switch (msg.getType()) {
|
||||
case PUBLIC_MESSAGE -> {
|
||||
// TODO: Broadcast this message to every connected client.
|
||||
}
|
||||
case PRIVATE_MESSAGE -> {
|
||||
// TODO: Forward this message to the receiver user.
|
||||
}
|
||||
case USER_LIST -> {
|
||||
// TODO: Reply to the requester with the list of online users.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
Files.write(sentPath, fileMsg.getData());
|
||||
Files.write(recvPath, fileMsg.getData());
|
||||
|
||||
// TODO: Forward the received file-message to the destination user.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.university.chat.client;
|
||||
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
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Object obj = in.readObject();
|
||||
|
||||
if (obj instanceof ChatMessage)
|
||||
{
|
||||
ChatMessage msg = (ChatMessage) obj;
|
||||
|
||||
String sender = msg.getSender();
|
||||
String content = msg.getContent();
|
||||
|
||||
System.out.println(sender + ": " + content);
|
||||
}
|
||||
|
||||
else if (obj instanceof FileMessage)
|
||||
{
|
||||
FileMessage fileMsg = (FileMessage) obj;
|
||||
|
||||
String sender = fileMsg.getSender();
|
||||
String fileName = fileMsg.getFilename();
|
||||
|
||||
System.out.println("File received from " + sender + ": " + fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {System.out.println("Disconnected from server");}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.university.chat.Client;
|
||||
package com.university.chat.client;
|
||||
|
||||
public class TransferProgress {
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.university.chat.client;
|
||||
import com.university.chat.common.ChatMessage;
|
||||
import com.university.chat.common.FileMessage;
|
||||
import com.university.chat.common.MessageType;
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class chatClient
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
Socket socket = new Socket("localhost", 12345);
|
||||
|
||||
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||
out.flush();
|
||||
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.print("Enter username: ");
|
||||
String username = scanner.nextLine();
|
||||
|
||||
ChatMessage loginMsg = new ChatMessage(MessageType.LOGIN, username, null, null);
|
||||
out.writeObject(loginMsg);
|
||||
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)
|
||||
{
|
||||
System.out.println("Usage: /msg <user> <message>");
|
||||
continue;
|
||||
}
|
||||
|
||||
String target = parts[1];
|
||||
String text = parts[2];
|
||||
|
||||
ChatMessage msg = new ChatMessage(
|
||||
MessageType.PRIVATE_MESSAGE,
|
||||
username,
|
||||
target,
|
||||
text
|
||||
);
|
||||
|
||||
out.writeObject(msg);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
else if (line.equals("/users"))
|
||||
{
|
||||
ChatMessage msg = new ChatMessage(
|
||||
MessageType.USER_LIST,
|
||||
username,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
out.writeObject(msg);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
else if (line.startsWith("/sendfile "))
|
||||
{
|
||||
String[] parts = line.split(" ", 3);
|
||||
if (parts.length < 3)
|
||||
{
|
||||
System.out.println("Usage: /sendfile <user> <filepath>");
|
||||
continue;
|
||||
}
|
||||
|
||||
String target = parts[1];
|
||||
String path = parts[2];
|
||||
|
||||
byte[] fileData = Files.readAllBytes(Paths.get(path));
|
||||
String fileName = Paths.get(path).getFileName().toString();
|
||||
|
||||
FileMessage fileMsg = new FileMessage(
|
||||
username,
|
||||
target,
|
||||
fileName,
|
||||
fileData
|
||||
);
|
||||
|
||||
out.writeObject(fileMsg);
|
||||
out.flush();
|
||||
|
||||
System.out.println("File sent: " + fileName);
|
||||
}
|
||||
|
||||
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());}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e) {System.out.println("Connection failed: " + e.getMessage());}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.university.chat.Common;
|
||||
package com.university.chat.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.university.chat.Common;
|
||||
package com.university.chat.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.university.chat.Common;
|
||||
package com.university.chat.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.university.chat.server;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ChatServer
|
||||
{
|
||||
public static final UserManager userManager = new UserManager();
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int port = 12345;
|
||||
|
||||
try
|
||||
{
|
||||
ServerSocket serverSocket = new ServerSocket(port);
|
||||
System.out.println("Server started on port " + port);
|
||||
|
||||
while (true)
|
||||
{
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
System.out.println("New client connected");
|
||||
|
||||
ClientSession session = new ClientSession(clientSocket, userManager);
|
||||
new Thread(session).start();
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e) {e.printStackTrace();}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
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.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class ClientSession implements Runnable
|
||||
{
|
||||
private Socket socket;
|
||||
private ObjectInputStream in;
|
||||
private ObjectOutputStream out;
|
||||
|
||||
private String username;
|
||||
private UserManager userManager;
|
||||
|
||||
public ClientSession(Socket socket, UserManager userManager)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.socket = socket;
|
||||
this.userManager = userManager;
|
||||
|
||||
this.out = new ObjectOutputStream(socket.getOutputStream());
|
||||
this.out.flush();
|
||||
this.in = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
}
|
||||
catch (IOException e) {System.out.println("Stream error: " + e.getMessage());}
|
||||
}
|
||||
|
||||
public void send(Object obj) throws IOException
|
||||
{
|
||||
out.writeObject(obj);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
ChatMessage loginMsg = (ChatMessage) in.readObject();
|
||||
|
||||
if (loginMsg.getType() != MessageType.LOGIN)
|
||||
{
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
this.username = loginMsg.getSender();
|
||||
|
||||
boolean ok = userManager.addUser(username, this);
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
send(new ChatMessage(
|
||||
MessageType.LOGIN_FAILED,
|
||||
"SERVER",
|
||||
username,
|
||||
"Username already taken"
|
||||
));
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
FileManager.createUserFolders(username);
|
||||
|
||||
send(new ChatMessage(
|
||||
MessageType.LOGIN_SUCCESS,
|
||||
"SERVER",
|
||||
username,
|
||||
"Welcome " + username
|
||||
));
|
||||
|
||||
while (true)
|
||||
{
|
||||
Object obj = in.readObject();
|
||||
|
||||
if (obj instanceof ChatMessage msg) {handleChat(msg);}
|
||||
|
||||
else if (obj instanceof FileMessage fileMsg) {handleFile(fileMsg);}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e) {System.out.println("Disconnected: " + username);}
|
||||
finally
|
||||
{
|
||||
if (username != null) {userManager.removeUser(username);}
|
||||
try { socket.close(); }
|
||||
catch (Exception ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleChat(ChatMessage msg) throws IOException
|
||||
{
|
||||
switch (msg.getType())
|
||||
{
|
||||
case PUBLIC_MESSAGE ->
|
||||
{
|
||||
for (ClientSession session : userManager.getAllSessions()) {session.send(msg);}
|
||||
}
|
||||
|
||||
case PRIVATE_MESSAGE ->
|
||||
{
|
||||
ClientSession target = userManager.getUser(msg.getReceiver());
|
||||
if (target != null) {target.send(msg);}
|
||||
}
|
||||
|
||||
case USER_LIST ->
|
||||
{
|
||||
send(new ChatMessage(
|
||||
MessageType.USER_LIST,
|
||||
"SERVER",
|
||||
username,
|
||||
userManager.listUsers()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleFile(FileMessage fileMsg) throws IOException
|
||||
{
|
||||
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());
|
||||
|
||||
ClientSession target = userManager.getUser(fileMsg.getReceiver());
|
||||
if (target != null) {target.send(fileMsg);}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.university.chat.Server;
|
||||
package com.university.chat.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.university.chat.Server;
|
||||
package com.university.chat.server;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
Reference in New Issue
Block a user