1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. //import OraclePinger.Connector;
  5. import OraclePinger.*; // a mod of : http://docs.oracle.com/javase/1.4.2/docs/guide/nio/example/Ping.java
  6.  
  7. import java.util.LinkedList;
  8.  
  9. /* This skeleton app creates a window frame with a chart panel. Extend
  10.  * this class and add more classes to complete the challenge.
  11.  */
  12. public class LatencyChallenge {
  13.     // The port we'll actually use
  14.     static int port = 80;
  15.     static String[] host = {"google.com", "yahoo.com"};
  16.     static int datapoints = 6;
  17.  
  18.     //private final JFrame frame;
  19.     private static final JFrame frame = new JFrame("Latency Challenge - by kendy");
  20.     private static int w;
  21.  
  22.     public static void main(String[] args) throws Exception /*, InterruptedException, IOException */ {
  23.  
  24.         // Create the targets List
  25.         LinkedList<Target> targetList = new LinkedList<Target>();
  26.  
  27.         int[][] pockets = new int[host.length + args.length][datapoints];
  28.         ChartPanel chartpanel = new ChartPanel();
  29.         chartpanel.dim2 = datapoints;
  30.  
  31.         frame.setSize(datapoints*50+20, 360);
  32.         frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
  33.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.         //frame.pack();
  35.  
  36.         frame.getContentPane().add(new JLabel("yahoo.com vs google.com latency"));
  37.         frame.getContentPane().add(chartpanel);
  38.  
  39.         frame.setVisible(true);
  40.  
  41.         w = 0;  //flag to the chartPanel- data points are new
  42.         while(true) {
  43.             if (w<datapoints)
  44.                 ++w;
  45.             chartpanel.w = w;
  46.  
  47.             //Create new Connector threads
  48.             Connector connector = new Connector();
  49.             connector.start();
  50.  
  51.             //Create Targets, Add them to the List and Add them to the connector
  52.             for (int i = 0; i < host.length; i++) {
  53.                 Target t = new Target(host[i], port);
  54.                 targetList.add(t);
  55.                 connector.add(t);
  56.             }
  57.             for (int i = 0; i < args.length; i++) {
  58.                 targetList.add(new Target(args[i], port));
  59.                 connector.add(targetList.getLast());
  60.             }
  61.  
  62.             // Wait for the Connector & Printer Thread to Finish
  63.             Thread.sleep(2000);
  64.             connector.shutdown();
  65.             connector.join();
  66.  
  67.             //updates the newest data
  68.             pockets = shiftArray(pockets);
  69.             int p = 0;
  70.             for (Target t : targetList ) {
  71.                 pockets[p][datapoints-1] = (int)(t.time());
  72.                 ++p;
  73.             }
  74.  
  75.             chartpanel.dataArray = pockets;
  76.  
  77.             array2dDebug(pockets);
  78.             System.out.println();
  79.  
  80.             targetList.clear(); //clear target List
  81.  
  82.         }//end While(true) loop
  83.     }
  84.  
  85.     //Move the values along the 2nd dim
  86.     static int[][] shiftArray(int[][] arr){
  87.         if(datapoints>1){
  88.             for (int i=0;i<arr.length;i++){
  89.                 for(int j=0;j<datapoints-1;j++) {
  90.                     arr[i][j]= arr[i][j+1];
  91.                 }
  92.             }
  93.         }
  94.         return arr;
  95.     }
  96.  
  97.     //To console print -debug data pockets array
  98.     static void array2dDebug(int[][] arr){
  99.         for (int i=0;i<arr.length;i++){
  100.             for(int j=0;j<datapoints;j++){
  101.                 System.out.print(arr[i][j]+",");
  102.             }
  103.             System.out.print('\n');
  104.         }
  105.     }
  106.  
  107. //Originally    
  108. //  private final JFrame frame;
  109. //
  110. //  LatencyChallenge() {
  111. //      frame = new JFrame("LatencyChallenge");
  112. //      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  113. //      frame.pack();
  114. //      frame.setSize(300, 160);
  115. //      frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
  116. //  }
  117. //
  118. //  public void run() {
  119. //      frame.getContentPane().add(new JLabel("yahoo.com vs google.com latency"));
  120. //      frame.getContentPane().add(new ChartPanel());
  121. //
  122. //      frame.setVisible(true);
  123. //  }
  124. //
  125. //  public static void main(String args[]) {
  126. //      (new LatencyChallenge()).run();
  127. //  }
  128.  
  129. }