Port the client to RACv2 beta (the server is still not functional)

This commit is contained in:
pixtaded 2025-02-08 20:34:47 +03:00
parent 7ba5bfbf9e
commit 74589d48b0
4 changed files with 38 additions and 29 deletions

View File

@ -92,18 +92,27 @@ public class CrabClient implements Crab {
break;
}
if (!message.isEmpty()) sendPacket(MESSAGE, this.nickname + message);
if (!message.isEmpty()) sendMessage(message);
}
}
private void sendPacket(byte PID, String argument) throws IOException {
connect();
String formattedMessage = String.valueOf((char) PID) + argument + "\n";
private void sendPacket(byte PID, String argument, boolean receiveResponse) throws IOException {
if (socket == null || socket.isClosed()) connect();
String formattedMessage = String.valueOf((char) PID) + argument;
out.print(formattedMessage);
out.flush();
receiveResponse(PID);
if (receiveResponse) receiveResponse(PID);
}
private void printLogs() {
clearScreen();
System.out.print(Sanitizer.sanitizeString(cache.content(), false));
}
private void sendMessage(String msg) throws IOException {
sendPacket(COMMUNICATION, this.nickname + msg, false);
closeConnection();
}
@ -112,14 +121,11 @@ public class CrabClient implements Crab {
case LOGS_SIZE -> {
char[] buffer = new char[10];
int response = in.read(buffer);
lastBufferLength = Integer.parseInt(new String(buffer).trim());
} case LOGS -> {
if (cache.sizeInBytes() != lastBufferLength) {
String convertedString = new String(buffer).trim();
if (!convertedString.isEmpty()) lastBufferLength = Integer.parseInt(convertedString);
} case COMMUNICATION -> {
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength);
cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8));
}
clearScreen();
System.out.print(Sanitizer.sanitizeString(cache.content(), false));
} default -> {
}
}
@ -136,8 +142,12 @@ public class CrabClient implements Crab {
}
private void getLogs() throws IOException {
sendPacket(LOGS_SIZE, "");
sendPacket(LOGS, "");
sendPacket(LOGS_SIZE, "", true);
if (this.cache.sizeInBytes() != lastBufferLength) {
sendPacket(COMMUNICATION, "", true);
}
closeConnection();
printLogs();
}
private void clearScreen() {

View File

@ -1,7 +1,6 @@
package net.pixtaded.crab.common;
public class PID {
public static final byte MESSAGE = 0x30;
public static final byte LOGS_SIZE = 0x31;
public static final byte LOGS = 0x32;
public static final byte LOGS_SIZE = 0x00;
public static final byte COMMUNICATION = 0x01;
}

View File

@ -10,7 +10,6 @@ import java.util.Scanner;
public class CrabServer implements Crab {
private ServerSocket serverSocket;
private Socket socket;
private boolean isStopped = false;
private int port;
private final Database db;
@ -72,7 +71,6 @@ public class CrabServer implements Crab {
public synchronized void stop() {
isStopped = true;
try {
if (socket != null) socket.close();
if (serverSocket != null) serverSocket.close();
} catch (IOException e) {
System.err.println("An error occured while closing the socket: " + e.getMessage());

View File

@ -38,9 +38,10 @@ public class ServerThread implements Runnable {
return;
}
switch (PID[0]) {
case MESSAGE -> {
Date date = new Date();
case COMMUNICATION -> {
String msg = new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim();
if (!msg.isEmpty()) {
Date date = new Date();
String address = socket.getInetAddress().getHostAddress();
String s = Sanitizer.sanitizeString(msg, true);
@ -48,9 +49,10 @@ public class ServerThread implements Runnable {
server.cache = new Logs(newContent.getBytes().length, newContent);
new Thread(new LogDBThread(date, address, msg)).start();
} case LOGS -> {
}
} /* case LOGS -> {
respond(server.cache.content());
} case LOGS_SIZE -> {
} */ case LOGS_SIZE -> {
respond(String.valueOf(server.cache.sizeInBytes()));
} default -> {
System.out.println("PID not implemented: " + PID[0]);