implement SIP

This commit is contained in:
pixtaded 2025-06-24 15:17:41 +03:00
parent 83cd1def66
commit 528d9c1c39
3 changed files with 37 additions and 5 deletions

View File

@ -84,16 +84,26 @@ public class CrabClient implements Crab {
private void communicate() throws IOException {
Scanner scanner = new Scanner(System.in);
String message;
boolean preserveScreen = false;
while (true) {
getLogs();
if (!preserveScreen) {
getLogs();
} else {
preserveScreen = false;
}
System.out.print("Enter a message (or type '/exit' to exit): ");
message = scanner.nextLine();
if (message.equalsIgnoreCase("/exit")) {
break;
} else if (message.equalsIgnoreCase("/info")) {
sendPacket(SERVER_INFO, "", true);
preserveScreen = true;
} else {
if (!message.isEmpty()) sendMessage(message);
}
if (!message.isEmpty()) sendMessage(message);
}
}
@ -128,7 +138,28 @@ public class CrabClient implements Crab {
} case LOGS -> {
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength);
cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8));
} default -> {
} case SERVER_INFO -> {
InputStream input = socket.getInputStream();
int versionByte = input.read();
if (versionByte == -1) {
System.out.println("The server did not provide any information...");
return;
}
String protocolVersion;
switch (versionByte) {
case 0x01 -> protocolVersion = "RACv1";
case 0x02 -> protocolVersion = "RACv1.99";
case 0x03 -> protocolVersion = "RACv2";
default -> protocolVersion = "Unknown RAC version";
}
byte[] nameBytes = input.readAllBytes();
String serverName = new String(nameBytes, StandardCharsets.UTF_8);
System.out.println("\n\033[36mServer Information:\033[0m");
System.out.println("\033[33mProtocol:\033[0m " + protocolVersion);
System.out.println("\033[33mName:\033[0m " + serverName + "\n");
}
}
}

View File

@ -7,4 +7,5 @@ public class PID {
public static final byte CACHED_LOGS = 0x02;
public static final byte AUTHENTICATED_MESSAGE = 0x02;
public static final byte REGISTER = 0x03;
public static final byte SERVER_INFO = 0x69;
}

View File

@ -20,7 +20,7 @@ public class CrabServer implements Crab {
private final Database db;
private Logs cache = new Logs(0, "");
public static final byte RAC_VERSION = 0x02;
public final ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public final ExecutorService threadPool = Executors.newCachedThreadPool();
public CrabServer() {
this.db = new Database("data.db");