Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import org.apache.commons.compress.archivers.tar.*;
  4. import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
  5.  
  6.  
  7. public class TrafficAnalysis {
  8.     public static void main (String argv[]) throws Exception
  9.     {
  10.  
  11.         //extracting CSV from tar.gz file
  12.         // ------ TROCAR NOME DO FICHEIRO PARA O NOSSO TRACE
  13.         TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream("testing_trace.csv.tar.gz")));
  14.         TarArchiveEntry file = tarInput.getNextTarEntry();
  15.         System.out.println("File = " + file.getName());
  16.  
  17.         //Printing CSV contents
  18.         BufferedReader br = new BufferedReader(new InputStreamReader(tarInput)); // Read directly from tarInput
  19.  
  20.         List<String> ipv4UniqueList = new ArrayList<String>();
  21.         List<String> tcpUniqueList = new ArrayList<String>();
  22.         String line;
  23.         int totalPacketLength = 0;
  24.         int counter = 0;
  25.         int ipv4Counter = 0;
  26.         int ipv6Counter = 0;
  27.         List<Integer> list = new ArrayList<Integer>();
  28.  
  29.         while ((line = br.readLine()) != null) {
  30.             // -------- delete this
  31.             if (counter++ == 100 ) break; //checking just a few lines for debugging first
  32.             System.out.println(line);
  33.  
  34.             //printing address of all packets
  35.             StringTokenizer token = new StringTokenizer(line,",");
  36.             String numStr = token.nextToken();
  37.             token.nextToken();
  38.             String sourceAddress = token.nextToken();
  39.             // -------- Ex 4
  40.             for(int i = 0; i < 2; i++)
  41.                 token.nextToken();
  42.             String sourcePort = token.nextToken();
  43.             if(sourcePort != "") {
  44.                 if(!tcpUniqueList.contains(sourcePort))
  45.                     tcpUniqueList.add(sourcePort);
  46.             }
  47.            
  48.             System.out.println("Address: " + sourceAddress);
  49.  
  50.             //Creating a list with the numbers associated with each packet
  51.             if (counter == 1) continue; //ignore first line
  52.             numStr = numStr.replace("\"", "");
  53.             int num = Integer.parseInt(numStr);
  54.             list.add(num);
  55.            
  56.             token.nextToken();
  57.             int length = Integer.parseInt(token.nextToken());
  58.             totalPacketLength += length;
  59.  
  60.             // ----- Ex1 e 2 Conta o n de 1pv6 e ipv4
  61.             for(int i = 1; i <= 4; i++) {
  62.                 if(sourceAddress.charAt(i) == ('.')) {
  63.                     ipv4Counter ++;
  64.                     // -------- Ex 3
  65.                     if(!ipv4UniqueList.contains(sourceAddress))
  66.                         ipv4UniqueList.add(sourceAddress);
  67.                     break;
  68.                 } else if (i == 4) {
  69.                     ipv6Counter ++;
  70.                     System.out.println("linha de ipv6 =" +  counter);
  71.                 }
  72.             }
  73.         }
  74.  
  75.         //Show list of numbers
  76.         System.out.println(list);
  77.         System.out.println(totalPacketLength/list.size());
  78.         System.out.println("tcpUniqueList: " +tcpUniqueList.size());
  79.         System.out.println("ipv4UniqueList: " +ipv4UniqueList.size());
  80.         System.out.println("numero de ipv6 " + ipv6Counter);
  81.         System.out.println("numero de ipv4 " + ipv4Counter);
  82.  
  83.         //create plot with list of numbers
  84.         //first, create data file
  85.         BufferedWriter myDataFile = new BufferedWriter(new FileWriter("myData.txt"));
  86.         Iterator itr = list.iterator();
  87.         while (itr.hasNext())
  88.         {
  89.             int i = (Integer)itr.next();
  90.             myDataFile.write(i + " " + i + "\n");
  91.         }
  92.  
  93.         //second, create gnuplot file
  94.         BufferedWriter myGnuplotFile = new BufferedWriter(new FileWriter("myPlot.gp"));
  95.         myGnuplotFile.write("set terminal svg size 350,262\n");
  96.         myGnuplotFile.write("set output 'plot.svg'\n");
  97.         myGnuplotFile.write("set xrange [0:7]\n");
  98.         myGnuplotFile.write("set yrange [0:7]\n");
  99.         myGnuplotFile.write("plot 'myData.txt'\n");
  100.  
  101.         myDataFile.close();
  102.         myGnuplotFile.close();
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement