Added command line arguments parser and flag to start app in seed mode. Also added default properties loader, but not using it yet.

This commit is contained in:
Steve Myers 2014-10-05 23:53:49 -07:00
parent d19be95a75
commit 47232eebc2
11 changed files with 165 additions and 73 deletions

View file

@ -1,9 +0,0 @@
<component name="CopyrightManager">
<copyright>
<option name="notice" value="This file is part of Bitsquare.&#10;&#10;Bitsquare is free software: you can redistribute it and/or modify it&#10;under the terms of the GNU Affero General Public License as published by&#10;the Free Software Foundation, either version 3 of the License, or (at&#10;your option) any later version.&#10;&#10;Bitsquare is distributed in the hope that it will be useful, but WITHOUT&#10;ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or&#10;FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public&#10;License for more details.&#10;&#10;You should have received a copy of the GNU Affero General Public License&#10;along with Bitsquare. If not, see &lt;http://www.gnu.org/licenses/&gt;." />
<option name="keyword" value="GNU Affero General Public License" />
<option name="allowReplaceKeyword" value="" />
<option name="myName" value="Bitsquare Affero GPLv3" />
<option name="myLocal" value="true" />
</copyright>
</component>

View file

@ -1,3 +1,3 @@
<component name="CopyrightManager">
<settings default="Bitsquare Affero GPLv3" />
<settings default="" />
</component>

View file

@ -43,6 +43,7 @@ dependencies {
compile 'com.google.code.findbugs:jsr305:2.0.3'
compile 'net.jcip:jcip-annotations:1.0'
compile 'org.jetbrains:annotations:13.0'
compile 'net.sourceforge.argparse4j:argparse4j:0.4.4'
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-all:1.9.5'
}

View file

