/* * ------------------------------------------------------------------------------- * This applet connects to the server from which it was downloaded, * and then measures and reports the time it takes to download * a test page * * Author: Wolfgang Bluhm, mail@wbluhm.com * * Copyright 2001 The Regents of the University of California, All Rights Reserved * ------------------------------------------------------------------------------- * * Some portions of this program were adopted from the book * "Java Examples in a Nutshell", 2nd Edition, * which carried the following copyright notice: * * Copyright (c) 2000 David Flanagan. All rights reserved. * This code is from the book Java Examples in a Nutshell, 2nd Edition. * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied. * You may study, use, and modify it for any non-commercial purpose. * You may distribute it non-commercially as long as you retain this notice. * For a commercial use license, or to purchase the book (recommended), * visit http://www.davidflanagan.com/javaexamples2. */ import java.io.*; import java.net.*; import java.applet.*; import java.awt.*; public class DownloadTime2 extends Applet { long time_download; long time_start; boolean timedOut = false; String display_time; String address; URL url; String protocol; String host; String filename; String test_file = "misc/WolfgangBluhm.pdf"; Socket socket; InputStream from_server; PrintWriter to_server; int port; final long MAX_TIME = 5000; final long BAD_TIME = 1000; int width = 75; int height = 15; public void init() { setBackground( Color.white ); } public void paint( Graphics g ) { super.paint( g ); try { display_time = "..."; // System.out.print("Waiting...\n"); g.clearRect(1,1,width,height); g.setColor( Color.black ); g.drawString( display_time, 10, 15 ); } catch ( Exception e ) { System.out.print("Some kind of error, first try block \n"); } try { // time in milliseconds at start of download time_start = System.currentTimeMillis(); // Now use the URL class to parse the user-specified URL into // its various parts. url = getCodeBase(); // System.out.print(url.getHost() + "\n"); protocol = url.getProtocol(); if (!protocol.equals("http")) // Check that we support the protocol throw new IllegalArgumentException("Must use 'http:' protocol"); host = url.getHost(); port = url.getPort(); if (port == -1) port = 80; // if no port, use the default HTTP port filename = url.getFile(); filename += test_file; // System.out.print(filename + "\n"); // Open a network socket connection to the specified host and port socket = new Socket(host, port); // Get input and output streams for the socket from_server = socket.getInputStream(); to_server = new PrintWriter(socket.getOutputStream()); // Send the HTTP GET command to the Web server, specifying the file // This uses an old and very simple version of the HTTP protocol to_server.print("GET " + filename + "\n\n"); to_server.flush(); // Send it right now! // Now read the server's response byte[] buffer = new byte[4096]; int bytes_read; // loop until download complete or timed out while((bytes_read = from_server.read(buffer)) != -1 && timedOut == false ) { // System.out.write(buffer, 0, bytes_read); // only for testing, do NOT use with binary files! if ( System.currentTimeMillis() - time_start > MAX_TIME ) { timedOut = true; display_time = "timed out"; // System.out.print( "timed out\n" ); g.clearRect(1,1,width,height); g.setColor( new Color(255,0,0) ); g.drawString( display_time, 10, 15 ); } } } catch ( Exception e ) { System.out.print("Some kind of error, second try block\n"); } try { // time it took to download page time_download = System.currentTimeMillis() - time_start; if ( time_download <= MAX_TIME ) { display_time = Long.toString( time_download ) + " ms"; // System.out.print( display_time + "\n" ); g.clearRect(1,1,width,height); if ( time_download > BAD_TIME ) { g.setColor( new Color(255,0,0) ); } else { g.setColor( new Color(0,255,0) ); } g.drawString( display_time, 10, 15 ); } } catch ( Exception e ) { System.out.print("Some kind of error, third try block\n"); } finally { stop(); } } public void stop() { // System.out.print( "Applet stopped\n" ); try { if ( socket != null ) { socket.close(); socket = null; } } catch ( Exception e ) { System.out.print( "Error closing socket\n" ); } } public void update( Graphics g ) { paint(g); } }