Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to call erlang from java using erjang.RPC.call(m,f,a). To…
… do this, we added a bif called rpc:call_from_java/4, and a special pseudo term mbox representing the caller's mbox which is where to send the result, one of: {ok,Value} or {error,CatchValue}.
- Loading branch information
1 parent
940434e
commit 00067b2
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package erjang; | ||
|
||
import erjang.m.erlang.ErlBif; | ||
import erjang.m.erlang.ErlProc; | ||
import erjang.m.rpc.MBox; | ||
|
||
public class RPC { | ||
|
||
static EAtom am_$gen_call = EAtom.intern("$gen_call"); | ||
static EAtom am_rex = EAtom.intern("rex"); | ||
static EAtom am_undefined = EAtom.intern("undefined"); | ||
static EAtom am_user = EAtom.intern("user"); | ||
static EAtom am_call = EAtom.intern("call"); | ||
static EAtom am_rpc = EAtom.intern("rpc"); | ||
static EAtom am_call_from_java = EAtom.intern("call_from_java"); | ||
|
||
|
||
|
||
public static EObject call(EAtom mod, EAtom fun, ESeq args) { | ||
|
||
MBox mbox = new MBox(); | ||
EProc proc = new EProc(null, am_rpc, am_call_from_java, EList.make(mod, fun, args, mbox)); | ||
ERT.run(proc); | ||
proc.joinb(); | ||
|
||
return mbox.get_b(); | ||
} | ||
|
||
|
||
public static EObject call(EAtom mod, EAtom fun, ESeq args, long timeout) { | ||
|
||
MBox mbox = new MBox(); | ||
|
||
EProc proc = new EProc(null, am_rpc, am_call_from_java, | ||
EList.make(mod, fun, args, mbox)); | ||
ERT.run(proc); | ||
|
||
return mbox.get_b(timeout); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package erjang.m.rpc; | ||
|
||
import kilim.Mailbox; | ||
import kilim.Pausable; | ||
import erjang.EObject; | ||
import erjang.EPseudoTerm; | ||
|
||
public class MBox extends EPseudoTerm { | ||
|
||
Mailbox<EObject> mbox; | ||
|
||
public MBox() { | ||
this.mbox = new Mailbox<EObject>(); | ||
} | ||
|
||
public void put(EObject value) throws Pausable { | ||
mbox.put(value); | ||
} | ||
|
||
public EObject get_b() { | ||
return mbox.getb(); | ||
} | ||
|
||
public EObject get_b(long timeout) { | ||
return mbox.getb(timeout); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package erjang.m.rpc; | ||
|
||
import kilim.Pausable; | ||
import erjang.BIF; | ||
import erjang.ENative; | ||
import erjang.EObject; | ||
import erjang.EProc; | ||
import erjang.ERT; | ||
import erjang.ETuple; | ||
import erjang.ErlangException; | ||
import erjang.m.erlang.ErlBif; | ||
|
||
public class Native extends ENative { | ||
|
||
@BIF | ||
static public EObject call_from_java(EProc self, EObject m, EObject f, EObject args, | ||
EObject mbox) throws Pausable | ||
|
||
{ | ||
if (mbox instanceof MBox) { | ||
MBox embox = (MBox) mbox; | ||
|
||
try { | ||
EObject res = ErlBif.apply(self, m, f, args); | ||
embox.put(ETuple.make(ERT.am_ok, res)); | ||
return res; | ||
} catch (ErlangException e) { | ||
embox.put(ETuple.make(ERT.am_error, e.getCatchValue())); | ||
return ERT.am_undefined; | ||
} | ||
|
||
} else { | ||
throw ERT.badarg(); | ||
} | ||
} | ||
|
||
} |