Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- //import OraclePinger.Connector;
- import OraclePinger.*; // a mod of : http://docs.oracle.com/javase/1.4.2/docs/guide/nio/example/Ping.java
- import java.util.LinkedList;
- /* This skeleton app creates a window frame with a chart panel. Extend
- * this class and add more classes to complete the challenge.
- */
- public class LatencyChallenge {
- // The port we'll actually use
- static int port = 80;
- static String[] host = {"google.com", "yahoo.com"};
- static int datapoints = 6;
- //private final JFrame frame;
- private static final JFrame frame = new JFrame("Latency Challenge - by kendy");
- private static int w;
- public static void main(String[] args) throws Exception /*, InterruptedException, IOException */ {
- // Create the targets List
- LinkedList<Target> targetList = new LinkedList<Target>();
- int[][] pockets = new int[host.length + args.length][datapoints];
- ChartPanel chartpanel = new ChartPanel();
- chartpanel.dim2 = datapoints;
- frame.setSize(datapoints*50+20, 360);
- frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //frame.pack();
- frame.getContentPane().add(new JLabel("yahoo.com vs google.com latency"));
- frame.getContentPane().add(chartpanel);
- frame.setVisible(true);
- w = 0; //flag to the chartPanel- data points are new
- while(true) {
- if (w<datapoints)
- ++w;
- chartpanel.w = w;
- //Create new Connector threads
- Connector connector = new Connector();
- connector.start();
- //Create Targets, Add them to the List and Add them to the connector
- for (int i = 0; i < host.length; i++) {
- Target t = new Target(host[i], port);
- targetList.add(t);
- connector.add(t);
- }
- for (int i = 0; i < args.length; i++) {
- targetList.add(new Target(args[i], port));
- connector.add(targetList.getLast());
- }
- // Wait for the Connector & Printer Thread to Finish
- Thread.sleep(2000);
- connector.shutdown();
- connector.join();
- //updates the newest data
- pockets = shiftArray(pockets);
- int p = 0;
- for (Target t : targetList ) {
- pockets[p][datapoints-1] = (int)(t.time());
- ++p;
- }
- chartpanel.dataArray = pockets;
- array2dDebug(pockets);
- System.out.println();
- targetList.clear(); //clear target List
- }//end While(true) loop
- }
- //Move the values along the 2nd dim
- static int[][] shiftArray(int[][] arr){
- if(datapoints>1){
- for (int i=0;i<arr.length;i++){
- for(int j=0;j<datapoints-1;j++) {
- arr[i][j]= arr[i][j+1];
- }
- }
- }
- return arr;
- }
- //To console print -debug data pockets array
- static void array2dDebug(int[][] arr){
- for (int i=0;i<arr.length;i++){
- for(int j=0;j<datapoints;j++){
- System.out.print(arr[i][j]+",");
- }
- System.out.print('\n');
- }
- }
- //Originally
- // private final JFrame frame;
- //
- // LatencyChallenge() {
- // frame = new JFrame("LatencyChallenge");
- // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // frame.pack();
- // frame.setSize(300, 160);
- // frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
- // }
- //
- // public void run() {
- // frame.getContentPane().add(new JLabel("yahoo.com vs google.com latency"));
- // frame.getContentPane().add(new ChartPanel());
- //
- // frame.setVisible(true);
- // }
- //
- // public static void main(String args[]) {
- // (new LatencyChallenge()).run();
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement