On Sun, May 06, 2007 at 12:23:38AM +0900, Lauri Pesonen wrote:
> Related to what the OP was asking for, I've been thinking of
> implementing a DSL in Ruby for defining message types for a C-based
> distributed framework. We're building a distributed system that
> consists of tasks sending each other messages. The messages have types
> and payloads.
> 
> As things are now, we have to define message types by hand. This
> includes defining a payload type, which might be as simple as a single
> int, a struct consisting of primitive types, or a hierarchical struct
> containing dynamic sized variables. In addition to the payload type we
> have to implement functions for marshalling/unmarshalling the payload
> and to print the contents out as text.
> 
> Writing all this for simple payload types is relatively painless, but
> boring. Writing all this for complex hierarchical payloads with
> dynamic sized variables is painful, boring, and extremely error prone,
> because it usually involves a lot of copy-pasting.
> 
> What I have at the moment is something like this (this is from memory,
> because I don't have the code at hand):
> 
> messages "c-file-basename" do |msgs|
>  msgs.define_message "message_type_name" do |m|
>    m.add_member :uin32_t, "uint32_t_variable_name"
>    m.add_pointer :uint8_t, "uint8_t_pointer_name"
>  end
> 
> # define other message types that will be included in the same C-file.
> # ...
> end
> 
> This would result in something like the C-code at the end of the email.
> 
> The difficulties come with more complicated messages. What if I have a
> struct used elsewhere in the system that should be a part of many
> different message types?

The thought which struck me when reading this was: "ASN.1"

OK, it's horrible, but it does pretty much exactly what you ask, and has its
own (standard) DSL for describing the message formats. So if you could find
a good C library which reads ASN.1 and outputs code to parse messages in
BER/DER format, maybe that would be an alternative solution.

The standards documentation is comprehensive, if not easy to read:
http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
http://www.itu.int/ITU-T/studygroups/com10/languages/X.690_1297.pdf

And of course there are probably books and other resources.

There might be Ruby libraries for handling ASN.1 directly. The only one I
know of is the one built into openssl, which is low-level but functional. I
used it in ruby-ldapserver, which you can find on rubyforge.org.

Regards,

Brian.