corefx - moved join session out into controller

This commit is contained in:
Blake J. Harnden 2018-11-21 10:48:42 -08:00
parent 5e33583dd4
commit 2694ae3630
5 changed files with 81 additions and 77 deletions

View file

@ -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<String> 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);
}

View file

@ -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;

View file

@ -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

View file

@ -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);
}

View file

@ -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());
}