corefx moved some data objects into a common package, removed libraries and code for using previous experimental rest interface

This commit is contained in:
Blake J. Harnden 2019-06-14 13:59:18 -07:00
parent 56993ec44f
commit 541e719c07
20 changed files with 9 additions and 887 deletions

View file

@ -15,6 +15,7 @@
<jung.version>2.1.1</jung.version>
<jackson.version>2.9.9</jackson.version>
<grpc.version>1.20.0</grpc.version>
<log4j.version>2.9.0</log4j.version>
</properties>
<dependencies>
@ -58,25 +59,15 @@
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.9.0</version>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>io.socket</groupId>
<artifactId>socket.io-client</artifactId>
<version>0.8.3</version>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>

View file

@ -1,8 +1,8 @@
package com.core.client;
import com.core.Controller;
import com.core.client.rest.ServiceFile;
import com.core.client.rest.WlanConfig;
import com.core.data.ServiceFile;
import com.core.data.WlanConfig;
import com.core.data.*;
import java.io.File;

View file

@ -2,8 +2,6 @@ package com.core.client.grpc;
import com.core.Controller;
import com.core.client.ICoreClient;
import com.core.client.rest.ServiceFile;
import com.core.client.rest.WlanConfig;
import com.core.data.*;
import com.core.ui.dialogs.MobilityPlayerDialog;
import inet.ipaddr.IPAddress;

View file

