[zeromq-dev] Hello World in Felix

john skaller skaller at users.sourceforge.net
Mon Jan 16 14:26:05 CET 2012


Here is the Felix binding for ZeroMQ:

http://felix-lang.org/lib/std/io/zmq.flx

This is work-in-progress of course! Note that the language facilities
are used to make a fairly safe code, compared to the C interface.
The type system is leveraged so that all the options, etc, have their
own types and can only be used where they're appropriate.

The nastiest bit -- handling the socket options, requires quite a bit
of code to map the ugly C representation into a variant type.
A good project: I found a bug in the compiler doing it, and I'm going
to add some grammar so all the "const" of one type can be declared
at once.

Below is the Felix code for hello world, from the Guide.
It looks a lot like the C version I hope.

The "maybe_exit" procedure you see checks the zmq function
return codes and exits on error. Something like this cannot be
avoided in Felix: the return value of a function cannot be silently ignored.

To run, using statically linked executables (on unix like box with bash):

flx --static -c hwserver
flx --static -c hwclient
hwserver & hwclient &

///////////// hwserver.flx //////////////////////////////////
// Hello World server
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"

include "std/posix/errno";

open ZeroMQ;
println "hwserver, Felix version";

var context = zmq_init (1);

// Socket to talk to clients
var responder = zmq_socket (context, ZMQ_REP);
maybe_exit$ zmq_bind (responder, "tcp://*:5555");

while true do 
    // Wait for next request from client
    var request = #zmq_msg_t;
    maybe_exit$ zmq_recvmsg (responder, request, ZMQ_XMIT_OPTIONS_NONE);
    print ("Received Hello\n");

    // Do some 'work'
    Faio::sleep (sys_clock,1.0);

    // Send reply back to client
    var reply = zmq_msg_t 5.size;
    memcpy (zmq_msg_data (reply), c"World".address, 5.size);
    maybe_exit$ zmq_sendmsg (responder, reply, ZMQ_XMIT_OPTIONS_NONE);
done


/////////// hwclient.flx //////////////////////////////
// Hello World client
// Connects REQ socket to tcp://localhost:5555
// Sends "Hello" to server, expects "World" back

include "std/posix/errno";

open ZeroMQ;
println "hwclient, Felix version";

var context = zmq_init (1);

// Socket to talk to server
print ("Connecting to hello world server…\n");
var requester = zmq_socket (context, ZMQ_REQ);
maybe_exit$ zmq_connect (requester, "tcp://localhost:5555");

for var request_nbr in 0 upto 9 do
    var request = zmq_msg_t 5.size;
    memcpy (zmq_msg_data request, c"Hello".address, 5.size);
    print$ f"Sending Hello %d…\n" request_nbr;
    maybe_exit$ zmq_sendmsg (requester, request, ZMQ_XMIT_OPTIONS_NONE);

   var reply = #zmq_msg_t;
   maybe_exit$ zmq_recvmsg (requester, reply, ZMQ_XMIT_OPTIONS_NONE);
   print$ f"Received World %d\n" request_nbr;
done
maybe_exit$ zmq_term context;
////////////////////////////////////////

--
john skaller
skaller at users.sourceforge.net







More information about the zeromq-dev mailing list