Skip to content

Commit

Permalink
Server: Remove redundant isStopped boolean
Browse files Browse the repository at this point in the history
We can track the server status with the interrupted status of the
server thread. The isStopped boolean is not needed.
  • Loading branch information
alvasw committed Feb 12, 2023
1 parent b482276 commit 81a224b
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions p2p/src/main/java/bisq/network/p2p/network/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -45,7 +44,6 @@ class Server implements Runnable {
private final ServerSocket serverSocket;
private final int localPort;
private final Set<Connection> connections = new CopyOnWriteArraySet<>();
private final AtomicBoolean isStopped = new AtomicBoolean(false);
private final NetworkProtoResolver networkProtoResolver;
private final Thread serverThread = new Thread(this);

Expand Down Expand Up @@ -97,7 +95,7 @@ public void run() {
}
}
} catch (IOException e) {
if (!isStopped.get())
if (isServerActive())
e.printStackTrace();
}
} catch (Throwable t) {
Expand All @@ -108,17 +106,14 @@ public void run() {

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) {
Expand All @@ -132,6 +127,6 @@ public void shutDown() {
}

private boolean isServerActive() {
return !isStopped.get() && !Thread.currentThread().isInterrupted();
return !serverThread.isInterrupted();
}
}

0 comments on commit 81a224b

Please sign in to comment.