@ -1,430 +0,0 @@
package com.core.client.rest;
import com.core.Controller;
import com.core.client.ICoreClient;
import com.core.data.*;
import com.core.utils.WebUtils;
import com.core.websocket.CoreWebSocket;
import lombok.Data;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
@Data
public class CoreRestClient implements ICoreClient {
private static final Logger logger = LogManager.getLogger();
private String address;
private int port;
private Integer sessionId;
private SessionState sessionState;
private CoreWebSocket coreWebSocket;
@Override
public void setConnection(String address, int port) {
this.address = address;
this.port = port;
}
@Override
public boolean isLocalConnection() {
return address.equals("127.0.0.1") || address.equals("localhost");
}
@Override
public Integer currentSession() {
return sessionId;
}
@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("http://%s:%s/%s", address, port, path);
}
@Override
public SessionOverview createSession() throws IOException {
String url = getUrl("sessions");
return WebUtils.post(url, SessionOverview.class);
}
@Override
public boolean deleteSession(Integer sessionId) throws IOException {
String path = String.format("sessions/%s", sessionId);
String url = getUrl(path);
return WebUtils.delete(url);
}
public Map<String, List<String>> getServices() throws IOException {
String url = getUrl("services");
GetServices getServices = WebUtils.getJson(url, GetServices.class);
return getServices.getGroups();
}
@Override
public Session getSession(Integer sessionId) throws IOException {
String path = String.format("sessions/%s", sessionId);
String url = getUrl(path);
return WebUtils.getJson(url, Session.class);
}
@Override
public List<SessionOverview> getSessions() throws IOException {
String url = getUrl("sessions");
GetSessions getSessions = WebUtils.getJson(url, GetSessions.class);
return getSessions.getSessions();
}
@Override
public boolean start(Collection<CoreNode> nodes, Collection<CoreLink> links, List<Hook> hooks) throws IOException {
boolean result = setState(SessionState.DEFINITION);
if (!result) {
return false;
}
result = setState(SessionState.CONFIGURATION);
if (!result) {
return false;
}
for (Hook hook : hooks) {
if (!createHook(hook)) {
return false;
}
}
for (CoreNode node : nodes) {
// must pre-configure wlan nodes, if not already
if (node.getNodeType().getValue() == NodeType.WLAN) {
WlanConfig config = getWlanConfig(node);
setWlanConfig(node, config);
}
if (!createNode(node)) {
return false;
}
}
for (CoreLink link : links) {
if (!createLink(link)) {
return false;
}
}
return setState(SessionState.INSTANTIATION);
}
@Override
public boolean stop() throws IOException {
return setState(SessionState.SHUTDOWN);
}
@Override
public boolean setState(SessionState state) throws IOException {
String url = getUrl(String.format("sessions/%s/state", sessionId));
Map<String, Integer> data = new HashMap<>();
data.put("state", state.getValue());
boolean result = WebUtils.putJson(url, data);
if (result) {
sessionState = state;
}
return result;
}
private boolean uploadFile(File file) throws IOException {
String url = getUrl("upload");
return WebUtils.postFile(url, file);
}
@Override
public boolean startThroughput(Controller controller) throws IOException {
String url = getUrl("throughput/start");
return WebUtils.putJson(url);
}
@Override
public boolean stopThroughput() throws IOException {
String url = getUrl("throughput/stop");
return WebUtils.putJson(url);
}
@Override
public Map<String, List<String>> getDefaultServices() throws IOException {
String url = getUrl(String.format("sessions/%s/services/default", sessionId));
GetDefaultServices getDefaultServices = WebUtils.getJson(url, GetDefaultServices.class);
return getDefaultServices.getDefaults();
}
@Override
public boolean setDefaultServices(Map<String, Set<String>> defaults) throws IOException {
String url = getUrl(String.format("sessions/%s/services/default", sessionId));
return WebUtils.postJson(url, defaults);
}
@Override
public CoreService getService(CoreNode node, String serviceName) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s", sessionId, node.getId(), serviceName));
return WebUtils.getJson(url, CoreService.class);
}
@Override
public boolean setService(CoreNode node, String serviceName, CoreService service) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s", sessionId, node.getId(), serviceName));
return WebUtils.putJson(url, service);
}
@Override
public String getServiceFile(CoreNode node, String serviceName, String fileName) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/file", sessionId, node.getId(),
serviceName));
Map<String, String> args = new HashMap<>();
args.put("file", fileName);
return WebUtils.getJson(url, String.class, args);
}
@Override
public boolean startService(CoreNode node, String serviceName) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/start", sessionId, node.getId(),
serviceName));
return WebUtils.putJson(url);
}
@Override
public boolean stopService(CoreNode node, String serviceName) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/stop", sessionId, node.getId(),
serviceName));
return WebUtils.putJson(url);
}
@Override
public boolean restartService(CoreNode node, String serviceName) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/restart", sessionId, node.getId(),
serviceName));
return WebUtils.putJson(url);
}
@Override
public boolean validateService(CoreNode node, String serviceName) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/validate", sessionId, node.getId(),
serviceName));
return WebUtils.putJson(url);
}
@Override
public boolean setServiceFile(CoreNode node, String serviceName, ServiceFile serviceFile) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/services/%s/file", sessionId, node.getId(),
serviceName));
return WebUtils.putJson(url, serviceFile);
}
@Override
public List<String> getEmaneModels() throws IOException {
String url = getUrl(String.format("sessions/%s/emane/models", sessionId));
GetEmaneModels getEmaneModels = WebUtils.getJson(url, GetEmaneModels.class);
return getEmaneModels.getModels();
}
@Override
public List<ConfigGroup> getEmaneModelConfig(Integer id, String model) throws IOException {
String url = getUrl(String.format("sessions/%s/emane/model/config", sessionId));
Map<String, String> args = new HashMap<>();
args.put("node", id.toString());
args.put("name", model);
GetConfig getConfig = WebUtils.getJson(url, GetConfig.class, args);
return getConfig.getGroups();
}
@Override
public List<ConfigGroup> getEmaneConfig(CoreNode node) throws IOException {
String url = getUrl(String.format("sessions/%s/emane/config", sessionId));
Map<String, String> args = new HashMap<>();
args.put("node", node.getId().toString());
GetConfig getConfig = WebUtils.getJson(url, GetConfig.class, args);
return getConfig.getGroups();
}
@Override
public boolean setEmaneConfig(CoreNode node, List<ConfigOption> options) throws IOException {
String url = getUrl(String.format("sessions/%s/emane/config", sessionId));
SetEmaneConfig setEmaneConfig = new SetEmaneConfig();
setEmaneConfig.setNode(node.getId());
setEmaneConfig.setValues(options);
return WebUtils.putJson(url, setEmaneConfig);
}
@Override
public boolean setEmaneModelConfig(Integer id, String model, List<ConfigOption> options) throws IOException {
String url = getUrl(String.format("sessions/%s/emane/model/config", sessionId));
SetEmaneModelConfig setEmaneModelConfig = new SetEmaneModelConfig();
setEmaneModelConfig.setNode(id);
setEmaneModelConfig.setName(model);
setEmaneModelConfig.setValues(options);
return WebUtils.putJson(url, setEmaneModelConfig);
}
@Override
public boolean isRunning() {
return sessionState == SessionState.RUNTIME;
}
@Override
public void saveSession(File file) throws IOException {
String path = String.format("sessions/%s/xml", sessionId);
String url = getUrl(path);
WebUtils.getFile(url, file);
}
@Override
public SessionOverview openSession(File file) throws IOException {
String url = getUrl("sessions/xml");
return WebUtils.postFile(url, file, SessionOverview.class);
}
@Override
public List<ConfigGroup> getSessionConfig() throws IOException {
String url = getUrl(String.format("sessions/%s/options", sessionId));
GetConfig getConfig = WebUtils.getJson(url, GetConfig.class);
return getConfig.getGroups();
}
@Override
public boolean setSessionConfig(List<ConfigOption> configOptions) throws IOException {
String url = getUrl(String.format("sessions/%s/options", sessionId));
SetConfig setConfig = new SetConfig(configOptions);
return WebUtils.putJson(url, setConfig);
}
@Override
public LocationConfig getLocationConfig() throws IOException {
String url = getUrl(String.format("sessions/%s/location", sessionId));
return WebUtils.getJson(url, LocationConfig.class);
}
@Override
public boolean setLocationConfig(LocationConfig config) throws IOException {
String url = getUrl(String.format("sessions/%s/location", sessionId));
return WebUtils.putJson(url, config);
}
@Override
public String nodeCommand(CoreNode node, String command) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/command", sessionId, node.getId()));
return WebUtils.putJson(url, command, String.class);
}
@Override
public boolean createNode(CoreNode node) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes", sessionId));
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()));
return WebUtils.delete(url);
}
@Override
public boolean createLink(CoreLink link) throws IOException {
String url = getUrl(String.format("sessions/%s/links", sessionId));
return WebUtils.postJson(url, link);
}
@Override
public boolean editLink(CoreLink link) throws IOException {
String url = getUrl(String.format("sessions/%s/links", sessionId));
return WebUtils.putJson(url, link);
}
@Override
public boolean createHook(Hook hook) throws IOException {
String url = getUrl(String.format("sessions/%s/hooks", sessionId));
return WebUtils.postJson(url, hook);
}
@Override
public List<Hook> getHooks() throws IOException {
String url = getUrl(String.format("sessions/%s/hooks", sessionId));
GetHooks getHooks = WebUtils.getJson(url, GetHooks.class);
return getHooks.getHooks();
}
@Override
public WlanConfig getWlanConfig(CoreNode node) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/wlan", sessionId, node.getId()));
return WebUtils.getJson(url, WlanConfig.class);
}
@Override
public boolean setWlanConfig(CoreNode node, WlanConfig config) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/wlan", sessionId, node.getId()));
return WebUtils.putJson(url, config);
}
@Override
public String getTerminalCommand(CoreNode node) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/terminal", sessionId, node.getId()));
return WebUtils.getJson(url, String.class);
}
@Override
public boolean setMobilityConfig(CoreNode node, MobilityConfig config) throws IOException {
boolean uploaded = uploadFile(config.getScriptFile());
if (!uploaded) {
throw new IOException("failed to upload mobility script");
}
String url = getUrl(String.format("sessions/%s/nodes/%s/mobility", sessionId, node.getId()));
config.setFile(config.getScriptFile().getName());
return WebUtils.postJson(url, config);
}
@Override
public Map<Integer, MobilityConfig> getMobilityConfigs() throws IOException {
String url = getUrl(String.format("sessions/%s/mobility/configs", sessionId));
GetMobilityConfigs getMobilityConfigs = WebUtils.getJson(url, GetMobilityConfigs.class);
return getMobilityConfigs.getConfigurations();
}
@Override
public MobilityConfig getMobilityConfig(CoreNode node) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/mobility", sessionId, node.getId()));
return WebUtils.getJson(url, MobilityConfig.class);
}
@Override
public boolean mobilityAction(CoreNode node, String action) throws IOException {
String url = getUrl(String.format("sessions/%s/nodes/%s/mobility/%s", sessionId, node.getId(), action));
return WebUtils.putJson(url);
}
@Override
public void setupEventHandlers(Controller controller) throws IOException {
coreWebSocket.stop();
coreWebSocket = new CoreWebSocket(controller);
try {
coreWebSocket.start(address, port);
} catch (URISyntaxException ex) {
throw new IOException("error starting web socket", ex);
}
}
}

