From e96b02357d39073f9e863f28ba9b9638448b2a20 Mon Sep 17 00:00:00 2001 From: pixtaded Date: Sun, 12 Jan 2025 17:26:21 +0300 Subject: [PATCH] Add serverside caching and improve sanitizing. --- .../net/pixtaded/crab/client/CrabClient.java | 1 + .../crab/{client => common}/Logs.java | 2 +- .../net/pixtaded/crab/common/Sanitizer.java | 6 +++- .../net/pixtaded/crab/server/CrabServer.java | 3 ++ .../net/pixtaded/crab/server/Database.java | 16 +++------ .../pixtaded/crab/server/ServerThread.java | 33 +++++++++++++++++-- 6 files changed, 45 insertions(+), 16 deletions(-) rename src/main/java/net/pixtaded/crab/{client => common}/Logs.java (62%) diff --git a/src/main/java/net/pixtaded/crab/client/CrabClient.java b/src/main/java/net/pixtaded/crab/client/CrabClient.java index 6e7d023..14a60ce 100644 --- a/src/main/java/net/pixtaded/crab/client/CrabClient.java +++ b/src/main/java/net/pixtaded/crab/client/CrabClient.java @@ -1,6 +1,7 @@ package net.pixtaded.crab.client; import net.pixtaded.crab.common.Crab; +import net.pixtaded.crab.common.Logs; import net.pixtaded.crab.common.Sanitizer; import java.io.*; diff --git a/src/main/java/net/pixtaded/crab/client/Logs.java b/src/main/java/net/pixtaded/crab/common/Logs.java similarity index 62% rename from src/main/java/net/pixtaded/crab/client/Logs.java rename to src/main/java/net/pixtaded/crab/common/Logs.java index 479e41c..cbccff4 100644 --- a/src/main/java/net/pixtaded/crab/client/Logs.java +++ b/src/main/java/net/pixtaded/crab/common/Logs.java @@ -1,4 +1,4 @@ -package net.pixtaded.crab.client; +package net.pixtaded.crab.common; public record Logs(int sizeInBytes, String content) { } diff --git a/src/main/java/net/pixtaded/crab/common/Sanitizer.java b/src/main/java/net/pixtaded/crab/common/Sanitizer.java index be553fb..2a38145 100644 --- a/src/main/java/net/pixtaded/crab/common/Sanitizer.java +++ b/src/main/java/net/pixtaded/crab/common/Sanitizer.java @@ -2,11 +2,15 @@ package net.pixtaded.crab.common; public class Sanitizer { public static String sanitizeString(String s, boolean sanitizeNewlines) { - String sanitized = s.replaceAll("\033", ""); + String sanitized = s.replaceAll("[\010\015\033]", ""); if (sanitizeNewlines) { sanitized = sanitized.replaceAll("\n", "\\\\n"); if (!s.endsWith("\n")) sanitized += '\n'; } return sanitized; } + + public static String formatMessage(long timeMillis, String address, String content) { + return String.format("[%td.%1$tm.%1$tY %1$tR] {%s} %s", timeMillis, address, content); + } } diff --git a/src/main/java/net/pixtaded/crab/server/CrabServer.java b/src/main/java/net/pixtaded/crab/server/CrabServer.java index 3af44dc..16ec627 100644 --- a/src/main/java/net/pixtaded/crab/server/CrabServer.java +++ b/src/main/java/net/pixtaded/crab/server/CrabServer.java @@ -1,5 +1,6 @@ package net.pixtaded.crab.server; import net.pixtaded.crab.common.Crab; +import net.pixtaded.crab.common.Logs; import java.io.IOException; import java.net.ServerSocket; @@ -13,6 +14,7 @@ public class CrabServer implements Crab { private boolean isStopped = false; private int port; private final Database db; + public Logs cache = new Logs(0, ""); public CrabServer() { this.db = new Database("data.db"); @@ -28,6 +30,7 @@ public class CrabServer implements Crab { try { if (this.port == 0) setup(); + this.cache = getDb().getLogs(); listen(); } catch (IOException e) { if (!isStopped) System.err.println("Error starting server: " + e.getMessage()); diff --git a/src/main/java/net/pixtaded/crab/server/Database.java b/src/main/java/net/pixtaded/crab/server/Database.java index 61986d5..b88c13f 100644 --- a/src/main/java/net/pixtaded/crab/server/Database.java +++ b/src/main/java/net/pixtaded/crab/server/Database.java @@ -1,15 +1,14 @@ package net.pixtaded.crab.server; +import net.pixtaded.crab.common.Logs; import net.pixtaded.crab.common.Sanitizer; import java.sql.*; import java.util.Date; -import java.util.Locale; public class Database implements AutoCloseable { private Connection connection; - private String logs = ""; public Database(String dbName) { try { @@ -50,22 +49,17 @@ public class Database implements AutoCloseable { } } - public String getLogs() { + public Logs getLogs() { StringBuilder s = new StringBuilder(); try (ResultSet rs = query("SELECT time, address, msg FROM messages")) { while (rs.next()) { - s.append(String.format("[%td.%1$tm.%1$tY %1$tR] {%s} %s", rs.getLong(1), rs.getString(2), rs.getString(3))); + s.append(Sanitizer.formatMessage(rs.getLong(1), rs.getString(2), rs.getString(3))); } } catch (SQLException e) { throw new RuntimeException(e); } - logs = s.toString(); - return logs; - } - - public int getLogSize() { - if (logs.isEmpty()) return 0; - else return logs.getBytes().length; + String logsString = s.toString(); + return new Logs(logsString.isEmpty() ? 0 : logsString.getBytes().length, logsString); } @Override diff --git a/src/main/java/net/pixtaded/crab/server/ServerThread.java b/src/main/java/net/pixtaded/crab/server/ServerThread.java index 2c13324..ee0ce2d 100644 --- a/src/main/java/net/pixtaded/crab/server/ServerThread.java +++ b/src/main/java/net/pixtaded/crab/server/ServerThread.java @@ -1,5 +1,8 @@ package net.pixtaded.crab.server; +import net.pixtaded.crab.common.Logs; +import net.pixtaded.crab.common.Sanitizer; + import java.io.*; import java.net.Socket; import java.nio.charset.StandardCharsets; @@ -36,11 +39,19 @@ public class ServerThread implements Runnable { } switch (PID[0]) { case MESSAGE -> { - server.getDb().logMessage(new Date(), socket.getInetAddress().getHostAddress(), new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim()); + Date date = new Date(); + String msg = new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim(); + String address = socket.getInetAddress().getHostAddress(); + + new Thread(new LogDBThread(date, address, msg)).start(); + + 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); } case LOGS -> { - respond(server.getDb().getLogs()); + respond(server.cache.content()); } case LOGS_SIZE -> { - respond(String.valueOf(server.getDb().getLogSize())); + respond(String.valueOf(server.cache.sizeInBytes())); } default -> { System.out.println("PID not implemented: " + PID[0]); } @@ -59,4 +70,20 @@ public class ServerThread implements Runnable { private void respond(String utf8) throws IOException { respond(utf8.getBytes(StandardCharsets.UTF_8)); } + + private class LogDBThread implements Runnable { + + Date date; + String msg; + + public LogDBThread(Date date, String address, String msg) { + this.date = date; + this.msg = msg; + } + + @Override + public void run() { + server.getDb().logMessage(date, socket.getInetAddress().getHostAddress(), msg); + } + } }