package examples.t3client;

import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;

import weblogic.common.*;

/**
 * @author Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class TimeoutClient implements DisconnectListener {
  private boolean called = false;
  
  /** 
   * Creates an instance of TimeoutClient and connects to the WebLogic Server
   */
  public static void main(String argv[]) throws Exception 
  {
    if (argv.length != 1) {
      System.out.println("Usage:   " +
			 "java examples.t3client.timeout WebLogicURL");
      System.out.println("Example: " +
			 "java examples.t3client.timeout t3://localhost:7001");
      return;
    }
    
    // Create an instance of the client, so that it can recieve
    // the timeout callback from the server. 
    TimeoutClient toc = new TimeoutClient(argv[0]);
  }
  
  /**
   * Creates a T3Client and connects to the WebLogic Server given 
   * by argv[0]. The class registers itself as a Disconnect Listener.  
   * The class sets the idle timeout to 1 minute, then sleeps for 2 
   * minutes. When the thread wakes, it checks that the 
   * <tt>disconnectOccurred()</tt> method was invoked. 
   */
  public TimeoutClient(String weblogicURL) throws Exception {
    
    // Create a T3Client with the command line arg.
    T3Client t3 = new T3Client(weblogicURL);
    t3.connect();
    
    // We can register our client as a disconnect listener since it implements
    // the DisconnectListener interface. 
    t3.addDisconnectListener(this);               
    
    // Set the idle disconnect timeout. (The default is NEVER, so
    // set it to x number of minutes for this example.) The disconnect
    // is pessimistic, that is, it waits until the end of the minute
    // in which the timeout expires.
    t3.setIdleDisconnectTimeoutMins(1);  
    
    System.out.println("Waiting for 3 minutes, unless interrupted...\n");
    
    // Set the client to sleep for two minutes, so that the
    // timeout will expire.
    synchronized(this) {
      wait(1000 * 60 * 3);
    }
    
    // Print out the result.
    if (called == true) 
      System.out.println("Was notified that disconnect was called");
    else 
      System.out.println("Was not notified that disconnect was called");
    
    // Print a notice if the client is still connected.
    if (t3.isConnected())
      System.out.println("Still connected.");
    else
      System.out.println("Not connected");
    
    // There may be cases in which you might want to listen for a
    // disconnect initially, but at some time listening for the
    // disconnect becomes unimportant. You can remove the T3Client as
    // a disconnect listener by calling this method. It's commented
    // out here because it's unnecessary; the WebLogic Server and T3Client
    // are at this point no longer connected.
    
    // t3.removeDisconnectListener(this);               
    
    // You should always call the disconnect() method when a client exits
    
    t3.disconnect();
  }

  /**
   * This method should include the work that happens when
   * the client is notified of its impending disconnect.
   * Here, we'll just set a variable to indicate that the method
   * was called, and wake the thread that's sleeping in this instance. 
   *
   * @param de                A DisconnectEvent
   */
  public void disconnectOccurred(DisconnectEvent de) {
    System.out.println("timeoutCallBack.disconnectOccurred() called");
    called = true;
    synchronized(this) {
      this.notifyAll();
    }
  }
}
