[Erlang Systems]

2 An Example

This section demonstrates how Jive is used to communicate between Erlang and a Java client, which is a Java Applet or a Java Application.

The example shown consists of a Java client which displays a clock. The Java client spawns a clock process on the Erlang server which sends a time stamp back to the Java client every second .

In the case of an Applet, an Erlang process is automatically spawned when a user visits a HTML page which has the appropriate <APPLET> tags. The Erlang process is killed automatically when the Applet is stopped.

In the case of an Application, an Erlang process is automatically spawned when a user starts the appropriate Java Application. The Erlang process is killed automatically when the Java Application is stopped.

This example and detailed instructions can be found in the distribution of Jive ($ERLANG_ROOT/lib/jive-2.x/examples/).

Do the following to test the Java Applet and the Java Application examples:

  1. start the Erlang server (clock.erl) on a host of your choice, e.g. clock:init().
  2. start an appletviewer of your choice and access the HTML file (timer.html), e.g. appletviewer timer.html
  3. start a Java Application (TimerAlt.java), e.g. java TimerAlt akvavit 14000.

Warning!

If you put the Java Applet in a Web-server; The Erlang server need to be started on the same host. This is true if the Web browser you intend to use only supports "Applet Host" security mode, e.g. Netscape Navigator etc.

2.1 The Java Applet (Timer.java)

import java.applet.Applet;
import java.awt.*;
import jive.erlang.*;

public class Timer extends EApplet implements EReceive {
  private int time=0;
  
  /*
   * The start method initialize the Erlang connection and spawns an
   * Erlang clock server.
   */  
  public void start() {
    // Call the EApplet constructor to initialize the connection 
    super.start();
    try {
      // Register this Applet so Erlang can send messages to it
      EInteger id=receiver().register(this);
      // Spawn a clock server on the Erlang node
      ESock sender=getESock();
      sender.spawn("clock","start",new EList(id,self()));
    } catch (JiveException e) {
      System.err.println(e);
    }
  }
  
  public void paint(Graphics g) {
    g.drawString("Timer has been running for "+time+" seconds",10,10);
  }
  
  public void receive(EVar var) {
    if(var.type() == EVar.EINTEGER) {
      time=((EInteger)var).value();
      repaint();
    }
  }
}
    

The HTML Page (timer.html)

This is the HTML page which loads the Applet. Note the PARAM tag that specifies which port to use when connecting to the Erlang server.

<HTML>
<HEAD>
<TITLE>Jive Example</TITLE>
</HEAD>
<BODY>
<P>This example shows how a simple Java Applet communicates with Erlang 
using Jive.
<P><APPLET CODE="Timer.class" HEIGHT=50 WIDTH=300>
<PARAM NAME=PORT VALUE=14000>
Sorry! Your browser cannot execute Java programs...
</APPLET>
</BODY>
</HTML>
  

2.2 The Java Application (TimerAlt.java)

import java.awt.*;
import jive.erlang.*;
public class TimerAlt extends EApplication implements EReceive {
  private int time=0;
  
  /*
   * Usage: Timer host port
   */
  public static void main(String args[]) {
    if(args.length != 2) {
      System.err.println("Usage: Timer host port");
    } else {
      try {
        int port=Integer.parseInt(args[1]);
        new TimerAlt(args[0],port);
      } catch (JiveException e) {
        System.err.println(e);
        System.exit(0);
      }
    }
  }
  
  /*
   * Constructor which initialize the Erlang connection and spawns an
   * Erlang clock server.
   */
  public TimerAlt(String host,int port) throws JiveException {
    // Call the EApplication constructor to initialize the connection
    super(host,port);
    // Connect to the remote server
    connect();
    // Register this object so Erlang can send messages to it
    EInteger id=receiver().register(this);
    // Spawn a clock server on the Erlang node
    ESock sender=getESock();
    sender.spawn("clock","start",new EList(id,self()));
  }
  /*
   * receive gets called each time an Erlang message is sent to an object
   * instance of this class. This function need to be implemented hence
   * the class implements the EReceive interface.
   */
  public void receive(EVar var) {
    // Print the 10 first received time stamps on stdout
    if(var.type() == EVar.EINTEGER) {
      time=((EInteger)var).value();
      if(time == 10) {
        // Disconnect from the Erlang server
        disconnect();
        System.exit(0);
      } else {
        System.out.println("Timer has been running for "+time+" seconds");
      }
    }
  }
}
    

2.3 The Erlang Server (clock.erl)

-module(clock).
-export([init/0, start/2]).
init() ->
  jive:start(),
  jive:allow({clock,start,2}).
start(Receiver,PidId) ->
  Pid = jive:get_pid(PidId),
  link(Pid),
  loop(Receiver,Pid, 0).
loop(Receiver,Pid,Time) ->
  receive 
    after 1000 ->
      Pid ! {send, Receiver, Time}
  end,
  loop(Receiver,Pid,Time+1).
    

Copyright © 1991-98 Ericsson Telecom AB