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.Crab;
|
||||||
import net.pixtaded.crab.common.Logs;
|
import net.pixtaded.crab.common.Logs;
|
||||||
import net.pixtaded.crab.common.Sanitizer;
|
import net.pixtaded.crab.common.Sanitizer;
|
||||||
|
import net.pixtaded.crab.common.Util;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
@ -119,13 +120,14 @@ public class CrabClient implements Crab {
|
|||||||
private void receiveResponse(byte PID) throws IOException {
|
private void receiveResponse(byte PID) throws IOException {
|
||||||
switch (PID) {
|
switch (PID) {
|
||||||
case LOGS_SIZE -> {
|
case LOGS_SIZE -> {
|
||||||
char[] buffer = new char[10];
|
String convertedString = Util.readAsciiNumber(in);
|
||||||
int response = in.read(buffer);
|
|
||||||
String convertedString = new String(buffer).trim();
|
|
||||||
if (!convertedString.isEmpty()) lastBufferLength = Integer.parseInt(convertedString);
|
if (!convertedString.isEmpty()) lastBufferLength = Integer.parseInt(convertedString);
|
||||||
} case LOGS -> {
|
} case LOGS -> {
|
||||||
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength - cache.sizeInBytes());
|
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength - cache.sizeInBytes());
|
||||||
cache = new Logs(lastBufferLength, cache.content() + new String(bytes, StandardCharsets.UTF_8));
|
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 -> {
|
} default -> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -143,8 +145,10 @@ public class CrabClient implements Crab {
|
|||||||
|
|
||||||
private void getLogs() throws IOException {
|
private void getLogs() throws IOException {
|
||||||
sendPacket(LOGS_SIZE, "", true);
|
sendPacket(LOGS_SIZE, "", true);
|
||||||
if (this.cache.sizeInBytes() != lastBufferLength) {
|
if (this.cache.sizeInBytes() < lastBufferLength) {
|
||||||
sendPacket(LOGS, String.valueOf(cache.sizeInBytes()), true);
|
sendPacket(LOGS, String.valueOf(cache.sizeInBytes()), true);
|
||||||
|
} else if (this.cache.sizeInBytes() != lastBufferLength) {
|
||||||
|
sendPacket(COMMUNICATION, "", true);
|
||||||
}
|
}
|
||||||
closeConnection();
|
closeConnection();
|
||||||
printLogs();
|
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.Logs;
|
||||||
import net.pixtaded.crab.common.Sanitizer;
|
import net.pixtaded.crab.common.Sanitizer;
|
||||||
|
import net.pixtaded.crab.common.Util;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
@ -12,13 +13,12 @@ import static net.pixtaded.crab.common.PID.*;
|
|||||||
|
|
||||||
public class ServerThread implements Runnable {
|
public class ServerThread implements Runnable {
|
||||||
|
|
||||||
private Socket socket;
|
private final Socket socket;
|
||||||
private PrintWriter out;
|
private PrintWriter out;
|
||||||
private BufferedReader in;
|
private final BufferedReader in;
|
||||||
private OutputStream output;
|
private OutputStream output;
|
||||||
private InputStream input;
|
private final InputStream input;
|
||||||
private byte PID;
|
private final CrabServer server;
|
||||||
private CrabServer server;
|
|
||||||
|
|
||||||
public ServerThread(Socket socket, CrabServer server) throws IOException {
|
public ServerThread(Socket socket, CrabServer server) throws IOException {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
@ -50,11 +50,13 @@ public class ServerThread implements Runnable {
|
|||||||
new Thread(new LogDBThread(date, address, msg)).start();
|
new Thread(new LogDBThread(date, address, msg)).start();
|
||||||
} case LOGS_SIZE -> {
|
} case LOGS_SIZE -> {
|
||||||
respond(String.valueOf(server.cache.sizeInBytes()));
|
respond(String.valueOf(server.cache.sizeInBytes()));
|
||||||
readPID();
|
byte[] logPID = readPID();
|
||||||
sendLogs();
|
if (logPID.length == 0) {
|
||||||
} default -> {
|
socket.close();
|
||||||
System.out.println("PID not implemented: " + PID[0]);
|
return;
|
||||||
}
|
}
|
||||||
|
sendLogs(logPID[0]);
|
||||||
|
} default -> System.out.println("PID not implemented: " + PID[0]);
|
||||||
}
|
}
|
||||||
socket.close();
|
socket.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -66,8 +68,17 @@ public class ServerThread implements Runnable {
|
|||||||
return input.readNBytes(1);
|
return input.readNBytes(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendLogs() throws IOException {
|
private void sendLogs(byte PID) throws IOException {
|
||||||
respond(server.cache.content());
|
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 {
|
private void respond(byte[] data) throws IOException {
|
||||||
|
Loading…
Reference in New Issue
Block a user