From 1b728770f1506617887a1be39b12e1f410d76846 Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Fri, 9 Nov 2018 10:08:17 -0800 Subject: [PATCH] corefx - added connect dialog to allow connections after initial attempt --- corefx/src/main/java/com/core/Controller.java | 41 +++++++++++-------- .../java/com/core/client/ICoreClient.java | 2 +- .../com/core/client/rest/CoreRestClient.java | 8 ++-- corefx/src/main/java/com/core/ui/Toast.java | 1 + .../com/core/ui/dialogs/ConnectDialog.java | 37 +++++++++++++++++ .../com/core/websocket/CoreWebSocket.java | 38 +++++++++-------- .../main/resources/fxml/connect_dialog.fxml | 16 ++++++++ corefx/src/main/resources/fxml/main.fxml | 7 +++- 8 files changed, 110 insertions(+), 40 deletions(-) create mode 100644 corefx/src/main/java/com/core/ui/dialogs/ConnectDialog.java create mode 100644 corefx/src/main/resources/fxml/connect_dialog.fxml diff --git a/corefx/src/main/java/com/core/Controller.java b/corefx/src/main/java/com/core/Controller.java index 5d9255ce..471b9648 100644 --- a/corefx/src/main/java/com/core/Controller.java +++ b/corefx/src/main/java/com/core/Controller.java @@ -52,8 +52,8 @@ public class Controller implements Initializable { private Stage window; // core client utilities - private CoreWebSocket coreWebSocket; private ICoreClient coreClient; + private CoreWebSocket coreWebSocket; // ui elements private NetworkGraph networkGraph = new NetworkGraph(this); @@ -77,28 +77,22 @@ public class Controller implements Initializable { private BackgroundDialog backgroundDialog = new BackgroundDialog(this); private LocationDialog locationDialog = new LocationDialog(this); private GeoDialog geoDialog = new GeoDialog(this); + private ConnectDialog connectDialog = new ConnectDialog(this); public Controller() { - // load configuration - Properties properties = ConfigUtils.load(); - String coreUrl = properties.getProperty("core-rest"); - logger.info("core rest: {}", coreUrl); + } - // start web socket thread - try { - coreWebSocket = new CoreWebSocket(this, coreUrl); - coreWebSocket.start(); - } catch (URISyntaxException ex) { - logger.error("error starting web socket", ex); - } + public void connectToCore(String coreUrl) { + coreWebSocket.stop(); - coreClient = new CoreRestClient(this, coreUrl); ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.submit(() -> { try { - coreClient.initialJoin(); - } catch (IOException ex) { - Toast.error(String.format("Initial join failure: %s", ex.getMessage()), ex); + coreWebSocket.start(coreUrl); + coreClient.initialJoin(coreUrl); + } 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); backgroundDialog.setOwner(window); locationDialog.setOwner(window); + connectDialog.setOwner(window); + } + + @FXML + private void onCoreMenuConnect(ActionEvent event) { + logger.info("showing connect!"); + connectDialog.showDialog(); } @FXML @@ -261,6 +262,14 @@ public class Controller implements Initializable { @Override 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"); swingNode.setContent(networkGraph.getGraphViewer()); diff --git a/corefx/src/main/java/com/core/client/ICoreClient.java b/corefx/src/main/java/com/core/client/ICoreClient.java index 335d107f..fd4efca0 100644 --- a/corefx/src/main/java/com/core/client/ICoreClient.java +++ b/corefx/src/main/java/com/core/client/ICoreClient.java @@ -16,7 +16,7 @@ public interface ICoreClient { GetSession getSession(Integer sessionId) throws IOException; - void initialJoin() throws IOException; + void initialJoin(String url) throws IOException; boolean start() throws IOException; diff --git a/corefx/src/main/java/com/core/client/rest/CoreRestClient.java b/corefx/src/main/java/com/core/client/rest/CoreRestClient.java index 08ad19d5..897cba92 100644 --- a/corefx/src/main/java/com/core/client/rest/CoreRestClient.java +++ b/corefx/src/main/java/com/core/client/rest/CoreRestClient.java @@ -22,13 +22,12 @@ public class CoreRestClient implements ICoreClient { private static final Logger logger = LogManager.getLogger(); private final Controller controller; private final NetworkGraph networkGraph; - private final String baseUrl; + private String baseUrl; private Integer sessionId; private SessionState sessionState; - public CoreRestClient(Controller controller, String baseUrl) { + public CoreRestClient(Controller controller) { this.controller = controller; - this.baseUrl = baseUrl; this.networkGraph = controller.getNetworkGraph(); } @@ -91,7 +90,8 @@ public class CoreRestClient implements ICoreClient { } @Override - public void initialJoin() throws IOException { + public void initialJoin(String url) throws IOException { + this.baseUrl = url; GetServices services = getServices(); logger.info("core services: {}", services); controller.getNodeServicesDialog().setServices(services); diff --git a/corefx/src/main/java/com/core/ui/Toast.java b/corefx/src/main/java/com/core/ui/Toast.java index f6d957b8..94994fcb 100644 --- a/corefx/src/main/java/com/core/ui/Toast.java +++ b/corefx/src/main/java/com/core/ui/Toast.java @@ -46,6 +46,7 @@ public final class Toast { } JFXSnackbar.SnackbarEvent snackbarEvent = new JFXSnackbar.SnackbarEvent(message, "toast-error", "X", TIMEOUT, true, event -> snackbar.close()); + snackbar.close(); snackbar.enqueue(snackbarEvent); } } diff --git a/corefx/src/main/java/com/core/ui/dialogs/ConnectDialog.java b/corefx/src/main/java/com/core/ui/dialogs/ConnectDialog.java new file mode 100644 index 00000000..c5e4d6ba --- /dev/null +++ b/corefx/src/main/java/com/core/ui/dialogs/ConnectDialog.java @@ -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(); + } +} diff --git a/corefx/src/main/java/com/core/websocket/CoreWebSocket.java b/corefx/src/main/java/com/core/websocket/CoreWebSocket.java index 7e5d0fcf..c5cc89b5 100644 --- a/corefx/src/main/java/com/core/websocket/CoreWebSocket.java +++ b/corefx/src/main/java/com/core/websocket/CoreWebSocket.java @@ -14,24 +14,33 @@ import java.net.URISyntaxException; public class CoreWebSocket { private static final Logger logger = LogManager.getLogger(); private final Controller controller; - private final String url; - private final Socket socket; private Thread socketThread; + private Socket socket; - public CoreWebSocket(Controller controller, String url) throws URISyntaxException { + public CoreWebSocket(Controller controller) { this.controller = controller; - this.url = url; - socket = IO.socket(this.url); - socket.on(Socket.EVENT_CONNECT, args -> { - logger.info("connected to web socket"); - }); + } + + public void start(String url) throws URISyntaxException { + socket = IO.socket(url); + socket.on(Socket.EVENT_CONNECT, args -> logger.info("connected to web socket")); socket.on("node", this::handleNodes); socket.on("event", this::handleEvents); socket.on("config", this::handleConfigs); socket.on("link", this::handleLinks); - socket.on(Socket.EVENT_DISCONNECT, args -> { - logger.info("disconnected from web socket"); - }); + socket.on(Socket.EVENT_DISCONNECT, args -> 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) { @@ -87,11 +96,4 @@ public class CoreWebSocket { 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(); - } } diff --git a/corefx/src/main/resources/fxml/connect_dialog.fxml b/corefx/src/main/resources/fxml/connect_dialog.fxml new file mode 100644 index 00000000..ec2b4f2e --- /dev/null +++ b/corefx/src/main/resources/fxml/connect_dialog.fxml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + diff --git a/corefx/src/main/resources/fxml/main.fxml b/corefx/src/main/resources/fxml/main.fxml index ab8f3640..c65917ba 100644 --- a/corefx/src/main/resources/fxml/main.fxml +++ b/corefx/src/main/resources/fxml/main.fxml @@ -17,10 +17,15 @@ + + + + + - +