com.github.sdnwiselab.sdnwise.controller.ControllerSocketIo Maven / Gradle / Ivy
/*
* Copyright (C) 2015 SDN-WISE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.github.sdnwiselab.sdnwise.controller;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import com.github.sdnwiselab.sdnwise.flowtable.FlowTableAction;
import com.github.sdnwiselab.sdnwise.flowtable.FlowTableEntry;
import com.github.sdnwiselab.sdnwise.flowtable.FlowTableStats;
import com.github.sdnwiselab.sdnwise.flowtable.FlowTableWindow;
import com.github.sdnwiselab.sdnwise.topology.SocketIoNetworkGraph;
import com.github.sdnwiselab.sdnwise.util.NodeAddress;
import java.net.URISyntaxException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class manages event with SocketIO and communication with Node.js Server.
*
* @author Sebastiano Milardo
*/
public class ControllerSocketIo {
private Controller controller;
private Socket socket;
/**
* Constructor Method for this Class.
*
* @param ctr The Controller manages event with SocketIO.
* @param address String Address to create a connection.
*/
public ControllerSocketIo(Controller ctr, String address) {
this.controller = ctr;
socket = null;
try {
socket = IO.socket(address);
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.emit("subscribeAsControllerGui");
}
}).on("requestTable", new Emitter.Listener() {
@Override
public void call(Object... args) {
String[] idArray = ((String) args[0]).split("\\.");
byte netId = (byte)Integer.parseInt(idArray[0]);
NodeAddress addr = new NodeAddress(idArray[1] + "." + idArray[2]);
List fTable = controller.getRules(netId, addr);
String flowTableHtml = "\n"
+ " \n"
+ " Matching Rule \n"
+ " Matching Rule \n"
+ " Matching Rule \n"
+ " Action \n"
+ " Statistics \n"
+ " "
+ " \n"
+ " Location \n"
+ " Offset \n"
+ " Size \n"
+ " Operator \n"
+ " Value \n"
+ " Location \n"
+ " Offset \n"
+ " Size \n"
+ " Operator \n"
+ " Value \n"
+ " Location \n"
+ " Offset \n"
+ " Size \n"
+ " Operator \n"
+ " Value \n"
+ " Type \n"
+ " Location \n"
+ " Offset \n"
+ " Value \n"
+ " MultiMatch \n"
+ " TTL \n"
+ " Count \n"
+ " ";
StringBuilder sb = new StringBuilder(flowTableHtml);
int j = 0;
for (FlowTableEntry ft : fTable) {
if (ft != null && ft.getWindow()[0].getOperator() != 0) {
j++;
sb.append(convertFlowTableEntryToHtml(ft, (j % 2 == 0) ? "even" : "odd"));
}
}
sb.append("
");
socket.emit("receiveTable", args[0], args[1], sb.toString());
}
}).on("requestSettings", new Emitter.Listener() {
@Override
public void call(Object... args) {
String[] idArray = ((String) args[0]).split("\\.");
byte netId = (byte)Integer.parseInt(idArray[0]);
NodeAddress addr = new NodeAddress(idArray[1] + "." + idArray[2]);
String flowTableHtml = ""
+ ""
+ " Beacon Period "
+ "" + controller.getNodeBeaconPeriod(netId, addr) + " "
+ "s "
+ " "
+ " Report Period "
+ "" + controller.getNodeReportPeriod(netId, addr) + " "
+ "s "
+ " "
+ " TTL Max "
+ "" + controller.getNodeTtlMax(netId, addr) + " "
+ "n. of hops "
+ " "
+ " RSSI Min "
+ "" + controller.getNodeRssiMin(netId, addr) + " "
+ "dBm "
+ " ";
StringBuilder sb = new StringBuilder(flowTableHtml);
sb.append("
");
socket.emit("receiveSettings", args[0], args[1], sb.toString());
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.emit("unsubscribeAsControllerGui");
}
});
socket.connect();
} catch (URISyntaxException ex) {
Logger.getLogger(SocketIoNetworkGraph.class.getName()).
log(Level.SEVERE, null, ex);
}
}
/**
* This method is used to Convert FlowTableEntry in HTML code.
*
* @param ft FlowTableEntry to represent.
* @param parity string value to identify odd or even value of the
* FlowTableEntry.
* @return string value of FlowTableEntry in HTML.
*/
public String convertFlowTableEntryToHtml(FlowTableEntry ft, String parity) {
StringBuilder row = new StringBuilder("");
for (FlowTableWindow w : ft.getWindow()) {
row.append("").append(w.getMemoryToString()).append(" ");
row.append("").append(w.getAddressToString()).append(" ");
row.append("").append(w.getSizeToString()).append(" ");
row.append("").append(w.getOperatorToString()).append(" ");
row.append("").append(w.getValueToString()).append(" ");
}
FlowTableAction fa = ft.getAction();
row.append("").append(fa.getTypeToString()).append(" ");
row.append("").append(fa.getMemoryToString()).append(" ");
row.append("").append(fa.getAddressToString()).append(" ");
row.append("").append(fa.getValueToString()).append(" ");
row.append("").append(fa.getMultimatch()).append(" ");
FlowTableStats fs = ft.getStats();
row.append("").append(fs.getTtl()).append(" ");
row.append("").append(fs.getCounter()).append(" ");
row.append(" ");
return row.toString();
}
}