better multithreading
This commit is contained in:
parent
7c3f25dd2e
commit
6551cf5130
@ -8,6 +8,8 @@ import java.net.ServerSocket;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
public class CrabServer implements Crab {
|
public class CrabServer implements Crab {
|
||||||
|
|
||||||
@ -16,7 +18,9 @@ public class CrabServer implements Crab {
|
|||||||
private boolean isProxied = false;
|
private boolean isProxied = false;
|
||||||
private int port;
|
private int port;
|
||||||
private final Database db;
|
private final Database db;
|
||||||
public Logs cache = new Logs(0, "");
|
private Logs cache = new Logs(0, "");
|
||||||
|
public static final byte RAC_VERSION = 0x02;
|
||||||
|
public final ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||||
|
|
||||||
public CrabServer() {
|
public CrabServer() {
|
||||||
this.db = new Database("data.db");
|
this.db = new Database("data.db");
|
||||||
@ -80,12 +84,10 @@ public class CrabServer implements Crab {
|
|||||||
serverSocket = new ServerSocket(port);
|
serverSocket = new ServerSocket(port);
|
||||||
}
|
}
|
||||||
System.out.printf("Server successfully started! Listening on port %s.\nTo stop the server, type 'q'.\n", port);
|
System.out.printf("Server successfully started! Listening on port %s.\nTo stop the server, type 'q'.\n", port);
|
||||||
ServerCLI cli = new ServerCLI(scanner, this);
|
threadPool.submit(new ServerCLI(scanner, this));
|
||||||
new Thread(cli).start();
|
|
||||||
while (!isStopped) {
|
while (!isStopped) {
|
||||||
Socket socket = serverSocket.accept();
|
Socket socket = serverSocket.accept();
|
||||||
ServerThread thread = new ServerThread(socket, this);
|
threadPool.submit(new ServerThread(socket, this));
|
||||||
new Thread(thread).start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,6 +105,14 @@ public class CrabServer implements Crab {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized Logs getCache() {
|
||||||
|
return cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void setCache(Logs cache) {
|
||||||
|
this.cache = cache;
|
||||||
|
}
|
||||||
|
|
||||||
public Database getDb() {
|
public Database getDb() {
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ public class ServerThread implements Runnable {
|
|||||||
String address = socket.getInetAddress().getHostAddress();
|
String address = socket.getInetAddress().getHostAddress();
|
||||||
if (PID[0] == 'P') {
|
if (PID[0] == 'P') {
|
||||||
if (!this.server.isProxied()) {
|
if (!this.server.isProxied()) {
|
||||||
System.err.println(address + " tried to use PROXY despite it being turned off.");
|
System.err.println(address + " tried to use PROXY despite it being off.");
|
||||||
socket.close();
|
socket.close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -74,12 +74,11 @@ public class ServerThread implements Runnable {
|
|||||||
Date date = new Date();
|
Date date = new Date();
|
||||||
|
|
||||||
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.getCache().content() + Sanitizer.formatMessage(date.getTime(), address, s);
|
||||||
server.cache = new Logs(newContent.getBytes().length, newContent);
|
server.setCache(new Logs(newContent.getBytes().length, newContent));
|
||||||
|
server.threadPool.submit(new LogDBThread(date, address, msg));
|
||||||
new Thread(new LogDBThread(date, address, msg)).start();
|
|
||||||
} case LOGS_SIZE -> {
|
} case LOGS_SIZE -> {
|
||||||
respond(String.valueOf(server.cache.sizeInBytes()));
|
respond(String.valueOf(server.getCache().sizeInBytes()));
|
||||||
byte[] logPID = readPID();
|
byte[] logPID = readPID();
|
||||||
if (logPID.length == 0) {
|
if (logPID.length == 0) {
|
||||||
socket.close();
|
socket.close();
|
||||||
@ -99,28 +98,32 @@ public class ServerThread implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private byte[] readUntilChar(char c) throws IOException {
|
private byte[] readUntilChar(char c) throws IOException {
|
||||||
byte[] b = new byte[256];
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||||
int i;
|
int b;
|
||||||
for (i = 0;; i++) {
|
while ((b = input.read()) != -1) {
|
||||||
b[i] = (byte)input.read();
|
if (b == (byte) c) {
|
||||||
if (b[i] == c)
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
buffer.write(b);
|
||||||
}
|
}
|
||||||
byte[] r = new byte[i];
|
if (b == -1) {
|
||||||
System.arraycopy(b, 0, r, 0, i);
|
throw new EOFException("Delimiter not found before EOF");
|
||||||
return r;
|
}
|
||||||
|
return buffer.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendLogs(byte PID) throws IOException {
|
private void sendLogs(byte PID) throws IOException {
|
||||||
if (PID == LOGS) {
|
if (PID == LOGS) {
|
||||||
respond(server.cache.content());
|
respond(server.getCache().content());
|
||||||
} else if (PID == CACHED_LOGS) {
|
} else if (PID == CACHED_LOGS) {
|
||||||
String clientSize = Util.readAsciiNumber(in);
|
String clientSize = Util.readAsciiNumber(in);
|
||||||
int clientSizeNum = Integer.parseInt(clientSize);
|
int clientSizeNum = Integer.parseInt(clientSize);
|
||||||
byte[] serverLogs = server.cache.content().getBytes(StandardCharsets.UTF_8);
|
byte[] serverLogs = server.getCache().content().getBytes(StandardCharsets.UTF_8);
|
||||||
int logPartSize = serverLogs.length - clientSizeNum;
|
int logPartSize = Math.max(0, serverLogs.length - clientSizeNum);
|
||||||
byte[] logPart = new byte[logPartSize];
|
byte[] logPart = new byte[logPartSize];
|
||||||
System.arraycopy(serverLogs, serverLogs.length - logPartSize, logPart, 0, logPartSize);
|
if (logPartSize > 0) {
|
||||||
|
System.arraycopy(serverLogs, clientSizeNum, logPart, 0, logPartSize);
|
||||||
|
}
|
||||||
respond(logPart);
|
respond(logPart);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user