views:

325

answers:

3

In another SO discussion, we were talking about interfacing an erlang application to another non-erlang app that is using XDR encoded packets for network communications.

Unfortunately, I couldn't really find any real pointers on dealing with XDR data using erlang.

So what is the recommended way of dealing with XDR encoded data in erlang?

Thanks

PS: So far, I could find the following resources:

A: 

It has been 4 days since this question was asked and still there are no answers. I did some searching and determined that XDR is meant to be a encode/decode strategy for cross platform communication. The original docs were written in 1995 and is replaced with a version in 2006.

With all like protocols there is a cost to encode/decode. The cost will never be zero because there is a wrapper around the structure.

Nonetheless, in modern applications I tend to use XML or JSON. This makes debugging easier and that I do not have to put a decode step in my logger code.

I know that this is ANSWER not directed at XDR, however, JSON and XML are cross platform, have a reasonable performance model, plenty of tools and libraries... and are basically a reasonable alternative to XDR.

[update] - based on your comment... you'll have to build your own. Sorry.

Richard
Thanks for responding, but the question really was about interfacing with applications that already use XDR internally, and thus there's no way to have the erlang side of things change that. Ideally, there'd be a simple erlang library/wrapper to handle XDR encoded data, so that it can be easily converted.
none
A: 

Building your own XDR encode and decode library isn't difficult.

Encoding Erlang terms to XDR is trivial:

%% @spec push_bool(bool()) -> binary()

push_bool(Value) ->
    case Value of
        true ->
           <<0, 0, 0, 1>>;
        false ->
            <<0, 0, 0, 0>>
    end.

Going the other way is a bit more verbose, depending on how you would like error reporting to be done. I've chosen exceptions:

%% @spec pull_bool(binary()) -> {bool(), binary()}

pull_bool(Bin) ->
    {Value, Tail} = 
        try
            <<0, 0, 0, V, T/binary>> = Bin,
            {V, T}
        catch 
            error:{badmatch, _} -> 
                throw({xdr_error, "Invalid boolean value"})
        end,
    Result = case Value of
                 0 -> false;
                 1 -> true;
                 _ -> throw({xdr_error, "Invalid boolean value"})
             end, 
    {Result, Tail}.

There really aren't that many XDR data types in total so it would maybe be a couple of hundred lines of code in total.

Tim Potter
Thanks for your response, as mentioned in the original posting, I am aware of the complexity, and there is a full implementation of an XDR library available from 2000: http://erlang.org/pipermail/erlang-questions/2000-August/001543.html I was just asking about the recommended/standard way of doing this reliably today.
none
+1  A: 

There is a project called 'rpc' in the jungerl erlang source collection. It has a code-generator that produces erlang code to manage xdr-based protocols.

Christian