
// $Id: LispApplet.java,v 1.1 2000/06/07 22:28:31 mm Exp $

import com.franz.jlinker.JavaLinkDist;
import com.franz.jlinker.TranStruct;

public class LispApplet extends java.applet.Applet {

  // A simple applet that forwards its main methods to Lisp
  // init() make a connection
  // start()  stop()  destroy() fwd to Lisp

  static Object lock = new Object();
  static int cons = 0;
  int call = 0;
  public static final String className = "LispApplet";
  public String name = "LispApplet";
  public String thisName = "";

  public LispApplet() { this(className); }
  public LispApplet(String nm) {
    synchronized (lock) { cons++; }
    thisName = nm + cons + ".";
  }
  

  void showThisStatus(String s){
    showStatus( thisName + call + ": " + s);
  }

  boolean connected = false;
  public void init () {

    //com.franz.jlinker.JavaLinkCommon.sdebug = true;
    //com.franz.jlinker.Transport.debug(0, 2);

    synchronized (this) { call++; }
    showThisStatus("Starting");
    if ( !JavaLinkDist.query(true) ) 
      {
	showThisStatus("Attempting to connect to Lisp");
	//System.out.println("Attempting to connect to Lisp");
	if 
	  // ( !JavaLinkDist.connect("", "", 0, 1000, 10) )
	  ( !JavaLinkDist.connect("", 4321, "", 0, -1, -1) )
	  {
	    showThisStatus("Failed to connect to Lisp");
	    connected = false;
	  }
	else
	  {
	    showThisStatus("Connected to Lisp");
	    connected = true;
	  }
      }
    else
      {
	showThisStatus("Already connected to Lisp");
	connected = true;
      }
  }

  public void destroy () {
    showThisStatus("Destroy called");
    if (connected)
      JavaLinkDist.invokeInLisp(-1, JavaLinkDist.newDistOb("destroy-applet"),
				new TranStruct[]
				{ JavaLinkDist.newDistOb( thisName+call ) }
				);
    else showThisStatus("Destroy called - not connected");
  }

  public void start () {
    showThisStatus("Start called");
    if (connected)
      JavaLinkDist.invokeInLisp(-1, JavaLinkDist.newDistOb("start-applet"),
			      new TranStruct[]
				{ JavaLinkDist.newDistOb(this),
				    JavaLinkDist.newDistOb( thisName+call )
				    });
    else showThisStatus("Start called - not connected");
  }

  public void stop () {
    showThisStatus("Stop called");
    if (connected)
      JavaLinkDist.invokeInLisp(-1, JavaLinkDist.newDistOb("stop-applet"),
				new TranStruct[]
				{ JavaLinkDist.newDistOb( thisName+call ) }
				);
    else showThisStatus("Stop called - not connected");
  }

}
