diff --git a/corefx/src/main/java/com/core/Controller.java b/corefx/src/main/java/com/core/Controller.java index a92345e6..7a2a7160 100644 --- a/corefx/src/main/java/com/core/Controller.java +++ b/corefx/src/main/java/com/core/Controller.java @@ -2,9 +2,7 @@ package com.core; import com.core.client.ICoreClient; import com.core.client.rest.*; -import com.core.data.CoreLink; -import com.core.data.CoreNode; -import com.core.data.MobilityConfig; +import com.core.data.*; import com.core.graph.NetworkGraph; import com.core.ui.*; import com.core.ui.dialogs.*; @@ -110,22 +108,68 @@ public class Controller implements Initializable { GetSessions response = coreClient.getSessions(); logger.info("existing sessions: {}", response); + Integer sessionId; if (response.getSessions().isEmpty()) { logger.info("creating initial session"); - coreClient.createSession(); - graphToolbar.setRunButton(coreClient.isRunning()); - hooksDialog.updateHooks(); + CreatedSession createdSession = coreClient.createSession(); + sessionId = createdSession.getId(); + Toast.info(String.format("Created Session %s", sessionId)); } else { GetSessionsData getSessionsData = response.getSessions().get(0); - Integer joinId = getSessionsData.getId(); - coreClient.joinSession(joinId, true); + sessionId = getSessionsData.getId(); + Toast.info(String.format("Joined Session %s", sessionId)); } + joinSession(sessionId); + // set emane models List emaneModels = coreClient.getEmaneModels().getModels(); nodeEmaneDialog.setModels(emaneModels); } + public void joinSession(Integer joinId) throws IOException { + // clear graph + networkGraph.reset(); + + // get session to join + GetSession session = coreClient.getSession(joinId); + SessionState sessionState = SessionState.get(session.getState()); + + // update client to use this session + coreClient.updateSession(joinId); + coreClient.updateState(sessionState); + + // display all nodes + logger.info("joining core session({}) state({}): {}", joinId, sessionState, session); + for (CoreNode node : session.getNodes()) { + NodeType nodeType = NodeType.find(node.getType(), node.getModel()); + if (nodeType == null) { + logger.info(String.format("failed to find node type(%s) model(%s): %s", + node.getType(), node.getModel(), node.getName())); + continue; + } + + node.setNodeType(nodeType); + networkGraph.addNode(node); + } + + // display all links + for (CoreLink link : session.getLinks()) { + if (link.getInterfaceOne() != null || link.getInterfaceTwo() != null) { + link.setType(LinkTypes.WIRED.getValue()); + } + + networkGraph.addLink(link); + } + + // refresh graph + networkGraph.getGraphViewer().repaint(); + + // update other components for new session + graphToolbar.setRunButton(coreClient.isRunning()); + hooksDialog.updateHooks(); + } + public void sessionStarted() { // configure and add mobility player CoreNode node = mobilityDialog.getNode(); @@ -152,7 +196,7 @@ public class Controller implements Initializable { } } - public void setWindow(Stage window) { + void setWindow(Stage window) { this.window = window; sessionsDialog.setOwner(window); hooksDialog.setOwner(window); @@ -221,7 +265,10 @@ public class Controller implements Initializable { if (file != null) { logger.info("opening session xml: {}", file.getPath()); try { - coreClient.openSession(file); + CreatedSession createdSession = coreClient.openSession(file); + Integer sessionId = createdSession.getId(); + joinSession(sessionId); + Toast.info(String.format("Joined Session %s", sessionId)); } catch (IOException ex) { logger.error("error opening session xml", ex); } diff --git a/corefx/src/main/java/com/core/client/ICoreClient.java b/corefx/src/main/java/com/core/client/ICoreClient.java index 2d977051..c3f2ca5b 100644 --- a/corefx/src/main/java/com/core/client/ICoreClient.java +++ b/corefx/src/main/java/com/core/client/ICoreClient.java @@ -10,9 +10,11 @@ import java.util.List; public interface ICoreClient { void setUrl(String url); - void joinSession(Integer joinId, boolean notification) throws IOException; + void updateSession(Integer sessionId); - void createSession() throws IOException; + void updateState(SessionState state); + + CreatedSession createSession() throws IOException; GetSessions getSessions() throws IOException; @@ -22,8 +24,6 @@ public interface ICoreClient { boolean stop() throws IOException; - void updateState(SessionState state); - boolean setState(SessionState state) throws IOException; GetServices getServices() throws IOException; @@ -50,7 +50,7 @@ public interface ICoreClient { void saveSession(File file) throws IOException; - void openSession(File file) throws IOException; + CreatedSession openSession(File file) throws IOException; GetConfig getSessionConfig() 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 b923b2e6..59514645 100644 --- a/corefx/src/main/java/com/core/client/rest/CoreRestClient.java +++ b/corefx/src/main/java/com/core/client/rest/CoreRestClient.java @@ -4,7 +4,6 @@ import com.core.Controller; import com.core.client.ICoreClient; import com.core.data.*; import com.core.graph.NetworkGraph; -import com.core.ui.Toast; import com.core.utils.WebUtils; import lombok.Data; import org.apache.logging.log4j.LogManager; @@ -36,57 +35,24 @@ public class CoreRestClient implements ICoreClient { this.baseUrl = url; } + @Override + public void updateState(SessionState state) { + sessionState = state; + } + + @Override + public void updateSession(Integer sessionId) { + this.sessionId = sessionId; + } + private String getUrl(String path) { return String.format("%s/%s", baseUrl, path); } @Override - public void joinSession(Integer joinId, boolean notification) throws IOException { - networkGraph.reset(); - GetSession session = getSession(joinId); - sessionId = joinId; - sessionState = SessionState.get(session.getState()); - - logger.info("joining core session({}) state({}): {}", sessionId, sessionState, session); - for (CoreNode node : session.getNodes()) { - NodeType nodeType = NodeType.find(node.getType(), node.getModel()); - if (nodeType == null) { - logger.info(String.format("failed to find node type(%s) model(%s): %s", - node.getType(), node.getModel(), node.getName())); - continue; - } - - node.setNodeType(nodeType); - networkGraph.addNode(node); - } - - for (CoreLink link : session.getLinks()) { - if (link.getInterfaceOne() != null || link.getInterfaceTwo() != null) { - link.setType(LinkTypes.WIRED.getValue()); - } - - networkGraph.addLink(link); - } - - networkGraph.getGraphViewer().repaint(); - - if (notification) { - Toast.info(String.format("Joined Session %s", sessionId.toString())); - } - - updateController(); - } - - @Override - public void createSession() throws IOException { + public CreatedSession createSession() throws IOException { String url = getUrl("sessions"); - CreatedSession session = WebUtils.post(url, CreatedSession.class); - - logger.info("created session: {}", session); - sessionId = session.getId(); - sessionState = SessionState.get(session.getState()); - Toast.info(String.format("Created Session %s", sessionId.toString())); - joinSession(sessionId, false); + return WebUtils.post(url, CreatedSession.class); } public GetServices getServices() throws IOException { @@ -159,11 +125,6 @@ public class CoreRestClient implements ICoreClient { return setState(SessionState.SHUTDOWN); } - @Override - public void updateState(SessionState state) { - sessionState = state; - } - @Override public boolean setState(SessionState state) throws IOException { String url = getUrl(String.format("sessions/%s/state", sessionId)); @@ -252,11 +213,6 @@ public class CoreRestClient implements ICoreClient { return WebUtils.putJson(url, setEmaneModelConfig); } - private void updateController() { - controller.getGraphToolbar().setRunButton(isRunning()); - controller.getHooksDialog().updateHooks(); - } - @Override public boolean isRunning() { return sessionState == SessionState.RUNTIME; @@ -270,10 +226,9 @@ public class CoreRestClient implements ICoreClient { } @Override - public void openSession(File file) throws IOException { + public CreatedSession openSession(File file) throws IOException { String url = getUrl("sessions/xml"); - CreatedSession createdSession = WebUtils.postFile(url, file, CreatedSession.class); - joinSession(createdSession.getId(), true); + return WebUtils.postFile(url, file, CreatedSession.class); } @Override diff --git a/corefx/src/main/java/com/core/ui/dialogs/SessionsDialog.java b/corefx/src/main/java/com/core/ui/dialogs/SessionsDialog.java index d459d5db..1d136cb8 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/SessionsDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/SessionsDialog.java @@ -34,7 +34,8 @@ public class SessionsDialog extends StageDialog { SessionRow row = sessionsTable.getSelectionModel().getSelectedItem(); logger.info("selected session: {}", row); try { - getCoreClient().joinSession(row.getId(), true); + getController().joinSession(row.getId()); + Toast.info(String.format("Joined Session %s", row.getId())); } catch (IOException ex) { Toast.error(String.format("error joining session: %s", row.getId()), ex); } diff --git a/corefx/src/main/java/com/core/ui/dialogs/SessionsFoenixDialog.java b/corefx/src/main/java/com/core/ui/dialogs/SessionsFoenixDialog.java index 12d3c1be..5d13d91e 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/SessionsFoenixDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/SessionsFoenixDialog.java @@ -4,7 +4,7 @@ import com.core.Controller; import com.core.client.rest.GetSessions; import com.core.client.rest.GetSessionsData; import com.core.data.SessionState; -import com.core.ui.dialogs.CoreFoenixDialog; +import com.core.ui.Toast; import com.jfoenix.controls.JFXButton; import javafx.fxml.FXML; import javafx.scene.control.TableColumn; @@ -32,7 +32,8 @@ public class SessionsFoenixDialog extends CoreFoenixDialog { SessionRow row = sessionsTable.getSelectionModel().getSelectedItem(); logger.info("selected session: {}", row); try { - getCoreClient().joinSession(row.getId(), true); + getController().joinSession(row.getId()); + Toast.info(String.format("Joined Session %s", row.getId())); } catch (IOException ex) { logger.error("error joining session: {}", row.getId()); }