Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import org.apache.commons.compress.archivers.tar.*;
- import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
- public class TrafficAnalysis {
- public static void main (String argv[]) throws Exception
- {
- //extracting CSV from tar.gz file
- // ------ TROCAR NOME DO FICHEIRO PARA O NOSSO TRACE
- TarArchiveInputStream tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream("testing_trace.csv.tar.gz")));
- TarArchiveEntry file = tarInput.getNextTarEntry();
- System.out.println("File = " + file.getName());
- //Printing CSV contents
- BufferedReader br = new BufferedReader(new InputStreamReader(tarInput)); // Read directly from tarInput
- List<String> ipv4UniqueList = new ArrayList<String>();
- List<String> tcpUniqueList = new ArrayList<String>();
- String line;
- int totalPacketLength = 0;
- int counter = 0;
- int ipv4Counter = 0;
- int ipv6Counter = 0;
- List<Integer> list = new ArrayList<Integer>();
- while ((line = br.readLine()) != null) {
- // -------- delete this
- if (counter++ == 100 ) break; //checking just a few lines for debugging first
- System.out.println(line);
- //printing address of all packets
- StringTokenizer token = new StringTokenizer(line,",");
- String numStr = token.nextToken();
- token.nextToken();
- String sourceAddress = token.nextToken();
- // -------- Ex 4
- for(int i = 0; i < 2; i++)
- token.nextToken();
- String sourcePort = token.nextToken();
- if(sourcePort != "") {
- if(!tcpUniqueList.contains(sourcePort))
- tcpUniqueList.add(sourcePort);
- }
- System.out.println("Address: " + sourceAddress);
- //Creating a list with the numbers associated with each packet
- if (counter == 1) continue; //ignore first line
- numStr = numStr.replace("\"", "");
- int num = Integer.parseInt(numStr);
- list.add(num);
- token.nextToken();
- int length = Integer.parseInt(token.nextToken());
- totalPacketLength += length;
- // ----- Ex1 e 2 Conta o n de 1pv6 e ipv4
- for(int i = 1; i <= 4; i++) {
- if(sourceAddress.charAt(i) == ('.')) {
- ipv4Counter ++;
- // -------- Ex 3
- if(!ipv4UniqueList.contains(sourceAddress))
- ipv4UniqueList.add(sourceAddress);
- break;
- } else if (i == 4) {
- ipv6Counter ++;
- System.out.println("linha de ipv6 =" + counter);
- }
- }
- }
- //Show list of numbers
- System.out.println(list);
- System.out.println(totalPacketLength/list.size());
- System.out.println("tcpUniqueList: " +tcpUniqueList.size());
- System.out.println("ipv4UniqueList: " +ipv4UniqueList.size());
- System.out.println("numero de ipv6 " + ipv6Counter);
- System.out.println("numero de ipv4 " + ipv4Counter);
- //create plot with list of numbers
- //first, create data file
- BufferedWriter myDataFile = new BufferedWriter(new FileWriter("myData.txt"));
- Iterator itr = list.iterator();
- while (itr.hasNext())
- {
- int i = (Integer)itr.next();
- myDataFile.write(i + " " + i + "\n");
- }
- //second, create gnuplot file
- BufferedWriter myGnuplotFile = new BufferedWriter(new FileWriter("myPlot.gp"));
- myGnuplotFile.write("set terminal svg size 350,262\n");
- myGnuplotFile.write("set output 'plot.svg'\n");
- myGnuplotFile.write("set xrange [0:7]\n");
- myGnuplotFile.write("set yrange [0:7]\n");
- myGnuplotFile.write("plot 'myData.txt'\n");
- myDataFile.close();
- myGnuplotFile.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement