diff --git a/network/src/main/java/io/bitsquare/p2p/network/Connection.java b/network/src/main/java/io/bitsquare/p2p/network/Connection.java index 499f4e056a..24a8404ce8 100644 --- a/network/src/main/java/io/bitsquare/p2p/network/Connection.java +++ b/network/src/main/java/io/bitsquare/p2p/network/Connection.java @@ -420,19 +420,13 @@ public class Connection implements MessageListener { Connection that = (Connection) o; - if (portInfo != null ? !portInfo.equals(that.portInfo) : that.portInfo != null) return false; - //noinspection SimplifiableIfStatement - if (uid != null ? !uid.equals(that.uid) : that.uid != null) return false; - return peersNodeAddressOptional != null ? peersNodeAddressOptional.equals(that.peersNodeAddressOptional) : that.peersNodeAddressOptional == null; + return !(uid != null ? !uid.equals(that.uid) : that.uid != null); } @Override public int hashCode() { - int result = portInfo != null ? portInfo.hashCode() : 0; - result = 31 * result + (uid != null ? uid.hashCode() : 0); - result = 31 * result + (peersNodeAddressOptional != null ? peersNodeAddressOptional.hashCode() : 0); - return result; + return uid != null ? uid.hashCode() : 0; } @Override diff --git a/network/src/main/java/io/bitsquare/p2p/network/NetworkNode.java b/network/src/main/java/io/bitsquare/p2p/network/NetworkNode.java index d90fd25c1a..97f5d1a2cd 100644 --- a/network/src/main/java/io/bitsquare/p2p/network/NetworkNode.java +++ b/network/src/main/java/io/bitsquare/p2p/network/NetworkNode.java @@ -147,7 +147,7 @@ public abstract class NetworkNode implements MessageListener, ConnectionListener @Nullable private InboundConnection getInboundConnection(@NotNull NodeAddress peersNodeAddress) { - Optional inboundConnectionOptional = lookupInboundConnection(peersNodeAddress); + Optional inboundConnectionOptional = lookupInBoundConnection(peersNodeAddress); if (inboundConnectionOptional.isPresent()) { InboundConnection connection = inboundConnectionOptional.get(); log.trace("We have found a connection in inBoundConnections. Connection.uid=" + connection.getUid()); @@ -165,7 +165,7 @@ public abstract class NetworkNode implements MessageListener, ConnectionListener @Nullable private OutboundConnection getOutboundConnection(@NotNull NodeAddress peersNodeAddress) { - Optional outboundConnectionOptional = lookupOutboundConnection(peersNodeAddress); + Optional outboundConnectionOptional = lookupOutBoundConnection(peersNodeAddress); if (outboundConnectionOptional.isPresent()) { OutboundConnection connection = outboundConnectionOptional.get(); log.trace("We have found a connection in outBoundConnections. Connection.uid=" + connection.getUid()); @@ -264,6 +264,11 @@ public abstract class NetworkNode implements MessageListener, ConnectionListener @Override public void onDisconnect(CloseConnectionReason closeConnectionReason, Connection connection) { + log.trace("onDisconnect connectionListener\n\tconnection={}" + connection); + if (inBoundConnections.contains(connection)) + log.warn("We have the connection in our inBoundConnections. That must not happen as it should be called " + + "from the server listener and get removed from there."); + printOutBoundConnections(); outBoundConnections.remove(connection); // inbound connections are removed in the listener of the server connectionListeners.stream().forEach(e -> e.onDisconnect(closeConnectionReason, connection)); @@ -335,7 +340,9 @@ public abstract class NetworkNode implements MessageListener, ConnectionListener @Override public void onDisconnect(CloseConnectionReason closeConnectionReason, Connection connection) { + log.trace("onDisconnect at server socket connectionListener\n\tconnection={}" + connection); inBoundConnections.remove(connection); + printInboundConnections(); NetworkNode.this.onDisconnect(closeConnectionReason, connection); } @@ -350,28 +357,36 @@ public abstract class NetworkNode implements MessageListener, ConnectionListener executorService.submit(server); } - private Optional lookupOutboundConnection(NodeAddress peersNodeAddress) { - StringBuilder sb = new StringBuilder("Lookup for peersNodeAddress="); - sb.append(peersNodeAddress.toString()).append("/ outBoundConnections.size()=") - .append(outBoundConnections.size()).append("/\n\toutBoundConnections="); - outBoundConnections.stream().forEach(e -> sb.append(e).append("\n\t")); - log.debug(sb.toString()); + private Optional lookupOutBoundConnection(NodeAddress peersNodeAddress) { + log.trace("lookupOutboundConnection for peersNodeAddress={}", peersNodeAddress.getFullAddress()); + printOutBoundConnections(); return outBoundConnections.stream() .filter(connection -> connection.hasPeersNodeAddress() && peersNodeAddress.equals(connection.getPeersNodeAddressOptional().get())).findAny(); } - private Optional lookupInboundConnection(NodeAddress peersNodeAddress) { - StringBuilder sb = new StringBuilder("Lookup for peersNodeAddress="); - sb.append(peersNodeAddress.toString()).append("/ inBoundConnections.size()=") - .append(inBoundConnections.size()).append("/\n\tinBoundConnections="); - inBoundConnections.stream().forEach(e -> sb.append(e).append("\n\t")); + 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")); log.debug(sb.toString()); + } + + private Optional lookupInBoundConnection(NodeAddress peersNodeAddress) { + log.trace("lookupInboundConnection for peersNodeAddress={}", peersNodeAddress.getFullAddress()); + printInboundConnections(); return inBoundConnections.stream() .filter(connection -> connection.hasPeersNodeAddress() && peersNodeAddress.equals(connection.getPeersNodeAddressOptional().get())).findAny(); } + 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")); + log.debug(sb.toString()); + } + abstract protected Socket createSocket(NodeAddress peersNodeAddress) throws IOException; @Nullable