
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.Toolkit;

/**
<!-- --*--*--*--*--*--*--*--*--*--*--*--*--*--* -->
* This class monitors if an Internet connection is
* available and optionally writes a message to
* standard output indicating the status of the
* Internet connection.
*
*
* 
* @author  M Bishop                   
<!-- --*--*--*--*--*--*--*--*--*--*--*--*--*--* -->
*/
public class NetConnectionPager extends Object
{
  boolean bIsInternetConnectionAvailable;
  boolean bUserAlertMode;
  int iPollingDelay;  //-- How often to check in Milli-seconds
  int iNumberOfPolls;
  Toolkit toolkit;
  Timer tmPollingTimer;

  //-- This Object should fire an event so that other
  //-- objects can know when the internet connection
  //-- becomes available.
  //--------------------------------------------
  public NetConnectionPager(int pollingDelay, boolean bAlertUser)
  {
    this.iPollingDelay = pollingDelay;
    this.bUserAlertMode = bAlertUser;
    toolkit = Toolkit.getDefaultToolkit();
    tmPollingTimer = new Timer();
    tmPollingTimer.schedule(new PollingTask(), 0, iPollingDelay);
  } //-- constr: (int, boolean)

  //--------------------------------------------
  public NetConnectionPager(int pollingDelay)
  {
    this(pollingDelay, true);
  } //-- constr: (int)

  //--------------------------------------------
  public boolean isInternetConnectionAvailable()
    { return bIsInternetConnectionAvailable; }

  //--------------------------------------------
  public void setPollingDelay(int iMilliSeconds)
    { this.iPollingDelay = iMilliSeconds; }

  //--------------------------------------------
  public int getPollingDelay()
    { return this.iPollingDelay; }

  //--------------------------------------------
  public void setUserAlertMode(boolean bAlertUser)
    { this.bUserAlertMode = bAlertUser; }

  //--------------------------------------------
  public boolean isUserAlertMode()
    { return this.bUserAlertMode; } 


  //--------------------------------------------
  class PollingTask extends TimerTask
  {
    public void run()
    {
      iNumberOfPolls++;
      try
      {
        URL url =
          new URL("http://www.yahoo.com");

        BufferedReader in = new BufferedReader(
          new InputStreamReader(url.openStream()));
        if (bUserAlertMode)
        {
          toolkit.beep();
          System.out.println("Internet Connection Available!!");
        }
        bUserAlertMode = false;
      }
      catch (IOException e)
      {
        if (bUserAlertMode)
        {
          System.out.println(
            iNumberOfPolls +
            "  Internet Connection not available");
        }
        bUserAlertMode = true;
      } //-- try, catch
    } //-- inner class method: run 
  } //-- inner class: PollingTask

  //--------------------------------------------
  public static void main(String[] args) throws Exception
  {
    int iPollingDelay = 0;
    boolean bAlertUser = true;

    if (args.length == 1)
    {
      iPollingDelay = Integer.parseInt(args[0]);
      bAlertUser = true;
    }
    else if (args.length == 2)
    {
      iPollingDelay = Integer.parseInt(args[0]);
      bAlertUser = Boolean.valueOf(args[1]).booleanValue();
    }
    else
    {
      System.out.println(
        "Class Name:                                         \n" +
        "-------------------                                 \n" + 
        "  NetConnectionPager:                             \n\n" +
        "Command line Usage:                                 \n" +
        "-------------------                                 \n" +
        "  java NetConnectionPager PollingDelay [AlertUser]  \n\n" +
        "Purpose:                                            \n" +
        "-------------------                                 \n" +
        "  Checks to see if a internet connection is         \n" +
        "  available, and continues checking every           \n" +
        "  determined period.                              \n\n" +
        "Examples:                                           \n" +
        "-------------------                                 \n" +
        "  java NetConnectionPager  4000                     \n" +
        "     Starts the program to check every 4 seconds    \n" +
        "     whether the internet connection has become     \n" +
        "     available.                                     \n" +
        "  java NetConnectionPager  10000 true               \n" +
        "     Starts the program to check every 10 seconds   \n" +
        "     if an internet connection is available and     \n" +
        "     alerts the user with some audio signal         \n" +
        "     when the connection becomes available          \n");
        System.exit(-1);
        
    } //-- if, else [Command Line Arguments]


    String sMessage = new String(
      "     \n" +
      "TESTING CLASS: NetConnectionPager()  \n" +
      "---------------------------------------- \n" +
      "     \n" +
      "     \n");
    
    System.out.println(sMessage);
    new NetConnectionPager(iPollingDelay, bAlertUser);
  } //-- main()
} //-- NetConnectionPager class
