Add base files
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.university.chat.Client;
|
||||
|
||||
public class TransferProgress {
|
||||
|
||||
private final long total;
|
||||
private final long startTime;
|
||||
private static final int WIDTH = 30;
|
||||
|
||||
public TransferProgress(long total) {
|
||||
this.total = total;
|
||||
this.startTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void update(long current) {
|
||||
|
||||
double percent = (double) current / total;
|
||||
int filled = (int) (percent * WIDTH);
|
||||
|
||||
StringBuilder bar = new StringBuilder("[");
|
||||
for (int i = 0; i < WIDTH; i++) {
|
||||
bar.append(i < filled ? "█" : " ");
|
||||
}
|
||||
bar.append("]");
|
||||
|
||||
long elapsed = System.currentTimeMillis() - startTime;
|
||||
double speed = current / 1024.0 / 1024.0 / (elapsed / 1000.0 + 0.001);
|
||||
|
||||
System.out.printf("\r%s %3d%% | %.2f MB/s",
|
||||
bar,
|
||||
(int) (percent * 100),
|
||||
speed);
|
||||
|
||||
if (current >= total) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user