From 9113d414abdde77a120662e9ff04fd9d29ec4e99 Mon Sep 17 00:00:00 2001 From: Juno Date: Thu, 2 Nov 2023 09:45:15 -0400 Subject: [PATCH] Move config and add shutdown hooks --- .gitignore | 1 + .../mirrorlog.conf.yml | 2 +- .../org/lavajuno/mirrorlog/main/LogMap.java | 7 +----- .../mirrorlog/main/MirrorLogApplication.java | 16 -------------- .../mirrorlog/server/ServerController.java | 22 ++++++------------- .../mirrorlog/server/ServerThread.java | 5 +++-- 6 files changed, 13 insertions(+), 40 deletions(-) rename mirrorlog.conf.yml => config/mirrorlog.conf.yml (99%) diff --git a/.gitignore b/.gitignore index fe9ed07..3c3b6be 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target/* logs/* test.yml +.vscode/* diff --git a/mirrorlog.conf.yml b/config/mirrorlog.conf.yml similarity index 99% rename from mirrorlog.conf.yml rename to config/mirrorlog.conf.yml index 1866c4d..0a6e7fb 100644 --- a/mirrorlog.conf.yml +++ b/config/mirrorlog.conf.yml @@ -52,7 +52,7 @@ server: # Should the server ignore requests from unknown addresses? (true/false) # -- Example -- # restricted: true - restricted: true + restricted: false # If 'restricted' is true, which addresses should we allow connections from? (list) # -- Example -- diff --git a/src/main/java/org/lavajuno/mirrorlog/main/LogMap.java b/src/main/java/org/lavajuno/mirrorlog/main/LogMap.java index 5058d00..71d9514 100644 --- a/src/main/java/org/lavajuno/mirrorlog/main/LogMap.java +++ b/src/main/java/org/lavajuno/mirrorlog/main/LogMap.java @@ -11,18 +11,13 @@ public class LogMap { /** * The path to the program's configuration file */ - public static final String CONFIG_FILE_PATH = "mirrorlog.conf.yml"; + public static final String CONFIG_FILE_PATH = "config/mirrorlog.conf.yml"; /** * The size of the input buffer for log events (bytes) */ public static final int INPUT_BUFFER_SIZE = 4096; - /** - * How long MirrorLogApplication should wait for ServerController to shut down (ms) - */ - public static final int SERVER_SHUTDOWN_TIMEOUT = 10000; - /** * How long ServerController should wait for OutputController to shut down (ms) */ diff --git a/src/main/java/org/lavajuno/mirrorlog/main/MirrorLogApplication.java b/src/main/java/org/lavajuno/mirrorlog/main/MirrorLogApplication.java index e9f17bc..0e0276c 100644 --- a/src/main/java/org/lavajuno/mirrorlog/main/MirrorLogApplication.java +++ b/src/main/java/org/lavajuno/mirrorlog/main/MirrorLogApplication.java @@ -20,22 +20,6 @@ public static void main(String[] args) { try { server_controller = new ServerController(); server_controller.start(); - Scanner scanner = new Scanner(System.in); - while(true) { - System.out.println("--- Input 'x' to shut down the server."); - if(scanner.nextLine().equalsIgnoreCase("x")) { - System.out.println("Shutting down MirrorLog server..."); - server_controller.interrupt(); - server_controller.close(); - try { - server_controller.join(LogMap.SERVER_SHUTDOWN_TIMEOUT); - } catch(InterruptedException e) { - System.err.println("Interrupted while shutting down. Skipping timeout..."); - } - System.out.println("Done."); - System.exit(0); - } - } } catch(IOException e) { System.err.println("Failed to start server! (IOException)"); System.err.println(e.getMessage()); diff --git a/src/main/java/org/lavajuno/mirrorlog/server/ServerController.java b/src/main/java/org/lavajuno/mirrorlog/server/ServerController.java index 49d68ac..f8412f2 100644 --- a/src/main/java/org/lavajuno/mirrorlog/server/ServerController.java +++ b/src/main/java/org/lavajuno/mirrorlog/server/ServerController.java @@ -44,6 +44,7 @@ public ServerController() throws IOException { output_controller = new OutputController(application_config); threadPool = Executors.newFixedThreadPool(application_config.getThreads()); socket = new ServerSocket(application_config.getPort()); + Runtime.getRuntime().addShutdownHook(new Thread(this::interrupt)); } @Override @@ -68,32 +69,23 @@ public void run() { @Override public void interrupt() { + System.out.println("Closing server socket..."); try { socket.close(); } catch(IOException e) { System.err.println("Failed to close server socket. (IOException)"); } - } - /** - * Shuts down the thread pool and stops the server. - */ - public void close() { - System.out.println("Sending shutdown signal to thread pool..."); - threadPool.shutdownNow(); - System.out.println("Sending shutdown signal to output controller..."); + System.out.println("Shutting down output controller..."); output_controller.interrupt(); + try { output_controller.join(LogMap.IO_SHUTDOWN_TIMEOUT); + if(output_controller.isAlive()) { + System.out.println("Still waiting on output controller to shut down."); + } } catch(InterruptedException e) { System.err.println("Interrupted while shutting down output controller. Skipping timeout."); } - if(output_controller.isAlive()) { - System.out.println("Waiting on output controller to shut down..."); - } - if(!threadPool.isTerminated()) { - System.out.println("Waiting on thread pool to shut down..."); - } - } } diff --git a/src/main/java/org/lavajuno/mirrorlog/server/ServerThread.java b/src/main/java/org/lavajuno/mirrorlog/server/ServerThread.java index 767141a..65f5334 100644 --- a/src/main/java/org/lavajuno/mirrorlog/server/ServerThread.java +++ b/src/main/java/org/lavajuno/mirrorlog/server/ServerThread.java @@ -34,6 +34,7 @@ public ServerThread(Socket socket, OutputController outputController, Applicatio this.client_component_name = "(not specified)"; this.client_address = socket.getInetAddress(); this.application_config = application_config; + Runtime.getRuntime().addShutdownHook(new Thread(this::interrupt)); } @Override @@ -150,10 +151,10 @@ else if(line_str.matches("^@KeepAlive*$")) { @Override public void interrupt() { try { - System.out.println("Connection to " + socket.getInetAddress() + " terminated."); + System.out.println("Connection to " + socket.getInetAddress() + " terminated. (Shutdown)"); this.socket.close(); } catch(IOException e) { - System.err.println("Failed to close socket."); + System.err.println("Failed to close connection to " + socket.getInetAddress()); } } }