(rest) - added broadcast for links,configs,exceptions,files and fixed edit node (gui) - added handling of broadcast links, different rendering for wireless links, removal of wirelesss links on stop
This commit is contained in:
parent
e7a56cc3ad
commit
f062e2868d
11 changed files with 307 additions and 44 deletions
|
@ -20,6 +20,8 @@ public interface ICoreClient {
|
|||
|
||||
boolean start() throws IOException;
|
||||
|
||||
boolean stop() throws IOException;
|
||||
|
||||
void updateState(SessionState state);
|
||||
|
||||
boolean setState(SessionState state) throws IOException;
|
||||
|
@ -56,6 +58,8 @@ public interface ICoreClient {
|
|||
|
||||
boolean createNode(CoreNode node) throws IOException;
|
||||
|
||||
boolean editNode(CoreNode node) throws IOException;
|
||||
|
||||
boolean deleteNode(CoreNode node) throws IOException;
|
||||
|
||||
boolean createLink(CoreLink link) throws IOException;
|
||||
|
|
|
@ -15,6 +15,7 @@ import java.io.IOException;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Data
|
||||
public class CoreRestClient implements ICoreClient {
|
||||
|
@ -55,6 +56,10 @@ public class CoreRestClient implements ICoreClient {
|
|||
}
|
||||
|
||||
for (CoreLink link : session.getLinks()) {
|
||||
if (link.getInterfaceOne() != null || link.getInterfaceTwo() != null) {
|
||||
link.setType(LinkTypes.WIRED.getValue());
|
||||
}
|
||||
|
||||
networkGraph.addLink(link);
|
||||
}
|
||||
|
||||
|
@ -157,6 +162,17 @@ public class CoreRestClient implements ICoreClient {
|
|||
return setState(SessionState.INSTANTIATION);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(SessionState state) {
|
||||
sessionState = state;
|
||||
|
@ -292,6 +308,12 @@ public class CoreRestClient implements ICoreClient {
|
|||
return WebUtils.postJson(url, node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean editNode(CoreNode node) throws IOException {
|
||||
String url = getUrl(String.format("sessions/%s/nodes/%s", sessionId, node.getId()));
|
||||
return WebUtils.putJson(url, node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteNode(CoreNode node) throws IOException {
|
||||
String url = getUrl(String.format("/sessions/%s/nodes/%s", sessionId, node.getId()));
|
||||
|
|
|
@ -20,6 +20,9 @@ public class CoreLink {
|
|||
@JsonIgnore
|
||||
private boolean visible = true;
|
||||
|
||||
@JsonProperty("message_type")
|
||||
private Integer messageType;
|
||||
|
||||
private Integer type = 1;
|
||||
|
||||
@JsonProperty("node_one")
|
||||
|
@ -41,4 +44,8 @@ public class CoreLink {
|
|||
this.weight = (float) id;
|
||||
this.loaded = false;
|
||||
}
|
||||
|
||||
public boolean isWireless() {
|
||||
return interfaceOne == null && interfaceTwo == null;
|
||||
}
|
||||
}
|
||||
|
|
31
corefx/src/main/java/com/core/data/LinkTypes.java
Normal file
31
corefx/src/main/java/com/core/data/LinkTypes.java
Normal file
|
@ -0,0 +1,31 @@
|
|||
package com.core.data;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum LinkTypes {
|
||||
WIRELESS(0),
|
||||
WIRED(1);
|
||||
|
||||
private static final Map<Integer, LinkTypes> LOOKUP = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (LinkTypes state : LinkTypes.values()) {
|
||||
LOOKUP.put(state.getValue(), state);
|
||||
}
|
||||
}
|
||||
|
||||
private final int value;
|
||||
|
||||
LinkTypes(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public static LinkTypes get(int value) {
|
||||
return LOOKUP.get(value);
|
||||
}
|
||||
}
|
36
corefx/src/main/java/com/core/data/MessageFlags.java
Normal file
36
corefx/src/main/java/com/core/data/MessageFlags.java
Normal file
|
@ -0,0 +1,36 @@
|
|||
package com.core.data;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public enum MessageFlags {
|
||||
ADD(1),
|
||||
DELETE(2),
|
||||
CRI(4),
|
||||
LOCAL(8),
|
||||
STRING(16),
|
||||
TEXT(32),
|
||||
TTY(64);
|
||||
|
||||
private static final Map<Integer, MessageFlags> LOOKUP = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (MessageFlags state : MessageFlags.values()) {
|
||||
LOOKUP.put(state.getValue(), state);
|
||||
}
|
||||
}
|
||||
|
||||
private final int value;
|
||||
|
||||
MessageFlags(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public static MessageFlags get(int value) {
|
||||
return LOOKUP.get(value);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,7 @@
|
|||
package com.core.graph;
|
||||
|
||||
import com.core.Controller;
|
||||
import com.core.data.CoreInterface;
|
||||
import com.core.data.CoreLink;
|
||||
import com.core.data.CoreNode;
|
||||
import com.core.data.NodeType;
|
||||
import com.core.data.*;
|
||||
import com.core.ui.Toast;
|
||||
import com.core.utils.IconUtils;
|
||||
import com.google.common.base.Supplier;
|
||||
|
@ -20,6 +17,7 @@ import edu.uci.ics.jung.visualization.annotations.AnnotationControls;
|
|||
import edu.uci.ics.jung.visualization.control.EditingModalGraphMouse;
|
||||
import edu.uci.ics.jung.visualization.control.GraphMouseListener;
|
||||
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
|
||||
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
|
||||
import edu.uci.ics.jung.visualization.renderers.Renderer;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.net.util.SubnetUtils;
|
||||
|
@ -30,7 +28,6 @@ import javax.swing.*;
|
|||
import java.awt.*;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.geom.Ellipse2D;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
@ -90,9 +87,25 @@ public class NetworkGraph {
|
|||
});
|
||||
|
||||
// edge render properties
|
||||
renderContext.setEdgeStrokeTransformer(edge -> new BasicStroke(1));
|
||||
renderContext.setEdgeShapeTransformer(edge -> new Rectangle2D.Float(0, 0, 1, 10));
|
||||
renderContext.setEdgeFillPaintTransformer(edge -> Color.BLACK);
|
||||
renderContext.setEdgeStrokeTransformer(edge -> {
|
||||
LinkTypes linkType = LinkTypes.get(edge.getType());
|
||||
if (LinkTypes.WIRELESS == linkType) {
|
||||
float[] dash = {15.0f};
|
||||
return new BasicStroke(5, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND,
|
||||
0, dash, 0);
|
||||
} else {
|
||||
return new BasicStroke(5);
|
||||
}
|
||||
});
|
||||
renderContext.setEdgeShapeTransformer(EdgeShape.line(graph));
|
||||
renderContext.setEdgeDrawPaintTransformer(edge -> {
|
||||
LinkTypes linkType = LinkTypes.get(edge.getType());
|
||||
if (LinkTypes.WIRELESS == linkType) {
|
||||
return Color.BLUE;
|
||||
} else {
|
||||
return Color.BLACK;
|
||||
}
|
||||
});
|
||||
renderContext.setEdgeIncludePredicate(predicate -> predicate.element.isVisible());
|
||||
|
||||
graphViewer.setVertexToolTipTransformer(renderContext.getVertexLabelTransformer());
|
||||
|
@ -144,6 +157,15 @@ public class NetworkGraph {
|
|||
logger.debug("graph moved node({}): {},{}", node.getName(), x, y);
|
||||
node.getPosition().setX(x);
|
||||
node.getPosition().setY(y);
|
||||
|
||||
// upate node when session is active
|
||||
if (controller.getCoreClient().isRunning()) {
|
||||
try {
|
||||
controller.getCoreClient().editNode(node);
|
||||
} catch (IOException ex) {
|
||||
Toast.error("failed to update node location");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -224,7 +246,7 @@ public class NetworkGraph {
|
|||
link.setInterfaceTwo(interfaceTwo);
|
||||
}
|
||||
|
||||
boolean isVisible = !isWirelessLink(nodeOne, nodeTwo);
|
||||
boolean isVisible = !checkForWirelessNode(nodeOne, nodeTwo);
|
||||
link.setVisible(isVisible);
|
||||
|
||||
logger.info("adding user created edge: {}", link);
|
||||
|
@ -342,7 +364,7 @@ public class NetworkGraph {
|
|||
return node.getType() == NodeType.EMANE || node.getType() == NodeType.WLAN;
|
||||
}
|
||||
|
||||
private boolean isWirelessLink(CoreNode nodeOne, CoreNode nodeTwo) {
|
||||
private boolean checkForWirelessNode(CoreNode nodeOne, CoreNode nodeTwo) {
|
||||
boolean result = isWirelessNode(nodeOne);
|
||||
return result || isWirelessNode(nodeTwo);
|
||||
}
|
||||
|
@ -363,12 +385,23 @@ public class NetworkGraph {
|
|||
CoreNode nodeOne = nodeMap.get(link.getNodeOne());
|
||||
CoreNode nodeTwo = nodeMap.get(link.getNodeTwo());
|
||||
|
||||
boolean isVisible = !isWirelessLink(nodeOne, nodeTwo);
|
||||
boolean isVisible = !checkForWirelessNode(nodeOne, nodeTwo);
|
||||
link.setVisible(isVisible);
|
||||
|
||||
graph.addEdge(link, nodeOne, nodeTwo);
|
||||
}
|
||||
|
||||
public void removeWirelessLink(CoreLink link) {
|
||||
logger.info("deleting link: {}", link);
|
||||
CoreNode nodeOne = nodeMap.get(link.getNodeOne());
|
||||
CoreNode nodeTwo = nodeMap.get(link.getNodeTwo());
|
||||
|
||||
CoreLink existingLink = graph.findEdge(nodeOne, nodeTwo);
|
||||
if (existingLink != null) {
|
||||
graph.removeEdge(existingLink);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeLink(CoreLink link) {
|
||||
graphViewer.getPickedEdgeState().pick(link, false);
|
||||
graph.removeEdge(link);
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.core.ui;
|
|||
|
||||
import com.core.Controller;
|
||||
import com.core.data.NodeType;
|
||||
import com.core.data.SessionState;
|
||||
import com.core.utils.IconUtils;
|
||||
import com.jfoenix.controls.JFXButton;
|
||||
import com.jfoenix.controls.JFXListView;
|
||||
|
@ -266,7 +265,7 @@ public class GraphToolbar extends VBox {
|
|||
|
||||
private void stopSession() {
|
||||
try {
|
||||
boolean result = controller.getCoreClient().setState(SessionState.SHUTDOWN);
|
||||
boolean result = controller.getCoreClient().stop();
|
||||
if (result) {
|
||||
controller.sessionStopped();
|
||||
Toast.success("Session Stopped");
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package com.core.websocket;
|
||||
|
||||
import com.core.Controller;
|
||||
import com.core.data.CoreEvent;
|
||||
import com.core.data.CoreNode;
|
||||
import com.core.data.SessionState;
|
||||
import com.core.data.*;
|
||||
import com.core.utils.JsonUtils;
|
||||
import io.socket.client.IO;
|
||||
import io.socket.client.Socket;
|
||||
|
@ -27,37 +25,69 @@ public class CoreWebSocket {
|
|||
socket.on(Socket.EVENT_CONNECT, args -> {
|
||||
logger.info("connected to web socket");
|
||||
});
|
||||
socket.on("node", args -> {
|
||||
for (Object arg : args) {
|
||||
try {
|
||||
CoreNode node = JsonUtils.read(arg.toString(), CoreNode.class);
|
||||
logger.info("core node update: {}", node);
|
||||
controller.getNetworkGraph().setNodeLocation(node);
|
||||
} catch (IOException ex) {
|
||||
logger.error("error getting core node", ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
socket.on("event", args -> {
|
||||
for (Object arg : args) {
|
||||
try {
|
||||
CoreEvent event = JsonUtils.read(arg.toString(), CoreEvent.class);
|
||||
logger.info("core event: {}", event);
|
||||
SessionState state = SessionState.get(event.getEventType().getValue());
|
||||
if (state != null) {
|
||||
logger.info("event updating session state: {}", state);
|
||||
controller.getCoreClient().updateState(state);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
logger.error("error getting core event", ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
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");
|
||||
});
|
||||
}
|
||||
|
||||
private void handleNodes(Object... args) {
|
||||
for (Object arg : args) {
|
||||
try {
|
||||
CoreNode node = JsonUtils.read(arg.toString(), CoreNode.class);
|
||||
logger.info("core node update: {}", node);
|
||||
controller.getNetworkGraph().setNodeLocation(node);
|
||||
} catch (IOException ex) {
|
||||
logger.error("error getting core node", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleEvents(Object... args) {
|
||||
for (Object arg : args) {
|
||||
try {
|
||||
CoreEvent event = JsonUtils.read(arg.toString(), CoreEvent.class);
|
||||
logger.info("handling broadcast event: {}", event);
|
||||
SessionState state = SessionState.get(event.getEventType().getValue());
|
||||
if (state != null) {
|
||||
logger.info("event updating session state: {}", state);
|
||||
controller.getCoreClient().updateState(state);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
logger.error("error getting core event", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleLinks(Object... args) {
|
||||
for (Object arg : args) {
|
||||
try {
|
||||
CoreLink link = JsonUtils.read(arg.toString(), CoreLink.class);
|
||||
logger.info("handling broadcast link: {}", link);
|
||||
MessageFlags flag = MessageFlags.get(link.getMessageType());
|
||||
if (MessageFlags.DELETE == flag) {
|
||||
logger.info("delete");
|
||||
controller.getNetworkGraph().removeWirelessLink(link);
|
||||
} else if (MessageFlags.ADD == flag) {
|
||||
link.setLoaded(true);
|
||||
controller.getNetworkGraph().addLink(link);
|
||||
}
|
||||
controller.getNetworkGraph().getGraphViewer().repaint();
|
||||
} catch (IOException ex) {
|
||||
logger.error("error handling broadcast link", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleConfigs(Object... args) {
|
||||
for (Object arg : args) {
|
||||
logger.info("handling broadcast config: {}", arg);
|
||||
}
|
||||
}
|
||||
|
||||
public void start() {
|
||||
logger.info("attempting to connect to web socket!");
|
||||
socketThread = new Thread(socket::connect);
|
||||
|
|
|
@ -56,8 +56,9 @@ def edit_node(session_id, node_id):
|
|||
data = request.get_json() or {}
|
||||
|
||||
node_options = NodeOptions()
|
||||
x = data.get("x")
|
||||
y = data.get("y")
|
||||
node_position = data["position"]
|
||||
x = node_position["x"]
|
||||
y = node_position["y"]
|
||||
node_options.set_position(x, y)
|
||||
lat = data.get("lat")
|
||||
lon = data.get("lon")
|
||||
|
|
|
@ -39,6 +39,10 @@ def create_session():
|
|||
# add handlers
|
||||
session.event_handlers.append(websocket_routes.broadcast_event)
|
||||
session.node_handlers.append(websocket_routes.broadcast_node)
|
||||
session.config_handlers.append(websocket_routes.broadcast_config)
|
||||
session.link_handlers.append(websocket_routes.broadcast_link)
|
||||
session.exception_handlers.append(websocket_routes.broadcast_exception)
|
||||
session.file_handlers.append(websocket_routes.broadtcast_file)
|
||||
|
||||
response_data = jsonify(
|
||||
id=session.session_id,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from flask_socketio import SocketIO
|
||||
|
||||
import core_utils
|
||||
from core import logger
|
||||
|
||||
socketio = None
|
||||
|
@ -18,6 +19,101 @@ def register(app):
|
|||
logger.info("websocket client disconnected")
|
||||
|
||||
|
||||
def broadtcast_file(file_data):
|
||||
socketio.emit("file", {
|
||||
"message_type": file_data.message_type,
|
||||
"node": file_data.node,
|
||||
"name": file_data.name,
|
||||
"mode": file_data.mode,
|
||||
"number": file_data.number,
|
||||
"type": file_data.type,
|
||||
"source": file_data.source,
|
||||
"session": file_data.session,
|
||||
"data": file_data.data,
|
||||
"compressed_data": file_data.compressed_data
|
||||
})
|
||||
|
||||
|
||||
def broadcast_exception(exception_data):
|
||||
socketio.emit("exception", {
|
||||
"node": exception_data.node,
|
||||
"session": exception_data.session,
|
||||
"level": exception_data.level,
|
||||
"source": exception_data.source,
|
||||
"date": exception_data.date,
|
||||
"text": exception_data.text,
|
||||
"opaque": exception_data.opaque
|
||||
})
|
||||
|
||||
|
||||
def broadcast_link(link_data):
|
||||
logger.info("broadcasting link")
|
||||
interface_one = None
|
||||
if link_data.interface1_id is not None:
|
||||
interface_one = {
|
||||
"id": link_data.interface1_id,
|
||||
"name": link_data.interface1_name,
|
||||
"mac": core_utils.convert_value(link_data.interface1_mac),
|
||||
"ip4": core_utils.convert_value(link_data.interface1_ip4),
|
||||
"ip4mask": link_data.interface1_ip4_mask,
|
||||
"ip6": core_utils.convert_value(link_data.interface1_ip6),
|
||||
"ip6mask": link_data.interface1_ip6_mask,
|
||||
}
|
||||
|
||||
interface_two = None
|
||||
if link_data.interface2_id is not None:
|
||||
interface_two = {
|
||||
"id": link_data.interface2_id,
|
||||
"name": link_data.interface2_name,
|
||||
"mac": core_utils.convert_value(link_data.interface2_mac),
|
||||
"ip4": core_utils.convert_value(link_data.interface2_ip4),
|
||||
"ip4mask": link_data.interface2_ip4_mask,
|
||||
"ip6": core_utils.convert_value(link_data.interface2_ip6),
|
||||
"ip6mask": link_data.interface2_ip6_mask,
|
||||
}
|
||||
|
||||
socketio.emit("link", {
|
||||
"message_type": link_data.message_type,
|
||||
"type": link_data.link_type,
|
||||
"node_one": link_data.node1_id,
|
||||
"node_two": link_data.node2_id,
|
||||
"interface_one": interface_one,
|
||||
"interface_two": interface_two,
|
||||
"options": {
|
||||
"opaque": link_data.opaque,
|
||||
"jitter": link_data.jitter,
|
||||
"key": link_data.key,
|
||||
"mburst": link_data.mburst,
|
||||
"mer": link_data.mer,
|
||||
"per": link_data.per,
|
||||
"bandwidth": link_data.bandwidth,
|
||||
"burst": link_data.burst,
|
||||
"delay": link_data.delay,
|
||||
"dup": link_data.dup,
|
||||
"unidirectional": link_data.unidirectional
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
def broadcast_config(config_data):
|
||||
socketio.emit("config", {
|
||||
"message_type": config_data.message_type,
|
||||
"node": config_data.node,
|
||||
"object": config_data.object,
|
||||
"type": config_data.type,
|
||||
"data_types": config_data.data_types,
|
||||
"data_values": config_data.data_values,
|
||||
"captions": config_data.captions,
|
||||
"bitmap": config_data.bitmap,
|
||||
"possible_values": config_data.possible_values,
|
||||
"groups": config_data.groups,
|
||||
"session": config_data.session,
|
||||
"interface_number": config_data.interface_number,
|
||||
"network_id": config_data.network_id,
|
||||
"opaque": config_data.opaque
|
||||
})
|
||||
|
||||
|
||||
def broadcast_event(event):
|
||||
socketio.emit("event", {
|
||||
"node": event.node,
|
||||
|
|
Loading…
Add table
Reference in a new issue