From 81a224bfeadba4f4434f60d7ef25d3ec9b2d4af1 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Sun, 12 Feb 2023 19:23:25 +0100 Subject: [PATCH] Server: Remove redundant isStopped boolean We can track the server status with the interrupted status of the server thread. The isStopped boolean is not needed. --- .../main/java/bisq/network/p2p/network/Server.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/network/Server.java b/p2p/src/main/java/bisq/network/p2p/network/Server.java index 1874e209d8..50cec7d79e 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/Server.java +++ b/p2p/src/main/java/bisq/network/p2p/network/Server.java @@ -27,7 +27,6 @@ import java.io.IOException; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; -import java.util.concurrent.atomic.AtomicBoolean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,7 +44,6 @@ class Server implements Runnable { private final ServerSocket serverSocket; private final int localPort; private final Set connections = new CopyOnWriteArraySet<>(); - private final AtomicBoolean isStopped = new AtomicBoolean(false); private final NetworkProtoResolver networkProtoResolver; private final Thread serverThread = new Thread(this); @@ -97,7 +95,7 @@ class Server implements Runnable { } } } catch (IOException e) { - if (!isStopped.get()) + if (isServerActive()) e.printStackTrace(); } } catch (Throwable t) { @@ -108,17 +106,14 @@ class Server implements Runnable { public void shutDown() { log.info("Server shutdown started"); - boolean isServerStopped = isStopped.getAndSet(true); - - if (!isServerStopped) { + if (isServerActive()) { + serverThread.interrupt(); connections.forEach(connection -> connection.shutDown(CloseConnectionReason.APP_SHUT_DOWN)); try { if (!serverSocket.isClosed()) { serverSocket.close(); } - serverThread.interrupt(); - } catch (SocketException e) { log.debug("SocketException at shutdown might be expected " + e.getMessage()); } catch (IOException e) { @@ -132,6 +127,6 @@ class Server implements Runnable { } private boolean isServerActive() { - return !isStopped.get() && !Thread.currentThread().isInterrupted(); + return !serverThread.isInterrupted(); } }