View file

@ -1,12 +0,0 @@
package com.core.client.rest;
import com.core.data.ConfigGroup;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class GetConfig {
private List<ConfigGroup> groups = new ArrayList<>();
}

View file

@ -1,16 +0,0 @@
package com.core.client.rest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GetDefaultServices {
private Map<String, List<String>> defaults = new HashMap<>();
}

View file

@ -1,11 +0,0 @@
package com.core.client.rest;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class GetEmaneModels {
private List<String> models = new ArrayList<>();
}

View file

@ -1,12 +0,0 @@
package com.core.client.rest;
import com.core.data.Hook;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class GetHooks {
private List<Hook> hooks = new ArrayList<>();
}

View file

@ -1,12 +0,0 @@
package com.core.client.rest;
import com.core.data.MobilityConfig;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class GetMobilityConfigs {
private Map<Integer, MobilityConfig> configurations = new HashMap<>();
}

View file

@ -1,13 +0,0 @@
package com.core.client.rest;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
import java.util.Map;
@Data
@NoArgsConstructor
public class GetServices {
private Map<String, List<String>> groups;
}

View file

@ -1,14 +0,0 @@
package com.core.client.rest;
import com.core.data.SessionOverview;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
public class GetSessions {
private List<SessionOverview> sessions = new ArrayList<>();
}

