Handle shutdown in BootstrappedPeerFactory

This commit is contained in:
Manfred Karrer 2014-11-13 11:20:02 +01:00
parent 2521023025
commit dc3911883c
2 changed files with 15 additions and 11 deletions

View File

@ -79,6 +79,7 @@ public class BootstrappedPeerFactory {
static final String NETWORK_INTERFACE_UNSPECIFIED = "<unspecified>";
private KeyPair keyPair;
private int port;
private final Node bootstrapNode;
private String networkInterface;
private final Persistence persistence;
@ -95,9 +96,11 @@ public class BootstrappedPeerFactory {
@Inject
public BootstrappedPeerFactory(Persistence persistence,
@Named(Node.PORT_KEY) int port,
@Named(BOOTSTRAP_NODE_KEY) Node bootstrapNode,
@Named(NETWORK_INTERFACE_KEY) String networkInterface) {
this.persistence = persistence;
this.port = port;
this.bootstrapNode = bootstrapNode;
this.networkInterface = networkInterface;
}
@ -116,7 +119,7 @@ public class BootstrappedPeerFactory {
// Public methods
///////////////////////////////////////////////////////////////////////////////////////////
public ListenableFuture<PeerDHT> start(int port) {
public ListenableFuture<PeerDHT> start() {
try {
setState(BootstrapState.PEER_CREATION, "We create a P2P node.");
@ -196,6 +199,11 @@ public class BootstrappedPeerFactory {
return settableFuture;
}
void shutDown() {
if (peerDHT != null)
peerDHT.shutdown();
}
// 1. Attempt: Try to discover our outside visible address
private void discover() {
setState(BootstrapState.DIRECT_INIT, "We are starting discovery against a bootstrap node.");

View File

@ -19,15 +19,12 @@ package io.bitsquare.msg.tomp2p;
import io.bitsquare.msg.MessageBroker;
import io.bitsquare.msg.listeners.BootstrapListener;
import io.bitsquare.network.Node;
import io.bitsquare.network.tomp2p.TomP2PPeer;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.name.Named;
import java.io.IOException;
import java.security.KeyPair;
@ -73,7 +70,6 @@ public class TomP2PNode {
private static final Logger log = LoggerFactory.getLogger(TomP2PNode.class);
private KeyPair keyPair;
private final int port;
private MessageBroker messageBroker;
private PeerAddress storedPeerAddress;
@ -87,9 +83,8 @@ public class TomP2PNode {
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public TomP2PNode(BootstrappedPeerFactory bootstrappedPeerFactory, @Named(Node.PORT_KEY) int port) {
public TomP2PNode(BootstrappedPeerFactory bootstrappedPeerFactory) {
this.bootstrappedPeerFactory = bootstrappedPeerFactory;
this.port = port;
}
// for unit testing
@ -99,7 +94,6 @@ public class TomP2PNode {
peerDHT.peerBean().keyPair(keyPair);
messageBroker = (message, peerAddress) -> {
};
port = Node.DEFAULT_PORT;
}
@ -119,7 +113,7 @@ public class TomP2PNode {
public void start(BootstrapListener bootstrapListener) {
setupTimerForIPCheck();
ListenableFuture<PeerDHT> bootstrapComplete = bootstrap(port);
ListenableFuture<PeerDHT> bootstrapComplete = bootstrap();
Futures.addCallback(bootstrapComplete, new FutureCallback<PeerDHT>() {
@Override
public void onSuccess(@Nullable PeerDHT result) {
@ -140,6 +134,8 @@ public class TomP2PNode {
public void shutDown() {
bootstrappedPeerFactory.shutDown();
if (peerDHT != null)
peerDHT.shutdown();
}
@ -295,8 +291,8 @@ public class TomP2PNode {
// Private
///////////////////////////////////////////////////////////////////////////////////////////
private ListenableFuture<PeerDHT> bootstrap(int port) {
ListenableFuture<PeerDHT> bootstrapComplete = bootstrappedPeerFactory.start(port);
private ListenableFuture<PeerDHT> bootstrap() {
ListenableFuture<PeerDHT> bootstrapComplete = bootstrappedPeerFactory.start();
Futures.addCallback(bootstrapComplete, new FutureCallback<PeerDHT>() {
@Override
public void onSuccess(@Nullable PeerDHT peerDHT) {