corefx - moved start/stop session into controller, remove coupling of core client and the controller itself, added early way to sync up with a singular mobility script
This commit is contained in:
parent
2694ae3630
commit
9575ce08be
5 changed files with 70 additions and 56 deletions
|
@ -30,11 +30,10 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Data
|
||||
public class Controller implements Initializable {
|
||||
|
@ -52,7 +51,7 @@ public class Controller implements Initializable {
|
|||
private Properties properties;
|
||||
|
||||
// core client utilities
|
||||
private ICoreClient coreClient;
|
||||
private ICoreClient coreClient = new CoreRestClient();
|
||||
private CoreWebSocket coreWebSocket;
|
||||
|
||||
// ui elements
|
||||
|
@ -80,9 +79,6 @@ public class Controller implements Initializable {
|
|||
private ConnectDialog connectDialog = new ConnectDialog(this);
|
||||
private GuiPreferencesDialog guiPreferencesDialog = new GuiPreferencesDialog(this);
|
||||
|
||||
public Controller() {
|
||||
}
|
||||
|
||||
public void connectToCore(String coreUrl) {
|
||||
coreWebSocket.stop();
|
||||
|
||||
|
@ -168,24 +164,64 @@ public class Controller implements Initializable {
|
|||
// update other components for new session
|
||||
graphToolbar.setRunButton(coreClient.isRunning());
|
||||
hooksDialog.updateHooks();
|
||||
}
|
||||
|
||||
public void sessionStarted() {
|
||||
// configure and add mobility player
|
||||
CoreNode node = mobilityDialog.getNode();
|
||||
if (node != null) {
|
||||
MobilityConfig mobilityConfig = mobilityDialog.getMobilityScripts().get(node.getId());
|
||||
if (mobilityConfig != null) {
|
||||
mobilityPlayer.show(node, mobilityConfig);
|
||||
bottom.getChildren().add(mobilityPlayer);
|
||||
}
|
||||
// display first mobility script in player
|
||||
GetMobilityConfigs getMobilityConfigs = coreClient.getMobilityConfigs();
|
||||
Optional<Integer> nodeIdOptional = getMobilityConfigs.getConfigurations().keySet().stream().findFirst();
|
||||
if (nodeIdOptional.isPresent()) {
|
||||
Integer nodeId = nodeIdOptional.get();
|
||||
MobilityConfig mobilityConfig = getMobilityConfigs.getConfigurations().get(nodeId);
|
||||
CoreNode node = networkGraph.getVertex(nodeId);
|
||||
mobilityPlayer.show(node, mobilityConfig);
|
||||
Platform.runLater(() -> bottom.getChildren().add(mobilityPlayer));
|
||||
}
|
||||
saveXmlMenuItem.setDisable(false);
|
||||
}
|
||||
|
||||
public void sessionStopped() {
|
||||
bottom.getChildren().remove(mobilityPlayer);
|
||||
saveXmlMenuItem.setDisable(true);
|
||||
public boolean startSession() throws IOException {
|
||||
// force nodes to get latest positions
|
||||
networkGraph.updatePositions();
|
||||
|
||||
// retrieve items for creation/start
|
||||
Collection<CoreNode> nodes = networkGraph.getGraph().getVertices();
|
||||
Collection<CoreLink> links = networkGraph.getGraph().getEdges();
|
||||
List<Hook> hooks = hooksDialog.getHooks();
|
||||
|
||||
// start/create session
|
||||
progressBar.setVisible(true);
|
||||
boolean result = coreClient.start(nodes, links, hooks);
|
||||
progressBar.setVisible(false);
|
||||
if (result) {
|
||||
// configure and add mobility player
|
||||
CoreNode node = mobilityDialog.getNode();
|
||||
if (node != null) {
|
||||
MobilityConfig mobilityConfig = mobilityDialog.getMobilityScripts().get(node.getId());
|
||||
if (mobilityConfig != null) {
|
||||
mobilityPlayer.show(node, mobilityConfig);
|
||||
Platform.runLater(() -> bottom.getChildren().add(mobilityPlayer));
|
||||
}
|
||||
}
|
||||
saveXmlMenuItem.setDisable(false);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean stopSession() throws IOException {
|
||||
// clear out any drawn wireless links
|
||||
List<CoreLink> wirelessLinks = networkGraph.getGraph().getEdges().stream()
|
||||
.filter(CoreLink::isWireless)
|
||||
.collect(Collectors.toList());
|
||||
wirelessLinks.forEach(networkGraph::removeWirelessLink);
|
||||
networkGraph.getGraphViewer().repaint();
|
||||
|
||||
// stop session
|
||||
progressBar.setVisible(true);
|
||||
boolean result = coreClient.stop();
|
||||
progressBar.setVisible(false);
|
||||
if (result) {
|
||||
Platform.runLater(() -> bottom.getChildren().remove(mobilityPlayer));
|
||||
saveXmlMenuItem.setDisable(true);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void deleteNode(CoreNode node) {
|
||||
|
@ -344,7 +380,6 @@ public class Controller implements Initializable {
|
|||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
coreClient = new CoreRestClient(this);
|
||||
coreWebSocket = new CoreWebSocket(this);
|
||||
properties = ConfigUtils.load();
|
||||
String coreUrl = properties.getProperty(ConfigUtils.REST_URL);
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.core.data.*;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public interface ICoreClient {
|
||||
|
@ -20,7 +21,7 @@ public interface ICoreClient {
|
|||
|
||||
GetSession getSession(Integer sessionId) throws IOException;
|
||||
|
||||
boolean start() throws IOException;
|
||||
boolean start(Collection<CoreNode> nodes, Collection<CoreLink> links, List<Hook> hooks) throws IOException;
|
||||
|
||||
boolean stop() throws IOException;
|
||||
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package com.core.client.rest;
|
||||
|
||||
import com.core.Controller;
|
||||
import com.core.client.ICoreClient;
|
||||
import com.core.data.*;
|
||||
import com.core.graph.NetworkGraph;
|
||||
import com.core.utils.WebUtils;
|
||||
import lombok.Data;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
@ -11,25 +9,18 @@ import org.apache.logging.log4j.Logger;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Data
|
||||
public class CoreRestClient implements ICoreClient {
|
||||
private static final Logger logger = LogManager.getLogger();
|
||||
private final Controller controller;
|
||||
private final NetworkGraph networkGraph;
|
||||
private String baseUrl;
|
||||
private Integer sessionId;
|
||||
private SessionState sessionState;
|
||||
|
||||
public CoreRestClient(Controller controller) {
|
||||
this.controller = controller;
|
||||
this.networkGraph = controller.getNetworkGraph();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUrl(String url) {
|
||||
this.baseUrl = url;
|
||||
|
@ -74,9 +65,7 @@ public class CoreRestClient implements ICoreClient {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean start() throws IOException {
|
||||
networkGraph.updatePositions();
|
||||
|
||||
public boolean start(Collection<CoreNode> nodes, Collection<CoreLink> links, List<Hook> hooks) throws IOException {
|
||||
boolean result = setState(SessionState.DEFINITION);
|
||||
if (!result) {
|
||||
return false;
|
||||
|
@ -87,13 +76,13 @@ public class CoreRestClient implements ICoreClient {
|
|||
return false;
|
||||
}
|
||||
|
||||
for (Hook hook : controller.getHooksDialog().getHooks()) {
|
||||
for (Hook hook : hooks) {
|
||||
if (!createHook(hook)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
for (CoreNode node : networkGraph.getGraph().getVertices()) {
|
||||
for (CoreNode node : nodes) {
|
||||
// must pre-configure wlan nodes, if not already
|
||||
if (node.getNodeType().getValue() == NodeType.WLAN) {
|
||||
WlanConfig config = getWlanConfig(node);
|
||||
|
@ -105,7 +94,7 @@ public class CoreRestClient implements ICoreClient {
|
|||
}
|
||||
}
|
||||
|
||||
for (CoreLink link : networkGraph.getGraph().getEdges()) {
|
||||
for (CoreLink link : links) {
|
||||
if (!createLink(link)) {
|
||||
return false;
|
||||
}
|
||||
|
@ -116,12 +105,6 @@ public class CoreRestClient implements ICoreClient {
|
|||
|
||||
@Override
|
||||
public boolean stop() throws IOException {
|
||||
List<CoreLink> wirelessLinks = networkGraph.getGraph().getEdges().stream()
|
||||
.filter(CoreLink::isWireless)
|
||||
.collect(Collectors.toList());
|
||||
wirelessLinks.forEach(networkGraph::removeWirelessLink);
|
||||
networkGraph.getGraphViewer().repaint();
|
||||
|
||||
return setState(SessionState.SHUTDOWN);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.core.client.rest;
|
||||
|
||||
import com.core.data.MobilityConfig;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
@ -7,5 +8,5 @@ import java.util.Map;
|
|||
|
||||
@Data
|
||||
public class GetMobilityConfigs {
|
||||
private Map<Integer, Map<String, ConfigGroup>> configurations = new HashMap<>();
|
||||
private Map<Integer, MobilityConfig> configurations = new HashMap<>();
|
||||
}
|
||||
|
|
|
@ -236,43 +236,38 @@ public class GraphToolbar extends VBox {
|
|||
}
|
||||
|
||||
private void startSession() {
|
||||
controller.getProgressBar().setVisible(true);
|
||||
runButton.setDisable(true);
|
||||
new Thread(() -> {
|
||||
try {
|
||||
boolean result = controller.getCoreClient().start();
|
||||
boolean result = controller.startSession();
|
||||
if (result) {
|
||||
Toast.success("Session Started");
|
||||
setRunButton(true);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
logger.error("failure starting session", ex);
|
||||
Toast.error("Failure Starting Session", ex);
|
||||
}
|
||||
controller.getProgressBar().setVisible(false);
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void stopSession() {
|
||||
controller.getProgressBar().setVisible(true);
|
||||
runButton.setDisable(true);
|
||||
new Thread(() -> {
|
||||
try {
|
||||
boolean result = controller.getCoreClient().stop();
|
||||
boolean result = controller.stopSession();
|
||||
if (result) {
|
||||
Toast.success("Session Stopped");
|
||||
setRunButton(false);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
logger.error("failure to stopSession session", ex);
|
||||
Toast.error("Failure Stopping Session", ex);
|
||||
}
|
||||
controller.getProgressBar().setVisible(false);
|
||||
}).start();
|
||||
}
|
||||
|
||||
public void setRunButton(boolean isRunning) {
|
||||
if (isRunning) {
|
||||
Platform.runLater(() -> {
|
||||
controller.sessionStarted();
|
||||
pickingButton.fire();
|
||||
editingButton.setDisable(true);
|
||||
runButton.pseudoClassStateChanged(START_CLASS, false);
|
||||
|
@ -284,7 +279,6 @@ public class GraphToolbar extends VBox {
|
|||
});
|
||||
} else {
|
||||
Platform.runLater(() -> {
|
||||
controller.sessionStopped();
|
||||
editingButton.setDisable(false);
|
||||
runButton.pseudoClassStateChanged(START_CLASS, true);
|
||||
runButton.pseudoClassStateChanged(STOP_CLASS, false);
|
||||
|
|
Loading…
Add table
Reference in a new issue