Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package src.Peer;
- import src.Utils.Coord2PeerControlMeasurement;
- import src.Utils.Coord2PeerControlSpecial;
- import src.Utils.Coord2PeerDataMeasurement;
- import src.Utils.Peer2PeerPDU;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class ExecProbing extends Thread {
- private Object coord2peerpdu;
- private Peer2PeerPDU p2pPDU;
- private boolean inLoop;
- private String atualRoute;
- private ObjectInputStream in;
- private ObjectOutputStream out;
- private String dataCoordAddress;
- private final int PROBING_PORT = 8089;
- private int PACKET_ID;
- public ExecProbing(Object coord2peerpdu, String dataCoordAddress) {
- this.coord2peerpdu = coord2peerpdu;
- this.dataCoordAddress = dataCoordAddress;
- this.PACKET_ID = 1;
- }
- private char checkAlarmCondition(String value, String condition){
- char alarm = '0';
- // >=
- if(condition.charAt(0) == '>' && condition.charAt(1) == '='){
- if(Float.parseFloat(value) >= Float.parseFloat(condition.substring(2)))
- alarm = '1';
- }
- // >
- else if(condition.charAt(0) == '>' && condition.charAt(1) != '='){
- if(Float.parseFloat(value) > Float.parseFloat(condition.substring(1)))
- alarm = '1';
- }
- // <=
- else if(condition.charAt(0) == '<' && condition.charAt(1) == '='){
- if(Float.parseFloat(value) <= Float.parseFloat(condition.substring(2)))
- alarm = '1';
- }
- // <
- else if(condition.charAt(0) == '<' && condition.charAt(1) != '='){
- if(Float.parseFloat(value) < Float.parseFloat(condition.substring(1)))
- alarm = '1';
- }
- // =
- else if(condition.charAt(0) == '='){
- if(Float.parseFloat(value) == Float.parseFloat(condition.substring(1)))
- alarm = '1';
- }
- // !=
- else if(condition.charAt(0) == '!' && condition.charAt(1) == '='){
- if(Float.parseFloat(condition.substring(1)) != Float.parseFloat(value))
- alarm = '1';
- }
- // RoutePath (!!)
- else{
- if(!value.equalsIgnoreCase(this.atualRoute)){
- this.atualRoute = value;
- alarm = '1';
- }
- }
- return alarm;
- }
- private String getAlarmMessage(int PROBE_ID, String peerSource, String peerDest, char type, String condition){
- StringBuilder sb = new StringBuilder();
- /*
- switch(type){
- case 'P':{
- break;
- }
- case 'R':{
- break;
- }
- case 'r':{
- break;
- }
- case 'J':{
- break;
- }
- default:{
- break;
- }
- }*/
- sb.append("\nALARM HAS FIRED ON (").append(condition).append(") !!!");
- return sb.toString();
- }
- private Coord2PeerDataMeasurement peerProbingRTT(Peer2PeerPDU p2ppdu, DatagramSocket socket, int PROBE_ID, int PACKET_ID, String sourcePeer, String destPeer, int packetSize){
- Coord2PeerDataMeasurement res = null;
- StringBuilder result = new StringBuilder();
- try {
- InetAddress address = InetAddress.getByName(destPeer);
- ByteArrayInputStream inputStream;
- DatagramPacket packet;
- byte [] sendBuff;
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream(5000);
- ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(outputStream));
- // Send Packet
- objectOut.writeObject(p2ppdu);
- objectOut.flush();
- sendBuff = outputStream.toByteArray();
- packet = new DatagramPacket(sendBuff, sendBuff.length, address, PROBING_PORT);
- long sentTime = System.nanoTime();
- socket.send(packet);
- objectOut.close();
- outputStream.close();
- // Receive Packet
- byte[] recvBuf = new byte[5000];
- packet = new DatagramPacket(recvBuf, recvBuf.length);
- socket.setSoTimeout(1000);
- socket.receive(packet);
- socket.setSoTimeout(0);
- long elapsedTime = System.nanoTime() - sentTime;
- inputStream = new ByteArrayInputStream(recvBuf);
- ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(inputStream));
- p2pPDU = (Peer2PeerPDU) is.readObject();
- inputStream.close();
- is.close();
- long serverTime = Long.parseLong(p2pPDU.getResult());
- float finalResult =(float)(elapsedTime - serverTime)/1000000f;
- result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(packetSize).append(" bytes from ").append(destPeer).append(" to ").append(sourcePeer);
- result.append(" in ").append(finalResult).append(" ms");
- return new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'R', sourcePeer, destPeer, result.toString());
- }catch(Exception e){
- e.printStackTrace();
- }
- return res;
- }
- /* ESTA MAL! */
- private long peerProbingJitter(Peer2PeerPDU p2ppdu, DatagramSocket socket, String destPeer){
- Coord2PeerDataMeasurement res = null;
- long time = -1;
- try {
- InetAddress address = InetAddress.getByName(destPeer);
- ByteArrayInputStream inputStream;
- DatagramPacket packet;
- byte [] sendBuff;
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream(5000);
- ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(outputStream));
- // Send Packet
- objectOut.writeObject(p2ppdu);
- objectOut.flush();
- sendBuff = outputStream.toByteArray();
- packet = new DatagramPacket(sendBuff, sendBuff.length, address, PROBING_PORT);
- socket.send(packet);
- objectOut.close();
- outputStream.close();
- // Receive Packet
- byte[] recvBuf = new byte[5000];
- packet = new DatagramPacket(recvBuf, recvBuf.length);
- socket.setSoTimeout(1000);
- socket.receive(packet);
- socket.setSoTimeout(0);
- inputStream = new ByteArrayInputStream(recvBuf);
- ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(inputStream));
- p2pPDU = (Peer2PeerPDU) is.readObject();
- time = Long.parseLong(p2pPDU.getResult());
- inputStream.close();
- is.close();
- Long finalResult = Long.parseLong(p2pPDU.getResult());
- p2ppdu.setResult(String.valueOf(finalResult));
- }catch(Exception e){
- e.printStackTrace();
- }
- return time;
- }
- private int peerProbingLoss(Peer2PeerPDU p2ppdu, DatagramSocket socket, String destPeer){
- //if(socket.isClosed())socket = new DatagramSocket(8088);
- Coord2PeerDataMeasurement res = null;
- int count = 0;
- try {
- InetAddress address = InetAddress.getByName(destPeer);
- ByteArrayInputStream inputStream;
- DatagramPacket packet;
- byte [] sendBuff;
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream(5000);
- ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(outputStream));
- // Send Packet
- objectOut.writeObject(p2ppdu);
- objectOut.flush();
- sendBuff = outputStream.toByteArray();
- packet = new DatagramPacket(sendBuff, sendBuff.length, address, PROBING_PORT);
- socket.send(packet);
- objectOut.close();
- outputStream.close();
- // Receive Packet
- byte[] recvBuf = new byte[5000];
- packet = new DatagramPacket(recvBuf, recvBuf.length);
- socket.setSoTimeout(1000);
- socket.receive(packet);
- socket.setSoTimeout(0);;
- inputStream = new ByteArrayInputStream(recvBuf);
- ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(inputStream));
- if(is.readObject() != null)
- count = 1;
- inputStream.close();
- is.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- return count;
- }
- /* DONE */
- private void jitter(Coord2PeerControlMeasurement c2pcm){
- String [] aux = null;
- long initial_time=0, final_time=0;
- float time=0;
- String sb;
- char ICMP = c2pcm.getICMP();
- int i=0;
- Peer2PeerPDU p2pPDU = null;
- int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize();
- String alarmCondition = c2pcm.getAlarmCondition();
- String destinationPeer = c2pcm.getDestinationPeer();
- int nrPackets = c2pcm.getNrPackets();
- char ALARM = c2pcm.getAlarm();
- StringBuilder result = new StringBuilder();
- try {
- String sourcePeer = c2pcm.getSourcePeer();
- // ICMP = 1
- if (ICMP == '1') {
- Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets*2 + " -s "+packetSize);
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
- //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
- while((sb = stdInput.readLine()) != null){
- if(!sb.contains("time=")) continue;
- aux = sb.split(": ");
- if(i%2==0) {
- time = Float.parseFloat(((aux[1].split("\\s"))[2].split("="))[1]);
- }
- else {
- time = Float.parseFloat(((aux[1].split("\\s")[2].split("=")[1]))) - time;
- result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(time);
- result.append(" ms From Round Trip Interarrival Jitter ").append(packetSize).append(" bytes Packets ");
- if (ALARM == '1' && checkAlarmCondition(String.valueOf(time), alarmCondition) != '0') {
- Coord2PeerDataMeasurement resultToCoord = new
- Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'J', sourcePeer, destinationPeer, result.toString(),
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'J', alarmCondition));
- sendToCoord(resultToCoord);
- }
- else {
- Coord2PeerDataMeasurement resultToCoord = new
- Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'J', sourcePeer, destinationPeer, result.toString());
- sendToCoord(resultToCoord);
- }
- result = new StringBuilder();
- this.PACKET_ID++;
- }
- i++;
- }
- /*
- System.out.println("Here is the standard error of the command:\n");
- while((sb = stdError.readLine()) != null);
- */
- }
- // ICMP = 0
- else{
- DatagramSocket socket = new DatagramSocket(8088);
- for(i=0; i < nrPackets*2; i++){
- if(i%2==0) {
- initial_time= peerProbingJitter(new Peer2PeerPDU('J', result.toString()),socket, destinationPeer);
- }
- else {
- final_time = peerProbingJitter(new Peer2PeerPDU('J', result.toString()),socket, destinationPeer);
- time = (float) (final_time - initial_time)/1000000f;
- result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(time);
- result.append(" ms From One Way Interarrival Jitter ").append(packetSize).append(" bytes Packets ");
- if (ALARM == '1' && checkAlarmCondition(String.valueOf(time), alarmCondition) != '0') {
- Coord2PeerDataMeasurement res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'J', sourcePeer, destinationPeer, result.toString(),
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'J', alarmCondition));
- sendToCoord(res);
- }
- else {
- Coord2PeerDataMeasurement res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'J', sourcePeer, destinationPeer, result.toString());
- sendToCoord(res);
- }
- result = new StringBuilder();
- this.PACKET_ID++;
- }
- }
- socket.close();
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- /* DONE */
- private void roundTripTime(Coord2PeerControlMeasurement c2pcm){
- String [] aux = null;
- String time = null, sb=null;
- char ICMP = c2pcm.getICMP();
- Peer2PeerPDU p2pPDU = null;
- int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize();
- String alarmCondition = c2pcm.getAlarmCondition();
- String destinationPeer = c2pcm.getDestinationPeer();
- int nrPackets = c2pcm.getNrPackets();
- char ALARM = c2pcm.getAlarm();
- StringBuilder result = new StringBuilder();
- try {
- String sourcePeer = c2pcm.getSourcePeer();
- // ICMP = 1
- if (ICMP == '1') {
- Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets + " -s "+packetSize);
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
- //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
- while((sb = stdInput.readLine()) != null){
- if(!sb.contains("time=")) continue;
- aux = sb.split(": ");
- time = ((aux[1].split("\\s"))[2].split("="))[1];
- result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(packetSize).append(" bytes from ").append(destinationPeer).append(" to ").append(sourcePeer);
- result.append(" in ").append(time).append(" ms");
- if(ALARM == '1' && checkAlarmCondition(time,alarmCondition)!='0') {
- Coord2PeerDataMeasurement resultToCoord = new
- Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID++,
- 'R', sourcePeer, destinationPeer, result.toString(),
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'R', alarmCondition));
- sendToCoord(resultToCoord);
- }
- else{
- Coord2PeerDataMeasurement resultToCoord = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID++,
- 'R', sourcePeer, destinationPeer, result.toString());
- sendToCoord(resultToCoord);
- }
- result=new StringBuilder();
- }
- /*
- System.out.println("Here is the standard error of the command:\n");
- while((sb = stdError.readLine()) != null);
- */
- }
- // ICMP = 0
- else{
- DatagramSocket socket = new DatagramSocket(8088);
- for(int i = 0; i < nrPackets; i++) {
- Coord2PeerDataMeasurement res = peerProbingRTT(new Peer2PeerPDU('R', result.toString()), socket, PROBE_ID, this.PACKET_ID, sourcePeer, destinationPeer, packetSize);
- // Depois de acabar a funcao peerProbing apanhar o tempo no resultado para o alarme
- time = res.getResult().split("in ")[1].split(" ms")[0];
- if (ALARM == '1' && checkAlarmCondition(time, alarmCondition) != '0') {
- res.setAlarmMessage(getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'R', alarmCondition));
- sendToCoord(res);
- } else {
- sendToCoord(res);
- }
- this.PACKET_ID++;
- }
- socket.close();
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- /* DONE */
- private void routePath(Coord2PeerControlMeasurement c2pcm){
- StringBuilder result=new StringBuilder();
- String sb;
- int PROBE_ID = c2pcm.getPROBE_ID();
- String alarmCondition = c2pcm.getAlarmCondition();
- String destinationPeer = c2pcm.getDestinationPeer();
- String res;
- char ALARM = c2pcm.getAlarm();
- try {
- String sourcePeer = c2pcm.getSourcePeer();
- Process p = Runtime.getRuntime().exec( "tracepath " + c2pcm.getDestinationPeer());
- BufferedReader stdInput = new BufferedReader(new
- InputStreamReader(p.getInputStream()));
- /*BufferedReader stdError = new BufferedReader(new
- InputStreamReader(p.getErrorStream()));
- */
- while((sb = stdInput.readLine()) != null){
- if(sb.toString().contains("Resume")) continue;
- res = sb.toString().split(" +")[2];
- if(!result.toString().contains(res))
- result.append(res).append(" -> ");
- }
- int len = result.toString().length();
- res = result.toString().substring(0,len-3);
- if(this.atualRoute == null)
- this.atualRoute = res;
- if(ALARM == '1' && checkAlarmCondition(res, alarmCondition) != '0') {
- Coord2PeerDataMeasurement resultToCoord = new
- Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'r', sourcePeer, destinationPeer, res,
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'r', alarmCondition));
- sendToCoord(resultToCoord);
- }
- else{
- Coord2PeerDataMeasurement resultToCoord = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'r', sourcePeer, destinationPeer, res);
- sendToCoord(resultToCoord);
- }
- this.PACKET_ID++;
- /*
- System.out.println("Here is the standard error of the command (if any):\n");
- while ((s = stdError.readLine()) != null) {
- System.out.println(s);
- }
- */
- }catch (IOException e) {
- System.out.println("exception happened - here's what I know: ");
- e.printStackTrace();
- }
- }
- /* DONE */
- private void packetLoss(Coord2PeerControlMeasurement c2pcm){
- String [] aux = null;
- String loss = null, sb=null;
- char ICMP = c2pcm.getICMP();
- Peer2PeerPDU p2pPDU = null;
- int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize();
- String alarmCondition = c2pcm.getAlarmCondition();
- String destinationPeer = c2pcm.getDestinationPeer();
- int nrPackets = c2pcm.getNrPackets();
- char ALARM = c2pcm.getAlarm();
- StringBuilder result = new StringBuilder();
- int count = 0,i=0;
- try {
- String sourcePeer = c2pcm.getSourcePeer();
- // ICMP = 1
- if (ICMP == '1') {
- Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets + " -s "+packetSize);
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
- //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
- while((sb = stdInput.readLine()) != null) {
- if (!sb.contains("packets")) continue;
- result.append(sb);
- loss = sb.split(", ")[2].split(" ")[0].replaceFirst("%", "");
- }
- if(ALARM == '1' && checkAlarmCondition(loss,alarmCondition)!='0'){
- Coord2PeerDataMeasurement resultToCoord = new
- Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'P', sourcePeer, destinationPeer, result.toString(),
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'P', alarmCondition));
- sendToCoord(resultToCoord);
- } else{
- Coord2PeerDataMeasurement resultToCoord = new
- Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'P', sourcePeer, destinationPeer, result.toString());
- sendToCoord(resultToCoord);
- }
- /*
- System.out.println("Here is the standard error of the command:\n");
- while((sb = stdError.readLine()) != null);
- */
- this.PACKET_ID++;
- }
- // ICMP = 0
- else{
- DatagramSocket socket = new DatagramSocket(8088);
- for(i = 0; i < nrPackets; i++) {
- count += peerProbingLoss(new Peer2PeerPDU('P', result.toString()), socket, destinationPeer);
- }
- float lossPercent = (1 - (count / nrPackets))*100;
- result.append(nrPackets).append(" packets transmitted, ").append(count).append(" packets received, ").append(lossPercent).append("% packet loss");
- if(ALARM == '1' && checkAlarmCondition(String.valueOf(lossPercent),alarmCondition)!='0'){
- Coord2PeerDataMeasurement resultToCoord = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'P', sourcePeer, destinationPeer, result.toString(),
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'P', alarmCondition));
- resultToCoord.setAlarmMessage(getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'P', alarmCondition));
- sendToCoord(resultToCoord);
- }
- else{
- Coord2PeerDataMeasurement resultToCoord = new
- Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'P', sourcePeer, destinationPeer, result.toString());
- sendToCoord(resultToCoord);
- }
- socket.close();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- /* ACABAR */
- private Coord2PeerDataMeasurement statisticPacketLoss(Coord2PeerControlMeasurement c2pcm) {
- String [] aux = null;
- String loss = null, sb=null;
- Coord2PeerDataMeasurement res = null;
- char ICMP = c2pcm.getICMP();
- Peer2PeerPDU p2pPDU = null;
- int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize(), count=0;
- String alarmCondition = c2pcm.getAlarmCondition();
- String destinationPeer = c2pcm.getDestinationPeer();
- char ALARM = c2pcm.getAlarm();
- int nrPackets = c2pcm.getNrPackets();
- StringBuilder result = new StringBuilder();
- try {
- String sourcePeer = c2pcm.getSourcePeer();
- // ICMP = 1
- if (ICMP == '1') {
- Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets + " -s "+packetSize);
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
- //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
- while((sb = stdInput.readLine()) != null){
- if(!sb.contains("packets")) continue;
- result.append(sb);
- loss = sb.split(", ")[2].split(" ")[0].replaceFirst("%","");
- if(ALARM == '1' && checkAlarmCondition(loss,alarmCondition)!='0') {
- res= new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'P', sourcePeer, destinationPeer, result.toString(),
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'P', alarmCondition));
- }
- else{
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'P', sourcePeer, destinationPeer, result.toString());
- }
- result=new StringBuilder();
- this.PACKET_ID++;
- }
- /*
- System.out.println("Here is the standard error of the command:\n");
- while((sb = stdError.readLine()) != null);
- */
- }
- // ICMP = 0
- else{
- DatagramSocket socket = new DatagramSocket(8088);
- for(int i=0; i < nrPackets; i++) {
- count += peerProbingLoss(new Peer2PeerPDU('R', result.toString()), socket, destinationPeer);
- }
- float lossPercent = (1 - (count / nrPackets))*100;
- result.append(nrPackets).append(" packets transmitted, ").append(count).append(" packets received, ").append(lossPercent).append("% packet loss");
- if(ALARM == '1' && checkAlarmCondition(String.valueOf(lossPercent),alarmCondition)!='0'){
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID, 'P', sourcePeer, destinationPeer, result.toString(),
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'P', alarmCondition));
- }
- else{
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'P', sourcePeer, destinationPeer, result.toString());
- }
- socket.close();
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- return res;
- }
- private Coord2PeerDataMeasurement statisticRoundTripTime(Coord2PeerControlMeasurement c2pcm) {
- String [] aux = null;
- String time = null, sb=null;
- char ICMP = c2pcm.getICMP();
- Peer2PeerPDU p2pPDU = null;
- Coord2PeerDataMeasurement res = null;
- int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize();
- String alarmCondition = c2pcm.getAlarmCondition();
- String destinationPeer = c2pcm.getDestinationPeer();
- char ALARM = c2pcm.getAlarm();
- int nrPackets = c2pcm.getNrPackets();
- StringBuilder result = new StringBuilder();
- try {
- String sourcePeer = c2pcm.getSourcePeer();
- // ICMP = 1
- if (ICMP == '1') {
- Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets + " -s "+packetSize);
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
- //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
- while((sb = stdInput.readLine()) != null){
- if(!sb.contains("time=")) continue;
- aux = sb.split(": ");
- this.PACKET_ID = Integer.parseInt(((aux[1].split("\\s")[0]).split("="))[1]);
- time = ((aux[1].split("\\s"))[2].split("="))[1];
- result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(packetSize).append(" bytes from ").append(destinationPeer).append(" to ").append(sourcePeer);
- result.append(" in ").append(time).append(" ms");
- if(ALARM == '1' && checkAlarmCondition(time,alarmCondition)!='0') {
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'R', sourcePeer, destinationPeer, result.toString(),
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'R', alarmCondition));
- }
- else{
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'R', sourcePeer, destinationPeer, result.toString());
- }
- result=new StringBuilder();
- this.PACKET_ID++;
- }
- /*
- System.out.println("Here is the standard error of the command:\n");
- while((sb = stdError.readLine()) != null);
- */
- }
- // ICMP = 0
- else{
- DatagramSocket socket = new DatagramSocket(8088);
- time = res.getResult().split("in ")[1].split(" ms")[0];
- if(ALARM == '1' && checkAlarmCondition(time,alarmCondition)!='0'){
- res = peerProbingRTT(new Peer2PeerPDU('R', result.toString()),socket, PROBE_ID, this.PACKET_ID, sourcePeer, destinationPeer, packetSize);
- res.setAlarmMessage(getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'R', alarmCondition));
- }
- else{
- res = peerProbingRTT(new Peer2PeerPDU('R', result.toString()),socket, PROBE_ID, this.PACKET_ID, sourcePeer, destinationPeer, packetSize);
- }
- socket.close();
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- return res;
- }
- private Coord2PeerDataMeasurement statisticRoutePath(Coord2PeerControlMeasurement c2pcm) {
- StringBuilder result=new StringBuilder();
- String sb;
- int PROBE_ID = c2pcm.getPROBE_ID();
- String alarmCondition = c2pcm.getAlarmCondition();
- String destinationPeer = c2pcm.getDestinationPeer();
- Coord2PeerDataMeasurement res = null;
- String reS;
- char ALARM = c2pcm.getAlarm();
- try {
- String sourcePeer = InetAddress.getLocalHost().getHostAddress();
- Process p = Runtime.getRuntime().exec( "tracepath " + c2pcm.getDestinationPeer());
- BufferedReader stdInput = new BufferedReader(new
- InputStreamReader(p.getInputStream()));
- /*BufferedReader stdError = new BufferedReader(new
- InputStreamReader(p.getErrorStream()));
- */
- while((sb = stdInput.readLine()) != null){ result.append(sb); }
- reS = result.toString();
- if(this.atualRoute == null)
- this.atualRoute = reS;
- if(ALARM == '1' && checkAlarmCondition(destinationPeer, alarmCondition) != '0') {
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'r', sourcePeer, destinationPeer, reS,
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'r', alarmCondition));
- }
- else{
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'r', sourcePeer, destinationPeer, reS);
- }
- this.PACKET_ID++;
- /*
- System.out.println("Here is the standard error of the command (if any):\n");
- while ((s = stdError.readLine()) != null) {
- System.out.println(s);
- }
- */
- }catch (IOException e) {
- System.out.println("exception happened - here's what I know: ");
- e.printStackTrace();
- }
- return res;
- }
- private Coord2PeerDataMeasurement statisticJitter(Coord2PeerControlMeasurement c2pcm) {
- String [] aux = null;
- float time = 0;
- String sb;
- char ICMP = c2pcm.getICMP();
- Coord2PeerDataMeasurement res = null;
- int i=0,id=0;
- Peer2PeerPDU p2pPDU = null;
- int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize();
- String alarmCondition = c2pcm.getAlarmCondition();
- String destinationPeer = c2pcm.getDestinationPeer();
- int nrPackets = c2pcm.getNrPackets();
- char ALARM = c2pcm.getAlarm();
- StringBuilder result = new StringBuilder();
- try {
- String sourcePeer = c2pcm.getSourcePeer();
- // ICMP = 1
- if (ICMP == '1') {
- Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets*2 + " -s "+packetSize);
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
- //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
- while((sb = stdInput.readLine()) != null){
- if(!sb.contains("time=")) continue;
- aux = sb.split(": ");
- if(i%2==0) {
- time = Float.parseFloat(((aux[1].split("\\s"))[2].split("="))[1]);
- }
- else {
- time = Float.parseFloat(((aux[1].split("\\s")[2].split("=")[1]))) - time;
- result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(time);
- result.append(" ms From Round Trip Interarrival Jitter ").append(packetSize).append(" bytes Packets ");
- if (checkAlarmCondition(String.valueOf(time), alarmCondition) != '0') {
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'J', sourcePeer, destinationPeer, result.toString(),
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'J', alarmCondition));
- }
- else {
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
- 'J', sourcePeer, destinationPeer, result.toString());
- }
- result = new StringBuilder();
- this.PACKET_ID++;
- }
- i++;
- }
- /*
- System.out.println("Here is the standard error of the command:\n");
- while((sb = stdError.readLine()) != null);
- */
- }
- // ICMP = 0
- else{
- DatagramSocket socket = new DatagramSocket(8088);
- for(i=0; i < nrPackets*2; i++){
- if(i%2==0) {
- time = peerProbingJitter(new Peer2PeerPDU('J', result.toString()),socket, destinationPeer);
- }
- else {
- time = peerProbingJitter(new Peer2PeerPDU('J', result.toString()),socket, destinationPeer) - time;
- result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(time);
- result.append(" ms From One Way Interarrival Jitter ").append(packetSize).append(" bytes Packets ");
- if (ALARM == '1' && checkAlarmCondition(String.valueOf(time), alarmCondition) != '0') {
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID, 'J', sourcePeer, destinationPeer, result.toString(),
- getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'J', alarmCondition));
- sendToCoord(res);
- }
- else {
- res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID, 'J', sourcePeer, destinationPeer, result.toString());
- sendToCoord(res);
- }
- result = new StringBuilder();
- this.PACKET_ID = ++id;
- }
- }
- socket.close();
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- return res;
- }
- private Coord2PeerDataMeasurement getRTTStatisticalPDU(ArrayList<Coord2PeerDataMeasurement> list, String alarmCondition) {
- Coord2PeerDataMeasurement res = list.get(0);
- double dev,avg;
- OptionalDouble min,max;
- int alarmCounter=0,counter=0;
- List<Double> l = new ArrayList<>();
- StringBuilder result = new StringBuilder();
- for(Coord2PeerDataMeasurement s : list) {
- String[] results = s.getResult().split(": ");
- l.add(Double.parseDouble(results[1].split(" ")[8]));
- counter++;
- if (s.getAlarmMessage() != null)
- alarmCounter++;
- }
- min = l.stream().mapToDouble(v -> v).min();
- max = l.stream().mapToDouble(v -> v).max();
- avg = l.stream().mapToDouble(v -> v).sum()/counter;
- dev = standardDev(l);
- result.append("STATISTICAL DATA").append("\n");
- result.append("Received: ").append(counter).append(" probes").append("\n");
- result.append("Minimum: ").append(min.getAsDouble()).append(" ms\n");
- result.append("Maximum: ").append(max.getAsDouble()).append(" ms\n");
- result.append("Average: ").append(avg).append(" ms\n");
- result.append("Standard Deviation: ").append(dev).append(" ms");
- if(alarmCondition != null)
- result.append("\nAlarm Fired with condition (").append(alarmCondition).append(") ").append(alarmCounter).append(" times");
- res.setResult(result.toString());
- res.setAlarmMessage("");
- return res;
- }
- private Coord2PeerDataMeasurement getLossStatisticalPDU(ArrayList<Coord2PeerDataMeasurement> list, String alarmCondition) {
- Coord2PeerDataMeasurement res = list.get(0);
- double dev,avg;
- OptionalDouble min,max;
- int alarmCounter=0, counter=0;
- List<Double> l = new ArrayList<>();
- StringBuilder result = new StringBuilder();
- for(Coord2PeerDataMeasurement s : list){
- String [] results = s.getResult().split(", ");
- l.add(Double.parseDouble(results[2].split(" ")[0].replaceFirst("%","")));
- counter++;
- if(s.getAlarmMessage()!=null)
- alarmCounter++;
- }
- min = l.stream().mapToDouble(v -> v).min();
- max = l.stream().mapToDouble(v -> v).max();
- avg = l.stream().mapToDouble(v -> v).sum()/l.size();
- dev = standardDev(l);
- result.append("STATISTICAL DATA").append("\n");
- result.append("Received: ").append(counter).append(" probes").append("\n");
- result.append("Minimum: ").append(min.getAsDouble()).append(" %\n");
- result.append("Maximum: ").append(max.getAsDouble()).append(" %\n");
- result.append("Average: ").append(avg).append(" %\n");
- result.append("Standard Deviation: ").append(dev).append(" %");
- if(alarmCondition != null)
- result.append("\nAlarm Fired with condition (").append(alarmCondition).append(") ").append(alarmCounter).append(" times!");
- res.setResult(result.toString());
- res.setAlarmMessage("");
- return res;
- }
- private Coord2PeerDataMeasurement getJitterStatisticalPDU(ArrayList<Coord2PeerDataMeasurement> list, String alarmCondition) {
- Coord2PeerDataMeasurement res = list.get(0);
- double dev,avg;
- OptionalDouble min,max;
- int alarmCounter=0, counter=0;
- List<Double> l = new ArrayList<>();
- StringBuilder result = new StringBuilder();
- for(Coord2PeerDataMeasurement s : list) {
- String[] results = s.getResult().split(": ");
- l.add(Double.parseDouble(results[1].split(" ")[1]));
- counter++;
- if (s.getAlarmMessage() != null)
- alarmCounter++;
- }
- min = l.stream().mapToDouble(v -> v).min();
- max = l.stream().mapToDouble(v -> v).max();
- avg = l.stream().mapToDouble(v -> v).sum()/counter;
- dev = standardDev(l);
- result.append("STATISTICAL DATA").append("\n");
- result.append("Received: ").append(counter).append(" probes").append("\n");
- result.append("Minimum: ").append(min.getAsDouble()).append(" ms\n");
- result.append("Maximum: ").append(max.getAsDouble()).append(" ms\n");
- result.append("Average: ").append(avg).append(" ms\n");
- result.append("Standard Deviation: ").append(dev).append(" ms");
- if(alarmCondition != null)
- result.append("\nAlarm Fired with condition (").append(alarmCondition).append(") ").append(alarmCounter).append(" times");
- res.setResult(result.toString());
- res.setAlarmMessage("");
- return res;
- }
- private Coord2PeerDataMeasurement getRouteStatisticalPDU(ArrayList<Coord2PeerDataMeasurement> list) {
- Coord2PeerDataMeasurement res = list.get(0);
- int alarmCounter=0, counter=0;
- StringBuilder result = new StringBuilder();
- for(Coord2PeerDataMeasurement s : list) {
- counter++;
- if (s.getAlarmMessage() != null)
- alarmCounter++;
- }
- result.append("STATISTICAL DATA").append("\n");
- result.append("Received: ").append(counter).append(" probes").append("\n");
- result.append("Route Swap: ").append(alarmCounter).append(" times");
- res.setResult(result.toString());
- res.setAlarmMessage("");
- return res;
- }
- private double standardDev(List<Double> l) {
- double sum = 0.0, standardDeviation = 0.0;
- int size = l.size();
- for(double num : l) {
- sum += num;
- }
- double mean = sum/size;
- for(double num: l) {
- standardDeviation += Math.pow(num - mean, 2);
- }
- return Math.sqrt(standardDeviation/size);
- }
- /* ACABAR */
- private void sendToCoord(Coord2PeerDataMeasurement result){
- try{
- Socket clientSocket = new Socket(dataCoordAddress,8082);
- this.out = new ObjectOutputStream(clientSocket.getOutputStream());
- this.in = new ObjectInputStream(clientSocket.getInputStream());
- this.out.writeObject(result);
- this.out.flush();
- //System.out.println(this.in.readObject().toString());
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- public void setLoopOff() {
- this.inLoop = false;
- }
- /* DONE SO FALTA ACABAR FUNCS */
- @Override
- public void run() {
- // Special
- if(this.coord2peerpdu instanceof Coord2PeerControlSpecial) {
- Coord2PeerControlSpecial c2pSpecial = (Coord2PeerControlSpecial) this.coord2peerpdu;
- char TYPE = c2pSpecial.getType();
- switch (TYPE) {
- // Activate
- case 'A': {
- break;
- }
- // Deactivate
- case 'D': {
- break;
- }
- // Packet Injection
- case 'i': {
- break;
- }
- default: {
- break;
- }
- }
- }
- // Measurement
- else{
- Coord2PeerControlMeasurement c2pMeasurement = (Coord2PeerControlMeasurement) this.coord2peerpdu;
- char TYPE = c2pMeasurement.getType();
- char LOOP = c2pMeasurement.getLoop();
- int feedbacktime=0;
- switch (TYPE) {
- // Packet Loss
- case 'P': {
- if (LOOP == '0') {
- packetLoss(c2pMeasurement);
- }
- else if (LOOP == '1') {
- this.inLoop = true;
- feedbacktime = Integer.parseInt(c2pMeasurement.getFeed_time()) * 1000;
- while (inLoop) {
- packetLoss(c2pMeasurement);
- try {
- this.sleep(feedbacktime);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- else {
- this.inLoop = true;
- ArrayList<Coord2PeerDataMeasurement> l = new ArrayList<>();
- while (inLoop) {
- Coord2PeerDataMeasurement res = statisticPacketLoss(c2pMeasurement);
- l.add(res);
- try {
- this.sleep(20000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if(c2pMeasurement.getAlarm()=='1')
- sendToCoord(getLossStatisticalPDU(l,c2pMeasurement.getAlarmCondition()));
- else
- sendToCoord(getLossStatisticalPDU(l,null));
- }
- break;
- }
- // Round Trip Time
- case 'R': {
- if (LOOP == '0') {
- roundTripTime(c2pMeasurement);
- }
- else if (LOOP == '1') {
- this.inLoop = true;
- feedbacktime = Integer.parseInt(c2pMeasurement.getFeed_time()) * 1000;
- while (inLoop) {
- roundTripTime(c2pMeasurement);
- try {
- this.sleep(feedbacktime);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- else {
- this.inLoop = true;
- ArrayList<Coord2PeerDataMeasurement> l = new ArrayList<>();
- while (inLoop) {
- Coord2PeerDataMeasurement res = statisticRoundTripTime(c2pMeasurement);
- l.add(res);
- try {
- this.sleep(20000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if(c2pMeasurement.getAlarm()=='1')
- sendToCoord(getRTTStatisticalPDU(l,c2pMeasurement.getAlarmCondition()));
- else
- sendToCoord(getRTTStatisticalPDU(l,null));
- }
- break;
- }
- // Route Path
- case 'r': {
- if (LOOP == '0') {
- routePath(c2pMeasurement);
- }
- else if (LOOP == '1') {
- this.inLoop = true;
- feedbacktime = Integer.parseInt(c2pMeasurement.getFeed_time()) * 1000;
- while (inLoop) {
- routePath(c2pMeasurement);
- try {
- this.sleep(feedbacktime);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- else {
- this.inLoop = true;
- ArrayList<Coord2PeerDataMeasurement> l = new ArrayList<>();
- while(inLoop) {
- Coord2PeerDataMeasurement res = statisticRoutePath(c2pMeasurement);
- l.add(res);
- try {
- this.sleep(20000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- sendToCoord(getRouteStatisticalPDU(l));
- }
- break;
- }
- // Jitter
- case 'J': {
- if(LOOP == '0') {
- jitter(c2pMeasurement);
- }
- else if (LOOP == '1') {
- this.inLoop = true;
- feedbacktime = Integer.parseInt(c2pMeasurement.getFeed_time()) * 1000;
- while (inLoop) {
- jitter(c2pMeasurement);
- try {
- this.sleep(feedbacktime);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- else {
- this.inLoop = true;
- ArrayList<Coord2PeerDataMeasurement> l = new ArrayList<>();
- while (inLoop) {
- Coord2PeerDataMeasurement res = statisticJitter(c2pMeasurement);
- l.add(res);
- try {
- this.sleep(20000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if(c2pMeasurement.getAlarm()=='1')
- sendToCoord(getJitterStatisticalPDU(l,c2pMeasurement.getAlarmCondition()));
- else
- sendToCoord(getJitterStatisticalPDU(l,null));
- }
- break;
- }
- default: {
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement