import java.applet.Applet;
import netscape.javascript.*;

/**
 * @author weber
 *
 * Counter Applet - zaehlt wert auf der HTML-Seite hoch
 */
public class Counter extends Applet 
{

    JSObject javaScript;
    String id_ = "jshallo";
    private int count=2;
    private int gc=0;
    private int verbose=0;

    public void init()
    {
	String countparam;
	String param;
	System.out.print("Counter::init\n");
	countparam=getParameter("count");
	if(countparam!=null)
	{
	    try
	    {
		count = Integer.parseInt(countparam);
		System.out.println("count="+count);
	    }
	    catch(NumberFormatException e)
	    {
		System.out.println("count=" + countparam + " NumberFormatException");
	    }
	}
	else
	{
	    System.out.println("Parameter count nicht gefunden\n");
	}
	param=getParameter("verbose");
	if(param!=null)
	{
	    try
	    {
		verbose = Integer.parseInt(param);
		System.out.println("verbose="+verbose);
	    }
	    catch(NumberFormatException e)
	    {
		System.out.println("verbose=" + param + " NumberFormatException");
	    }
	}
	param=getParameter("garbagecollect");
	if(param!=null)
	{
	    try
	    {
		gc = Integer.parseInt(param);
		System.out.println("gc="+gc);
	    }
	    catch(NumberFormatException e)
	    {
		System.out.println("garbagecollect=" + param + " NumberFormatException");
	    }
	}
	System.out.println("------ Vor getWindow --------");
	javaScript = JSObject.getWindow(this);
	System.out.println("------ Nach getWindow --------");
	if (null == javaScript)
	{
	    System.out.println("JSObject nicht gefunden\n");
	    stop();
	} //End if
    }
    
    /* (non-Javadoc)
     * @see java.applet.Applet#start()
     */
    public void start()
    {
	Runtime rt = Runtime.getRuntime();
	Object arg[] = new Object[1];
	StringBuffer valuebuffer = new StringBuffer(64);
	int i;
	double sumfree=0.0;
	long curfree;
	long minfree=rt.maxMemory();
	int  numberofmemorychecks=0;
	Thread myself = Thread.currentThread();
	System.out.println("Counter::start "+myself.toString());
	arg[0] = valuebuffer;
	System.out.println("valuebuffer.capacity="+valuebuffer.capacity()
			   +" valuebuffer.length="+valuebuffer.length());
	try
	{
	    long startt = System.currentTimeMillis();
	    for(i=0;i<=count;i++)
	    {
		valuebuffer.setLength(0);
		valuebuffer.append(i);
		//System.out.println("----- vor call ------");
		javaScript.call(id_,arg);
		if(verbose>0)
		{
		    System.out.println("Inhalt: " + arg[0]);
		}
		if(i % 100 ==0)
		{
		    numberofmemorychecks++;
		    curfree=rt.freeMemory();
		    sumfree+=curfree;
		    minfree=(curfree<minfree ? curfree : minfree);
		    System.err.println("Resourcen: max=" + rt.maxMemory()
				       + " free="+ curfree
				       + " total="+rt.totalMemory());  
		    if(gc>0) 
		    {
			System.gc();
		    }
		}
	    }
	    long stopt = System.currentTimeMillis();
	    System.err.println("Resourcen: max=" + rt.maxMemory()
			       + " free="+ rt.freeMemory()
			       + " total="+rt.totalMemory());
	    if(numberofmemorychecks> 0)
	    {
		System.err.println("Minfree: " + minfree 
				   + " meanfree=" + (sumfree/numberofmemorychecks));
	    }
	    System.err.println("Time in ms: " + (stopt-startt));
	}
	catch(JSException jse)
	{
	    System.out.println(jse);
	}
	// System.gc();
	//javaScript.call("neuefarbe",null);
	}
    
    /* (non-Javadoc)
     * @see java.applet.Applet#stop()
     */
    public void stop()
	{
	    System.out.print("Counter::stop\n");
	}
	
	/* (non-Javadoc)
	 * @see java.applet.Applet#destroy()
	 */
    public void destroy()
	{
	    System.out.print("Counter::destroy\n");
	    System.gc();
	}
}
