Chapter 2. Erlang integration

2.1. Introduction

There is an open source project located on github called Erlang.NET. It provides to .NET what JInterface provides to Java users, namely a means to communicate with an Erlang process. The API is very low level and rather tedious to use. The Spring Elang project makes accessing functions in Erlang from .NET easy, often they can be one liners.

2.2. Communicating with Erlang processes


2.2.1. Executing RPC

The interface IErlangOperations is the high level API for interacting with an Erlang process.

    public interface IErlangOperations
        T Execute<T>(ConnectionCallbackDelegate<T> action);

        OtpErlangObject ExecuteErlangRpc(string module, string function, OtpErlangList args);

        OtpErlangObject ExecuteErlangRpc(string module, string function, params OtpErlangObject[] args);

        OtpErlangObject ExecuteRpc(string module, string function, params object[] args);

        object ExecuteAndConvertRpc(string module, string function, IErlangConverter converterToUse,
                                    params object[] args);

        // Sweet!
        object ExecuteAndConvertRpc(string module, string function, params object[] args);

The class that implements this interface is called ErlangTemplate. There are a few convenience methods, most notably ExecuteAndConvertRpc, as well as the Execute method which gives you access to the 'native' API of the Erlang.NET project. For simple functions, you can invoke ExecuteAndConvertRpc with the appropriate Erlang module name, function, and arguments in a one-liner. For example, here is the implementation of the RabbitBrokerAdmin method 'DeleteUser'

        public void DeleteUser(string username)
            erlangTemplate.ExecuteAndConvertRpc("rabbit_access_control", "delete_user", encoding.GetBytes(username));

The 'encoding' field is simply and instance of ASCIIEncoding.

As the Erlang.NET library uses specific classes such as OtpErlangDouble, OtpErlangString to represent the primitive types in Erlang RPC calls, there is a converter class that works in concert with ErlangTemplate that knows how to translate from .NET primitive types to their Erlang class equivalents. You can also create custom converters and register them with the ErlangTemplate to handle more complex data format translations.

2.2.2. ErlangConverter

The IErlangConverter interface is shown below

    public interface IErlangConverter
        /// <summary>
        /// Convert a .NET object to a Erlang data type.
        /// </summary>
        OtpErlangObject ToErlang(object objectToConvert);

        /// <summary>
        /// Convert from an Erlang data type to a .NET data type.
        /// </summary>
        object FromErlang(OtpErlangObject erlangObject);

        /// <summary>
        /// The return value from executing the Erlang RPC.
        /// </summary>
        object FromErlangRpc(string module, string function, OtpErlangObject erlangObject);

The provided implementation is SimpleErlangConverter which is used by default with ErlangTemplate and handles all basic types.