[Erlang Systems]

4 The Erlang Interface

This section describes the Erlang Interface to Jive and includes the following topics:

4.1 Starting the Jive Server

Jive contains a module which handles the interaction between Java and Erlang. Before using any of the functionality in the jive module a Jive server has to be started. By default, this server listens to port 14000, but a different port can be supplied.

To start a server on port 14000, enter:

jive:start( ).

To start a server on port 4711, enter:

jive:start(4711).

4.2 Access Control

Using the Java interface, it is possible to do Erlang apply on functions, or to spawn processes on the Erlang server. A strict access policy is used to prevent security problems. It is only possible to access functions which are explicitly declared from the Erlang side.

To allow Java to access the function io:format/1:

jive:allow({io,format,1})

To allow access to everything:

jive:allow(all)

Warning!

Use the all argument with care.

4.3 Using Processes

In order for Java to send messages to a specific Erlang process, the process must register its Pid and receive a corresponding Pid-Id.

PidId = jive:register_pid(Pid).

To get a Pid from a Pid-Id:

Pid = jive:get_pid(PidId).

4.4 Using Strings

Erlang does not differentiate between lists and strings. In Java, this differentiation is necessary for reasons of efficiency and clarity, and the jive module contains converters which convert between strings and lists.

A jive string is represented by the tuple {string, [<char>]}.

Conversion functions supplied are:

String = jive:list_to_string(List).
List = jive:string_to_list(String).

4.5 Sending a Message to Java

When a Java client connects to the Jive server, the server starts an Erlang process which allows the Jive server to send messages to it.

The Java client knows this Erlang process as the self process. The Java client must supply the self Pid-id to each process that should send messages back to the client.

The following example illustrates:

new EProcess("example","test",new EList(receiverID, self()));

A new Erlang process test is spawned with the self Pid-id as a parameter. The receiverID is an EInteger which specifies the Java object that should receive the message (see The Run-time Environment).

The test process can then use the PidId to send messages to the client:

test(Receiver, PidId) ->
  Pid = jive:get_pid(PidId),
  Pid ! {send, Receiver, "This is a reply"}.

Note!

Messages sent to the client are wrapped into the tuple {send, <receiver>, <message>}.

4.6 Detecting Client Disconnect

The self process automatically dies when a client disconnects. Before termination it removes itself from the jive process registry.

If you need to detect when the client disconnects, then link your process to the self process.


Copyright © 1991-98 Ericsson Telecom AB