Develop #1

Open
ShayanEdalatjoo wants to merge 4 commits from develop into main
Showing only changes of commit 87a5ca499d - Show all commits
@@ -1,29 +1,108 @@
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.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
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.
final String SERVER_HOST = "localhost";
final int SERVER_PORT = 600;
Scanner input = new Scanner(System.in);
try {
Socket clientSocket = new Socket(SERVER_HOST, SERVER_PORT);
ObjectOutputStream output = new ObjectOutputStream(clientSocket.getOutputStream());
output.flush();
ObjectInputStream inputStream = new ObjectInputStream(clientSocket.getInputStream());
System.out.print("Username: ");
String username = input.nextLine();
output.writeObject(new ChatMessage(MessageType.LOGIN, username, null, null));
output.flush();
Thread serverThread = new Thread(new ServerListener(inputStream));
serverThread.start();
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());
String command = input.nextLine();
if (command.startsWith("/msg ")) {
String[] args = command.substring(5).split(" ", 2);
String recipient = args[0];
String message = args.length > 1 ? args[1] : "";
output.writeObject(new ChatMessage(
MessageType.PRIVATE_MESSAGE,
username,
recipient,
message));
output.flush();
continue;
}
if (command.equals("/users")) {
output.writeObject(new ChatMessage(
MessageType.USER_LIST,
username,
null,
null));
output.flush();
continue;
}
if (command.startsWith("/sendfile ")) {
String[] args = command.substring(10).split(" ", 2);
String recipient = args[0];
Path file = Paths.get(args[1]);
byte[] fileData = Files.readAllBytes(file);
String fileName = file.getFileName().toString();
output.writeObject(new FileMessage(
username,
recipient,
fileName,
fileData));
output.flush();
continue;
}
output.writeObject(new ChatMessage(
MessageType.PUBLIC_MESSAGE,
username,
null,
command));
output.flush();
} catch (Exception ex) {
System.out.println("Command failed: " + ex.getMessage());
}
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}