/**
 *
 * @author Copyright (c) 2000 by BEA Systems, Inc. All Rights Reserved.
 */

package examples.applets.ping;


import java.net.*;
import java.io.*;
import java.applet.*;
import java.awt.*;
import weblogic.common.*;

public class PingApplet extends TextApplet {

    public PingApplet() {
	start = new Button("start ping");
	addButton(start);
	stop = new Button("stop ping");
	addButton(stop);

	protocols = new Choice();
	protocols.addItem("http");
	protocols.addItem("https");
	protocols.addItem("t3");
	protocols.addItem("t3s");
	protocols.addItem("iiop");
	add(protocols);
	stats = new Statistics(10, "T3 Pings/minute", "10-second intervals",
			       500F, 5);
	stats.resize(300, 200);
	add(stats);
    }

    boolean startedThreads = false;
    Choice protocols;
    Statistics stats;
    Button start, stop;
    Pinger pinger;
    
    public boolean action(Event evt, Object what) {
	if (evt.target == start) {
	    p("***starting to ping...");
	    String proto = protocols.getSelectedItem();
	    int port = getCodeBase().getPort();
	    if (port == -1) {
		port = 80;
	    }
	    String cbproto = getCodeBase().getProtocol();
	    if (!cbproto.equals("https")) {
		if (proto.equals("https") || proto.equals("t3s")) {
		    // assume ssl port is 1+
		    port++;
		}
	    } else if (!(proto.equals("https") || proto.equals("t3s"))) {
		// assume cleartext port is ssl port -1
		port--;
	    }
	    pinger.start(proto, port);
	    stop.enable();
	    start.disable();
	    return true;
	} else if (evt.target == stop) {
	    p("***disconnecting pinger...");
	    pinger.stop();
	    stop.disable();
	    start.enable();
	    return true;
	}
	return false;
    }

    public void init() {
	p("applet::init()");
	if (!startedThreads) {
	    pinger = new Pinger(this, stats);
	    (new Thread(stats, "stats")).start();
	    (new Thread(pinger, "PINGER")).start();
	    startedThreads = true;
	}
    }

    public void start() {
	p("applet::start()");
	stop.disable();
	start.enable();
    }

    public void stop() {
	p("applet::stop()");
	pinger.stop();
	start.enable();
	stop.disable();
    }

    public void destroy() {
	p("applet::destroy()");
	pinger.quit();
	stats.quit();
    }
}













