Archives

Archives / 2008 / January
  • Integrating .NET and Erlang using OTP.NET


    Summary:
    A simple example using OTP.NET to connect a .NET node to an erlang node. I have decided to write this after reading the article on "Integrating Java and Erlang" on ServerSide.com so I highly recommend that you read that article before continuing here as there are many things which are already described there which I'm not going to repeat.

     

    OTP.NET is a port to the Jinterface that the author used in the serverside article.  Here, I'll use the same process to integrate erlang code with a C# application.

     

    [I found OTP.NET while browsing through the Jungerl code repository. Jungerl contains miscellanous utilities for erlang programmers]

     

    The source code for the erlang mathserver application is also listed here. I've changed it to do multiplcation instead of addition and made it simpler by removing the send/receive code.

     

    File: Mathserver.erl


    -module(mathserver).

    -compile(export_all).

     

    multiply(First, Second) ->

    First * Second.

     

    Now, you can try this code in an erlang shell to see if it is working

     

    (1) >> werl.exe -sname servernode -setcookie cookie

    (2) (servernode@apollo)3> c(mathserver).

    {ok,mathserver}

    (3) (servernode@apollo)7> mathserver:multiply(10, 3).

    30

     

    (1) starts an erlang shell in a window using servernode as the short node name. The cookie is also required for security and you can see how we will use it in the client.

    (2) & (3) we then compile our application and call the exposed method to get the result back.

     

    Before starting to write a C# Sharp application, we can quickly start another erlang shell to see if we can connect to our server node from a new node.

    >> werl.exe -sname clientnode -setcookie cookie

    >> (clientnode@apollo)3> rpc:call(servernode@apollo, mathserver, multiply, [10, 2]).

    20

     

    Once we are confident that the erlang application is working, we can close the client node shell and start devenv.exe and a new project .

     

    The first thing we need to do is to include the reference to the OTP.dll file. You can either compile it yourself by downloading the source code from jungerl[sourceforge] or just download the sample application(sample download link). Please note that I've only included the dll in the sample and not the full source code.

     

    Now you can add the following code to the main method.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Otp;
    
    namespace ErsharpClient 
    {
        class Program
        {
            static void Main(string[] a)
            {
                OtpSelf cNode = new OtpSelf("clientnode", "cookie");
                OtpPeer sNode = new OtpPeer("servernode@apollo");
                OtpConnection connection = cNode.connect(sNode);
    
                Otp.Erlang.Object[] args = new Otp.Erlang.Object[] { 
                    new Otp.Erlang.Long(1), new Otp.Erlang.Long(4)
                }; 
                connection.sendRPC("mathserver", "multiply", args);
    
                Otp.Erlang.Long sum = (Otp.Erlang.Long)connection.receiveRPC();
                Console.WriteLine("Return Value:" + sum.ToString());
            }
        }
    }

    This is exactly similar to the code described in the article therefore I'm not going to duplicate that information.

     

    Download Sample Application 

     

    Other Links:

    [ServerSide Article] http://www.theserverside.com/tt/articles/article.tss?l=IntegratingJavaandErlang

    [JInterface] http://www.erlang.org/doc/apps/jinterface/index.html

    [OTP.NET Announcement] http://www.erlang.org/pipermail/erlang-questions/2004-May/012313.html

    [Jungerl] http://jungerl.sourceforge.net/

    [OTP.NET] http://jungerl.cvs.sourceforge.net/jungerl/jungerl/lib/otp.net/

     

    So, Java comes to rescue .NET again and OTP.NET makes inter node communication between erlang and .NET possible :)

    Read more...