[erlang-questions] JInterface, RPC, self()

Steven Edwards <>
Fri Jan 23 21:09:05 CET 2009

`I still heart Erlang, but we're having some trouble communicating.  I'm
trying to get RPCs to work correctly, but receive {Pid, Result} fails.  I'm
pretty sure that it fails because I use {mbox, } as the
initial Pid and JInterface responds with a differently named process id.
(Same process, but Erlang's representation.)

I can get it working through a hack (by returning {pid_result,
mbox.whereis("mbox")} from the Java server on a {getpid} request), but is
there a more elegant solution?





-export([echo/1, add/2, add2/2]).

rpc(Pid, Msg) ->
    Pid ! {self(), Msg},
        {Pid, Result} -> {pid_received, Result};
        Result -> {nope, Result}
    after 750 -> false

echo(Msg) -> rpc({mbox, }, {echo, Msg}).

add(X, Y) -> rpc({mbox, }, {add, X, Y}).

add2(X, Y) -> rpc({mbox, }, {add2, X, Y}).


import com.ericsson.otp.erlang.*;

public class SimpleServer {
    public static void main(String[] args) throws Exception {
        OtpNode self = new OtpNode("simpleserver", "cookie");
        OtpMbox mbox = self.createMbox("mbox");
        OtpErlangObject[] tupleElements = new OtpErlangObject[2];

        tupleElements[0] = mbox.self();

        while (true) try {
            OtpErlangTuple terms = (OtpErlangTuple) mbox.receive();
            OtpErlangPid from = (OtpErlangPid) terms.elementAt(0);
            OtpErlangTuple msg = (OtpErlangTuple) terms.elementAt(1);

            String command = msg.elementAt(0).toString();

            if (command.equals("echo")) {
                mbox.send(from, new
            else if (command.equals("add")) {
                long x = ((OtpErlangLong) msg.elementAt(1)).longValue();
                long y = ((OtpErlangLong) msg.elementAt(2)).longValue();

                mbox.send(from, new OtpErlangLong(x+y));
            else if (command.equals("add2")) {
                long x = ((OtpErlangLong) msg.elementAt(1)).longValue();
                long y = ((OtpErlangLong) msg.elementAt(2)).longValue();
                tupleElements[1] = new OtpErlangLong(x+y);

                mbox.send(from, new OtpErlangTuple(tupleElements));
        catch (OtpErlangExit e) {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090123/1d97c5d6/attachment.html>

More information about the erlang-questions mailing list