[zeromq-dev] Code-style question

MinRK benjaminrk at gmail.com
Thu May 24 21:44:28 CEST 2012


On Wed, May 23, 2012 at 4:42 AM, Marco Trapanese
<marcotrapanese at gmail.com>wrote:

> Il 23/05/2012 13:32, Andrew Hume ha scritto:
>
> > as you point out, "commands" are different from "data".
> >
> > in my world, we are all using identical intel hardware/software, so data
> > is sent as C structs.
> >
> > commands are different. the time spent parsing/generating commands is
> > almost unmeasurably small,
> > so i hew to Plan 9 style and simply send blank-delimited words.
> > i use a very efficient field splitter, and then use strcmp, as in
> >
> > nf = split(msg, f, " ");
> > if(nf < 1) return;
> > if(strcmp(f[0], "func1") == 0){
> > func1(strtol(f[1], 10, 0), f[2]);
> > } else if(strcmp(f[0], "func2") == 0){
> > so on ...
> > } else {
> > // rats; complain
> > }
> >
> > its not terribly elegant, but it is straightforward and easy to maintain.
> > and having regular text makes it easier to manually scan and debug.
> > i have used something like JSON before, but it seemed like a lot of
> > unnecessary work.
>
>
> Thanks for you hints!
>

In IPython, we abstract serialization into a single object, where
serialization is any function that turns a Message (in our case, dicts)
into a sequence of binary blobs.  This lets us switch between JSON, pickle,
msgpack, etc. as appropriate with a single line of config.  This lets us
use JSON for easy human-readable debugging, and msgpack (or blosc or
protobuf) for better performance.  The application code exclusively talks
high-level messages, and there are calls like `session.send(socket,
message)` and `message = session.recv(socket)`.

This sort of thing may be more trouble than it's worth to implement in C,
but it's dead easy in higher level languages, and I strongly encourage it,
as it makes the question of serialization entirely isolated from the rest
of your application, as it should be, and it also lets us put any
authentication, message metadata construction, etc. in the same place at a
later time, without touching any application code.

-MinRK


> Marco
>
>
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120524/8a0a0ce3/attachment.htm>


More information about the zeromq-dev mailing list