Implement message caching (Only send LOGS packet to server when the LOGS_SIZE returned by server is different from cached one)
This commit is contained in:
parent
bdf7e6bbae
commit
57b20f69cd
@ -18,6 +18,7 @@ public class CrabClient implements Crab {
|
||||
private BufferedReader in;
|
||||
private int lastBufferLength = 0;
|
||||
private String nickname;
|
||||
private Logs cache = new Logs(0, "");
|
||||
|
||||
public CrabClient() {
|
||||
this.nickname = "";
|
||||
@ -111,9 +112,12 @@ public class CrabClient implements Crab {
|
||||
int response = in.read(buffer);
|
||||
lastBufferLength = Integer.parseInt(new String(buffer).trim());
|
||||
} case LOGS -> {
|
||||
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength);
|
||||
System.out.print("\033[H\033[2J");
|
||||
System.out.print(Sanitizer.sanitizeString(new String(bytes, StandardCharsets.UTF_8), false));
|
||||
if (cache.sizeInBytes() != lastBufferLength) {
|
||||
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 -> {
|
||||
}
|
||||
}
|
||||
@ -135,4 +139,9 @@ public class CrabClient implements Crab {
|
||||
sendPacket(LOGS_SIZE, "");
|
||||
sendPacket(LOGS, "");
|
||||
}
|
||||
|
||||
private void clearScreen() {
|
||||
System.out.print("\033[H\033[2J");
|
||||
}
|
||||
|
||||
}
|
||||
|
4
src/main/java/net/pixtaded/crab/client/Logs.java
Normal file
4
src/main/java/net/pixtaded/crab/client/Logs.java
Normal file
@ -0,0 +1,4 @@
|
||||
package net.pixtaded.crab.client;
|
||||
|
||||
public record Logs(int sizeInBytes, String content) {
|
||||
}
|
@ -12,7 +12,7 @@ public class CrabServer implements Crab {
|
||||
private Socket socket;
|
||||
private boolean isStopped = false;
|
||||
private int port;
|
||||
private Database db;
|
||||
private final Database db;
|
||||
|
||||
public CrabServer() {
|
||||
this.db = new Database("data.db");
|
||||
|
Loading…
Reference in New Issue
Block a user