corefx - added connect dialog to allow connections after initial attempt

This commit is contained in:
Blake J. Harnden 2018-11-09 10:08:17 -08:00
parent 24157e7be5
commit 1b728770f1
8 changed files with 110 additions and 40 deletions

View file

@ -52,8 +52,8 @@ public class Controller implements Initializable {
private Stage window; private Stage window;
// core client utilities // core client utilities
private CoreWebSocket coreWebSocket;
private ICoreClient coreClient; private ICoreClient coreClient;
private CoreWebSocket coreWebSocket;
// ui elements // ui elements
private NetworkGraph networkGraph = new NetworkGraph(this); private NetworkGraph networkGraph = new NetworkGraph(this);
@ -77,28 +77,22 @@ public class Controller implements Initializable {
private BackgroundDialog backgroundDialog = new BackgroundDialog(this); private BackgroundDialog backgroundDialog = new BackgroundDialog(this);
private LocationDialog locationDialog = new LocationDialog(this); private LocationDialog locationDialog = new LocationDialog(this);
private GeoDialog geoDialog = new GeoDialog(this); private GeoDialog geoDialog = new GeoDialog(this);
private ConnectDialog connectDialog = new ConnectDialog(this);
public Controller() { public Controller() {
// load configuration }
Properties properties = ConfigUtils.load();
String coreUrl = properties.getProperty("core-rest");
logger.info("core rest: {}", coreUrl);
// start web socket thread public void connectToCore(String coreUrl) {
try { coreWebSocket.stop();
coreWebSocket = new CoreWebSocket(this, coreUrl);
coreWebSocket.start();
} catch (URISyntaxException ex) {
logger.error("error starting web socket", ex);
}
coreClient = new CoreRestClient(this, coreUrl);
ExecutorService executorService = Executors.newSingleThreadExecutor(); ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(() -> { executorService.submit(() -> {
try { try {
coreClient.initialJoin(); coreWebSocket.start(coreUrl);
} catch (IOException ex) { coreClient.initialJoin(coreUrl);
Toast.error(String.format("Initial join failure: %s", ex.getMessage()), ex); } catch (IOException | URISyntaxException ex) {
Toast.error(String.format("Connection failure: %s", ex.getMessage()), ex);
Platform.runLater(() -> connectDialog.showDialog());
} }
}); });
} }
@ -144,6 +138,13 @@ public class Controller implements Initializable {
nodeTypesDialog.setOwner(window); nodeTypesDialog.setOwner(window);
backgroundDialog.setOwner(window); backgroundDialog.setOwner(window);
locationDialog.setOwner(window); locationDialog.setOwner(window);
connectDialog.setOwner(window);
}
@FXML
private void onCoreMenuConnect(ActionEvent event) {
logger.info("showing connect!");
connectDialog.showDialog();
} }
@FXML @FXML
@ -261,6 +262,14 @@ public class Controller implements Initializable {
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
coreClient = new CoreRestClient(this);
coreWebSocket = new CoreWebSocket(this);
Properties properties = ConfigUtils.load();
String coreUrl = properties.getProperty("core-rest");
logger.info("core rest: {}", coreUrl);
connectDialog.setCoreUrl(coreUrl);
connectToCore(coreUrl);
logger.info("controller initialize"); logger.info("controller initialize");
swingNode.setContent(networkGraph.getGraphViewer()); swingNode.setContent(networkGraph.getGraphViewer());

View file

@ -16,7 +16,7 @@ public interface ICoreClient {
GetSession getSession(Integer sessionId) throws IOException; GetSession getSession(Integer sessionId) throws IOException;
void initialJoin() throws IOException; void initialJoin(String url) throws IOException;
boolean start() throws IOException; boolean start() throws IOException;

View file

@ -22,13 +22,12 @@ public class CoreRestClient implements ICoreClient {
private static final Logger logger = LogManager.getLogger(); private static final Logger logger = LogManager.getLogger();
private final Controller controller; private final Controller controller;
private final NetworkGraph networkGraph; private final NetworkGraph networkGraph;
private final String baseUrl; private String baseUrl;
private Integer sessionId; private Integer sessionId;
private SessionState sessionState; private SessionState sessionState;
public CoreRestClient(Controller controller, String baseUrl) { public CoreRestClient(Controller controller) {
this.controller = controller; this.controller = controller;
this.baseUrl = baseUrl;
this.networkGraph = controller.getNetworkGraph(); this.networkGraph = controller.getNetworkGraph();
} }
@ -91,7 +90,8 @@ public class CoreRestClient implements ICoreClient {
} }
@Override @Override
public void initialJoin() throws IOException { public void initialJoin(String url) throws IOException {
this.baseUrl = url;
GetServices services = getServices(); GetServices services = getServices();
logger.info("core services: {}", services); logger.info("core services: {}", services);
controller.getNodeServicesDialog().setServices(services); controller.getNodeServicesDialog().setServices(services);

View file

@ -46,6 +46,7 @@ public final class Toast {
} }
JFXSnackbar.SnackbarEvent snackbarEvent = new JFXSnackbar.SnackbarEvent(message, JFXSnackbar.SnackbarEvent snackbarEvent = new JFXSnackbar.SnackbarEvent(message,
"toast-error", "X", TIMEOUT, true, event -> snackbar.close()); "toast-error", "X", TIMEOUT, true, event -> snackbar.close());
snackbar.close();
snackbar.enqueue(snackbarEvent); snackbar.enqueue(snackbarEvent);
} }
} }

