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

Steven Edwards cureadvocate@REDACTED
Sat Jan 24 02:14:45 CET 2009


A simple solution came to mind: if receive {Pid, Result} isn't working
because Pid is bound to {mbox, simpleserver@REDACTED}, why not return that
instead of mbox.self()?  Unsurprisingly, it works!

The updated server code:

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

        OtpErlangObject[] selfTuple = new OtpErlangObject[2];
        selfTuple[0] = new OtpErlangAtom("mbox");
        selfTuple[1] = new OtpErlangAtom(self.node());

        OtpEpmd.publishPort(self);
        tupleElements[0] = new OtpErlangTuple(selfTuple);

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


On Fri, Jan 23, 2009 at 3:09 PM, Steven Edwards <cureadvocate@REDACTED>wrote:

> `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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090123/f601154f/attachment.htm>


More information about the erlang-questions mailing list