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.sql.SQLException;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class CrabServer implements Crab {
|
||||
|
||||
@ -16,7 +18,9 @@ public class CrabServer implements Crab {
|
||||
private boolean isProxied = false;
|
||||
private int port;
|
||||
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() {
|
||||
this.db = new Database("data.db");
|
||||
@ -80,12 +84,10 @@ public class CrabServer implements Crab {
|
||||
serverSocket = new ServerSocket(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);
|
||||
new Thread(cli).start();
|
||||
threadPool.submit(new ServerCLI(scanner, this));
|
||||
while (!isStopped) {
|
||||
Socket socket = serverSocket.accept();
|
||||
ServerThread thread = new ServerThread(socket, this);
|
||||
new Thread(thread).start();
|
||||
threadPool.submit(new ServerThread(socket, this));
|
||||
}
|
||||
}
|
||||
|
||||
@ -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() {
|
||||
return db;
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ public class ServerThread implements Runnable {
|
||||
String address = socket.getInetAddress().getHostAddress();
|
||||
if (PID[0] == 'P') {
|
||||
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();
|
||||
return;
|
||||
}
|
||||
@ -74,12 +74,11 @@ public class ServerThread implements Runnable {
|
||||
Date date = new Date();
|
||||
|
||||
String s = Sanitizer.sanitizeString(msg, true);
|
||||
String newContent = server.cache.content() + Sanitizer.formatMessage(date.getTime(), address, s);
|
||||
server.cache = new Logs(newContent.getBytes().length, newContent);
|
||||
|
||||
new Thread(new LogDBThread(date, address, msg)).start();
|
||||
String newContent = server.getCache().content() + Sanitizer.formatMessage(date.getTime(), address, s);
|
||||
server.setCache(new Logs(newContent.getBytes().length, newContent));
|
||||
server.threadPool.submit(new LogDBThread(date, address, msg));
|
||||
} case LOGS_SIZE -> {
|
||||
respond(String.valueOf(server.cache.sizeInBytes()));
|
||||
respond(String.valueOf(server.getCache().sizeInBytes()));
|
||||
byte[] logPID = readPID();
|
||||
if (logPID.length == 0) {
|
||||
socket.close();
|
||||
@ -99,28 +98,32 @@ public class ServerThread implements Runnable {
|
||||
}
|
||||
|
||||
private byte[] readUntilChar(char c) throws IOException {
|
||||
byte[] b = new byte[256];
|
||||
int i;
|
||||
for (i = 0;; i++) {
|
||||
b[i] = (byte)input.read();
|
||||
if (b[i] == c)
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
int b;
|
||||
while ((b = input.read()) != -1) {
|
||||
if (b == (byte) c) {
|
||||
break;
|
||||
}
|
||||
buffer.write(b);
|
||||
}
|
||||
byte[] r = new byte[i];
|
||||
System.arraycopy(b, 0, r, 0, i);
|
||||
return r;
|
||||
if (b == -1) {
|
||||
throw new EOFException("Delimiter not found before EOF");
|
||||
}
|
||||
return buffer.toByteArray();
|
||||
}
|
||||
|
||||
private void sendLogs(byte PID) throws IOException {
|
||||
if (PID == LOGS) {
|
||||
respond(server.cache.content());
|
||||
respond(server.getCache().content());
|
||||
} else if (PID == CACHED_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[] serverLogs = server.getCache().content().getBytes(StandardCharsets.UTF_8);
|
||||
int logPartSize = Math.max(0, serverLogs.length - clientSizeNum);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user