P2P status indicator with update prompt

Co-authored-by: jmacxx <47253594+jmacxx@users.noreply.github.com>
Co-authored-by: Christoph Atteneder <christoph.atteneder@gmail.com>
This commit is contained in:
napoly 2022-11-20 21:01:42 +01:00 committed by woodser
parent a2929035bc
commit bd70b935e4
13 changed files with 354 additions and 64 deletions

View file

@ -79,6 +79,17 @@ public final class NodeAddress implements PersistablePayload, NetworkPayload, Us
return hostName.replace(".onion", "");
}
// tor v3 onions are too long to display for example in a table grid, so this convenience method
// produces a display-friendly format which includes [first 7]..[last 7] characters.
// tor v2 and localhost will be displayed in full, as they are 16 chars or fewer.
public String getHostNameForDisplay() {
String work = getHostNameWithoutPostFix();
if (work.length() > 16) {
return work.substring(0, 7) + ".." + work.substring(work.length() - 7);
}
return work;
}
// We use just a few chars from the full address to blur the potential receiver for sent network_messages
public byte[] getAddressPrefixHash() {
if (addressPrefixHash == null)

View file

@ -39,12 +39,12 @@ import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import java.net.ConnectException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
@ -127,10 +127,10 @@ public abstract class NetworkNode implements MessageListener {
Thread.currentThread().setName("NetworkNode:SendMessage-to-" + peersNodeAddress.getFullAddress());
if (peersNodeAddress.equals(getNodeAddress())) {
throw new ConnectException("We do not send a message to ourselves");
log.warn("We are sending a message to ourselves");
}
OutboundConnection outboundConnection = null;
OutboundConnection outboundConnection;
try {
// can take a while when using tor
long startTs = System.currentTimeMillis();
@ -145,7 +145,7 @@ public abstract class NetworkNode implements MessageListener {
if (duration > CREATE_SOCKET_TIMEOUT)
throw new TimeoutException("A timeout occurred when creating a socket.");
// Tor needs sometimes quite long to create a connection. To avoid that we get too many double
// Tor needs sometimes quite long to create a connection. To avoid that we get too many double-
// sided connections we check again if we still don't have any connection for that node address.
Connection existingConnection = getInboundConnection(peersNodeAddress);
if (existingConnection == null)
@ -212,9 +212,7 @@ public abstract class NetworkNode implements MessageListener {
return outboundConnection;
}
} catch (Throwable throwable) {
if (!(throwable instanceof ConnectException ||
throwable instanceof IOException ||
throwable instanceof TimeoutException)) {
if (!(throwable instanceof IOException || throwable instanceof TimeoutException)) {
log.warn("Executing task failed. " + throwable.getMessage());
}
throw throwable;
@ -389,7 +387,7 @@ public abstract class NetworkNode implements MessageListener {
@Override
public void onMessage(NetworkEnvelope networkEnvelope, Connection connection) {
messageListeners.stream().forEach(e -> e.onMessage(networkEnvelope, connection));
messageListeners.forEach(e -> e.onMessage(networkEnvelope, connection));
}
@ -441,7 +439,7 @@ public abstract class NetworkNode implements MessageListener {
if (!connection.isStopped()) {
inBoundConnections.add((InboundConnection) connection);
printInboundConnections();
connectionListeners.stream().forEach(e -> e.onConnection(connection));
connectionListeners.forEach(e -> e.onConnection(connection));
}
}
@ -451,13 +449,13 @@ public abstract class NetworkNode implements MessageListener {
//noinspection SuspiciousMethodCalls
inBoundConnections.remove(connection);
printInboundConnections();
connectionListeners.stream().forEach(e -> e.onDisconnect(closeConnectionReason, connection));
connectionListeners.forEach(e -> e.onDisconnect(closeConnectionReason, connection));
}
@Override
public void onError(Throwable throwable) {
log.error("server.ConnectionListener.onError " + throwable.getMessage());
connectionListeners.stream().forEach(e -> e.onError(throwable));
connectionListeners.forEach(e -> e.onError(throwable));
}
};
server = new Server(serverSocket,
@ -479,7 +477,7 @@ public abstract class NetworkNode implements MessageListener {
private void printOutBoundConnections() {
StringBuilder sb = new StringBuilder("outBoundConnections size()=")
.append(outBoundConnections.size()).append("\n\toutBoundConnections=");
outBoundConnections.stream().forEach(e -> sb.append(e).append("\n\t"));
outBoundConnections.forEach(e -> sb.append(e).append("\n\t"));
log.debug(sb.toString());
}
@ -494,7 +492,7 @@ public abstract class NetworkNode implements MessageListener {
private void printInboundConnections() {
StringBuilder sb = new StringBuilder("inBoundConnections size()=")
.append(inBoundConnections.size()).append("\n\tinBoundConnections=");
inBoundConnections.stream().forEach(e -> sb.append(e).append("\n\t"));
inBoundConnections.forEach(e -> sb.append(e).append("\n\t"));
log.debug(sb.toString());
}
@ -512,4 +510,22 @@ public abstract class NetworkNode implements MessageListener {
.map(Connection::getCapabilities)
.findAny();
}
public long upTime() {
// how long Haveno has been running with at least one connection
// uptime is relative to last all connections lost event
long earliestConnection = new Date().getTime();
for (Connection connection : outBoundConnections) {
earliestConnection = Math.min(earliestConnection, connection.getStatistic().getCreationDate().getTime());
}
return new Date().getTime() - earliestConnection;
}
public int getInboundConnectionCount() {
return inBoundConnections.size();
}
public int getOutboundConnectionCount() {
return outBoundConnections.size();
}
}