View file

@ -1,16 +0,0 @@
package com.core.client.rest;
import com.core.data.ConfigOption;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SetConfig {
private List<ConfigOption> values = new ArrayList<>();
}

View file

@ -1,13 +0,0 @@
package com.core.client.rest;
import com.core.data.ConfigOption;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class SetEmaneConfig {
private Integer node;
private List<ConfigOption> values = new ArrayList<>();
}

View file

@ -1,14 +0,0 @@
package com.core.client.rest;
import com.core.data.ConfigOption;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class SetEmaneModelConfig {
private Integer node;
private String name;
private List<ConfigOption> values = new ArrayList<>();
}

View file

@ -1,4 +1,4 @@
package com.core.client.rest;
package com.core.data;
import lombok.AllArgsConstructor;
import lombok.Data;

View file

@ -1,4 +1,4 @@
package com.core.client.rest;
package com.core.data;
import lombok.Data;

View file

@ -1,7 +1,7 @@
package com.core.ui.dialogs;
import com.core.Controller;
import com.core.client.rest.WlanConfig;
import com.core.data.WlanConfig;
import com.core.data.CoreNode;
import com.core.ui.Toast;
import com.jfoenix.controls.JFXButton;

View file

@ -1,7 +1,7 @@
package com.core.ui.dialogs;
import com.core.Controller;
import com.core.client.rest.ServiceFile;
import com.core.data.ServiceFile;
import com.core.data.CoreNode;
import com.core.data.CoreService;
import com.jfoenix.controls.JFXButton;

View file

