From 15c22d0456809214b43f50236514fae21e70c1ee Mon Sep 17 00:00:00 2001 From: HenrikJannsen Date: Sun, 2 Jun 2024 14:58:21 +0700 Subject: [PATCH] Cleanups, refactorings --- .../java/bisq/inventory/InventoryMonitor.java | 47 +++++++------- .../bisq/inventory/InventoryMonitorMain.java | 63 +++++++++---------- .../bisq/inventory/InventoryWebServer.java | 24 +++---- 3 files changed, 64 insertions(+), 70 deletions(-) diff --git a/inventory/src/main/java/bisq/inventory/InventoryMonitor.java b/inventory/src/main/java/bisq/inventory/InventoryMonitor.java index 945e0fe1cd..400a13f5e1 100644 --- a/inventory/src/main/java/bisq/inventory/InventoryMonitor.java +++ b/inventory/src/main/java/bisq/inventory/InventoryMonitor.java @@ -40,6 +40,7 @@ import bisq.common.util.Tuple2; import java.time.Clock; +import java.io.BufferedReader; import java.io.File; import java.util.ArrayList; @@ -62,10 +63,10 @@ public class InventoryMonitor implements SetupListener { private final File appDir; private final boolean useLocalhostForP2P; private final int intervalSec; - private NetworkNode networkNode; - private GetInventoryRequestManager getInventoryRequestManager; + private final NetworkNode networkNode; + private final GetInventoryRequestManager getInventoryRequestManager; - private ArrayList seedNodes; + private final List seedNodes; private InventoryWebServer inventoryWebServer; private int requestCounter = 0; @@ -78,40 +79,36 @@ public class InventoryMonitor implements SetupListener { boolean useLocalhostForP2P, BaseCurrencyNetwork network, int intervalSec, - int port, boolean cleanupTorFiles) { this.appDir = appDir; this.useLocalhostForP2P = useLocalhostForP2P; this.intervalSec = intervalSec; - // We get more connectivity issues. Cleaning tor cache files helps usually for those problems. File torDir = new File(appDir, "tor"); - if (!torDir.exists()) { - torDir.mkdir(); + if (!torDir.exists() && !torDir.mkdir()) { + log.warn("make torDir failed"); } - TorSetup torSetup = new TorSetup(torDir); - if (cleanupTorFiles) { - torSetup.cleanupTorFiles(() -> initialize(network, port, torDir), log::error); - } else { - initialize(network, port, torDir); - } - } - private void initialize(BaseCurrencyNetwork network, int port, File torDir) { networkNode = getNetworkNode(torDir); getInventoryRequestManager = new GetInventoryRequestManager(networkNode); - // We maintain our own list as we want to monitor also old v2 nodes which are not part of the normal seed - // node list anymore. String networkName = network.name().toLowerCase(); String fileName = network.isMainnet() ? "inv_" + networkName : networkName; - DefaultSeedNodeRepository.readSeedNodePropertyFile(fileName) - .ifPresent(bufferedReader -> { - seedNodes = new ArrayList<>(DefaultSeedNodeRepository.getSeedNodeAddressesFromPropertyFile(fileName)); - addJsonFileManagers(seedNodes); - inventoryWebServer = new InventoryWebServer(port, seedNodes, bufferedReader); - networkNode.start(this); - }); + seedNodes = new ArrayList<>(DefaultSeedNodeRepository.getSeedNodeAddressesFromPropertyFile(fileName)); + addJsonFileManagers(seedNodes); + + BufferedReader bufferedReader = DefaultSeedNodeRepository.readSeedNodePropertyFile(fileName).orElseThrow(); + inventoryWebServer = new InventoryWebServer(seedNodes, bufferedReader); + + TorSetup torSetup = new TorSetup(torDir); + if (cleanupTorFiles) { + torSetup.cleanupTorFiles(() -> log.info("Tor directory cleaned up"), log::error); + } + } + + void start(int port) { + networkNode.start(this); + inventoryWebServer.start(port); } @@ -121,8 +118,8 @@ public class InventoryMonitor implements SetupListener { public void shutDown(Runnable shutDownCompleteHandler) { networkNode.shutDown(shutDownCompleteHandler); - jsonFileManagerByNodeAddress.values().forEach(JsonFileManager::shutDown); inventoryWebServer.shutDown(); + jsonFileManagerByNodeAddress.values().forEach(JsonFileManager::shutDown); } diff --git a/inventory/src/main/java/bisq/inventory/InventoryMonitorMain.java b/inventory/src/main/java/bisq/inventory/InventoryMonitorMain.java index 709e641959..23231919d0 100644 --- a/inventory/src/main/java/bisq/inventory/InventoryMonitorMain.java +++ b/inventory/src/main/java/bisq/inventory/InventoryMonitorMain.java @@ -25,22 +25,22 @@ import bisq.common.app.AsciiLogo; import bisq.common.app.Log; import bisq.common.app.Version; import bisq.common.config.BaseCurrencyNetwork; +import bisq.common.util.SingleThreadExecutorUtils; import bisq.common.util.Utilities; -import com.google.common.util.concurrent.ThreadFactoryBuilder; - import java.nio.file.Paths; import java.io.File; -import java.util.concurrent.Executors; -import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import ch.qos.logback.classic.Level; import lombok.extern.slf4j.Slf4j; +import static bisq.core.app.BisqExecutable.EXIT_SUCCESS; + import sun.misc.Signal; @@ -51,42 +51,51 @@ public class InventoryMonitorMain { private static InventoryMonitor inventoryMonitor; private static boolean stopped; - // prog args for regtest: 10 1 BTC_REGTEST + // Example prog args: + // 8080 0 10 1 BTC_REGTEST public static void main(String[] args) { // Default values + int port = 80; + boolean cleanupTorFiles = false; int intervalSec = 120; boolean useLocalhostForP2P = false; BaseCurrencyNetwork network = BaseCurrencyNetwork.BTC_MAINNET; - int port = 8080; + + /* port = 8080; + useLocalhostForP2P = true; + network = BaseCurrencyNetwork.BTC_REGTEST;*/ if (args.length > 0) { - intervalSec = Integer.parseInt(args[0]); + port = Integer.parseInt(args[0]); } if (args.length > 1) { - useLocalhostForP2P = args[1].equals("1"); + cleanupTorFiles = args[1].equals("1"); } if (args.length > 2) { - network = BaseCurrencyNetwork.valueOf(args[2]); + intervalSec = Integer.parseInt(args[2]); } if (args.length > 3) { - port = Integer.parseInt(args[3]); + useLocalhostForP2P = args[3].equals("1"); + } + if (args.length > 4) { + network = BaseCurrencyNetwork.valueOf(args[4]); } - - // todo - boolean cleanupTorFiles = false; String appName = "bisq-inventory-monitor-" + network; File appDir = new File(Utilities.getUserDataDir(), appName); if (!appDir.exists() && !appDir.mkdir()) { log.warn("make appDir failed"); } - inventoryMonitor = new InventoryMonitor(appDir, useLocalhostForP2P, network, intervalSec, port, cleanupTorFiles); setup(network, appDir); - // We shutdown after 5 days to avoid potential memory leak issue. + inventoryMonitor = new InventoryMonitor(appDir, useLocalhostForP2P, network, intervalSec, cleanupTorFiles); + inventoryMonitor.start(port); + // We shut down after 5 days to avoid potential memory leak issue. // The start script will restart the app. UserThread.runAfter(InventoryMonitorMain::shutDown, TimeUnit.DAYS.toSeconds(5)); + + keepRunning(); } private static void setup(BaseCurrencyNetwork network, File appDir) { @@ -99,29 +108,17 @@ public class InventoryMonitorMain { Res.setup(); // Used for some formatting in the webserver // We do not set any capabilities as we don't want to receive any network data beside our response. - // We also do not use capabilities for the request/response messages as we only connect to seeds nodes and + // We also do not use capabilities for the request/response messages as we only connect to seeds nodes + ExecutorService executorService = SingleThreadExecutorUtils.getSingleThreadExecutor(InventoryMonitorMain.class); + UserThread.setExecutor(executorService); - ThreadFactory threadFactory = new ThreadFactoryBuilder() - .setNameFormat(inventoryMonitor.getClass().getSimpleName()) - .setDaemon(true) - .build(); - UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory)); - - Signal.handle(new Signal("INT"), signal -> { - UserThread.execute(InventoryMonitorMain::shutDown); - }); - - Signal.handle(new Signal("TERM"), signal -> { - UserThread.execute(InventoryMonitorMain::shutDown); - }); - keepRunning(); + Signal.handle(new Signal("INT"), signal -> UserThread.execute(InventoryMonitorMain::shutDown)); + Signal.handle(new Signal("TERM"), signal -> UserThread.execute(InventoryMonitorMain::shutDown)); } private static void shutDown() { stopped = true; - inventoryMonitor.shutDown(() -> { - System.exit(0); - }); + inventoryMonitor.shutDown(() -> System.exit(EXIT_SUCCESS)); } private static void keepRunning() { diff --git a/inventory/src/main/java/bisq/inventory/InventoryWebServer.java b/inventory/src/main/java/bisq/inventory/InventoryWebServer.java index 0689dc190e..b6ed594b88 100644 --- a/inventory/src/main/java/bisq/inventory/InventoryWebServer.java +++ b/inventory/src/main/java/bisq/inventory/InventoryWebServer.java @@ -67,17 +67,9 @@ public class InventoryWebServer { // Constructor /////////////////////////////////////////////////////////////////////////////////////////// - public InventoryWebServer(int port, - List seedNodes, - BufferedReader seedNodeFile) { + public InventoryWebServer(List seedNodes, BufferedReader seedNodeFile) { this.seedNodes = seedNodes; setupOperatorMap(seedNodeFile); - - Spark.port(port); - Spark.get("/", (req, res) -> { - log.info("Incoming request from: {}", req.userAgent()); - return html == null ? "Starting up..." : html; - }); } @@ -85,15 +77,23 @@ public class InventoryWebServer { // API /////////////////////////////////////////////////////////////////////////////////////////// - public void onNewRequestInfo(Map> requestInfoListByNode, int requestCounter) { - this.requestCounter = requestCounter; - html = generateHtml(requestInfoListByNode); + void start(int port) { + Spark.port(port); + Spark.get("/", (req, res) -> { + log.info("Incoming request from: {}", req.userAgent()); + return html == null ? "Starting up..." : html; + }); } public void shutDown() { Spark.stop(); } + public void onNewRequestInfo(Map> requestInfoListByNode, int requestCounter) { + this.requestCounter = requestCounter; + html = generateHtml(requestInfoListByNode); + } + /////////////////////////////////////////////////////////////////////////////////////////// // HTML