Port the server to RACv1.99.2 and handle logs erasing in client
This commit is contained in:
parent
61ddfc86d1
commit
57d5f5ea1e
@ -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();
|
||||
|
12
src/main/java/net/pixtaded/crab/common/Util.java
Normal file
12
src/main/java/net/pixtaded/crab/common/Util.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user