Change default port, Add ip to args, pass namespace to messageModule

This commit is contained in:
Manfred Karrer 2014-11-08 16:28:49 +01:00
parent d72d7299df
commit 3033a19b46
9 changed files with 73 additions and 32 deletions

View file

@ -40,6 +40,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorSystem;
import net.sourceforge.argparse4j.inf.Namespace;
import scala.concurrent.duration.Duration;
/**
@ -48,10 +49,12 @@ import scala.concurrent.duration.Duration;
public class AppModule extends BitsquareModule {
private static final Logger log = LoggerFactory.getLogger(AppModule.class);
private Namespace argumentsNamespace;
private final String appName;
public AppModule(Properties properties, String appName) {
public AppModule(Properties properties, Namespace argumentsNamespace, String appName) {
super(properties);
this.argumentsNamespace = argumentsNamespace;
this.appName = appName;
}
@ -75,7 +78,7 @@ public class AppModule extends BitsquareModule {
}
protected MessageModule messageModule() {
return new TomP2PMessageModule(properties);
return new TomP2PMessageModule(properties, argumentsNamespace);
}
protected BitcoinModule bitcoinModule() {

View file

@ -17,9 +17,6 @@
package io.bitsquare.app;
import io.bitsquare.network.BootstrapNodes;
import io.bitsquare.network.Node;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
@ -27,10 +24,10 @@ import net.sourceforge.argparse4j.inf.Namespace;
public class ArgumentParser {
public static final String PEER_ID_FLAG = "peerid";
public static final String IP_FLAG = "ip";
public static final String PORT_FLAG = "port";
public static final String INTERFACE_HINT_FLAG = "interface";
public static final String NAME_FLAG = "name";
public static final String PEER_ID_DEFAULT = BootstrapNodes.DIGITAL_OCEAN_1.getId();
private final net.sourceforge.argparse4j.inf.ArgumentParser parser;
@ -39,15 +36,15 @@ public class ArgumentParser {
.defaultHelp(true)
.description("Bitsquare - The decentralized bitcoin exchange");
parser.addArgument("-d", "--" + PEER_ID_FLAG)
.setDefault(PEER_ID_DEFAULT)
.help("Seed peer ID");
parser.addArgument("-d", "--" + IP_FLAG)
.help("Seed node IP");
parser.addArgument("-p", "--" + PORT_FLAG)
.setDefault(Node.DEFAULT_PORT)
.help("Port to listen on");
.help("Seed node port");
parser.addArgument("-i", "--" + INTERFACE_HINT_FLAG)
.help("Network interface to listen on");
parser.addArgument("-n", "--" + NAME_FLAG)
.help("Name to append name to default application name");
.help("Name to append to default application name");
}
public Namespace parseArgs(String... args) {

View file

@ -48,25 +48,23 @@ import net.sourceforge.argparse4j.inf.Namespace;
public class Main extends Application {
private static final Logger log = LoggerFactory.getLogger(Main.class);
private static String appName = "Bitsquare";
private static Namespace argumentsNamespace;
private MainModule mainModule;
private Injector injector;
public static void main(String[] args) {
ArgumentParser parser = new ArgumentParser();
Namespace namespace = parser.parseArgs(args);
if (namespace.getString(ArgumentParser.NAME_FLAG) != null) {
appName = appName + "-" + namespace.getString(ArgumentParser.NAME_FLAG);
}
argumentsNamespace = new ArgumentParser().parseArgs(args);
Application.launch(Main.class, args);
}
@Override
public void start(Stage primaryStage) {
mainModule = new MainModule(appName, primaryStage);
String appName = "Bitsquare";
if (argumentsNamespace.getString(ArgumentParser.NAME_FLAG) != null)
appName = "Bitsquare-" + argumentsNamespace.getString(ArgumentParser.NAME_FLAG);
mainModule = new MainModule(appName, argumentsNamespace, primaryStage);
injector = Guice.createInjector(mainModule);
@ -106,8 +104,8 @@ public class Main extends Application {
Scene scene = new Scene(view, 1000, 600);
scene.getStylesheets().setAll(
"/io/bitsquare/gui/bitsquare.css",
"/io/bitsquare/gui/images.css");
"/io/bitsquare/gui/bitsquare.css",
"/io/bitsquare/gui/images.css");
// configure the system tray

View file

@ -24,20 +24,24 @@ import io.bitsquare.util.ConfigLoader;
import javafx.stage.Stage;
import net.sourceforge.argparse4j.inf.Namespace;
class MainModule extends BitsquareModule {
private final String appName;
private final Stage primaryStage;
private final Namespace argumentsNamespace;
public MainModule(String appName, Stage primaryStage) {
public MainModule(String appName, Namespace argumentsNamespace, Stage primaryStage) {
super(ConfigLoader.loadConfig(appName));
this.appName = appName;
this.argumentsNamespace = argumentsNamespace;
this.primaryStage = primaryStage;
}
@Override
protected void configure() {
install(new AppModule(properties, appName));
install(new AppModule(properties, argumentsNamespace, appName));
install(new GuiModule(properties, primaryStage));
}
}

View file

@ -18,6 +18,7 @@
package io.bitsquare.msg;
import io.bitsquare.BitsquareModule;
import io.bitsquare.app.ArgumentParser;
import io.bitsquare.network.BootstrapNodes;
import io.bitsquare.network.Node;
@ -26,10 +27,16 @@ import com.google.inject.name.Names;
import java.util.Properties;
import net.sourceforge.argparse4j.inf.Namespace;
public abstract class MessageModule extends BitsquareModule {
protected MessageModule(Properties properties) {
private final Namespace argumentsNamespace;
protected MessageModule(Properties properties, Namespace argumentsNamespace) {
super(properties);
this.argumentsNamespace = argumentsNamespace;
}
@Override
@ -40,9 +47,22 @@ public abstract class MessageModule extends BitsquareModule {
// we will probably later use disk storage instead of memory storage for TomP2P
bind(Boolean.class).annotatedWith(Names.named("useDiskStorage")).toInstance(false);
Node bootstrapNode = BootstrapNodes.DIGITAL_OCEAN_1;
// Passed program args will override the properties of the default bootstrapNode
// So you can use the same id and ip but different ports (e.g. running several nodes on one server with
// different ports)
if (argumentsNamespace.getString(ArgumentParser.PEER_ID_FLAG) != null)
bootstrapNode.setId(argumentsNamespace.getString(ArgumentParser.PEER_ID_FLAG));
if (argumentsNamespace.getString(ArgumentParser.IP_FLAG) != null)
bootstrapNode.setIp(argumentsNamespace.getString(ArgumentParser.IP_FLAG));
if (argumentsNamespace.getString(ArgumentParser.PORT_FLAG) != null)
bootstrapNode.setPort(Integer.valueOf(argumentsNamespace.getString(ArgumentParser.PORT_FLAG)));
bind(Node.class)
.annotatedWith(Names.named("bootstrapNode"))
.toInstance(BootstrapNodes.DIGITAL_OCEAN_1);
.toInstance(bootstrapNode);
doConfigure();
}

View file

@ -22,10 +22,12 @@ import io.bitsquare.msg.MessageModule;
import java.util.Properties;
import net.sourceforge.argparse4j.inf.Namespace;
public class TomP2PMessageModule extends MessageModule {
public TomP2PMessageModule(Properties properties) {
super(properties);
public TomP2PMessageModule(Properties properties, Namespace argumentsNamespace) {
super(properties, argumentsNamespace);
}
@Override

View file

@ -20,6 +20,10 @@ package io.bitsquare.network;
import java.util.Arrays;
import java.util.List;
// Ports 7366-7390 are not registered @see
// <a href="https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml?&page=103</a>
// Lets use ports in that range 7366-7390
// 7366 will be used as default port
public interface BootstrapNodes {
Node LOCALHOST = Node.at("localhost", "127.0.0.1");
Node DIGITAL_OCEAN_1 = Node.at("digitalocean1.bitsquare.io", "188.226.179.109");

View file

@ -20,11 +20,11 @@ package io.bitsquare.network;
import com.google.common.base.Objects;
public final class Node {
public static final int DEFAULT_PORT = 5000;
public static final int DEFAULT_PORT = 7366;
private final String id;
private final String ip;
private final int port;
private String id;
private String ip;
private int port;
private Node(String id, String ip, int port) {
this.id = id;
@ -40,6 +40,18 @@ public final class Node {
return new Node(id, ip, port);
}
public void setId(String id) {
this.id = id;
}
public void setIp(String ip) {
this.ip = ip;
}
public void setPort(int port) {
this.port = port;
}
public String getId() {
return id;
}

View file

@ -36,6 +36,7 @@ public class ViewLoaderTests {
public static class TestApp extends Application {
static Stage primaryStage;
@Override
public void start(Stage primaryStage) throws Exception {
TestApp.primaryStage = primaryStage;
@ -58,7 +59,7 @@ public class ViewLoaderTests {
@Before
public void setUp() {
Injector injector = Guice.createInjector(new MainModule("testApp", TestApp.primaryStage));
Injector injector = Guice.createInjector(new MainModule("testApp", null, TestApp.primaryStage));
ViewLoader.setInjector(injector);
}