This commit is contained in:
Manfred Karrer 2014-11-27 12:54:17 +01:00
parent 7dbe3e33b1
commit c8a6c8b330
3 changed files with 17 additions and 17 deletions

View file

@ -66,8 +66,8 @@ import io.netty.util.concurrent.DefaultEventExecutorGroup;
/** /**
* Creates a DHT peer and bootstraps to the network via a bootstrap node * Creates a DHT peer and bootstraps to the network via a bootstrap node
*/ */
class BootstrappedPeerFactory { class BootstrappedPeerBuilder {
private static final Logger log = LoggerFactory.getLogger(BootstrappedPeerFactory.class); private static final Logger log = LoggerFactory.getLogger(BootstrappedPeerBuilder.class);
static final String BOOTSTRAP_NODE_KEY = "bootstrapNode"; static final String BOOTSTRAP_NODE_KEY = "bootstrapNode";
static final String NETWORK_INTERFACE_KEY = "interface"; static final String NETWORK_INTERFACE_KEY = "interface";
@ -93,7 +93,7 @@ class BootstrappedPeerFactory {
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@Inject @Inject
public BootstrappedPeerFactory(@Named(Node.PORT_KEY) int port, public BootstrappedPeerBuilder(@Named(Node.PORT_KEY) int port,
@Named(USE_MANUAL_PORT_FORWARDING_KEY) boolean useManualPortForwarding, @Named(USE_MANUAL_PORT_FORWARDING_KEY) boolean useManualPortForwarding,
@Named(BOOTSTRAP_NODE_KEY) Node bootstrapNode, @Named(BOOTSTRAP_NODE_KEY) Node bootstrapNode,
@Named(NETWORK_INTERFACE_KEY) String networkInterface) { @Named(NETWORK_INTERFACE_KEY) String networkInterface) {

View file

@ -30,15 +30,15 @@ import javax.inject.Singleton;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import static io.bitsquare.msg.tomp2p.BootstrappedPeerFactory.*; import static io.bitsquare.msg.tomp2p.BootstrappedPeerBuilder.*;
public class TomP2PMessageModule extends MessageModule { public class TomP2PMessageModule extends MessageModule {
public static final String BOOTSTRAP_NODE_NAME_KEY = "bootstrap.node.name"; public static final String BOOTSTRAP_NODE_NAME_KEY = "bootstrap.node.name";
public static final String BOOTSTRAP_NODE_IP_KEY = "bootstrap.node.ip"; public static final String BOOTSTRAP_NODE_IP_KEY = "bootstrap.node.ip";
public static final String BOOTSTRAP_NODE_PORT_KEY = "bootstrap.node.port"; public static final String BOOTSTRAP_NODE_PORT_KEY = "bootstrap.node.port";
public static final String NETWORK_INTERFACE_KEY = BootstrappedPeerFactory.NETWORK_INTERFACE_KEY; public static final String NETWORK_INTERFACE_KEY = BootstrappedPeerBuilder.NETWORK_INTERFACE_KEY;
public static final String USE_MANUAL_PORT_FORWARDING_KEY = BootstrappedPeerFactory.USE_MANUAL_PORT_FORWARDING_KEY; public static final String USE_MANUAL_PORT_FORWARDING_KEY = BootstrappedPeerBuilder.USE_MANUAL_PORT_FORWARDING_KEY;
public TomP2PMessageModule(Environment env) { public TomP2PMessageModule(Environment env) {
super(env); super(env);
@ -63,14 +63,14 @@ public class TomP2PMessageModule extends MessageModule {
); );
bindConstant().annotatedWith(Names.named(NETWORK_INTERFACE_KEY)).to( bindConstant().annotatedWith(Names.named(NETWORK_INTERFACE_KEY)).to(
env.getProperty(NETWORK_INTERFACE_KEY, NETWORK_INTERFACE_UNSPECIFIED)); env.getProperty(NETWORK_INTERFACE_KEY, NETWORK_INTERFACE_UNSPECIFIED));
bind(BootstrappedPeerFactory.class).asEagerSingleton(); bind(BootstrappedPeerBuilder.class).asEagerSingleton();
} }
@Override @Override
protected void doClose(Injector injector) { protected void doClose(Injector injector) {
super.doClose(injector); super.doClose(injector);
injector.getInstance(BootstrappedPeerFactory.class).shutDown(); injector.getInstance(BootstrappedPeerBuilder.class).shutDown();
} }
@Override @Override

View file

@ -80,7 +80,7 @@ public class TomP2PNode implements ClientNode {
private PeerAddress storedPeerAddress; private PeerAddress storedPeerAddress;
private PeerDHT peerDHT; private PeerDHT peerDHT;
private BootstrappedPeerFactory bootstrappedPeerFactory; private BootstrappedPeerBuilder bootstrappedPeerBuilder;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -88,8 +88,8 @@ public class TomP2PNode implements ClientNode {
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@Inject @Inject
public TomP2PNode(BootstrappedPeerFactory bootstrappedPeerFactory) { public TomP2PNode(BootstrappedPeerBuilder bootstrappedPeerBuilder) {
this.bootstrappedPeerFactory = bootstrappedPeerFactory; this.bootstrappedPeerBuilder = bootstrappedPeerBuilder;
} }
// for unit testing // for unit testing
@ -112,16 +112,16 @@ public class TomP2PNode implements ClientNode {
this.messageBroker = messageBroker; this.messageBroker = messageBroker;
this.keyPair = keyPair; this.keyPair = keyPair;
bootstrappedPeerFactory.setKeyPair(keyPair); bootstrappedPeerBuilder.setKeyPair(keyPair);
Subject<BootstrapState, BootstrapState> bootstrapStateSubject = BehaviorSubject.create(); Subject<BootstrapState, BootstrapState> bootstrapStateSubject = BehaviorSubject.create();
bootstrappedPeerFactory.getBootstrapState().addListener((ov, oldValue, newValue) -> { bootstrappedPeerBuilder.getBootstrapState().addListener((ov, oldValue, newValue) -> {
log.debug("BootstrapState changed " + newValue); log.debug("BootstrapState changed " + newValue);
bootstrapStateSubject.onNext(newValue); bootstrapStateSubject.onNext(newValue);
}); });
SettableFuture<PeerDHT> bootstrapFuture = bootstrappedPeerFactory.start(); SettableFuture<PeerDHT> bootstrapFuture = bootstrappedPeerBuilder.start();
Futures.addCallback(bootstrapFuture, new FutureCallback<PeerDHT>() { Futures.addCallback(bootstrapFuture, new FutureCallback<PeerDHT>() {
@Override @Override
public void onSuccess(@Nullable PeerDHT peerDHT) { public void onSuccess(@Nullable PeerDHT peerDHT) {
@ -374,7 +374,7 @@ public class TomP2PNode implements ClientNode {
@Override @Override
public ConnectionType getConnectionType() { public ConnectionType getConnectionType() {
BootstrapState bootstrapState = bootstrappedPeerFactory.getBootstrapState().get(); BootstrapState bootstrapState = bootstrappedPeerBuilder.getBootstrapState().get();
switch (bootstrapState) { switch (bootstrapState) {
case DISCOVERY_DIRECT_SUCCEEDED: case DISCOVERY_DIRECT_SUCCEEDED:
return ConnectionType.DIRECT; return ConnectionType.DIRECT;
@ -400,6 +400,6 @@ public class TomP2PNode implements ClientNode {
@Override @Override
public Node getBootstrapNodeAddress() { public Node getBootstrapNodeAddress() {
return bootstrappedPeerFactory.getBootstrapNode(); return bootstrappedPeerBuilder.getBootstrapNode();
} }
} }