From 46a9075faf59212fdf299e9622ef25595cfe66f9 Mon Sep 17 00:00:00 2001 From: kennycud Date: Wed, 20 Dec 2023 04:24:51 -0800 Subject: [PATCH] Extracted Server classes into ChainableServer in order offer foreign blockchain server information to the Core API. They were not extracted into the ServerConfigurationInfo class as stated in a recent commit. --- .../qortal/api/resource/CrossChainUtils.java | 47 ++++++++++++ .../qortal/crosschain/ChainableServer.java | 15 ++++ .../org/qortal/crosschain/ServerInfo.java | 74 +++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/main/java/org/qortal/api/resource/CrossChainUtils.java create mode 100644 src/main/java/org/qortal/crosschain/ChainableServer.java create mode 100644 src/main/java/org/qortal/crosschain/ServerInfo.java diff --git a/src/main/java/org/qortal/api/resource/CrossChainUtils.java b/src/main/java/org/qortal/api/resource/CrossChainUtils.java new file mode 100644 index 00000000..d45ba257 --- /dev/null +++ b/src/main/java/org/qortal/api/resource/CrossChainUtils.java @@ -0,0 +1,47 @@ +package org.qortal.api.resource; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.qortal.crosschain.*; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class CrossChainUtils { + private static final Logger LOGGER = LogManager.getLogger(CrossChainUtils.class); + + public static ServerConfigurationInfo buildServerConfigurationInfo(Bitcoiny blockchain) { + + BitcoinyBlockchainProvider blockchainProvider = blockchain.getBlockchainProvider(); + ChainableServer currentServer = blockchainProvider.getCurrentServer(); + + return new ServerConfigurationInfo( + buildInfos(blockchainProvider.getServers(), currentServer), + buildInfos(blockchainProvider.getRemainingServers(), currentServer), + buildInfos(blockchainProvider.getUselessServers(), currentServer) + ); + } + + public static ServerInfo buildInfo(ChainableServer server, boolean isCurrent) { + return new ServerInfo( + server.averageResponseTime(), + server.getHostName(), + server.getPort(), + server.getConnectionType().toString(), + isCurrent); + + } + + public static List buildInfos(Collection servers, ChainableServer currentServer) { + + List infos = new ArrayList<>( servers.size() ); + + for( ChainableServer server : servers ) + { + infos.add(buildInfo(server, server.equals(currentServer))); + } + + return infos; + } +} diff --git a/src/main/java/org/qortal/crosschain/ChainableServer.java b/src/main/java/org/qortal/crosschain/ChainableServer.java new file mode 100644 index 00000000..ac29e5f9 --- /dev/null +++ b/src/main/java/org/qortal/crosschain/ChainableServer.java @@ -0,0 +1,15 @@ +package org.qortal.crosschain; + +public interface ChainableServer { + public void addResponseTime(long responseTime); + + public long averageResponseTime(); + + public String getHostName(); + + public int getPort(); + + public ConnectionType getConnectionType(); + + public enum ConnectionType {TCP, SSL} +} diff --git a/src/main/java/org/qortal/crosschain/ServerInfo.java b/src/main/java/org/qortal/crosschain/ServerInfo.java new file mode 100644 index 00000000..0efe510b --- /dev/null +++ b/src/main/java/org/qortal/crosschain/ServerInfo.java @@ -0,0 +1,74 @@ +package org.qortal.crosschain; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import java.util.Objects; + +@XmlAccessorType(XmlAccessType.FIELD) +public class ServerInfo { + + private long averageResponseTime; + + private String hostName; + + private int port; + + private String connectionType; + + private boolean isCurrent; + + public ServerInfo() { + } + + public ServerInfo(long averageResponseTime, String hostName, int port, String connectionType, boolean isCurrent) { + this.averageResponseTime = averageResponseTime; + this.hostName = hostName; + this.port = port; + this.connectionType = connectionType; + this.isCurrent = isCurrent; + } + + public long getAverageResponseTime() { + return averageResponseTime; + } + + public String getHostName() { + return hostName; + } + + public int getPort() { + return port; + } + + public String getConnectionType() { + return connectionType; + } + + public boolean isCurrent() { + return isCurrent; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ServerInfo that = (ServerInfo) o; + return averageResponseTime == that.averageResponseTime && port == that.port && isCurrent == that.isCurrent && Objects.equals(hostName, that.hostName) && Objects.equals(connectionType, that.connectionType); + } + + @Override + public int hashCode() { + return Objects.hash(averageResponseTime, hostName, port, connectionType, isCurrent); + } + + @Override + public String toString() { + return "ServerInfo{" + + "averageResponseTime=" + averageResponseTime + + ", hostName='" + hostName + '\'' + + ", port=" + port + + ", connectionType='" + connectionType + '\'' + + ", isCurrent=" + isCurrent + + '}'; + } +}