[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Demexp-dev] [Gerd Stolpmann] Re: Looking for an OCaml library to encode

From: David MENTRE
Subject: [Demexp-dev] [Gerd Stolpmann] Re: Looking for an OCaml library to encode/decode XDR
Date: Wed, 10 Sep 2003 18:26:04 +0200
User-agent: Gnus/5.1002 (Gnus v5.10.2) Emacs/21.2 (gnu/linux)

Answer from Gerd Stolpmann.


--- Begin Message --- Subject: Re: Looking for an OCaml library to encode/decode XDR Date: 09 Sep 2003 23:06:55 +0200
Am Die, 2003-09-09 um 20.53 schrieb David MENTRE:
> Hello Mr Stolpmann,
> For an OCaml project (under GNU GPL license), I'm looking for a library
> that is able to encode and decode XDR encoded messages.
> Making a search with google, I found your RPC[1] package. Do you think
> it would be easy to extract from your code such a library? Have you any
> advice to do that?

XDR encoding/decoding is an essential part of the rpc package, and of
course, it is also possible to use it without the rest. There are a
number of possibilities:

There is an RPC stub generator, called ocamlrpcgen. The input is a
classic XDR definition file, the output are up to three files: a module
for servers, a module for clients, and a module for XDR
encoding/decoding (the "aux" module). It is possible to only generate
the "aux" module. For every XDR type there is a corresponding O'Caml
type, and a conversion function to and from xdr_type.

To make an example:

struct x {
  int x1;
  int *x2;
  int x3[];

is represented as O'Caml type:

type x = { mutable x1 : int (* or int32 *);
           mutable x2 : int option;
           mutable x3 : int array;

Furthermore, two conversion functions are generated:

val _to_x : Xdr.xdr_value -> x
val _of_x : x -> Xdr.xdr_value

An xdr_value is a dynamic representation of the XDR value. It can be
printed into a string, or parsed from a string. To do so, you need the
third value:

val xdrt_x : Xdr.xdr_type_term

This is a dynamic representation of the XDR type. To print an xdr_value
v into a string, use:

Xdr.pack_xdr_value_as_string v (Xdr.validate_xdr_type xdrt_x) []

To parse a string s as xdr_value, use:

unpack_xdr_value ~fast:true s (Xdr.validate_xdr_type xdrt_x) []

(Better factor the validate_xdr_type call out, it is quite expensive
compared to the rest.)

Another possibility would be not to use the stub generator. Because XDR
types and values are dynamically represented (unlike in C), it is also
possible to create xdr_values directly, and to match them against
patterns. See the Xdr module for details; in the RPC source code, the
module Rpc_portmapper is an example for this style of XDR handling.

There are almost no limits for XDR: The encoded messages may have
arbitrary length (until maximum string length), numbers may be
represented as int or int32 or int64, recursive definitions are
supported (even forward definitions). One drawback is that
single-precision floats are en/decoded only slowly.

> Last but not least, looking at the LICENSE it seems that your code is
> under a BSD-like license, with no advertising clause. So your code would
> be compatible with GNU GPL license. Can you confirm this?

To be exact, it is the MIT/X11 license. It is compatible with the GPL.

Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 

--- End Message ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]