This package provides support for communication with Erlang and representation of Erlang datatypes.

Note: By default, jinterface is only guaranteed to be compatible with other Erlang/OTP components from the same release as jinterface itself. For example, jinterface from the OTP R10 release is not compatible with an Erlang emulator from the OTP R9 release by default. jinterface can be set in compatibility mode of an earlier release (not earlier that R7), though. The compatibility mode is set by usage of the OtpCompatRel property. By starting the jvm with the command-line argument -DOtpCompatRel=9, jinterface will be compatible with the R9 release of OTP. Warning! You may run into trouble if this feature is used carelessly. Always make sure that all communicating components are either from the same Erlang/OTP release, or from release X and release Y where all components from release Y are in compatibility mode of release X.

The classes {@link com.ericsson.otp.erlang.OtpErlangList}, {@link com.ericsson.otp.erlang.OtpErlangTuple}, {@link com.ericsson.otp.erlang.OtpErlangBinary}, {@link com.ericsson.otp.erlang.OtpErlangAtom}, {@link com.ericsson.otp.erlang.OtpErlangBoolean}, {@link com.ericsson.otp.erlang.OtpErlangByte}, {@link com.ericsson.otp.erlang.OtpErlangChar}, {@link com.ericsson.otp.erlang.OtpErlangDouble}, {@link com.ericsson.otp.erlang.OtpErlangFloat}, {@link com.ericsson.otp.erlang.OtpErlangLong}, {@link com.ericsson.otp.erlang.OtpErlangInt}, {@link com.ericsson.otp.erlang.OtpErlangUInt}, {@link com.ericsson.otp.erlang.OtpErlangShort}, {@link com.ericsson.otp.erlang.OtpErlangUShort}, {@link com.ericsson.otp.erlang.OtpErlangString}, {@link com.ericsson.otp.erlang.OtpErlangObject}, {@link com.ericsson.otp.erlang.OtpErlangPid}, {@link com.ericsson.otp.erlang.OtpErlangPort}, and {@link com.ericsson.otp.erlang.OtpErlangRef} represent the various Erlang datatypes.

There are two basic mechanisms for communicating with Erlang, described briefly here. Note that the two mechanisms are not intended to be used together. Which mechanism you choose depends on your application and the level of control it needs.

You can use {@link com.ericsson.otp.erlang.OtpNode}, which can manage incoming and outgoing connections for you. With {@link com.ericsson.otp.erlang.OtpNode} a thread is automatically started to listen for incoming connections, make necessary outgoing connections, and dispatch messages to their recipients. {@link com.ericsson.otp.erlang.OtpNode} supports the concept of {@link com.ericsson.otp.erlang.OtpMbox mailboxes}, allowing you to have several Java components communicating independently with Erlang.

  OtpNode node = new OtpNode("bingo");
  OtpMbox mbox = node.createMbox();
  
  mbox.send("foo@localhost",new OtpErlangAtom("hej"));

If you need more control (but less support from the library), you can manage connections yourself using the {@link com.ericsson.otp.erlang.OtpSelf} and {@link com.ericsson.otp.erlang.OtpConnection} classes, in which case you can control explicitly which connections are made and which messages are sent. Received messages are not dispatched by {@link com.ericsson.otp.erlang.OtpConnection}.

The classes {@link com.ericsson.otp.erlang.OtpPeer}, {@link com.ericsson.otp.erlang.OtpSelf} and {@link com.ericsson.otp.erlang.OtpServer} are used to represent OTP nodes and are neccessary in order to set up communication between the Java thread and a remote node. Once a connection has been established, it is represented by an {@link com.ericsson.otp.erlang.OtpConnection}, through which all communication goes.

Setting up a connection with a remote node is straightforward. You create objects representing the local and remote nodes, then call the local node's {@link com.ericsson.otp.erlang.OtpSelf#connect(com.ericsson.otp.erlang.OtpPeer) connect()} method:

  OtpSelf self = new OtpSelf("client","cookie");
  OtpPeer other = new OtpPeer("server");
  OtpConnection conn = self.connect(other);

If you wish to be able to accept incoming connections as well as make outgoing ones, you first must register the listen port with EPMD (described in the Erlang documentation). Once that is done, you can accept incoming connections:

  OtpServer self = new OtpSelf("server","cookie");
  self.publishPort();
  OtpConnection conn = self.accept();

Once the connection is established by one of the above methods ({@link com.ericsson.otp.erlang.OtpSelf#connect(com.ericsson.otp.erlang.OtpPeer) connect()} or {@link com.ericsson.otp.erlang.OtpSelf#accept() accept()}), you can use the resulting {@link com.ericsson.otp.erlang.OtpConnection OtpConnection} to send and receive messages:

  OtpErlangAtom msg = new ErlangOtpAtom("hello");
  conn.send("echoserver", msg);
  
  OtpErlangObject reply = conn.receive();
  System.out.println("Received " + reply);

Finally, you can get an even greater level of control (and even less support from the library) by subclassing {@link com.ericsson.otp.erlang.AbstractConnection} and implementing the communication primitives needed by your application.