Signed-off-by: HenrikJannsen <boilingfrog@gmx.com>
This commit is contained in:
HenrikJannsen 2022-12-13 17:49:51 -05:00
parent 43233db70c
commit 553dd84f28
No known key found for this signature in database
GPG Key ID: 02AA2BAE387C8307
2 changed files with 8 additions and 12 deletions

View File

@ -39,7 +39,6 @@ import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty; import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleObjectProperty;
import java.net.ConnectException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
@ -134,7 +133,7 @@ public abstract class NetworkNode implements MessageListener {
log.warn("We are sending a message to ourselves"); log.warn("We are sending a message to ourselves");
} }
OutboundConnection outboundConnection = null; OutboundConnection outboundConnection;
try { try {
// can take a while when using tor // can take a while when using tor
long startTs = System.currentTimeMillis(); long startTs = System.currentTimeMillis();
@ -149,8 +148,8 @@ public abstract class NetworkNode implements MessageListener {
if (duration > CREATE_SOCKET_TIMEOUT) if (duration > CREATE_SOCKET_TIMEOUT)
throw new TimeoutException("A timeout occurred when creating a socket."); 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
// sided connections we check again if we still don't have any connection for that node address. // connections with the same peer we check again if we still don't have any connection for that node address.
Connection existingConnection = getInboundConnection(peersNodeAddress); Connection existingConnection = getInboundConnection(peersNodeAddress);
if (existingConnection == null) if (existingConnection == null)
existingConnection = getOutboundConnection(peersNodeAddress); existingConnection = getOutboundConnection(peersNodeAddress);
@ -172,7 +171,7 @@ public abstract class NetworkNode implements MessageListener {
existingConnection.sendMessage(networkEnvelope); existingConnection.sendMessage(networkEnvelope);
return existingConnection; return existingConnection;
} else { } else {
final ConnectionListener connectionListener = new ConnectionListener() { ConnectionListener connectionListener = new ConnectionListener() {
@Override @Override
public void onConnection(Connection connection) { public void onConnection(Connection connection) {
if (!connection.isStopped()) { if (!connection.isStopped()) {
@ -220,8 +219,7 @@ public abstract class NetworkNode implements MessageListener {
return outboundConnection; return outboundConnection;
} }
} catch (Throwable throwable) { } catch (Throwable throwable) {
if (!(throwable instanceof ConnectException || if (!(throwable instanceof IOException ||
throwable instanceof IOException ||
throwable instanceof TimeoutException)) { throwable instanceof TimeoutException)) {
log.warn("Executing task failed. " + throwable.getMessage()); log.warn("Executing task failed. " + throwable.getMessage());
} }
@ -294,8 +292,8 @@ public abstract class NetworkNode implements MessageListener {
connection.sendMessage(networkEnvelope); connection.sendMessage(networkEnvelope);
return connection; return connection;
}); });
final SettableFuture<Connection> resultFuture = SettableFuture.create(); SettableFuture<Connection> resultFuture = SettableFuture.create();
Futures.addCallback(future, new FutureCallback<Connection>() { Futures.addCallback(future, new FutureCallback<>() {
public void onSuccess(Connection connection) { public void onSuccess(Connection connection) {
UserThread.execute(() -> resultFuture.set(connection)); UserThread.execute(() -> resultFuture.set(connection));
} }
@ -445,7 +443,7 @@ public abstract class NetworkNode implements MessageListener {
} }
void startServer(ServerSocket serverSocket) { void startServer(ServerSocket serverSocket) {
final ConnectionListener connectionListener = new ConnectionListener() { ConnectionListener connectionListener = new ConnectionListener() {
@Override @Override
public void onConnection(Connection connection) { public void onConnection(Connection connection) {
if (!connection.isStopped()) { if (!connection.isStopped()) {

View File

@ -33,7 +33,6 @@ import org.slf4j.LoggerFactory;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
// Runs in UserThread
class Server implements Runnable { class Server implements Runnable {
private static final Logger log = LoggerFactory.getLogger(Server.class); private static final Logger log = LoggerFactory.getLogger(Server.class);
@ -42,7 +41,6 @@ class Server implements Runnable {
@Nullable @Nullable
private final NetworkFilter networkFilter; private final NetworkFilter networkFilter;
// accessed from different threads
private final ServerSocket serverSocket; private final ServerSocket serverSocket;
private final Set<Connection> connections = new CopyOnWriteArraySet<>(); private final Set<Connection> connections = new CopyOnWriteArraySet<>();
private volatile boolean stopped; private volatile boolean stopped;