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

Caoyuan <>
Fri Jan 23 22:31:47 CET 2009


http://blogtrader.net/page/dcaoyuan/entry/rpc_server_for_erlang_in

2009/1/24 Steven Edwards <>:
> `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?
>
> 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, }, {echo, Msg}).
>
> add(X, Y) -> rpc({mbox, }, {add, X, Y}).
>
> add2(X, Y) -> rpc({mbox, }, {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;
>         }
>     }
> }
> ---
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list