Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 51.15 KB | None | 0 0
  1. package src.Peer;
  2.  
  3. import src.Utils.Coord2PeerControlMeasurement;
  4. import src.Utils.Coord2PeerControlSpecial;
  5. import src.Utils.Coord2PeerDataMeasurement;
  6. import src.Utils.Peer2PeerPDU;
  7.  
  8. import java.io.*;
  9. import java.net.*;
  10.  
  11.  
  12. import java.util.*;
  13.  
  14. public class ExecProbing extends Thread {
  15.     private Object coord2peerpdu;
  16.     private Peer2PeerPDU p2pPDU;
  17.     private boolean inLoop;
  18.     private String atualRoute;
  19.     private ObjectInputStream in;
  20.     private ObjectOutputStream out;
  21.     private String dataCoordAddress;
  22.     private final int PROBING_PORT = 8089;
  23.     private int PACKET_ID;
  24.    
  25.     public ExecProbing(Object coord2peerpdu, String dataCoordAddress) {
  26.         this.coord2peerpdu = coord2peerpdu;
  27.         this.dataCoordAddress = dataCoordAddress;
  28.         this.PACKET_ID = 1;
  29.     }
  30.  
  31.     private char checkAlarmCondition(String value, String condition){
  32.         char alarm = '0';
  33.  
  34.         // >=
  35.         if(condition.charAt(0) == '>' && condition.charAt(1) == '='){
  36.             if(Float.parseFloat(value) >= Float.parseFloat(condition.substring(2)))
  37.                 alarm = '1';
  38.         }
  39.         // >
  40.         else if(condition.charAt(0) == '>' && condition.charAt(1) != '='){
  41.             if(Float.parseFloat(value) > Float.parseFloat(condition.substring(1)))
  42.                 alarm = '1';
  43.         }
  44.         // <=
  45.         else if(condition.charAt(0) == '<' && condition.charAt(1) == '='){
  46.             if(Float.parseFloat(value) <= Float.parseFloat(condition.substring(2)))
  47.                 alarm = '1';
  48.         }
  49.         // <
  50.         else if(condition.charAt(0) == '<' && condition.charAt(1) != '='){
  51.             if(Float.parseFloat(value) < Float.parseFloat(condition.substring(1)))
  52.                 alarm = '1';
  53.         }
  54.         // =
  55.         else if(condition.charAt(0) == '='){
  56.             if(Float.parseFloat(value) == Float.parseFloat(condition.substring(1)))
  57.                 alarm = '1';
  58.         }
  59.         // !=
  60.         else if(condition.charAt(0) == '!' && condition.charAt(1) == '='){
  61.             if(Float.parseFloat(condition.substring(1)) != Float.parseFloat(value))
  62.                 alarm = '1';
  63.         }
  64.         // RoutePath (!!)
  65.         else{
  66.             if(!value.equalsIgnoreCase(this.atualRoute)){
  67.                 this.atualRoute = value;
  68.                 alarm = '1';
  69.             }
  70.         }
  71.         return alarm;
  72.     }
  73.  
  74.     private String getAlarmMessage(int PROBE_ID, String peerSource, String peerDest, char type, String condition){
  75.         StringBuilder sb = new StringBuilder();
  76.         /*
  77.         switch(type){
  78.             case 'P':{
  79.  
  80.                 break;
  81.             }
  82.             case 'R':{
  83.  
  84.                 break;
  85.             }
  86.             case 'r':{
  87.  
  88.                 break;
  89.             }
  90.             case 'J':{
  91.  
  92.                 break;
  93.             }
  94.             default:{
  95.  
  96.                 break;
  97.             }
  98.         }*/
  99.         sb.append("\nALARM HAS FIRED ON (").append(condition).append(") !!!");
  100.         return sb.toString();
  101.     }
  102.  
  103.  
  104.     private Coord2PeerDataMeasurement peerProbingRTT(Peer2PeerPDU p2ppdu, DatagramSocket socket, int PROBE_ID, int PACKET_ID, String sourcePeer, String destPeer, int packetSize){
  105.         Coord2PeerDataMeasurement res = null;
  106.         StringBuilder result = new StringBuilder();
  107.  
  108.         try {
  109.             InetAddress address = InetAddress.getByName(destPeer);
  110.             ByteArrayInputStream inputStream;
  111.             DatagramPacket packet;
  112.             byte [] sendBuff;
  113.             ByteArrayOutputStream outputStream = new ByteArrayOutputStream(5000);
  114.             ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(outputStream));
  115.  
  116.             // Send Packet
  117.             objectOut.writeObject(p2ppdu);
  118.             objectOut.flush();
  119.             sendBuff = outputStream.toByteArray();
  120.             packet = new DatagramPacket(sendBuff, sendBuff.length, address, PROBING_PORT);
  121.  
  122.             long sentTime = System.nanoTime();
  123.             socket.send(packet);
  124.            
  125.             objectOut.close();
  126.             outputStream.close();
  127.  
  128.             // Receive Packet
  129.             byte[] recvBuf = new byte[5000];
  130.             packet = new DatagramPacket(recvBuf, recvBuf.length);
  131.            
  132.             socket.setSoTimeout(1000);
  133.             socket.receive(packet);
  134.             socket.setSoTimeout(0);
  135.             long elapsedTime = System.nanoTime() - sentTime;
  136.            
  137.             inputStream = new ByteArrayInputStream(recvBuf);
  138.             ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(inputStream));
  139.             p2pPDU = (Peer2PeerPDU) is.readObject();
  140.  
  141.             inputStream.close();
  142.             is.close();
  143.  
  144.             long serverTime = Long.parseLong(p2pPDU.getResult());
  145.  
  146.             float finalResult =(float)(elapsedTime - serverTime)/1000000f;
  147.             result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(packetSize).append(" bytes from ").append(destPeer).append(" to ").append(sourcePeer);
  148.             result.append(" in ").append(finalResult).append(" ms");
  149.  
  150.             return new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  151.                                 'R', sourcePeer, destPeer, result.toString());
  152.  
  153.         }catch(Exception e){
  154.             e.printStackTrace();
  155.         }
  156.  
  157.         return res;
  158.     }
  159.  
  160.     /* ESTA MAL! */
  161.     private long peerProbingJitter(Peer2PeerPDU p2ppdu, DatagramSocket socket, String destPeer){
  162.         Coord2PeerDataMeasurement res = null;
  163.         long time = -1;
  164.         try {
  165.             InetAddress address = InetAddress.getByName(destPeer);
  166.             ByteArrayInputStream inputStream;
  167.             DatagramPacket packet;
  168.             byte [] sendBuff;
  169.             ByteArrayOutputStream outputStream = new ByteArrayOutputStream(5000);
  170.             ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(outputStream));
  171.  
  172.             // Send Packet
  173.             objectOut.writeObject(p2ppdu);
  174.             objectOut.flush();
  175.             sendBuff = outputStream.toByteArray();
  176.             packet = new DatagramPacket(sendBuff, sendBuff.length, address, PROBING_PORT);
  177.  
  178.             socket.send(packet);
  179.  
  180.             objectOut.close();
  181.             outputStream.close();
  182.  
  183.             // Receive Packet
  184.             byte[] recvBuf = new byte[5000];
  185.             packet = new DatagramPacket(recvBuf, recvBuf.length);
  186.  
  187.             socket.setSoTimeout(1000);
  188.             socket.receive(packet);
  189.             socket.setSoTimeout(0);
  190.  
  191.             inputStream = new ByteArrayInputStream(recvBuf);
  192.             ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(inputStream));
  193.             p2pPDU = (Peer2PeerPDU) is.readObject();
  194.             time = Long.parseLong(p2pPDU.getResult());
  195.             inputStream.close();
  196.             is.close();
  197.  
  198.             Long finalResult = Long.parseLong(p2pPDU.getResult());
  199.             p2ppdu.setResult(String.valueOf(finalResult));
  200.  
  201.         }catch(Exception e){
  202.             e.printStackTrace();
  203.         }
  204.  
  205.         return time;
  206.     }
  207.  
  208.  
  209.     private int peerProbingLoss(Peer2PeerPDU p2ppdu, DatagramSocket socket, String destPeer){
  210.         //if(socket.isClosed())socket = new DatagramSocket(8088);
  211.         Coord2PeerDataMeasurement res = null;
  212.         int count = 0;
  213.         try {
  214.             InetAddress address = InetAddress.getByName(destPeer);
  215.             ByteArrayInputStream inputStream;
  216.             DatagramPacket packet;
  217.             byte [] sendBuff;
  218.             ByteArrayOutputStream outputStream = new ByteArrayOutputStream(5000);
  219.             ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(outputStream));
  220.  
  221.             // Send Packet
  222.             objectOut.writeObject(p2ppdu);
  223.             objectOut.flush();
  224.             sendBuff = outputStream.toByteArray();
  225.             packet = new DatagramPacket(sendBuff, sendBuff.length, address, PROBING_PORT);
  226.  
  227.             socket.send(packet);
  228.  
  229.             objectOut.close();
  230.             outputStream.close();
  231.  
  232.             // Receive Packet
  233.             byte[] recvBuf = new byte[5000];
  234.             packet = new DatagramPacket(recvBuf, recvBuf.length);
  235.             socket.setSoTimeout(1000);
  236.             socket.receive(packet);
  237.             socket.setSoTimeout(0);;
  238.             inputStream = new ByteArrayInputStream(recvBuf);
  239.             ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(inputStream));
  240.  
  241.             if(is.readObject() != null)
  242.                 count = 1;
  243.  
  244.             inputStream.close();
  245.             is.close();
  246.  
  247.         }catch(Exception e){
  248.             e.printStackTrace();
  249.         }
  250.  
  251.         return count;
  252.     }
  253.  
  254.     /* DONE */
  255.     private void jitter(Coord2PeerControlMeasurement c2pcm){
  256.         String [] aux = null;
  257.         long initial_time=0, final_time=0;
  258.         float time=0;
  259.         String sb;
  260.         char ICMP = c2pcm.getICMP();
  261.         int i=0;
  262.         Peer2PeerPDU p2pPDU = null;
  263.         int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize();
  264.         String alarmCondition = c2pcm.getAlarmCondition();
  265.         String destinationPeer = c2pcm.getDestinationPeer();
  266.         int nrPackets = c2pcm.getNrPackets();
  267.         char ALARM = c2pcm.getAlarm();
  268.         StringBuilder result = new StringBuilder();
  269.         try {
  270.  
  271.             String sourcePeer = c2pcm.getSourcePeer();
  272.  
  273.             // ICMP = 1
  274.  
  275.             if (ICMP == '1') {
  276.                 Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets*2 + " -s "+packetSize);
  277.  
  278.                 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  279.  
  280.                 //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  281.  
  282.                 while((sb = stdInput.readLine()) != null){
  283.  
  284.                     if(!sb.contains("time=")) continue;
  285.  
  286.                     aux = sb.split(": ");
  287.  
  288.                     if(i%2==0) {
  289.                         time = Float.parseFloat(((aux[1].split("\\s"))[2].split("="))[1]);
  290.                     }
  291.  
  292.                     else {
  293.                         time = Float.parseFloat(((aux[1].split("\\s")[2].split("=")[1]))) - time;
  294.                         result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(time);
  295.                         result.append(" ms From Round Trip Interarrival Jitter ").append(packetSize).append(" bytes Packets ");
  296.  
  297.                         if (ALARM == '1' && checkAlarmCondition(String.valueOf(time), alarmCondition) != '0') {
  298.                             Coord2PeerDataMeasurement resultToCoord = new
  299.                                     Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  300.                                     'J', sourcePeer, destinationPeer, result.toString(),
  301.                                     getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'J', alarmCondition));
  302.                             sendToCoord(resultToCoord);
  303.                         }
  304.                         else {
  305.                             Coord2PeerDataMeasurement resultToCoord = new
  306.                                     Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  307.                                     'J', sourcePeer, destinationPeer, result.toString());
  308.                             sendToCoord(resultToCoord);
  309.                         }
  310.                         result = new StringBuilder();
  311.                         this.PACKET_ID++;
  312.                     }
  313.                     i++;
  314.                 }
  315.  
  316.                 /*
  317.                 System.out.println("Here is the standard error of the command:\n");
  318.                 while((sb = stdError.readLine()) != null);
  319.                 */
  320.  
  321.             }
  322.  
  323.             // ICMP = 0
  324.             else{
  325.                 DatagramSocket socket = new DatagramSocket(8088);
  326.                 for(i=0; i < nrPackets*2; i++){
  327.  
  328.                     if(i%2==0) {
  329.                         initial_time= peerProbingJitter(new Peer2PeerPDU('J', result.toString()),socket, destinationPeer);
  330.                     }
  331.  
  332.                     else {
  333.                         final_time = peerProbingJitter(new Peer2PeerPDU('J', result.toString()),socket, destinationPeer);
  334.                         time = (float) (final_time - initial_time)/1000000f;
  335.                         result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(time);
  336.                         result.append(" ms From One Way Interarrival Jitter ").append(packetSize).append(" bytes Packets ");
  337.  
  338.                         if (ALARM == '1' && checkAlarmCondition(String.valueOf(time), alarmCondition) != '0') {
  339.                             Coord2PeerDataMeasurement res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  340.                                     'J', sourcePeer, destinationPeer, result.toString(),
  341.                                     getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'J', alarmCondition));
  342.                             sendToCoord(res);
  343.                         }
  344.                         else {
  345.                             Coord2PeerDataMeasurement res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  346.                                     'J', sourcePeer, destinationPeer, result.toString());
  347.                             sendToCoord(res);
  348.                         }
  349.                         result = new StringBuilder();
  350.                         this.PACKET_ID++;
  351.                     }
  352.                 }
  353.                 socket.close();
  354.             }
  355.         }catch(IOException e){
  356.             e.printStackTrace();
  357.         }
  358.     }
  359.  
  360.     /* DONE */
  361.     private void roundTripTime(Coord2PeerControlMeasurement c2pcm){
  362.         String [] aux = null;
  363.         String time = null, sb=null;
  364.         char ICMP = c2pcm.getICMP();
  365.         Peer2PeerPDU p2pPDU = null;
  366.         int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize();
  367.         String alarmCondition = c2pcm.getAlarmCondition();
  368.         String destinationPeer = c2pcm.getDestinationPeer();
  369.         int nrPackets = c2pcm.getNrPackets();
  370.         char ALARM = c2pcm.getAlarm();
  371.         StringBuilder result = new StringBuilder();
  372.         try {
  373.  
  374.             String sourcePeer = c2pcm.getSourcePeer();
  375.  
  376.             // ICMP = 1
  377.  
  378.             if (ICMP == '1') {
  379.                 Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets + " -s "+packetSize);
  380.  
  381.                 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  382.  
  383.                 //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  384.  
  385.                 while((sb = stdInput.readLine()) != null){
  386.                     if(!sb.contains("time=")) continue;
  387.  
  388.                     aux = sb.split(": ");
  389.                     time = ((aux[1].split("\\s"))[2].split("="))[1];
  390.                     result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(packetSize).append(" bytes from ").append(destinationPeer).append(" to ").append(sourcePeer);
  391.                     result.append(" in ").append(time).append(" ms");
  392.  
  393.                     if(ALARM == '1' && checkAlarmCondition(time,alarmCondition)!='0') {
  394.                         Coord2PeerDataMeasurement resultToCoord = new
  395.                                 Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID++,
  396.                                 'R', sourcePeer, destinationPeer, result.toString(),
  397.                                 getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'R', alarmCondition));
  398.                         sendToCoord(resultToCoord);
  399.  
  400.                     }
  401.                     else{
  402.                         Coord2PeerDataMeasurement resultToCoord = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID++,
  403.                                 'R', sourcePeer, destinationPeer, result.toString());
  404.                         sendToCoord(resultToCoord);
  405.                     }
  406.                     result=new StringBuilder();
  407.                 }
  408.  
  409.                 /*
  410.                 System.out.println("Here is the standard error of the command:\n");
  411.                 while((sb = stdError.readLine()) != null);
  412.                 */
  413.  
  414.             }
  415.  
  416.             // ICMP = 0
  417.             else{
  418.                 DatagramSocket socket = new DatagramSocket(8088);
  419.  
  420.  
  421.                     for(int i = 0; i < nrPackets; i++) {
  422.                         Coord2PeerDataMeasurement res = peerProbingRTT(new Peer2PeerPDU('R', result.toString()), socket, PROBE_ID, this.PACKET_ID, sourcePeer, destinationPeer, packetSize);
  423.  
  424.                         // Depois de acabar a funcao peerProbing apanhar o tempo no resultado para o alarme
  425.  
  426.                         time = res.getResult().split("in ")[1].split(" ms")[0];
  427.                         if (ALARM == '1' && checkAlarmCondition(time, alarmCondition) != '0') {
  428.  
  429.                             res.setAlarmMessage(getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'R', alarmCondition));
  430.                             sendToCoord(res);
  431.  
  432.                         } else {
  433.                             sendToCoord(res);
  434.                         }
  435.                         this.PACKET_ID++;
  436.                     }
  437.  
  438.                 socket.close();
  439.                 }
  440.         }catch(IOException e){
  441.             e.printStackTrace();
  442.         }
  443.     }
  444.  
  445.     /* DONE */
  446.     private void routePath(Coord2PeerControlMeasurement c2pcm){
  447.         StringBuilder result=new StringBuilder();
  448.         String sb;
  449.         int PROBE_ID = c2pcm.getPROBE_ID();
  450.         String alarmCondition = c2pcm.getAlarmCondition();
  451.         String destinationPeer = c2pcm.getDestinationPeer();
  452.         String res;
  453.         char ALARM = c2pcm.getAlarm();
  454.         try {
  455.  
  456.             String sourcePeer = c2pcm.getSourcePeer();
  457.  
  458.             Process p = Runtime.getRuntime().exec( "tracepath " + c2pcm.getDestinationPeer());
  459.  
  460.             BufferedReader stdInput = new BufferedReader(new
  461.                     InputStreamReader(p.getInputStream()));
  462.  
  463.             /*BufferedReader stdError = new BufferedReader(new
  464.                     InputStreamReader(p.getErrorStream()));
  465.             */
  466.  
  467.             while((sb = stdInput.readLine()) != null){
  468.                 if(sb.toString().contains("Resume")) continue;
  469.  
  470.                 res = sb.toString().split(" +")[2];
  471.                 if(!result.toString().contains(res))
  472.                     result.append(res).append(" -> ");
  473.             }
  474.             int len = result.toString().length();
  475.             res = result.toString().substring(0,len-3);
  476.  
  477.             if(this.atualRoute == null)
  478.                 this.atualRoute = res;
  479.  
  480.             if(ALARM == '1' && checkAlarmCondition(res, alarmCondition) != '0') {
  481.                 Coord2PeerDataMeasurement resultToCoord = new
  482.                         Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  483.                         'r', sourcePeer, destinationPeer, res,
  484.                         getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'r', alarmCondition));
  485.                 sendToCoord(resultToCoord);
  486.             }
  487.  
  488.             else{
  489.                 Coord2PeerDataMeasurement resultToCoord = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  490.                         'r', sourcePeer, destinationPeer, res);
  491.                 sendToCoord(resultToCoord);
  492.             }
  493.  
  494.             this.PACKET_ID++;
  495.  
  496.             /*
  497.             System.out.println("Here is the standard error of the command (if any):\n");
  498.             while ((s = stdError.readLine()) != null) {
  499.                 System.out.println(s);
  500.             }
  501.             */
  502.         }catch (IOException e) {
  503.             System.out.println("exception happened - here's what I know: ");
  504.             e.printStackTrace();
  505.         }
  506.     }
  507.  
  508.     /* DONE */
  509.     private void packetLoss(Coord2PeerControlMeasurement c2pcm){
  510.         String [] aux = null;
  511.         String loss = null, sb=null;
  512.         char ICMP = c2pcm.getICMP();
  513.         Peer2PeerPDU p2pPDU = null;
  514.         int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize();
  515.         String alarmCondition = c2pcm.getAlarmCondition();
  516.         String destinationPeer = c2pcm.getDestinationPeer();
  517.         int nrPackets = c2pcm.getNrPackets();
  518.         char ALARM = c2pcm.getAlarm();
  519.         StringBuilder result = new StringBuilder();
  520.         int count = 0,i=0;
  521.  
  522.         try {
  523.  
  524.             String sourcePeer = c2pcm.getSourcePeer();
  525.  
  526.             // ICMP = 1
  527.  
  528.             if (ICMP == '1') {
  529.                 Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets + " -s "+packetSize);
  530.  
  531.                 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  532.  
  533.                 //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  534.  
  535.                 while((sb = stdInput.readLine()) != null) {
  536.                     if (!sb.contains("packets")) continue;
  537.                     result.append(sb);
  538.                     loss = sb.split(", ")[2].split(" ")[0].replaceFirst("%", "");
  539.                 }
  540.  
  541.  
  542.                 if(ALARM == '1' && checkAlarmCondition(loss,alarmCondition)!='0'){
  543.                     Coord2PeerDataMeasurement resultToCoord = new
  544.                             Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  545.                             'P', sourcePeer, destinationPeer, result.toString(),
  546.                             getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'P', alarmCondition));
  547.  
  548.                     sendToCoord(resultToCoord);
  549.  
  550.                 } else{
  551.                     Coord2PeerDataMeasurement resultToCoord = new
  552.                             Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  553.                             'P', sourcePeer, destinationPeer, result.toString());
  554.  
  555.                     sendToCoord(resultToCoord);
  556.                 }
  557.  
  558.                 /*
  559.                 System.out.println("Here is the standard error of the command:\n");
  560.                 while((sb = stdError.readLine()) != null);
  561.                 */
  562.  
  563.                 this.PACKET_ID++;
  564.             }
  565.  
  566.             // ICMP = 0
  567.             else{
  568.                 DatagramSocket socket = new DatagramSocket(8088);
  569.  
  570.                 for(i = 0; i < nrPackets; i++) {
  571.                    count += peerProbingLoss(new Peer2PeerPDU('P', result.toString()), socket, destinationPeer);
  572.                 }
  573.  
  574.                 float lossPercent = (1 - (count / nrPackets))*100;
  575.                 result.append(nrPackets).append(" packets transmitted, ").append(count).append(" packets received, ").append(lossPercent).append("% packet loss");
  576.  
  577.  
  578.                 if(ALARM == '1' && checkAlarmCondition(String.valueOf(lossPercent),alarmCondition)!='0'){
  579.  
  580.                     Coord2PeerDataMeasurement resultToCoord = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  581.                             'P', sourcePeer, destinationPeer, result.toString(),
  582.                             getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'P', alarmCondition));
  583.  
  584.                     resultToCoord.setAlarmMessage(getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'P', alarmCondition));
  585.  
  586.                     sendToCoord(resultToCoord);
  587.  
  588.                 }
  589.                 else{
  590.                     Coord2PeerDataMeasurement resultToCoord = new
  591.                             Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  592.                             'P', sourcePeer, destinationPeer, result.toString());
  593.                     sendToCoord(resultToCoord);
  594.                 }
  595.  
  596.                 socket.close();
  597.             }
  598.         }catch(Exception e){
  599.             e.printStackTrace();
  600.         }
  601.     }
  602.  
  603.     /* ACABAR */
  604.     private Coord2PeerDataMeasurement statisticPacketLoss(Coord2PeerControlMeasurement c2pcm) {
  605.         String [] aux = null;
  606.         String loss = null, sb=null;
  607.         Coord2PeerDataMeasurement res = null;
  608.         char ICMP = c2pcm.getICMP();
  609.         Peer2PeerPDU p2pPDU = null;
  610.         int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize(), count=0;
  611.         String alarmCondition = c2pcm.getAlarmCondition();
  612.         String destinationPeer = c2pcm.getDestinationPeer();
  613.         char ALARM = c2pcm.getAlarm();
  614.         int nrPackets = c2pcm.getNrPackets();
  615.         StringBuilder result = new StringBuilder();
  616.         try {
  617.  
  618.             String sourcePeer = c2pcm.getSourcePeer();
  619.  
  620.             // ICMP = 1
  621.  
  622.             if (ICMP == '1') {
  623.                 Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets + " -s "+packetSize);
  624.  
  625.                 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  626.  
  627.                 //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  628.  
  629.                 while((sb = stdInput.readLine()) != null){
  630.                     if(!sb.contains("packets")) continue;
  631.                     result.append(sb);
  632.                     loss = sb.split(", ")[2].split(" ")[0].replaceFirst("%","");
  633.                     if(ALARM == '1' && checkAlarmCondition(loss,alarmCondition)!='0') {
  634.                         res= new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  635.                                 'P', sourcePeer, destinationPeer, result.toString(),
  636.                                 getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'P', alarmCondition));
  637.                     }
  638.                     else{
  639.                         res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  640.                                 'P', sourcePeer, destinationPeer, result.toString());
  641.                         }
  642.                     result=new StringBuilder();
  643.                     this.PACKET_ID++;
  644.                 }
  645.  
  646.                 /*
  647.                 System.out.println("Here is the standard error of the command:\n");
  648.                 while((sb = stdError.readLine()) != null);
  649.                 */
  650.  
  651.             }
  652.  
  653.             // ICMP = 0
  654.             else{
  655.                 DatagramSocket socket = new DatagramSocket(8088);
  656.  
  657.                 for(int i=0; i < nrPackets; i++) {
  658.                     count += peerProbingLoss(new Peer2PeerPDU('R', result.toString()), socket, destinationPeer);
  659.                 }
  660.  
  661.                 float lossPercent = (1 - (count / nrPackets))*100;
  662.                 result.append(nrPackets).append(" packets transmitted, ").append(count).append(" packets received, ").append(lossPercent).append("% packet loss");
  663.  
  664.                 if(ALARM == '1' && checkAlarmCondition(String.valueOf(lossPercent),alarmCondition)!='0'){
  665.  
  666.                     res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID, 'P', sourcePeer, destinationPeer, result.toString(),
  667.                             getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'P', alarmCondition));
  668.  
  669.                 }
  670.                 else{
  671.                     res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  672.                             'P', sourcePeer, destinationPeer, result.toString());
  673.                 }
  674.  
  675.                 socket.close();
  676.             }
  677.         }catch(IOException e){
  678.             e.printStackTrace();
  679.         }
  680.  
  681.         return res;
  682.     }
  683.  
  684.     private Coord2PeerDataMeasurement statisticRoundTripTime(Coord2PeerControlMeasurement c2pcm) {
  685.         String [] aux = null;
  686.         String time = null, sb=null;
  687.         char ICMP = c2pcm.getICMP();
  688.         Peer2PeerPDU p2pPDU = null;
  689.         Coord2PeerDataMeasurement res = null;
  690.         int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize();
  691.         String alarmCondition = c2pcm.getAlarmCondition();
  692.         String destinationPeer = c2pcm.getDestinationPeer();
  693.         char ALARM = c2pcm.getAlarm();
  694.         int nrPackets = c2pcm.getNrPackets();
  695.         StringBuilder result = new StringBuilder();
  696.         try {
  697.  
  698.             String sourcePeer = c2pcm.getSourcePeer();
  699.  
  700.             // ICMP = 1
  701.  
  702.             if (ICMP == '1') {
  703.                 Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets + " -s "+packetSize);
  704.  
  705.                 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  706.  
  707.                 //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  708.  
  709.                 while((sb = stdInput.readLine()) != null){
  710.                     if(!sb.contains("time=")) continue;
  711.  
  712.                     aux = sb.split(": ");
  713.                     this.PACKET_ID = Integer.parseInt(((aux[1].split("\\s")[0]).split("="))[1]);
  714.                     time = ((aux[1].split("\\s"))[2].split("="))[1];
  715.                     result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(packetSize).append(" bytes from ").append(destinationPeer).append(" to ").append(sourcePeer);
  716.                     result.append(" in ").append(time).append(" ms");
  717.  
  718.                     if(ALARM == '1' && checkAlarmCondition(time,alarmCondition)!='0') {
  719.                         res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  720.                                 'R', sourcePeer, destinationPeer, result.toString(),
  721.                                 getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'R', alarmCondition));
  722.                     }
  723.                     else{
  724.                         res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  725.                                 'R', sourcePeer, destinationPeer, result.toString());
  726.                     }
  727.                     result=new StringBuilder();
  728.                     this.PACKET_ID++;
  729.                 }
  730.  
  731.                 /*
  732.                 System.out.println("Here is the standard error of the command:\n");
  733.                 while((sb = stdError.readLine()) != null);
  734.                 */
  735.  
  736.             }
  737.  
  738.             // ICMP = 0
  739.             else{
  740.                 DatagramSocket socket = new DatagramSocket(8088);
  741.                     time = res.getResult().split("in ")[1].split(" ms")[0];
  742.                     if(ALARM == '1' && checkAlarmCondition(time,alarmCondition)!='0'){
  743.                         res = peerProbingRTT(new Peer2PeerPDU('R', result.toString()),socket, PROBE_ID, this.PACKET_ID, sourcePeer, destinationPeer, packetSize);
  744.                         res.setAlarmMessage(getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'R', alarmCondition));
  745.                     }
  746.                     else{
  747.                         res = peerProbingRTT(new Peer2PeerPDU('R', result.toString()),socket, PROBE_ID, this.PACKET_ID, sourcePeer, destinationPeer, packetSize);
  748.                     }
  749.  
  750.                 socket.close();
  751.             }
  752.         }catch(IOException e){
  753.             e.printStackTrace();
  754.         }
  755.  
  756.         return res;
  757.     }
  758.  
  759.     private Coord2PeerDataMeasurement statisticRoutePath(Coord2PeerControlMeasurement c2pcm) {
  760.         StringBuilder result=new StringBuilder();
  761.         String sb;
  762.         int PROBE_ID = c2pcm.getPROBE_ID();
  763.         String alarmCondition = c2pcm.getAlarmCondition();
  764.         String destinationPeer = c2pcm.getDestinationPeer();
  765.         Coord2PeerDataMeasurement res = null;
  766.         String reS;
  767.         char ALARM = c2pcm.getAlarm();
  768.         try {
  769.  
  770.             String sourcePeer = InetAddress.getLocalHost().getHostAddress();
  771.  
  772.             Process p = Runtime.getRuntime().exec( "tracepath " + c2pcm.getDestinationPeer());
  773.  
  774.             BufferedReader stdInput = new BufferedReader(new
  775.                     InputStreamReader(p.getInputStream()));
  776.  
  777.             /*BufferedReader stdError = new BufferedReader(new
  778.                     InputStreamReader(p.getErrorStream()));
  779.             */
  780.  
  781.             while((sb = stdInput.readLine()) != null){ result.append(sb); }
  782.             reS = result.toString();
  783.  
  784.             if(this.atualRoute == null)
  785.                 this.atualRoute = reS;
  786.  
  787.             if(ALARM == '1' && checkAlarmCondition(destinationPeer, alarmCondition) != '0') {
  788.                 res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  789.                         'r', sourcePeer, destinationPeer, reS,
  790.                         getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'r', alarmCondition));
  791.             }
  792.  
  793.             else{
  794.                 res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  795.                         'r', sourcePeer, destinationPeer, reS);
  796.             }
  797.  
  798.             this.PACKET_ID++;
  799.             /*
  800.             System.out.println("Here is the standard error of the command (if any):\n");
  801.             while ((s = stdError.readLine()) != null) {
  802.                 System.out.println(s);
  803.             }
  804.             */
  805.         }catch (IOException e) {
  806.             System.out.println("exception happened - here's what I know: ");
  807.             e.printStackTrace();
  808.         }
  809.  
  810.         return res;
  811.     }
  812.  
  813.     private Coord2PeerDataMeasurement statisticJitter(Coord2PeerControlMeasurement c2pcm) {
  814.         String [] aux = null;
  815.         float time = 0;
  816.         String sb;
  817.         char ICMP = c2pcm.getICMP();
  818.         Coord2PeerDataMeasurement res = null;
  819.         int i=0,id=0;
  820.         Peer2PeerPDU p2pPDU = null;
  821.         int PROBE_ID = c2pcm.getPROBE_ID(), packetSize = c2pcm.getPacketSize();
  822.         String alarmCondition = c2pcm.getAlarmCondition();
  823.         String destinationPeer = c2pcm.getDestinationPeer();
  824.         int nrPackets = c2pcm.getNrPackets();
  825.         char ALARM = c2pcm.getAlarm();
  826.         StringBuilder result = new StringBuilder();
  827.         try {
  828.  
  829.             String sourcePeer = c2pcm.getSourcePeer();
  830.  
  831.             // ICMP = 1
  832.  
  833.             if (ICMP == '1') {
  834.                 Process p = Runtime.getRuntime().exec("ping " + c2pcm.getDestinationPeer() + " -c " + nrPackets*2 + " -s "+packetSize);
  835.  
  836.                 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  837.  
  838.                 //BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  839.  
  840.                 while((sb = stdInput.readLine()) != null){
  841.  
  842.                     if(!sb.contains("time=")) continue;
  843.  
  844.                     aux = sb.split(": ");
  845.  
  846.                     if(i%2==0) {
  847.                         time = Float.parseFloat(((aux[1].split("\\s"))[2].split("="))[1]);
  848.                     }
  849.  
  850.                     else {
  851.                         time = Float.parseFloat(((aux[1].split("\\s")[2].split("=")[1]))) - time;
  852.                         result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(time);
  853.                         result.append(" ms From Round Trip Interarrival Jitter ").append(packetSize).append(" bytes Packets ");
  854.  
  855.                         if (checkAlarmCondition(String.valueOf(time), alarmCondition) != '0') {
  856.                             res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  857.                                     'J', sourcePeer, destinationPeer, result.toString(),
  858.                                     getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'J', alarmCondition));
  859.                         }
  860.                         else {
  861.                             res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID,
  862.                                     'J', sourcePeer, destinationPeer, result.toString());
  863.                         }
  864.                         result = new StringBuilder();
  865.                         this.PACKET_ID++;
  866.                     }
  867.                     i++;
  868.                 }
  869.                 /*
  870.                 System.out.println("Here is the standard error of the command:\n");
  871.                 while((sb = stdError.readLine()) != null);
  872.                 */
  873.  
  874.             }
  875.  
  876.             // ICMP = 0
  877.             else{
  878.                 DatagramSocket socket = new DatagramSocket(8088);
  879.                 for(i=0; i < nrPackets*2; i++){
  880.  
  881.                     if(i%2==0) {
  882.                         time = peerProbingJitter(new Peer2PeerPDU('J', result.toString()),socket, destinationPeer);
  883.                     }
  884.  
  885.                     else {
  886.                         time = peerProbingJitter(new Peer2PeerPDU('J', result.toString()),socket, destinationPeer) - time;
  887.                         result.append("Packet ").append(this.PACKET_ID).append(": ").append("Received ").append(time);
  888.                         result.append(" ms From One Way Interarrival Jitter ").append(packetSize).append(" bytes Packets ");
  889.  
  890.                         if (ALARM == '1' && checkAlarmCondition(String.valueOf(time), alarmCondition) != '0') {
  891.                             res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID, 'J', sourcePeer, destinationPeer, result.toString(),
  892.                                     getAlarmMessage(PROBE_ID, sourcePeer, destinationPeer, 'J', alarmCondition));
  893.                             sendToCoord(res);
  894.                         }
  895.                         else {
  896.                              res = new Coord2PeerDataMeasurement(PROBE_ID, this.PACKET_ID, 'J', sourcePeer, destinationPeer, result.toString());
  897.                             sendToCoord(res);
  898.                         }
  899.                         result = new StringBuilder();
  900.                         this.PACKET_ID = ++id;
  901.                     }
  902.                 }
  903.                 socket.close();
  904.             }
  905.         }catch(IOException e){
  906.             e.printStackTrace();
  907.         }
  908.  
  909.         return res;
  910.     }
  911.  
  912.     private Coord2PeerDataMeasurement getRTTStatisticalPDU(ArrayList<Coord2PeerDataMeasurement> list, String alarmCondition) {
  913.         Coord2PeerDataMeasurement res = list.get(0);
  914.         double dev,avg;
  915.         OptionalDouble min,max;
  916.         int alarmCounter=0,counter=0;
  917.         List<Double> l = new ArrayList<>();
  918.         StringBuilder result = new StringBuilder();
  919.  
  920.         for(Coord2PeerDataMeasurement s : list) {
  921.             String[] results = s.getResult().split(": ");
  922.             l.add(Double.parseDouble(results[1].split(" ")[8]));
  923.             counter++;
  924.             if (s.getAlarmMessage() != null)
  925.                 alarmCounter++;
  926.         }
  927.  
  928.         min = l.stream().mapToDouble(v -> v).min();
  929.         max = l.stream().mapToDouble(v -> v).max();
  930.         avg = l.stream().mapToDouble(v -> v).sum()/counter;
  931.         dev = standardDev(l);
  932.  
  933.         result.append("STATISTICAL DATA").append("\n");
  934.         result.append("Received: ").append(counter).append(" probes").append("\n");
  935.         result.append("Minimum: ").append(min.getAsDouble()).append(" ms\n");
  936.         result.append("Maximum: ").append(max.getAsDouble()).append(" ms\n");
  937.         result.append("Average: ").append(avg).append(" ms\n");
  938.         result.append("Standard Deviation: ").append(dev).append(" ms");
  939.  
  940.         if(alarmCondition != null)
  941.             result.append("\nAlarm Fired with condition (").append(alarmCondition).append(") ").append(alarmCounter).append(" times");
  942.  
  943.         res.setResult(result.toString());
  944.         res.setAlarmMessage("");
  945.  
  946.         return res;
  947.     }
  948.  
  949.     private Coord2PeerDataMeasurement getLossStatisticalPDU(ArrayList<Coord2PeerDataMeasurement> list, String alarmCondition) {
  950.         Coord2PeerDataMeasurement res = list.get(0);
  951.         double dev,avg;
  952.         OptionalDouble min,max;
  953.         int alarmCounter=0, counter=0;
  954.         List<Double> l = new ArrayList<>();
  955.         StringBuilder result = new StringBuilder();
  956.  
  957.         for(Coord2PeerDataMeasurement s : list){
  958.             String [] results = s.getResult().split(", ");
  959.             l.add(Double.parseDouble(results[2].split(" ")[0].replaceFirst("%","")));
  960.             counter++;
  961.             if(s.getAlarmMessage()!=null)
  962.                 alarmCounter++;
  963.         }
  964.  
  965.         min = l.stream().mapToDouble(v -> v).min();
  966.         max = l.stream().mapToDouble(v -> v).max();
  967.         avg = l.stream().mapToDouble(v -> v).sum()/l.size();
  968.         dev = standardDev(l);
  969.  
  970.         result.append("STATISTICAL DATA").append("\n");
  971.         result.append("Received: ").append(counter).append(" probes").append("\n");
  972.         result.append("Minimum: ").append(min.getAsDouble()).append(" %\n");
  973.         result.append("Maximum: ").append(max.getAsDouble()).append(" %\n");
  974.         result.append("Average: ").append(avg).append(" %\n");
  975.         result.append("Standard Deviation: ").append(dev).append(" %");
  976.  
  977.         if(alarmCondition != null)
  978.             result.append("\nAlarm Fired with condition (").append(alarmCondition).append(") ").append(alarmCounter).append(" times!");
  979.         res.setResult(result.toString());
  980.         res.setAlarmMessage("");
  981.  
  982.         return res;
  983.     }
  984.  
  985.     private Coord2PeerDataMeasurement getJitterStatisticalPDU(ArrayList<Coord2PeerDataMeasurement> list, String alarmCondition) {
  986.         Coord2PeerDataMeasurement res = list.get(0);
  987.         double dev,avg;
  988.         OptionalDouble min,max;
  989.         int alarmCounter=0, counter=0;
  990.         List<Double> l = new ArrayList<>();
  991.         StringBuilder result = new StringBuilder();
  992.  
  993.         for(Coord2PeerDataMeasurement s : list) {
  994.             String[] results = s.getResult().split(": ");
  995.             l.add(Double.parseDouble(results[1].split(" ")[1]));
  996.             counter++;
  997.             if (s.getAlarmMessage() != null)
  998.                 alarmCounter++;
  999.         }
  1000.  
  1001.         min = l.stream().mapToDouble(v -> v).min();
  1002.         max = l.stream().mapToDouble(v -> v).max();
  1003.         avg = l.stream().mapToDouble(v -> v).sum()/counter;
  1004.         dev = standardDev(l);
  1005.  
  1006.         result.append("STATISTICAL DATA").append("\n");
  1007.         result.append("Received: ").append(counter).append(" probes").append("\n");
  1008.         result.append("Minimum: ").append(min.getAsDouble()).append(" ms\n");
  1009.         result.append("Maximum: ").append(max.getAsDouble()).append(" ms\n");
  1010.         result.append("Average: ").append(avg).append(" ms\n");
  1011.         result.append("Standard Deviation: ").append(dev).append(" ms");
  1012.  
  1013.         if(alarmCondition != null)
  1014.             result.append("\nAlarm Fired with condition (").append(alarmCondition).append(") ").append(alarmCounter).append(" times");
  1015.  
  1016.         res.setResult(result.toString());
  1017.         res.setAlarmMessage("");
  1018.  
  1019.         return res;
  1020.     }
  1021.  
  1022.     private Coord2PeerDataMeasurement getRouteStatisticalPDU(ArrayList<Coord2PeerDataMeasurement> list) {
  1023.         Coord2PeerDataMeasurement res = list.get(0);
  1024.         int alarmCounter=0, counter=0;
  1025.         StringBuilder result = new StringBuilder();
  1026.  
  1027.         for(Coord2PeerDataMeasurement s : list) {
  1028.             counter++;
  1029.             if (s.getAlarmMessage() != null)
  1030.                 alarmCounter++;
  1031.         }
  1032.  
  1033.         result.append("STATISTICAL DATA").append("\n");
  1034.         result.append("Received: ").append(counter).append(" probes").append("\n");
  1035.         result.append("Route Swap: ").append(alarmCounter).append(" times");
  1036.  
  1037.         res.setResult(result.toString());
  1038.         res.setAlarmMessage("");
  1039.  
  1040.         return res;
  1041.     }
  1042.  
  1043.     private double standardDev(List<Double> l) {
  1044.         double sum = 0.0, standardDeviation = 0.0;
  1045.         int size = l.size();
  1046.  
  1047.         for(double num : l) {
  1048.             sum += num;
  1049.         }
  1050.  
  1051.         double mean = sum/size;
  1052.  
  1053.         for(double num: l) {
  1054.             standardDeviation += Math.pow(num - mean, 2);
  1055.         }
  1056.  
  1057.         return Math.sqrt(standardDeviation/size);
  1058.     }
  1059.  
  1060.     /* ACABAR */
  1061.  
  1062.     private void sendToCoord(Coord2PeerDataMeasurement result){
  1063.         try{
  1064.             Socket clientSocket = new Socket(dataCoordAddress,8082);
  1065.             this.out = new ObjectOutputStream(clientSocket.getOutputStream()); 
  1066.             this.in = new ObjectInputStream(clientSocket.getInputStream());
  1067.             this.out.writeObject(result);
  1068.             this.out.flush();
  1069.             //System.out.println(this.in.readObject().toString());
  1070.         }catch(Exception e){
  1071.             e.printStackTrace();
  1072.         }
  1073.     }
  1074.  
  1075.  
  1076.     public void setLoopOff() {
  1077.         this.inLoop = false;
  1078.     }
  1079.  
  1080.     /* DONE SO FALTA ACABAR FUNCS */
  1081.     @Override
  1082.     public void run() {
  1083.         // Special
  1084.         if(this.coord2peerpdu instanceof Coord2PeerControlSpecial) {
  1085.             Coord2PeerControlSpecial c2pSpecial = (Coord2PeerControlSpecial) this.coord2peerpdu;
  1086.             char TYPE = c2pSpecial.getType();
  1087.             switch (TYPE) {
  1088.                 // Activate
  1089.                 case 'A': {
  1090.  
  1091.                     break;
  1092.                 }
  1093.                 // Deactivate
  1094.                 case 'D': {
  1095.  
  1096.                     break;
  1097.                 }
  1098.                 // Packet Injection
  1099.                 case 'i': {
  1100.                     break;
  1101.                 }
  1102.                 default: {
  1103.                     break;
  1104.                 }
  1105.             }
  1106.         }
  1107.         // Measurement
  1108.         else{
  1109.             Coord2PeerControlMeasurement c2pMeasurement = (Coord2PeerControlMeasurement) this.coord2peerpdu;
  1110.             char TYPE = c2pMeasurement.getType();
  1111.             char LOOP = c2pMeasurement.getLoop();
  1112.             int feedbacktime=0;
  1113.             switch (TYPE) {
  1114.                 // Packet Loss
  1115.                 case 'P': {
  1116.                     if (LOOP == '0') {
  1117.                         packetLoss(c2pMeasurement);
  1118.                     }
  1119.                     else if (LOOP == '1') {
  1120.                         this.inLoop = true;
  1121.                         feedbacktime = Integer.parseInt(c2pMeasurement.getFeed_time()) * 1000;
  1122.                         while (inLoop) {
  1123.                             packetLoss(c2pMeasurement);
  1124.                             try {
  1125.                                 this.sleep(feedbacktime);
  1126.                             } catch (InterruptedException e) {
  1127.                                 e.printStackTrace();
  1128.                             }
  1129.                         }
  1130.                     }
  1131.                     else {
  1132.                         this.inLoop = true;
  1133.                         ArrayList<Coord2PeerDataMeasurement> l = new ArrayList<>();
  1134.                         while (inLoop) {
  1135.                             Coord2PeerDataMeasurement res = statisticPacketLoss(c2pMeasurement);
  1136.                             l.add(res);
  1137.                             try {
  1138.                                 this.sleep(20000);
  1139.                             } catch (InterruptedException e) {
  1140.                                 e.printStackTrace();
  1141.                             }
  1142.                         }
  1143.                         if(c2pMeasurement.getAlarm()=='1')
  1144.                             sendToCoord(getLossStatisticalPDU(l,c2pMeasurement.getAlarmCondition()));
  1145.                         else
  1146.                             sendToCoord(getLossStatisticalPDU(l,null));
  1147.  
  1148.                     }
  1149.                     break;
  1150.                 }
  1151.                 // Round Trip Time
  1152.                 case 'R': {
  1153.                     if (LOOP == '0') {
  1154.                         roundTripTime(c2pMeasurement);
  1155.                     }
  1156.                     else if (LOOP == '1') {
  1157.                         this.inLoop = true;
  1158.                         feedbacktime = Integer.parseInt(c2pMeasurement.getFeed_time()) * 1000;
  1159.                         while (inLoop) {
  1160.                             roundTripTime(c2pMeasurement);
  1161.                             try {
  1162.                                 this.sleep(feedbacktime);
  1163.                             } catch (InterruptedException e) {
  1164.                                 e.printStackTrace();
  1165.                             }
  1166.                         }
  1167.                     }
  1168.                     else {
  1169.                         this.inLoop = true;
  1170.                         ArrayList<Coord2PeerDataMeasurement> l = new ArrayList<>();
  1171.                         while (inLoop) {
  1172.                             Coord2PeerDataMeasurement res = statisticRoundTripTime(c2pMeasurement);
  1173.                             l.add(res);
  1174.                             try {
  1175.                                 this.sleep(20000);
  1176.                             } catch (InterruptedException e) {
  1177.                                 e.printStackTrace();
  1178.                             }
  1179.                         }
  1180.                         if(c2pMeasurement.getAlarm()=='1')
  1181.                             sendToCoord(getRTTStatisticalPDU(l,c2pMeasurement.getAlarmCondition()));
  1182.                         else
  1183.                             sendToCoord(getRTTStatisticalPDU(l,null));
  1184.                     }
  1185.                     break;
  1186.                 }
  1187.  
  1188.                 // Route Path
  1189.                 case 'r': {
  1190.                     if (LOOP == '0') {
  1191.                         routePath(c2pMeasurement);
  1192.                     }
  1193.                     else if (LOOP == '1') {
  1194.                         this.inLoop = true;
  1195.                         feedbacktime = Integer.parseInt(c2pMeasurement.getFeed_time()) * 1000;
  1196.                         while (inLoop) {
  1197.                             routePath(c2pMeasurement);
  1198.                             try {
  1199.                                 this.sleep(feedbacktime);
  1200.                             } catch (InterruptedException e) {
  1201.                                 e.printStackTrace();
  1202.                             }
  1203.                         }
  1204.                     }
  1205.                     else {
  1206.                         this.inLoop = true;
  1207.                         ArrayList<Coord2PeerDataMeasurement> l = new ArrayList<>();
  1208.                         while(inLoop) {
  1209.                             Coord2PeerDataMeasurement res = statisticRoutePath(c2pMeasurement);
  1210.                             l.add(res);
  1211.                             try {
  1212.                                 this.sleep(20000);
  1213.                             } catch (InterruptedException e) {
  1214.                                 e.printStackTrace();
  1215.                             }
  1216.                         }
  1217.                         sendToCoord(getRouteStatisticalPDU(l));
  1218.                     }
  1219.                     break;
  1220.                 }
  1221.                 // Jitter
  1222.                 case 'J': {
  1223.                     if(LOOP == '0') {
  1224.                         jitter(c2pMeasurement);
  1225.                     }
  1226.                     else if (LOOP == '1') {
  1227.                         this.inLoop = true;
  1228.                         feedbacktime = Integer.parseInt(c2pMeasurement.getFeed_time()) * 1000;
  1229.                         while (inLoop) {
  1230.                             jitter(c2pMeasurement);
  1231.                             try {
  1232.                                 this.sleep(feedbacktime);
  1233.                             } catch (InterruptedException e) {
  1234.                                 e.printStackTrace();
  1235.                             }
  1236.                         }
  1237.                     }
  1238.                     else {
  1239.                         this.inLoop = true;
  1240.                         ArrayList<Coord2PeerDataMeasurement> l = new ArrayList<>();
  1241.                         while (inLoop) {
  1242.                             Coord2PeerDataMeasurement res = statisticJitter(c2pMeasurement);
  1243.                             l.add(res);
  1244.                             try {
  1245.                                 this.sleep(20000);
  1246.                             } catch (InterruptedException e) {
  1247.                                 e.printStackTrace();
  1248.                             }
  1249.                         }
  1250.                         if(c2pMeasurement.getAlarm()=='1')
  1251.                             sendToCoord(getJitterStatisticalPDU(l,c2pMeasurement.getAlarmCondition()));
  1252.                         else
  1253.                             sendToCoord(getJitterStatisticalPDU(l,null));
  1254.                     }
  1255.                     break;
  1256.                 }
  1257.                 default: {
  1258.                     break;
  1259.                 }
  1260.             }
  1261.         }
  1262.     }
  1263.  
  1264.  
  1265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement