From 57d5f5ea1ec5c04730b9b4864fbbde1927453cef Mon Sep 17 00:00:00 2001 From: pixtaded Date: Sun, 9 Feb 2025 15:32:22 +0300 Subject: [PATCH] Port the server to RACv1.99.2 and handle logs erasing in client --- .../net/pixtaded/crab/client/CrabClient.java | 12 ++++--- .../java/net/pixtaded/crab/common/Util.java | 12 +++++++ .../pixtaded/crab/server/ServerThread.java | 35 ++++++++++++------- 3 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 src/main/java/net/pixtaded/crab/common/Util.java diff --git a/src/main/java/net/pixtaded/crab/client/CrabClient.java b/src/main/java/net/pixtaded/crab/client/CrabClient.java index b00e932..00491b9 100644 --- a/src/main/java/net/pixtaded/crab/client/CrabClient.java +++ b/src/main/java/net/pixtaded/crab/client/CrabClient.java @@ -3,6 +3,7 @@ package net.pixtaded.crab.client; import net.pixtaded.crab.common.Crab; import net.pixtaded.crab.common.Logs; import net.pixtaded.crab.common.Sanitizer; +import net.pixtaded.crab.common.Util; import java.io.*; import java.net.InetSocketAddress; @@ -119,13 +120,14 @@ public class CrabClient implements Crab { private void receiveResponse(byte PID) throws IOException { switch (PID) { case LOGS_SIZE -> { - char[] buffer = new char[10]; - int response = in.read(buffer); - String convertedString = new String(buffer).trim(); + String convertedString = Util.readAsciiNumber(in); if (!convertedString.isEmpty()) lastBufferLength = Integer.parseInt(convertedString); } case LOGS -> { byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength - cache.sizeInBytes()); cache = new Logs(lastBufferLength, cache.content() + new String(bytes, StandardCharsets.UTF_8)); + } case COMMUNICATION -> { + byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength); + cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8)); } default -> { } } @@ -143,8 +145,10 @@ public class CrabClient implements Crab { private void getLogs() throws IOException { sendPacket(LOGS_SIZE, "", true); - if (this.cache.sizeInBytes() != lastBufferLength) { + if (this.cache.sizeInBytes() < lastBufferLength) { sendPacket(LOGS, String.valueOf(cache.sizeInBytes()), true); + } else if (this.cache.sizeInBytes() != lastBufferLength) { + sendPacket(COMMUNICATION, "", true); } closeConnection(); printLogs(); diff --git a/src/main/java/net/pixtaded/crab/common/Util.java b/src/main/java/net/pixtaded/crab/common/Util.java new file mode 100644 index 0000000..52583d8 --- /dev/null +++ b/src/main/java/net/pixtaded/crab/common/Util.java @@ -0,0 +1,12 @@ +package net.pixtaded.crab.common; + +import java.io.BufferedReader; +import java.io.IOException; + +public class Util { + public static String readAsciiNumber(BufferedReader in) throws IOException { + char[] buffer = new char[10]; + int response = in.read(buffer); + return new String(buffer).trim(); + } +} diff --git a/src/main/java/net/pixtaded/crab/server/ServerThread.java b/src/main/java/net/pixtaded/crab/server/ServerThread.java index ef0927c..40df329 100644 --- a/src/main/java/net/pixtaded/crab/server/ServerThread.java +++ b/src/main/java/net/pixtaded/crab/server/ServerThread.java @@ -2,6 +2,7 @@ package net.pixtaded.crab.server; import net.pixtaded.crab.common.Logs; import net.pixtaded.crab.common.Sanitizer; +import net.pixtaded.crab.common.Util; import java.io.*; import java.net.Socket; @@ -12,13 +13,12 @@ import static net.pixtaded.crab.common.PID.*; public class ServerThread implements Runnable { - private Socket socket; + private final Socket socket; private PrintWriter out; - private BufferedReader in; + private final BufferedReader in; private OutputStream output; - private InputStream input; - private byte PID; - private CrabServer server; + private final InputStream input; + private final CrabServer server; public ServerThread(Socket socket, CrabServer server) throws IOException { this.socket = socket; @@ -50,11 +50,13 @@ public class ServerThread implements Runnable { new Thread(new LogDBThread(date, address, msg)).start(); } case LOGS_SIZE -> { respond(String.valueOf(server.cache.sizeInBytes())); - readPID(); - sendLogs(); - } default -> { - System.out.println("PID not implemented: " + PID[0]); - } + byte[] logPID = readPID(); + if (logPID.length == 0) { + socket.close(); + return; + } + sendLogs(logPID[0]); + } default -> System.out.println("PID not implemented: " + PID[0]); } socket.close(); } catch (Exception e) { @@ -66,8 +68,17 @@ public class ServerThread implements Runnable { return input.readNBytes(1); } - private void sendLogs() throws IOException { - respond(server.cache.content()); + private void sendLogs(byte PID) throws IOException { + if (PID == COMMUNICATION) respond(server.cache.content()); + if (PID == LOGS) { + String clientSize = Util.readAsciiNumber(in); + int clientSizeNum = Integer.parseInt(clientSize); + byte[] serverLogs = server.cache.content().getBytes(StandardCharsets.UTF_8); + int logPartSize = serverLogs.length - clientSizeNum; + byte[] logPart = new byte[logPartSize]; + System.arraycopy(serverLogs, serverLogs.length - logPartSize, logPart, 0, logPartSize); + respond(logPart); + } } private void respond(byte[] data) throws IOException {