View file

@ -0,0 +1,37 @@
package com.core.ui.dialogs;
import com.core.Controller;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import javafx.fxml.FXML;
import javafx.stage.Modality;
import javafx.stage.StageStyle;
import lombok.Data;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@Data
public class ConnectDialog extends StageDialog {
private static final Logger logger = LogManager.getLogger();
private String coreUrl;
private JFXButton saveButton;
@FXML JFXTextField urlTextField;
public ConnectDialog(Controller controller) {
super(controller, "/fxml/connect_dialog.fxml");
saveButton = createButton("Connect");
saveButton.setOnAction(event -> {
coreUrl = urlTextField.getText();
controller.connectToCore(coreUrl);
close();
});
addCancelButton();
setTitle("CORE Connection");
getStage().sizeToScene();
}
public void showDialog() {
urlTextField.setText(coreUrl);
show();
}
}

View file

@ -14,24 +14,33 @@ import java.net.URISyntaxException;
public class CoreWebSocket { public class CoreWebSocket {
private static final Logger logger = LogManager.getLogger(); private static final Logger logger = LogManager.getLogger();
private final Controller controller; private final Controller controller;
private final String url;
private final Socket socket;
private Thread socketThread; private Thread socketThread;
private Socket socket;
public CoreWebSocket(Controller controller, String url) throws URISyntaxException { public CoreWebSocket(Controller controller) {
this.controller = controller; this.controller = controller;
this.url = url; }
socket = IO.socket(this.url);
socket.on(Socket.EVENT_CONNECT, args -> { public void start(String url) throws URISyntaxException {
logger.info("connected to web socket"); socket = IO.socket(url);
}); socket.on(Socket.EVENT_CONNECT, args -> logger.info("connected to web socket"));
socket.on("node", this::handleNodes); socket.on("node", this::handleNodes);
socket.on("event", this::handleEvents); socket.on("event", this::handleEvents);
socket.on("config", this::handleConfigs); socket.on("config", this::handleConfigs);
socket.on("link", this::handleLinks); socket.on("link", this::handleLinks);
socket.on(Socket.EVENT_DISCONNECT, args -> { socket.on(Socket.EVENT_DISCONNECT, args -> logger.info("disconnected from web socket"));
logger.info("disconnected from web socket");
}); logger.info("attempting to connect to web socket!");
socketThread = new Thread(socket::connect);
socketThread.setDaemon(true);
socketThread.start();
}
public void stop() {
if (socketThread != null) {
socket.close();
socketThread.interrupt();
}
} }
private void handleNodes(Object... args) { private void handleNodes(Object... args) {
@ -87,11 +96,4 @@ public class CoreWebSocket {
logger.info("handling broadcast config: {}", arg); logger.info("handling broadcast config: {}", arg);
} }
} }
public void start() {
logger.info("attempting to connect to web socket!");
socketThread = new Thread(socket::connect);
socketThread.setDaemon(true);
socketThread.start();
}
} }

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox prefWidth="800.0" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<children>
<Label maxWidth="1.7976931348623157E308" text="URL" />
<JFXTextField fx:id="urlTextField" />
</children>
</VBox>

View file

@ -17,10 +17,15 @@
<children> <children>
<MenuBar> <MenuBar>
<menus> <menus>
<Menu mnemonicParsing="false" text="Main">
<items>
<MenuItem mnemonicParsing="false" onAction="#onCoreMenuConnect" text="Connection" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="File"> <Menu mnemonicParsing="false" text="File">
<items> <items>
<MenuItem mnemonicParsing="false" onAction="#onOpenXmlAction" text="Open XML" />
<MenuItem fx:id="saveXmlMenuItem" disable="true" mnemonicParsing="false" onAction="#onSaveXmlAction" text="Save XML" /> <MenuItem fx:id="saveXmlMenuItem" disable="true" mnemonicParsing="false" onAction="#onSaveXmlAction" text="Save XML" />
<MenuItem mnemonicParsing="false" onAction="#onOpenXmlAction" text="Open XML" />
</items> </items>
</Menu> </Menu>
<Menu mnemonicParsing="false" text="Session"> <Menu mnemonicParsing="false" text="Session">