ZeroMQ wrapper for D2

simendsjo simendsjo at gmail.com
Sun Oct 23 06:10:02 PDT 2011


On 23.10.2011 14:25, Kean Forrest wrote:
> On 22/10/11 23:24, simendsjo wrote:
>> I saw someone mention ZeroMQ in a subthread the other day. I watched a
>> few videos, and it looks to me like a good fit for D. The philosophies
>> matches pretty well: small, clean api, no bloat (only transport, no
>> protocols), very flexible setup/usage and message passing for
>> communication.
>
> +1
>
> ZMQ seems almost too good, I never knew it existed. If D had this as a
> networking/concurrency method it would be a huge plus AFAIK.

Agreed. IPC doesn't work on windows though. Not sure if this 
could/should be simulated to some degree.

> This wrapper (2.1.10 stable) is closer to the python version:
>
> http://min.us/lOZ88QxPWyB6q
>
> Hello world:
>
> import zmq.zmq;
> import std.stdio;
> void main()
> {
> auto ctx = new Context();
> version(client)
> {
> auto sock = ctx.socket(ZMQ_REQ);
> sock.connect("tcp://localhost:5555");
> sock.send("hello");
> writeln(sock.recv!string);
> }
> else
> {
> auto sock = ctx.socket(ZMQ_REP);
> sock.bind("tcp://*:5555");
> writeln(sock.recv!string);
> sock.send("world");
> }
> }

Pretty similar to what I did. Zeromq's porting guide recommend using 
available language features and naming standards, so I added a default 
context using shared and module ctor/dtor's and named enums according to 
phobos rules. Already found some bugs with my wrapper though :)

version(client)
{
   auto sock = connect("tcp://localhost:5555", SocketType.request);
   sock.send("hello");
   writeln(sock.receive!string());
}
else
{
   auto sock = bind("tcp://*:5555");
   writeln(sock.receive!string());
   sock.send("world");
}

The default context is only created on the first call to connect/bind, 
so you can avoid this and write it as:
auto ctx = new Context();
auto sock = ctx.create(SocketType.request);
...

I'm in the process of porting more of the examples to my wrapper to see 
how it holds up. I'll post updated code once I get a little further.
But I really don't know enough D or zmq to write a good wrapper. It 
would be great if you (or someone else) could write a wrapper.


More information about the Digitalmars-d-learn mailing list