Port the client to RACv2 beta (the server is still not functional)
This commit is contained in:
parent
7ba5bfbf9e
commit
74589d48b0
@ -92,18 +92,27 @@ public class CrabClient implements Crab {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!message.isEmpty()) sendPacket(MESSAGE, this.nickname + message);
|
if (!message.isEmpty()) sendMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendPacket(byte PID, String argument) throws IOException {
|
private void sendPacket(byte PID, String argument, boolean receiveResponse) throws IOException {
|
||||||
connect();
|
if (socket == null || socket.isClosed()) connect();
|
||||||
String formattedMessage = String.valueOf((char) PID) + argument + "\n";
|
String formattedMessage = String.valueOf((char) PID) + argument;
|
||||||
|
|
||||||
out.print(formattedMessage);
|
out.print(formattedMessage);
|
||||||
out.flush();
|
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();
|
closeConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,14 +121,11 @@ public class CrabClient implements Crab {
|
|||||||
case LOGS_SIZE -> {
|
case LOGS_SIZE -> {
|
||||||
char[] buffer = new char[10];
|
char[] buffer = new char[10];
|
||||||
int response = in.read(buffer);
|
int response = in.read(buffer);
|
||||||
lastBufferLength = Integer.parseInt(new String(buffer).trim());
|
String convertedString = new String(buffer).trim();
|
||||||
} case LOGS -> {
|
if (!convertedString.isEmpty()) lastBufferLength = Integer.parseInt(convertedString);
|
||||||
if (cache.sizeInBytes() != lastBufferLength) {
|
} case COMMUNICATION -> {
|
||||||
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength);
|
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength);
|
||||||
cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8));
|
cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8));
|
||||||
}
|
|
||||||
clearScreen();
|
|
||||||
System.out.print(Sanitizer.sanitizeString(cache.content(), false));
|
|
||||||
} default -> {
|
} default -> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -136,8 +142,12 @@ public class CrabClient implements Crab {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void getLogs() throws IOException {
|
private void getLogs() throws IOException {
|
||||||
sendPacket(LOGS_SIZE, "");
|
sendPacket(LOGS_SIZE, "", true);
|
||||||
sendPacket(LOGS, "");
|
if (this.cache.sizeInBytes() != lastBufferLength) {
|
||||||
|
sendPacket(COMMUNICATION, "", true);
|
||||||
|
}
|
||||||
|
closeConnection();
|
||||||
|
printLogs();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearScreen() {
|
private void clearScreen() {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package net.pixtaded.crab.common;
|
package net.pixtaded.crab.common;
|
||||||
|
|
||||||
public class PID {
|
public class PID {
|
||||||
public static final byte MESSAGE = 0x30;
|
public static final byte LOGS_SIZE = 0x00;
|
||||||
public static final byte LOGS_SIZE = 0x31;
|
public static final byte COMMUNICATION = 0x01;
|
||||||
public static final byte LOGS = 0x32;
|
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ import java.util.Scanner;
|
|||||||
public class CrabServer implements Crab {
|
public class CrabServer implements Crab {
|
||||||
|
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
private Socket socket;
|
|
||||||
private boolean isStopped = false;
|
private boolean isStopped = false;
|
||||||
private int port;
|
private int port;
|
||||||
private final Database db;
|
private final Database db;
|
||||||
@ -72,7 +71,6 @@ public class CrabServer implements Crab {
|
|||||||
public synchronized void stop() {
|
public synchronized void stop() {
|
||||||
isStopped = true;
|
isStopped = true;
|
||||||
try {
|
try {
|
||||||
if (socket != null) socket.close();
|
|
||||||
if (serverSocket != null) serverSocket.close();
|
if (serverSocket != null) serverSocket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("An error occured while closing the socket: " + e.getMessage());
|
System.err.println("An error occured while closing the socket: " + e.getMessage());
|
||||||
|
@ -38,19 +38,21 @@ public class ServerThread implements Runnable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (PID[0]) {
|
switch (PID[0]) {
|
||||||
case MESSAGE -> {
|
case COMMUNICATION -> {
|
||||||
Date date = new Date();
|
|
||||||
String msg = new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim();
|
String msg = new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim();
|
||||||
String address = socket.getInetAddress().getHostAddress();
|
if (!msg.isEmpty()) {
|
||||||
|
Date date = new Date();
|
||||||
|
String address = socket.getInetAddress().getHostAddress();
|
||||||
|
|
||||||
String s = Sanitizer.sanitizeString(msg, true);
|
String s = Sanitizer.sanitizeString(msg, true);
|
||||||
String newContent = server.cache.content() + Sanitizer.formatMessage(date.getTime(), address, s);
|
String newContent = server.cache.content() + Sanitizer.formatMessage(date.getTime(), address, s);
|
||||||
server.cache = new Logs(newContent.getBytes().length, newContent);
|
server.cache = new Logs(newContent.getBytes().length, newContent);
|
||||||
|
|
||||||
new Thread(new LogDBThread(date, address, msg)).start();
|
new Thread(new LogDBThread(date, address, msg)).start();
|
||||||
} case LOGS -> {
|
}
|
||||||
|
} /* case LOGS -> {
|
||||||
respond(server.cache.content());
|
respond(server.cache.content());
|
||||||
} case LOGS_SIZE -> {
|
} */ case LOGS_SIZE -> {
|
||||||
respond(String.valueOf(server.cache.sizeInBytes()));
|
respond(String.valueOf(server.cache.sizeInBytes()));
|
||||||
} default -> {
|
} default -> {
|
||||||
System.out.println("PID not implemented: " + PID[0]);
|
System.out.println("PID not implemented: " + PID[0]);
|
||||||
|
Loading…
Reference in New Issue
Block a user