So, you can integrate Java with Erlang, using the jinterface code supplied as part of the standard Erlang distribution. It's Java, so can we do this with .Net?
Let's start with J#. We set up the erlang source
-module(mathserver).
-export([start/0, add/2]).
start() ->
Pid = spawn(fun() -> loop() end),
register(mathserver, Pid).
loop() ->
receive
{From, {add, First, Second}} ->
From ! {mathserver, First + Second},
loop()
end.
add(First, Second) ->
mathserver ! {self(), {add, First, Second}},
receive
{mathserver, Reply} -> Reply
end.
and test it with
>erl -name servernode@chloe.ravnaandtines.com -setcookie cookie
Eshell V5.6 (abort with ^G)
(servernode@chloe.ravnaandtines.com)1> node().
'servernode@chloe.ravnaandtines.com'
(servernode@chloe.ravnaandtines.com)2> pwd().
jinterface mathserver.erl
ok
(servernode@chloe.ravnaandtines.com)3> c(mathserver).
l{ok,mathserver}
(servernode@chloe.ravnaandtines.com)4> s().
jinterface mathserver.beam mathserver.erl
ok
(servernode@chloe.ravnaandtines.com)5> mathserver:start().
true
(servernode@chloe.ravnaandtines.com)6> mathserver:add(1,2).
3
(servernode@chloe.ravnaandtines.com)7>
Now fire up Visual Studio, and create a J# class library project. Add existing items to copy the jinterface code into the project, and build it.
Cannot find type 'java.lang.ref.WeakReference'
Well, that's not too bad. Give it a stub, and build again:
package java.lang.ref;
import System.*;
public class WeakReference extends System.WeakReference
{
public WeakReference()
{
}
}
gives us
error VJS1227: Cannot find constructor 'java.lang.ref.WeakReference(com.ericsson.otp.erlang.OtpMbox)' error VJS1223: Cannot find method 'get()' in 'java.lang.ref.WeakReference'
So give it what it needs:
package java.lang.ref;
import System.*;
public class WeakReference extends System.WeakReference
{
public WeakReference(Object o)
{
super(o);
}
public Object get()
{
try
{
return this.get_Target();
}
catch (InvalidOperationException ioe)
{
return null;
}
}
}
And it builds. Now put in the driver program:
import com.ericsson.otp.erlang.*;
public class ClientNode
{
public static void main(String[] _args) throws Exception
{
OtpSelf cNode = new OtpSelf("clientnode", "cookie");
OtpPeer sNode = new OtpPeer("servernode"+"@"+"chloe.ravnaandtines.com");
OtpConnection connection = cNode.connect(sNode);
OtpErlangObject[] args = new OtpErlangObject[]{
new OtpErlangLong(1), new OtpErlangLong(2)};
connection.sendRPC("mathserver", "add", args);
OtpErlangLong sum = (OtpErlangLong)connection.receiveRPC();
if (sum.intValue() != 3)
{
throw new IllegalStateException("Assertion failed, returned = " + sum.intValue());
}
System.out.println("OK!");
}
}
Now, run that and get:
OK!
So, we have first light!


1 comment:
There is OTP.NET.
http://weblogs.asp.net/nleghari/archive/2008/01/08/integrating-net-and-erlang-using-otp-net.aspx
Post a Comment