@ -1,174 +0,0 @@
package com.core.utils;
import com.fasterxml.jackson.core.type.TypeReference;
import okhttp3.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.util.Collections;
import java.util.Map;
public final class WebUtils {
private static final Logger logger = LogManager.getLogger();
private static final OkHttpClient client = new OkHttpClient();
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private WebUtils() {
}
public static <T> T getJson(String url, Class<T> clazz) throws IOException {
return getJson(url, clazz, Collections.emptyMap());
}
public static <T> T getJson(String url, Class<T> clazz, Map<String, String> args) throws IOException {
logger.debug("get json: {}", url);
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
args.forEach(urlBuilder::addQueryParameter);
HttpUrl httpUrl = urlBuilder.build();
Request request = new Request.Builder()
.url(httpUrl)
.build();
String response = readResponse(request);
return JsonUtils.read(response, clazz);
}
public static void getFile(String url, File file) throws IOException {
logger.debug("get file: {}", url);
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
InputStream input = response.body().byteStream();
try (OutputStream output = new FileOutputStream(file)) {
int count;
byte[] data = new byte[1024];
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
}
}
}
public static boolean postFile(String url, File file) throws IOException {
MediaType mediaType = MediaType.parse("File/*");
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file))
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
try (Response response = client.newCall(request).execute()) {
return response.isSuccessful();
}
}
public static <T> T postFile(String url, File file, Class<T> clazz) throws IOException {
MediaType mediaType = MediaType.parse("File/*");
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file))
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
String response = readResponse(request);
return JsonUtils.read(response, clazz);
}
public static <T> T getJson(String url, TypeReference<T> reference) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
String response = readResponse(request);
return JsonUtils.getMapper().readValue(response, reference);
}
private static String readResponse(Request request) throws IOException {
try (Response response = client.newCall(request).execute()) {
ResponseBody body = response.body();
if (body == null) {
throw new IOException("failed to received body");
} else {
return body.string();
}
}
}
public static boolean postJson(String url, Object json) throws IOException {
logger.debug("post json: {} - {}", url, json);
RequestBody body = RequestBody.create(JSON, JsonUtils.toString(json));
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.isSuccessful();
}
}
public static boolean putJson(String url) throws IOException {
logger.debug("put json: {}", url);
RequestBody body = new FormBody.Builder().build();
Request request = new Request.Builder()
.url(url)
.put(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.isSuccessful();
}
}
public static boolean putJson(String url, Object json) throws IOException {
logger.debug("put json: {} - {}", url, json);
RequestBody body = RequestBody.create(JSON, JsonUtils.toString(json));
Request request = new Request.Builder()
.url(url)
.put(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.isSuccessful();
}
}
public static <T> T putJson(String url, Object json, Class<T> clazz) throws IOException {
logger.debug("put json: {} - {}", url, json);
RequestBody body = RequestBody.create(JSON, JsonUtils.toString(json));
Request request = new Request.Builder()
.url(url)
.put(body)
.build();
String response = readResponse(request);
return JsonUtils.read(response, clazz);
}
public static <T> T post(String url, Class<T> clazz) throws IOException {
logger.debug("post: {}", url);
RequestBody body = new FormBody.Builder().build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
String response = readResponse(request);
return JsonUtils.read(response, clazz);
}
public static boolean delete(String url) throws IOException {
logger.debug("delete: {}", url);
Request request = new Request.Builder()
.url(url)
.delete()
.build();
try (Response response = client.newCall(request).execute()) {
return response.isSuccessful();
}
}
}

View file

@ -1,130 +0,0 @@
package com.core.websocket;
import com.core.Controller;
import com.core.data.*;
import com.core.ui.dialogs.MobilityPlayerDialog;
import com.core.utils.JsonUtils;
import io.socket.client.IO;
import io.socket.client.Socket;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
import java.net.URISyntaxException;
public class CoreWebSocket {
private static final Logger logger = LogManager.getLogger();
private final Controller controller;
private Thread socketThread;
private Socket socket;
public CoreWebSocket(Controller controller) {
this.controller = controller;
}
public void start(String address, int port) throws URISyntaxException {
socket = IO.socket(String.format("http://%s:%s", address, port));
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("throughput", this::handleThroughputs);
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 handleThroughputs(Object... args) {
for (Object arg : args) {
logger.info("throughput update: {}", arg);
try {
Throughputs throughputs = JsonUtils.read(arg.toString(), Throughputs.class);
controller.handleThroughputs(throughputs);
} catch (IOException ex) {
logger.error("error getting throughputs", ex);
}
}
}
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.warn("unknown event type: {}", event.getEventType().getValue());
return;
}
// session state event
if (state.getValue() <= 6) {
logger.info("event updating session state: {}", state);
controller.getCoreClient().updateState(state);
// mobility script event
} else if (state.getValue() <= 9) {
Integer nodeId = event.getNode();
String[] values = event.getData().split("\\s+");
Integer start = Integer.parseInt(values[0].split("=")[1]);
Integer end = Integer.parseInt(values[1].split("=")[1]);
logger.info(String.format("node(%s) mobility event (%s) - start(%s) stop(%s)",
nodeId, state, start, end));
logger.info("all dialogs: {}", controller.getMobilityPlayerDialogs().keySet());
MobilityPlayerDialog mobilityPlayerDialog = controller.getMobilityPlayerDialogs().get(nodeId);
mobilityPlayerDialog.event(state, start, end);
}
} 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);
}
}
}