moved UI Application to BitSquareUI class so BitSquare class can startup as seed peer on headless server.

This commit is contained in:
Steve Myers 2014-10-07 09:24:44 -07:00
parent ce1b57d87a
commit b4cd7467b2
7 changed files with 209 additions and 165 deletions

View File

@ -10,7 +10,7 @@ sourceSets.main.resources.srcDirs += 'src/main/java'
javafx {
appID 'Bitsquare'
appName 'Bitsquare'
mainClass 'io.bitsquare.BitSquare'
mainClass 'io.bitsquare.BitSquareUI'
profiles {
windows {

View File

@ -17,37 +17,14 @@
package io.bitsquare;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.di.BitSquareModule;
import io.bitsquare.gui.AWTSystemTray;
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.MessageFacade;
import io.bitsquare.msg.actor.DHTManager;
import io.bitsquare.msg.actor.command.InitializePeer;
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;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.stage.Stage;
import net.tomp2p.peers.Number160;
@ -57,27 +34,20 @@ import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Inbox;
import lighthouse.files.AppDirectory;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
public class BitSquare extends Application {
private static final Logger log = LoggerFactory.getLogger(BitSquare.class);
public class BitSquare {
public static final boolean fillFormsWithDummyData = true;
private static String appName = "Bitsquare";
private static String APP_NAME = "Bitsquare";
private static Injector injector;
private static Stage primaryStage;
private WalletFacade walletFacade;
private MessageFacade messageFacade;
public static String getAppName() {
return appName;
}
public static void main(String[] args) {
Profiler.init();
Profiler.printMsgWithTime("BitSquare.main called with args " + Arrays.asList(args).toString());
injector = Guice.createInjector(new BitSquareModule());
BitsquareArgumentParser parser = new BitsquareArgumentParser();
Namespace namespace = null;
@ -90,7 +60,7 @@ public class BitSquare extends Application {
if (namespace != null) {
if (namespace.getString(BitsquareArgumentParser.NAME_FLAG) != null) {
APP_NAME = APP_NAME + "-" + namespace.getString(BitsquareArgumentParser.NAME_FLAG);
appName = appName + "-" + namespace.getString(BitsquareArgumentParser.NAME_FLAG);
}
Integer port = BitsquareArgumentParser.PORT_DEFAULT;
@ -98,133 +68,42 @@ public class BitSquare extends Application {
port = Integer.valueOf(namespace.getString(BitsquareArgumentParser.PORT_FLAG));
}
if (namespace.getBoolean(BitsquareArgumentParser.SEED_FLAG) == true) {
ActorSystem system = injector.getInstance(ActorSystem.class);
ActorRef seedNode = system.actorOf(DHTManager.getProps(), DHTManager.SEED_NAME);
Inbox inbox = Inbox.create(system);
ActorSystem actorSystem = ActorSystem.create(getAppName());
ActorRef seedNode = actorSystem.actorOf(DHTManager.getProps(), DHTManager.SEED_NAME);
Inbox inbox = Inbox.create(actorSystem);
inbox.send(seedNode, new InitializePeer(Number160.createHash("localhost"), port, null));
//dhtManager.tell(new InitializePeer(Number160.createHash("localhost"), port, null), null);
Boolean quit = false;
Thread seedNodeThread = new Thread(new Runnable() {
@Override
public void run() {
while (!quit) {
try {
Object m = inbox.receive(FiniteDuration.create(5L, "seconds"));
if (m instanceof PeerInitialized) {
System.out.println("Seed Peer Initialized on port " + ((PeerInitialized) m)
.getPort());
}
} catch (Exception e) {
// do nothing
Thread seedNodeThread = new Thread(() -> {
Boolean quit = false;
while (!quit) {
try {
Object m = inbox.receive(FiniteDuration.create(5L, "seconds"));
if (m instanceof PeerInitialized) {
System.out.println("Seed Peer Initialized on port " + ((PeerInitialized) m).getPort
());
}
} catch (Exception e) {
if (!(e instanceof TimeoutException)) {
quit = true;
}
}
}
actorSystem.shutdown();
try {
actorSystem.awaitTermination(Duration.create(5L, "seconds"));
} catch (Exception ex) {
if (ex instanceof TimeoutException)
System.out.println("ActorSystem did not shutdown properly.");
else
System.out.println(ex.getMessage());
}
});
seedNodeThread.start();
}
else {
launch(args);
Application.launch(BitSquareUI.class, args);
}
}
}
public static Stage getPrimaryStage() {
return primaryStage;
}
public static String getAppName() {
return APP_NAME;
}
@Override
public void start(Stage primaryStage) {
Profiler.printMsgWithTime("BitSquare.start called");
BitSquare.primaryStage = primaryStage;
Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> Popups.handleUncaughtExceptions
(Throwables.getRootCause(throwable)));
try {
AppDirectory.initAppDir(APP_NAME);
} catch (IOException e) {
log.error(e.getMessage());
}
// 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));
walletFacade = injector.getInstance(WalletFacade.class);
messageFacade = injector.getInstance(MessageFacade.class);
Profiler.printMsgWithTime("BitSquare: messageFacade, walletFacade created");
// apply stored data
final User user = injector.getInstance(User.class);
final Settings settings = injector.getInstance(Settings.class);
final Persistence persistence = injector.getInstance(Persistence.class);
persistence.init();
User persistedUser = (User) persistence.read(user);
user.applyPersistedUser(persistedUser);
settings.applyPersistedSettings((Settings) persistence.read(settings.getClass().getName()));
primaryStage.setTitle("BitSquare (" + APP_NAME + ")");
// sometimes there is a rendering bug, see https://github.com/bitsquare/bitsquare/issues/160
if (ImageUtil.isRetina())
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/images/window_icon@2x.png")));
else
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/images/window_icon.png")));
ViewLoader.setInjector(injector);
final ViewLoader loader =
new ViewLoader(getClass().getResource(Navigation.Item.MAIN.getFxmlUrl()), false);
try {
final Parent view = loader.load();
final Scene scene = new Scene(view, 1000, 900);
scene.getStylesheets().setAll(getClass().getResource("/io/bitsquare/gui/bitsquare.css").toExternalForm(),
getClass().getResource("/io/bitsquare/gui/images.css").toExternalForm());
setupCloseHandlers(primaryStage, scene);
primaryStage.setScene(scene);
// TODO resizing not fully supported yet
primaryStage.setMinWidth(75);
primaryStage.setMinHeight(50);
/* primaryStage.setMinWidth(1000);
primaryStage.setMinHeight(750);*/
Profiler.initScene(primaryStage.getScene());
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
private void setupCloseHandlers(Stage primaryStage, Scene scene) {
primaryStage.setOnCloseRequest(e -> AWTSystemTray.setStageHidden());
KeyCodeCombination keyCodeCombination = new KeyCodeCombination(KeyCode.W, KeyCombination.SHORTCUT_DOWN);
scene.setOnKeyReleased(keyEvent -> {
if (keyCodeCombination.match(keyEvent)) AWTSystemTray.setStageHidden();
});
}
@Override
public void stop() throws Exception {
walletFacade.shutDown();
messageFacade.shutDown();
super.stop();
System.exit(0);
}
}

View File

@ -0,0 +1,161 @@
/*
* 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;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.di.BitSquareModule;
import io.bitsquare.gui.AWTSystemTray;
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.MessageFacade;
import io.bitsquare.persistence.Persistence;
import io.bitsquare.settings.Settings;
import io.bitsquare.user.User;
import io.bitsquare.util.ViewLoader;
import com.google.common.base.Throwables;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.io.IOException;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorSystem;
import lighthouse.files.AppDirectory;
public class BitSquareUI extends Application {
private static final Logger log = LoggerFactory.getLogger(BitSquareUI.class);
public static final boolean fillFormsWithDummyData = true;
private static Injector injector;
private static Stage primaryStage;
private WalletFacade walletFacade;
private MessageFacade messageFacade;
public void BitSquareUI() {
Profiler.init();
}
public static Stage getPrimaryStage() {
return primaryStage;
}
@Override
public void start(Stage primaryStage) {
Profiler.printMsgWithTime("BitSquare.start called");
BitSquareUI.primaryStage = primaryStage;
Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> Popups.handleUncaughtExceptions
(Throwables.getRootCause(throwable)));
try {
AppDirectory.initAppDir(BitSquare.getAppName());
} catch (IOException e) {
log.error(e.getMessage());
}
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));
walletFacade = injector.getInstance(WalletFacade.class);
messageFacade = injector.getInstance(MessageFacade.class);
Profiler.printMsgWithTime("BitSquare: messageFacade, walletFacade created");
// apply stored data
final User user = injector.getInstance(User.class);
final Settings settings = injector.getInstance(Settings.class);
final Persistence persistence = injector.getInstance(Persistence.class);
persistence.init();
User persistedUser = (User) persistence.read(user);
user.applyPersistedUser(persistedUser);
settings.applyPersistedSettings((Settings) persistence.read(settings.getClass().getName()));
primaryStage.setTitle("BitSquare (" + BitSquare.getAppName() + ")");
// sometimes there is a rendering bug, see https://github.com/bitsquare/bitsquare/issues/160
if (ImageUtil.isRetina())
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/images/window_icon@2x.png")));
else
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/images/window_icon.png")));
ViewLoader.setInjector(injector);
final ViewLoader loader =
new ViewLoader(getClass().getResource(Navigation.Item.MAIN.getFxmlUrl()), false);
try {
final Parent view = loader.load();
final Scene scene = new Scene(view, 1000, 900);
scene.getStylesheets().setAll(getClass().getResource("/io/bitsquare/gui/bitsquare.css").toExternalForm(),
getClass().getResource("/io/bitsquare/gui/images.css").toExternalForm());
setupCloseHandlers(primaryStage, scene);
primaryStage.setScene(scene);
// TODO resizing not fully supported yet
primaryStage.setMinWidth(75);
primaryStage.setMinHeight(50);
/* primaryStage.setMinWidth(1000);
primaryStage.setMinHeight(750);*/
Profiler.initScene(primaryStage.getScene());
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
private void setupCloseHandlers(Stage primaryStage, Scene scene) {
primaryStage.setOnCloseRequest(e -> AWTSystemTray.setStageHidden());
KeyCodeCombination keyCodeCombination = new KeyCodeCombination(KeyCode.W, KeyCombination.SHORTCUT_DOWN);
scene.setOnKeyReleased(keyEvent -> {
if (keyCodeCombination.match(keyEvent)) AWTSystemTray.setStageHidden();
});
}
@Override
public void stop() throws Exception {
walletFacade.shutDown();
messageFacade.shutDown();
super.stop();
System.exit(0);
}
}

View File

@ -18,6 +18,7 @@
package io.bitsquare.gui.components;
import io.bitsquare.BitSquare;
import io.bitsquare.BitSquareUI;
import io.bitsquare.gui.OverlayManager;
import io.bitsquare.locale.BSResources;
@ -69,7 +70,7 @@ public class Popups {
public static void openInfo(String masthead, String message, List<Action> actions) {
Dialogs.create()
.owner(BitSquare.getPrimaryStage())
.owner(BitSquareUI.getPrimaryStage())
.message(message)
.masthead(masthead)
.actions(actions)
@ -103,7 +104,7 @@ public class Popups {
public static Action openConfirmPopup(String title, String masthead, String message, List<Action> actions) {
return Dialogs.create()
.owner(BitSquare.getPrimaryStage())
.owner(BitSquareUI.getPrimaryStage())
.title(title)
.message(message)
.masthead(masthead)
@ -135,7 +136,7 @@ public class Popups {
private static void openWarningPopup(String title, String masthead, String message, List<Action> actions) {
Dialogs.create()
.owner(BitSquare.getPrimaryStage())
.owner(BitSquareUI.getPrimaryStage())
.title(title)
.message(message)
.masthead(masthead)
@ -167,7 +168,7 @@ public class Popups {
private static Action openErrorPopup(String title, String masthead, String message, List<Action> actions) {
return Dialogs.create()
.owner(BitSquare.getPrimaryStage())
.owner(BitSquareUI.getPrimaryStage())
.title(title)
.message(message)
.masthead(masthead)
@ -195,7 +196,7 @@ public class Popups {
}
});
return Dialogs.create()
.owner(BitSquare.getPrimaryStage())
.owner(BitSquareUI.getPrimaryStage())
.title(title)
.message(message)
.masthead(masthead)

View File

@ -18,6 +18,7 @@
package io.bitsquare.gui.main.account.arbitrator;
import io.bitsquare.BitSquare;
import io.bitsquare.BitSquareUI;
import io.bitsquare.gui.CachedViewCB;
import io.bitsquare.gui.Navigation;
import io.bitsquare.gui.main.account.arbitrator.registration.ArbitratorRegistrationViewCB;
@ -102,7 +103,7 @@ public class ArbitratorSettingsViewCB extends CachedViewCB {
final Parent view = loader.load();
arbitratorRegistrationViewCB = loader.getController();
final Stage rootStage = BitSquare.getPrimaryStage();
final Stage rootStage = BitSquareUI.getPrimaryStage();
final Stage stage = new Stage();
stage.setTitle("Arbitrator");
stage.setMinWidth(800);

View File

@ -18,6 +18,7 @@
package io.bitsquare.gui.main.account.content.restrictions;
import io.bitsquare.BitSquare;
import io.bitsquare.BitSquareUI;
import io.bitsquare.arbitrator.Arbitrator;
import io.bitsquare.gui.CachedViewCB;
import io.bitsquare.gui.Navigation;
@ -196,7 +197,7 @@ public class RestrictionsViewCB extends CachedViewCB<RestrictionsPM> implements
Initializable childController = loader.getController();
//childController.setParentController(this);
final Stage rootStage = BitSquare.getPrimaryStage();
final Stage rootStage = BitSquareUI.getPrimaryStage();
final Stage stage = new Stage();
stage.setTitle("Arbitrator selection");
stage.setMinWidth(800);

View File

@ -18,6 +18,7 @@
package io.bitsquare.gui.main.help;
import io.bitsquare.BitSquare;
import io.bitsquare.BitSquareUI;
import java.net.MalformedURLException;
import java.net.URL;
@ -49,7 +50,7 @@ public class Help {
if (helpWindow == null) {
helpWindow = new Stage();
helpWindow.initModality(Modality.NONE);
helpWindow.initOwner(BitSquare.getPrimaryStage());
helpWindow.initOwner(BitSquareUI.getPrimaryStage());
webView = new WebView();
helpWindow.setScene(new Scene(webView, 800, 600));
}