<%@ page import = "com.cyscape.browserhawk.*" %>

<%
   // This sample is provided under the terms of the BrowserHawk End User License Agreement.
   // Copyright (c) 2003-2011 cyScape, Inc. All rights reserved.

   // Display a friendly error message if the edition of BrowserHawk being used is
   // not sufficient to run this sample.
   // You should not include this check in your BrowserHawk code.
   if (BrowserHawk.getVersion().indexOf("Pro") == -1 &&
       BrowserHawk.getVersion().indexOf("Ent") == -1) {
     out.println("Professional or Enterprise License Required.  ");
     out.println("You are using " + BrowserHawk.getVersion() + ".  ");
     out.println("See <a href='http://www.cyscape.com/order/'>http://www.cyscape.com/order/</a> for upgrade information.");
     return;
   }

   // This sample demonstrates how to perform the new "Progressive" download connection
   // speed tests.  This speed test provides greater accuracy and more fine grain control
   // over the test execution than was possible in previous versions of BrowserHawk. 

   // IMPORTANT - MANDATORY CONFIGURATION
   // The Progressive connection speed and latency tests require some initial configuration.
   // To ensure you do not run this sample without first performing the mandatory
   // configuration (and then wonder why it didn't work) we hard coded this sample to display
   // this message indicating that the setup has not yet been completed.  If you see this
   // message, it means that you need to please review steps 1 - 4 outlined below and set the
   // SupportFilesRoot property according (as described below).
   
   // In order to perform a progressive speed check, you must first perform the following
   // actions:
   
   // STEP 1: YOU MUST DOWNLOAD A SET OF IMAGE FILES AND PLACE THESE ON YOUR WEB SERVER

   // The Progressive connection speed test uses a group of image files to test the user's
   // download speed.  As a one-time configuration step, these files must be downloaded from
   // cyScape and placed on your server.

   // Download the image files from: 
   //       http://www.cyscape.com/download/speed-supportfiles.zip 

   // STEP 2: Set SupportFilesRoot

   // You must set the SupportFilesRoot property to the location where you stored the image
   // files.  Note that this should be an absolute or relative path, not a file system path.
   // For example, if you installed the files in \mysite\bhspeed and the "mysite" directory
   // is your site's root directory, then you should set SupportFilesRoot to "/bhspeed".

   // STEP 3: Verify your path setting is correct

   // Next you should verify that the path setting you chose is correct and that the files
   // are in the right location and accessible to the browser.  Open your browser and try to
   // directly access one of the image files. To do this, you should construct a URL using
   // your web site name, the path set in Step 2, and one of the image file names, such as
   // img10.gif.  For example,
   //   http://www.mydomain.com/bhspeed/img10.gif
   //
   // When entering your URL you should see the image displayed in the browser.  If so, the
   // verification is complete. If not, double check your work from steps 1 and 2.
   
   // STEP 4: Enable the Progressive speed check option

   // To ensure backward compatibility for customers upgrading to BH 12 who have not yet set
   // these options, BrowserHawk defaults to using the old-style ConnectionSpeed test.  To
   // have BH use the Progressive speed test, you must set the SpeedCheckType property to
   // indicate a Progressive speed test is desired.
   
   // For additional information you can see the SPEED_README.txt file contained in the zip
   // referenced above or the BH documentation.
  
   ExtendedOptions options = new ExtendedOptions();
   options.setPageTitle("Please wait...");
   options.setPageMessage("Please wait while your connection speed is detected...");
   options.addProperties("ConnectionSpeed, ConnectionLatency");

   // TODO - Set the SupportFilesRoot property below, based on the instructions in Step 2
   // above.  The sample will not run until you do so.
   options.setSupportFilesRoot("");   // YOU FILL THIS IN

   // Here's a check to make sure you read the above notes and have performed the necessary
   // configuration.
   if ("".equals(options.getSupportFilesRoot())) {
   %>

<html>
  <body>
    <h2>THIS SAMPLE REQUIRES CONFIGURATION BEFORE IT WILL RUN</h2>
	<blockquote>
	  The Progressive connection speed and latency tests require some initial configuration.
	  To ensure you do not run this sample without first performing the mandatory
      configuration (and then wonder why it didn't work) we hard coded this sample to
      display an error message indicating that the setup has not yet been completed.<p>

     <b><font color="red">If you see this message, it means that you need to please
	  review the source code of this sample and follow steps 1 - 4 as outlined in
      its comments.  In particular, you must set the SupportFilesRoot property in
      the sample. Search for "TODO" in this sample's source code.</b></font><p>

     After performing the requires setup please <b><a href="speed.jsp">run this sample
     again</a></b> to see it in action.<p>

     <b><a href="index.html">Return to list of other JSP samples</a></b><P>
	   </blockquote>
  <%
    } else {
   
      options.setSpeedCheckType(ExtendedOptions.SPEED_CHECK_TYPE_PROGRESSIVE);
  
      // Notice how the SpeedCheckTpe and SupportFilesRoot must be set prior to calling
      // the GetExtendedBrowser method.

      // Note: Use of the Progressive speed check can use a lot of bandwidth on the site where
      // the speed test files are hosted, which could cause a significant increase in your fees,
      // depending on your service plan with such providers and how traffic/bandwidth is billed.
      // cyScape is not responsible for any such fees you may incur.  See the README file
      // referenced above for more information on how bandwidth is used.
   
      ExtendedBrowserInfo extBrow = BrowserHawk.getExtendedBrowserInfo(request, response, options);
  %>

<html>
  <head>
    <title>BrowserHawk Sample - Progressive Connection Speed Test (Java)</title>
  </head>
  <body><font face="arial"> 
  <blockquote>
	Your download connection speed is: <%= extBrow.getConnectionSpeed() %> <P>
	Your download connection latency is: <%= extBrow.getConnectionLatency() %> <P>

	Connection speed for display purposes: 
    <%=
      // The translate() method is a convenient way to display the speed in Mbps
      // and Kbps (dialup users) to end users so we demonstrate that here.
      extBrow.translate("ConnectionSpeed")
    %> <P>

<%

// Although this script does not demonstrate it, below is an example of how you
// would get the result and do something with it.  For example, assume you wanted
// to handle dial up and broadband users differently.  To get the speed in bits
// per second just check the connectionspeed property like this:
//    int speed = extBrow.getConnectionSpeed();
// If you want to To calculate kbytes per second use this formula:
//    kbytesPerSec = speed / 8 / 1024;

// If you wanted to redirect users with slow connections here's how you can do it --
// in this example we redirect anyone slower than 768 kbits per second to a more
// basic page:
// if (speed > 0 && speed < 768000) {
//    response.sendRedirect("/basicpage.jsp");
// else if (speed < 1)
//    response.sendRedirect("/detection_na.jsp"); // timeout returns -1
// else
//    response.sendRedirect("/highspeed.jsp");
// }
%>
<p>
Note: The ConnectionSpeed property should be used when you want to know the vistor's
actual connection speed, such as the case if you are estimating a download time.  
If you mainly just want to detect broadband vs. dial-up users, the Broadband property
is better suited for that purpose.  See the topic "Detecting Connection Speeds and
Dialup Users" in the BrowserHawk documentation for more information. Also see the
<a href="uploadspeed.jsp">upload connection speed sample</a> and the 
<a href="broadband.jsp">broadband sample</a>.
<P>
Note: The Progressive speed check can use a lot of bandwidth on the site where
the speed test files are hosted, which could cause a significant increase in
your hosting fees, depending on your service plan with such providers and how
traffic/bandwidth is billed. See the SPEED_README.txt file reference in the source's
sample or the BH documentation for more information.
<p></p>
<HR>
<P>
<b><a href="speed.jsp">Retry this test</a></b><P>
<b><a href="index.html">Return to list of other JSP samples</a></b><P>
<b><a href="http://www.cyscape.com/order/">BrowserHawk Pricing and Ordering</a></b><P>
<b><a href="http://www.cyscape.com/">cyScape home page</a></b><P>
</font></blockquote>
</body>
</html>
<%
  }
%>