@ -24,11 +24,13 @@ import io.bitsquare.gui.Navigation;
import io.bitsquare.gui.components.Popups;
import io.bitsquare.gui.util.ImageUtil;
import io.bitsquare.gui.util.Profiler;
import io.bitsquare.msg.DHTSeedService;
import io.bitsquare.msg.MessageFacade;
import io.bitsquare.msg.SeedNodeAddress;
import io.bitsquare.msg.actor.event.PeerInitialized;
import io.bitsquare.persistence.Persistence;
import io.bitsquare.settings.Settings;
import io.bitsquare.user.User;
import io.bitsquare.util.BitsquareArgumentParser;
import io.bitsquare.util.ViewLoader;
import com.google.common.base.Throwables;
@ -39,7 +41,6 @@ import com.google.inject.Injector;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.scene.*;
@ -52,6 +53,8 @@ import org.slf4j.LoggerFactory;
import akka.actor.ActorSystem;
import lighthouse.files.AppDirectory;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
public class BitSquare extends Application {
private static final Logger log = LoggerFactory.getLogger(BitSquare.class);
@ -59,6 +62,7 @@ public class BitSquare extends Application {
public static final boolean fillFormsWithDummyData = true;
private static String APP_NAME = "Bitsquare";
private static Injector injector;
private static Stage primaryStage;
private WalletFacade walletFacade;
private MessageFacade messageFacade;
@ -67,37 +71,68 @@ public class BitSquare extends Application {
Profiler.init();
Profiler.printMsgWithTime("BitSquare.main called with args " + Arrays.asList(args).toString());
if (args.length > 0)
APP_NAME = APP_NAME + "-" + args[0];
/*Thread seedNodeThread = new Thread(new Runnable() {
@Override
public void run() {
startSeedNode();
}
});
seedNodeThread.start();*/
launch(args);
}
private static void startSeedNode() {
List<SeedNodeAddress.StaticSeedNodeAddresses> staticSedNodeAddresses = SeedNodeAddress
.StaticSeedNodeAddresses.getAllSeedNodeAddresses();
SeedNode seedNode = new SeedNode(new SeedNodeAddress(staticSedNodeAddresses.get(0)));
seedNode.setDaemon(true);
seedNode.start();
injector = Guice.createInjector(new BitSquareModule());
BitsquareArgumentParser parser = new BitsquareArgumentParser();
Namespace namespace = null;
try {
// keep main thread up
Thread.sleep(Long.MAX_VALUE);
log.debug("Localhost seed node started");
} catch (InterruptedException e) {
log.error(e.toString());
//System.out.println(parser.parseArgs(args));
namespace = parser.parseArgs(args);
} catch (ArgumentParserException e) {
parser.handleError(e);
System.exit(1);
}
if (namespace != null) {
if (namespace.getString(BitsquareArgumentParser.NAME_FLAG) != null) {
APP_NAME = APP_NAME + "-" + namespace.getString(BitsquareArgumentParser.NAME_FLAG);
}
Integer port = BitsquareArgumentParser.PORT_DEFAULT;
if (namespace.getString(BitsquareArgumentParser.PORT_FLAG) != null) {
port = Integer.valueOf(namespace.getString(BitsquareArgumentParser.PORT_FLAG));
}
if (namespace.getBoolean(BitsquareArgumentParser.SEED_FLAG) == true) {
DHTSeedService dhtSeed = injector.getInstance(DHTSeedService.class);
dhtSeed.setHandler(m -> {
if (m instanceof PeerInitialized) {
System.out.println("Seed Peer Initialized on port " + ((PeerInitialized) m).getPort
());
}
});
dhtSeed.initializePeer("localhost", port);
}
else {
launch(args);
}
}
// Thread seedNodeThread = new Thread(new Runnable() {
// @Override
// public void run() {
// startSeedNode();
// }
// });
// seedNodeThread.start();
}
// private static void startSeedNode() {
// List<SeedNodeAddress.StaticSeedNodeAddresses> staticSedNodeAddresses = SeedNodeAddress
// .StaticSeedNodeAddresses.getAllSeedNodeAddresses();
// SeedNode seedNode = new SeedNode(new SeedNodeAddress(staticSedNodeAddresses.get(0)));
// seedNode.setDaemon(true);
// seedNode.start();
//
// try {
// // keep main thread up
// Thread.sleep(Long.MAX_VALUE);
// log.debug("Localhost seed node started");
// } catch (InterruptedException e) {
// log.error(e.toString());
// }
// }
public static Stage getPrimaryStage() {
return primaryStage;
@ -121,7 +156,7 @@ public class BitSquare extends Application {
log.error(e.getMessage());
}
final Injector injector = Guice.createInjector(new BitSquareModule());
// final Injector injector = Guice.createInjector(new BitSquareModule());
// currently there is not SystemTray support for java fx (planned for version 3) so we use the old AWT
AWTSystemTray.createSystemTray(primaryStage, injector.getInstance(ActorSystem.class));

View file

@ -104,27 +104,6 @@ class MainModel extends UIModel {
// For testing with the serverside seednode we need the BootstrappedPeerFactory which gets started form
// messageFacade.init
/*dhtSeedService.setHandler(m -> {
if (m instanceof PeerInitialized) {
log.debug("dht seed initialized. ");
// init messageFacade after seed node initialized
messageFacade.init(new BootstrapListener() {
@Override
public void onCompleted() {
messageFacadeInited = true;
if (walletFacadeInited) onFacadesInitialised();
}
@Override
public void onFailed(Throwable throwable) {
log.error(throwable.toString());
}
});
}
});
dhtSeedService.initializePeer();*/
messageFacade.init(new BootstrapListener() {
@Override

View file

@ -39,9 +39,9 @@ public class DHTSeedService extends ActorService {
super(system, "/user/" + DHTManager.SEED_NAME);
}
public void initializePeer() {
public void initializePeer(String id, Integer port) {
// TODO hard coded seed peer config for now, should read from config properties file
send(new InitializePeer(new Number160(5001), 5001, null));
send(new InitializePeer(Number160.createHash(id), port, null));
}
}

View file

@ -67,12 +67,11 @@ public class DHTManager extends AbstractActor {
.StaticSeedNodeAddresses.getAllSeedNodeAddresses();
SeedNodeAddress seedNodeAddress = new SeedNodeAddress(staticSedNodeAddresses.get(0));
peer = new PeerBuilder(
Number160.createHash(seedNodeAddress.getId())).ports(seedNodeAddress.getPort
()).start();
peer = new PeerBuilder(ip.getPeerId()).ports(ip.getPort())
.start();
// Need to add all features the clients will use (otherwise msg type is UNKNOWN_ID)
new PeerBuilderDHT(peer).start();
peerDHT = new PeerBuilderDHT(peer).start();
PeerNAT nodeBehindNat = new PeerBuilderNAT(peer).start();
new RelayRPC(peer);
//new PeerBuilderTracker(peer);
@ -92,11 +91,11 @@ public class DHTManager extends AbstractActor {
.bootstrapTo(ip.getBootstrapPeers()).start();
futureBootstrap.awaitUninterruptibly(bootstrapTimeout);
}*/
sender().tell(new PeerInitialized(peer.peerID()), self());
sender().tell(new PeerInitialized(peer.peerID(), ip.getPort()), self());
} catch (Throwable t) {
log.info("The second instance has been started. If that happens at the first instance" +
" we are in trouble... " + t.getMessage());
sender().tell(new PeerInitialized(null), self());
sender().tell(new PeerInitialized(null, null), self());
}
})
.matchAny(o -> log.info("received unknown message")).build()

View file

@ -26,13 +26,18 @@ import net.tomp2p.peers.Number160;
public class PeerInitialized {
private final Number160 peerId;
private final Integer port;
public PeerInitialized(Number160 peerId) {
public PeerInitialized(Number160 peerId, Integer port) {
this.peerId = peerId;
this.port = port;
}
public Number160 getPeerId() {
return peerId;
}
public Integer getPort() {
return port;
}
}

View file

@ -0,0 +1,56 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.util;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
public class BitsquareArgumentParser {
public static String SEED_FLAG = "seed";
public static String PORT_FLAG = "port";
public static Integer PORT_DEFAULT = 5000;
public static String NAME_FLAG = "name";
private final ArgumentParser parser;
public BitsquareArgumentParser() {
parser = ArgumentParsers.newArgumentParser("BitSquare")
.defaultHelp(true)
.description("BitSquare decentralized bitcoin exchange.");
parser.addArgument("-s", "--" + SEED_FLAG)
.action(Arguments.storeTrue())
.help("Start in DHT seed mode, no UI.");
parser.addArgument("-p", "--"+PORT_FLAG)
.setDefault(PORT_DEFAULT)
.help("IP port to listen on.");
parser.addArgument("-n", "--"+NAME_FLAG)
.help("Append name to application name.");
}
public Namespace parseArgs(String... args) throws ArgumentParserException {
return parser.parseArgs(args);
}
public void handleError(ArgumentParserException e) {
parser.handleError(e);
}
}

View file

@ -34,20 +34,39 @@ public class ConfigLoader {
private static final String configFilePath = AppDirectory.dir() + "/bitsquare.conf";
public static Properties loadConfig() {
Properties properties = new Properties();
InputStream inputStream = null;
// load default properties from class path
Properties defaultProperties = new Properties();
try {
InputStream is = ConfigLoader.class.getResourceAsStream("/bitsquare.properties");
defaultProperties.load(is);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
// load properties file from config file path
Properties properties = new Properties(defaultProperties);
if (new File(configFilePath).exists()) {
try {
inputStream = new FileInputStream(configFilePath);
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e2) {
e2.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}

View file

@ -0,0 +1,7 @@
seed.0.id=localhost
seed.0.address=127.0.0.1
seed.0.port=5001
seed.1.id=digitalocean.bitsquare.io
seed.1.address=188.226.179.109
seed.1.port=5000