diff --git a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java index 305094e78..febdf3710 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java @@ -561,9 +561,12 @@ public class PeerGroup { */ public ChannelFuture connectTo(SocketAddress address) { ChannelFuture future = bootstrap.connect(address); - TCPNetworkConnection connection = (TCPNetworkConnection)future.getChannel().getPipeline().get("codec"); - if (connection != null) - connection.setRemoteAddress(address); + TCPNetworkConnection.NetworkHandler networkHandler = + (TCPNetworkConnection.NetworkHandler) future.getChannel().getPipeline().get("codec"); + if (networkHandler != null) { + // This can be null in unit tests or apps that don't use TCP connections. + networkHandler.getOwnerObject().setRemoteAddress(address); + } Peer peer = peerFromChannelFuture(future); channelFutures.put(peer, future); return future; diff --git a/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java b/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java index 279139093..095d0760b 100644 --- a/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java +++ b/core/src/main/java/com/google/bitcoin/core/TCPNetworkConnection.java @@ -16,14 +16,6 @@ package com.google.bitcoin.core; -import static org.jboss.netty.channel.Channels.write; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.util.Date; - import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBufferInputStream; import org.jboss.netty.buffer.ChannelBufferOutputStream; @@ -34,6 +26,14 @@ import org.jboss.netty.handler.codec.replay.VoidEnum; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.util.Date; + +import static org.jboss.netty.channel.Channels.write; + /** * A {@code TCPNetworkConnection} is used for connecting to a Bitcoin node over the standard TCP/IP protocol.

* @@ -173,6 +173,10 @@ public class TCPNetworkConnection { serializer.serialize(message, new ChannelBufferOutputStream(buffer)); write(ctx, e.getFuture(), buffer, e.getRemoteAddress()); } + + public TCPNetworkConnection getOwnerObject() { + return TCPNetworkConnection.this; + } } /** Returns the Netty Pipeline stage handling Bitcoin serialization for this connection. */