mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-23 15:00:30 +01:00
Cleanups, refactorings
This commit is contained in:
parent
ae37041a27
commit
15c22d0456
3 changed files with 64 additions and 70 deletions
|
@ -40,6 +40,7 @@ import bisq.common.util.Tuple2;
|
||||||
|
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -62,10 +63,10 @@ public class InventoryMonitor implements SetupListener {
|
||||||
private final File appDir;
|
private final File appDir;
|
||||||
private final boolean useLocalhostForP2P;
|
private final boolean useLocalhostForP2P;
|
||||||
private final int intervalSec;
|
private final int intervalSec;
|
||||||
private NetworkNode networkNode;
|
private final NetworkNode networkNode;
|
||||||
private GetInventoryRequestManager getInventoryRequestManager;
|
private final GetInventoryRequestManager getInventoryRequestManager;
|
||||||
|
|
||||||
private ArrayList<NodeAddress> seedNodes;
|
private final List<NodeAddress> seedNodes;
|
||||||
private InventoryWebServer inventoryWebServer;
|
private InventoryWebServer inventoryWebServer;
|
||||||
private int requestCounter = 0;
|
private int requestCounter = 0;
|
||||||
|
|
||||||
|
@ -78,40 +79,36 @@ public class InventoryMonitor implements SetupListener {
|
||||||
boolean useLocalhostForP2P,
|
boolean useLocalhostForP2P,
|
||||||
BaseCurrencyNetwork network,
|
BaseCurrencyNetwork network,
|
||||||
int intervalSec,
|
int intervalSec,
|
||||||
int port,
|
|
||||||
boolean cleanupTorFiles) {
|
boolean cleanupTorFiles) {
|
||||||
this.appDir = appDir;
|
this.appDir = appDir;
|
||||||
this.useLocalhostForP2P = useLocalhostForP2P;
|
this.useLocalhostForP2P = useLocalhostForP2P;
|
||||||
this.intervalSec = intervalSec;
|
this.intervalSec = intervalSec;
|
||||||
|
|
||||||
// We get more connectivity issues. Cleaning tor cache files helps usually for those problems.
|
|
||||||
File torDir = new File(appDir, "tor");
|
File torDir = new File(appDir, "tor");
|
||||||
if (!torDir.exists()) {
|
if (!torDir.exists() && !torDir.mkdir()) {
|
||||||
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);
|
networkNode = getNetworkNode(torDir);
|
||||||
getInventoryRequestManager = new GetInventoryRequestManager(networkNode);
|
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 networkName = network.name().toLowerCase();
|
||||||
String fileName = network.isMainnet() ? "inv_" + networkName : networkName;
|
String fileName = network.isMainnet() ? "inv_" + networkName : networkName;
|
||||||
DefaultSeedNodeRepository.readSeedNodePropertyFile(fileName)
|
seedNodes = new ArrayList<>(DefaultSeedNodeRepository.getSeedNodeAddressesFromPropertyFile(fileName));
|
||||||
.ifPresent(bufferedReader -> {
|
addJsonFileManagers(seedNodes);
|
||||||
seedNodes = new ArrayList<>(DefaultSeedNodeRepository.getSeedNodeAddressesFromPropertyFile(fileName));
|
|
||||||
addJsonFileManagers(seedNodes);
|
BufferedReader bufferedReader = DefaultSeedNodeRepository.readSeedNodePropertyFile(fileName).orElseThrow();
|
||||||
inventoryWebServer = new InventoryWebServer(port, seedNodes, bufferedReader);
|
inventoryWebServer = new InventoryWebServer(seedNodes, bufferedReader);
|
||||||
networkNode.start(this);
|
|
||||||
});
|
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) {
|
public void shutDown(Runnable shutDownCompleteHandler) {
|
||||||
networkNode.shutDown(shutDownCompleteHandler);
|
networkNode.shutDown(shutDownCompleteHandler);
|
||||||
jsonFileManagerByNodeAddress.values().forEach(JsonFileManager::shutDown);
|
|
||||||
inventoryWebServer.shutDown();
|
inventoryWebServer.shutDown();
|
||||||
|
jsonFileManagerByNodeAddress.values().forEach(JsonFileManager::shutDown);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -25,22 +25,22 @@ import bisq.common.app.AsciiLogo;
|
||||||
import bisq.common.app.Log;
|
import bisq.common.app.Log;
|
||||||
import bisq.common.app.Version;
|
import bisq.common.app.Version;
|
||||||
import bisq.common.config.BaseCurrencyNetwork;
|
import bisq.common.config.BaseCurrencyNetwork;
|
||||||
|
import bisq.common.util.SingleThreadExecutorUtils;
|
||||||
import bisq.common.util.Utilities;
|
import bisq.common.util.Utilities;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
||||||
|
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.ThreadFactory;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import ch.qos.logback.classic.Level;
|
import ch.qos.logback.classic.Level;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import static bisq.core.app.BisqExecutable.EXIT_SUCCESS;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import sun.misc.Signal;
|
import sun.misc.Signal;
|
||||||
|
@ -51,42 +51,51 @@ public class InventoryMonitorMain {
|
||||||
private static InventoryMonitor inventoryMonitor;
|
private static InventoryMonitor inventoryMonitor;
|
||||||
private static boolean stopped;
|
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) {
|
public static void main(String[] args) {
|
||||||
// Default values
|
// Default values
|
||||||
|
int port = 80;
|
||||||
|
boolean cleanupTorFiles = false;
|
||||||
int intervalSec = 120;
|
int intervalSec = 120;
|
||||||
boolean useLocalhostForP2P = false;
|
boolean useLocalhostForP2P = false;
|
||||||
BaseCurrencyNetwork network = BaseCurrencyNetwork.BTC_MAINNET;
|
BaseCurrencyNetwork network = BaseCurrencyNetwork.BTC_MAINNET;
|
||||||
int port = 8080;
|
|
||||||
|
/* port = 8080;
|
||||||
|
useLocalhostForP2P = true;
|
||||||
|
network = BaseCurrencyNetwork.BTC_REGTEST;*/
|
||||||
|
|
||||||
if (args.length > 0) {
|
if (args.length > 0) {
|
||||||
intervalSec = Integer.parseInt(args[0]);
|
port = Integer.parseInt(args[0]);
|
||||||
}
|
}
|
||||||
if (args.length > 1) {
|
if (args.length > 1) {
|
||||||
useLocalhostForP2P = args[1].equals("1");
|
cleanupTorFiles = args[1].equals("1");
|
||||||
}
|
}
|
||||||
if (args.length > 2) {
|
if (args.length > 2) {
|
||||||
network = BaseCurrencyNetwork.valueOf(args[2]);
|
intervalSec = Integer.parseInt(args[2]);
|
||||||
}
|
}
|
||||||
if (args.length > 3) {
|
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;
|
String appName = "bisq-inventory-monitor-" + network;
|
||||||
File appDir = new File(Utilities.getUserDataDir(), appName);
|
File appDir = new File(Utilities.getUserDataDir(), appName);
|
||||||
if (!appDir.exists() && !appDir.mkdir()) {
|
if (!appDir.exists() && !appDir.mkdir()) {
|
||||||
log.warn("make appDir failed");
|
log.warn("make appDir failed");
|
||||||
}
|
}
|
||||||
inventoryMonitor = new InventoryMonitor(appDir, useLocalhostForP2P, network, intervalSec, port, cleanupTorFiles);
|
|
||||||
|
|
||||||
setup(network, appDir);
|
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.
|
// The start script will restart the app.
|
||||||
UserThread.runAfter(InventoryMonitorMain::shutDown, TimeUnit.DAYS.toSeconds(5));
|
UserThread.runAfter(InventoryMonitorMain::shutDown, TimeUnit.DAYS.toSeconds(5));
|
||||||
|
|
||||||
|
keepRunning();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setup(BaseCurrencyNetwork network, File appDir) {
|
private static void setup(BaseCurrencyNetwork network, File appDir) {
|
||||||
|
@ -99,29 +108,17 @@ public class InventoryMonitorMain {
|
||||||
Res.setup(); // Used for some formatting in the webserver
|
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 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()
|
Signal.handle(new Signal("INT"), signal -> UserThread.execute(InventoryMonitorMain::shutDown));
|
||||||
.setNameFormat(inventoryMonitor.getClass().getSimpleName())
|
Signal.handle(new Signal("TERM"), signal -> UserThread.execute(InventoryMonitorMain::shutDown));
|
||||||
.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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void shutDown() {
|
private static void shutDown() {
|
||||||
stopped = true;
|
stopped = true;
|
||||||
inventoryMonitor.shutDown(() -> {
|
inventoryMonitor.shutDown(() -> System.exit(EXIT_SUCCESS));
|
||||||
System.exit(0);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void keepRunning() {
|
private static void keepRunning() {
|
||||||
|
|
|
@ -67,17 +67,9 @@ public class InventoryWebServer {
|
||||||
// Constructor
|
// Constructor
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
public InventoryWebServer(int port,
|
public InventoryWebServer(List<NodeAddress> seedNodes, BufferedReader seedNodeFile) {
|
||||||
List<NodeAddress> seedNodes,
|
|
||||||
BufferedReader seedNodeFile) {
|
|
||||||
this.seedNodes = seedNodes;
|
this.seedNodes = seedNodes;
|
||||||
setupOperatorMap(seedNodeFile);
|
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
|
// API
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
public void onNewRequestInfo(Map<NodeAddress, List<RequestInfo>> requestInfoListByNode, int requestCounter) {
|
void start(int port) {
|
||||||
this.requestCounter = requestCounter;
|
Spark.port(port);
|
||||||
html = generateHtml(requestInfoListByNode);
|
Spark.get("/", (req, res) -> {
|
||||||
|
log.info("Incoming request from: {}", req.userAgent());
|
||||||
|
return html == null ? "Starting up..." : html;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void shutDown() {
|
public void shutDown() {
|
||||||
Spark.stop();
|
Spark.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void onNewRequestInfo(Map<NodeAddress, List<RequestInfo>> requestInfoListByNode, int requestCounter) {
|
||||||
|
this.requestCounter = requestCounter;
|
||||||
|
html = generateHtml(requestInfoListByNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// HTML
|
// HTML
|
||||||
|
|
Loading…
Add table
Reference in a new issue