Skip to content

Commit

Permalink
Move config and add shutdown hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
lavajuno committed Nov 2, 2023
1 parent da4ffbb commit 9113d41
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
target/*
logs/*
test.yml
.vscode/*
2 changes: 1 addition & 1 deletion mirrorlog.conf.yml → config/mirrorlog.conf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/lavajuno/mirrorlog/main/LogMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/org/lavajuno/mirrorlog/server/ServerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...");
}

}
}
5 changes: 3 additions & 2 deletions src/main/java/org/lavajuno/mirrorlog/server/ServerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 9113d41

Please sign in to comment.