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

Steven Edwards cureadvocate@REDACTED
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, simpleserver@REDACTED} 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?

Thanks,

Steven

//

Client:

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

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

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

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

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

Server:

---
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];

        OtpEpmd.publishPort(self);
        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
OtpErlangAtom(msg.elementAt(1).toString()));
            }
            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) {
            break;
        }
    }
}
---
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090123/1d97c5d6/attachment.htm>


More information about the erlang-questions mailing list