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

View file

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

View file

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

View file

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

View file

@ -18,6 +18,7 @@
package io.bitsquare.msg; package io.bitsquare.msg;
import io.bitsquare.BitsquareModule; import io.bitsquare.BitsquareModule;
import io.bitsquare.app.ArgumentParser;
import io.bitsquare.network.BootstrapNodes; import io.bitsquare.network.BootstrapNodes;
import io.bitsquare.network.Node; import io.bitsquare.network.Node;
@ -26,10 +27,16 @@ import com.google.inject.name.Names;
import java.util.Properties; import java.util.Properties;
import net.sourceforge.argparse4j.inf.Namespace;
public abstract class MessageModule extends BitsquareModule { public abstract class MessageModule extends BitsquareModule {
protected MessageModule(Properties properties) {
private final Namespace argumentsNamespace;
protected MessageModule(Properties properties, Namespace argumentsNamespace) {
super(properties); super(properties);
this.argumentsNamespace = argumentsNamespace;
} }
@Override @Override
@ -40,9 +47,22 @@ public abstract class MessageModule extends BitsquareModule {
// we will probably later use disk storage instead of memory storage for TomP2P // we will probably later use disk storage instead of memory storage for TomP2P
bind(Boolean.class).annotatedWith(Names.named("useDiskStorage")).toInstance(false); 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) bind(Node.class)
.annotatedWith(Names.named("bootstrapNode")) .annotatedWith(Names.named("bootstrapNode"))
.toInstance(BootstrapNodes.DIGITAL_OCEAN_1); .toInstance(bootstrapNode);
doConfigure(); doConfigure();
} }

View file

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

View file

@ -20,6 +20,10 @@ package io.bitsquare.network;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; 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 { public interface BootstrapNodes {
Node LOCALHOST = Node.at("localhost", "127.0.0.1"); Node LOCALHOST = Node.at("localhost", "127.0.0.1");
Node DIGITAL_OCEAN_1 = Node.at("digitalocean1.bitsquare.io", "188.226.179.109"); 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; import com.google.common.base.Objects;
public final class Node { public final class Node {
public static final int DEFAULT_PORT = 5000; public static final int DEFAULT_PORT = 7366;
private final String id; private String id;
private final String ip; private String ip;
private final int port; private int port;
private Node(String id, String ip, int port) { private Node(String id, String ip, int port) {
this.id = id; this.id = id;
@ -40,6 +40,18 @@ public final class Node {
return new Node(id, ip, port); 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() { public String getId() {
return id; return id;
} }

View file

@ -36,6 +36,7 @@ public class ViewLoaderTests {
public static class TestApp extends Application { public static class TestApp extends Application {
static Stage primaryStage; static Stage primaryStage;
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
TestApp.primaryStage = primaryStage; TestApp.primaryStage = primaryStage;
@ -58,7 +59,7 @@ public class ViewLoaderTests {
@Before @Before
public void setUp() { 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); ViewLoader.setInjector(injector);
} }