ZMQ::LibZMQ3 - A libzmq 3.x wrapper for Perl
use ZMQ::LibZMQ; my $ctxt = zmq_init($threads); my $rv = zmq_term($ctxt); my $msg = zmq_msg_init(); my $msg = zmq_msg_init_size( $size ); my $msg = zmq_msg_init_data( $data ); my $rv = zmq_msg_close( $msg ); my $rv = zmq_msg_move( $dest, $src ); my $rv = zmq_msg_copy( $dest, $src ); my $data = zmq_msg_data( $msg ); my $size = zmq_msg_size( $msg); my $sock = zmq_socket( $ctxt, $type ); my $rv = zmq_close( $sock ); my $rv = zmq_setsockopt( $socket, $option, $value ); my $val = zmq_getsockopt( $socket, $option ); my $rv = zmq_bind( $sock, $addr ); my $rv = zmq_send( $sock, $msg, $flags ); my $msg = zmq_recv( $sock, $flags );
If you have libzmq registered with pkg-config:
perl Makefile.PL make make test make install
If you don't have pkg-config, and libzmq is installed under /usr/local/libzmq:
ZMQ_HOME=/usr/local/libzmq \ perl Makefile.PL make make test make install
If you want to customize include directories and such:
ZMQ_INCLUDES=/path/to/libzmq/include \ ZMQ_LIBS=/path/to/libzmq/lib \ ZMQ_H=/path/to/libzmq/include/zmq.h \ perl Makefile.PL make make test make install
If you want to compile with debugging on:
perl Makefile.PL -g
The ZMQ::LibZMQ3 module is a wrapper of the 0MQ message passing library for Perl. It's a thin wrapper around the C API. Please read http://zeromq.org for more details on 0MQ.
ZMQ::LibZMQ3
Note that this is a wrapper for libzmq 2.x. For 3.x, you need to check ZMQ::LibZMQ3
To start using ZMQ::LibZMQ3, you need to create a context object, then as many ZMQ::LibZMQ3::Socket obects as you need:
my $ctxt = zmq_init; my $socket = zmq_socket( $ctxt, ... options );
You need to call zmq_bind() or zmq_connect() on the socket, depending on your usage. For example on a typical server-client model you would write on the server side:
zmq_bind()
zmq_connect()
zmq_bind( $socket, "tcp://127.0.0.1:9999" );
and on the client side:
zmq_connect( $socket, "tcp://127.0.0.1:9999" );
The underlying zeromq library offers TCP, multicast, in-process, and ipc connection patterns. Read the zeromq manual for more details on other ways to setup the socket.
When sending data, you can either pass a ZMQ::LibZMQ3::Message object or a Perl string.
# the following two send() calls are equivalent my $msg = zmq_msg_init_data( "a simple message" ); zmq_send( $socket, $msg ); zmq_send( $socket, "a simple message" );
In most cases using ZMQ::LibZMQ3::Message is redundunt, so you will most likely use the string version.
To receive, simply call zmq_recv() on the socket
zmq_recv()
my $msg = zmq_recv( $socket );
The received message is an instance of ZMQ::LibZMQ3::Message object, and you can access the content held in the message via the data() method:
data()
my $data = zmq_msg_data( $msg );
By default 0MQ comes with its own zmq_poll() mechanism that can handle non-blocking sockets. You can use this by calling zmq_poll with a list of hashrefs:
zmq_poll([ { fd => fileno(STDOUT), events => ZMQ_POLLOUT, callback => \&callback, }, { socket => $zmq_socket, events => ZMQ_POLLIN, callback => \&callback }, ], $timeout );
Unfortunately this custom polling scheme doesn't play too well with AnyEvent.
As of zeromq2-2.1.0, you can use getsockopt to retrieve the underlying file descriptor, so use that to integrate ZMQ::LibZMQ3 and AnyEvent:
my $socket = zmq_socket( $ctxt, ZMQ_REP ); my $fh = zmq_getsockopt( $socket, ZMQ_FD ); my $w; $w = AE::io $fh, 0, sub { while ( my $msg = zmq_recv( $socket, ZMQ_RCVMORE ) ) { # do something with $msg; } undef $w; };
0MQ works on both multi-process and multi-threaded use cases, but you need to be careful bout sharing ZMQ::LibZMQ3 objects.
For multi-process environments, you should not be sharing the context object. Create separate contexts for each process, and therefore you shouldn't be sharing the socket objects either.
For multi-thread environemnts, you can share the same context object. However you cannot share sockets.
ZMQ::LibZMQ3 attempts to stick to the libzmq interface as much as possible. Unless there is a structure is what the Perl binding expects, no function should throw an exception.
Return values should resemble that of libzmq, except for when new data is allocated and returned to the user - That includes things like zmq_init(), zmq_socket(), zmq_msg_data(), etc.
zmq_init()
zmq_socket()
zmq_msg_data()
Where applicable, $! should be updated to match the value set by libzmq, so you should be able to do:
my $cxt = zmq_init(); if (! $cxt) { die "zmq_init() failed with $!"; }
Returns the value of errno variable for the calling thread. You normally should not need to use this function. See the man page for zmq_errno() provided by libzmq.
Returns the string representation of $errno. Use this to stringify errors that libzmq provides.
$errno
Creates a new context object. $threads argument is optional. Context objects can be reused across threads.
$threads
Returns undef upon error, and sets $!.
Terminates the context. Be careful, as it might hang if you have pending socket operations.
Returns a non-zero status upon failure, and sets $!.
Creates a new socket object. $socket_types are constants declared in ZMQ::Constants. Sockets cannot be reused across threads.
$socket_types
Binds the socket to listen to specified $address.
$address
Returns a non-zero status upon failure, and sets $!
Connects the socket to specified $address.
Closes the socket explicitly.
Gets the value of the specified option.
If the particular version of ZMQ::LibZMQ3 does not implement the named socket option, an exception will be thrown:
/* barfs, because we don't know what type this new option is */ zmq_getsockopt( $socket, ZMQ_NEW_SHINY_OPTION );
In this case you can either use ZMQ::Constants, or you can use one of the utility functions that ZMQ::LibZMQ3 provides.
ZMQ::LibZMQ3 internally refers to ZMQ::Constants to learn about the type of a socket option. You can easily add new constants to this map:
use ZMQ::Constants; ZMQ::Constants::add_sockopt_type( "int" => ZMQ_NEW_SHINY_OPTION );
/* say you know that the value is an int, int64, uint64, or char * by reading the zmq docs */ $int = zmq_getsockopt_int( $socket, ZMQ_NEW_SHINY_OPTION ); $int64 = zmq_getsockopt_int64( $socket, ZMQ_NEW_SHINY_OPTION ); $uint64 = zmq_getsockopt_uint64( $socket, ZMQ_NEW_SHINY_OPTION ); $string = zmq_getsockopt_string( $socket, ZMQ_NEW_SHINY_OPTION );
Sets the value of the specified option. Returns the status.
See zmq_getsockopt() if you have problems with ZMQ::LibZMQ3 not knowing the type of the option.
zmq_getsockopt()
Queues $size bytes from $buffer to be sent from the socket. Argument $flags may be omitted. If $size is -1, then the size of the buffer calcualted via SvPV() will be used.
$size
$buffer
$flags
SvPV()
Returns the number of bytes sent on success (which should be exact $size)
Returns -1 upon failure, and sets $!.
Queues $message to be sent via $sock. Argument $flags may be omitted.
$message
$sock
If $message is a non-ref, creates a new ZMQ::LibZMQ2::Message object via zmq_msg_init_data(), and uses that to pass to the underlying C layer..
zmq_msg_init_data()
Receives a new message from $sock, and store the message payload in $buffer, up to $len bytes. Argument $flags may be omitted.
$len
Returns the number of bytes in the original message, which may exceed $len (if you have $rv > $len, then the message was truncated).
$rv
Receives a new message from $sock. Argument $flags may be omitted. Returns the message object.
Returns undef upon failure, and sets $!.
Creates a new message object.
Creates a new message object, and sets the message payload to the string in $string.
$string
Creates a new message object, allocating $size bytes. This call isn't so useful from within Perl
Returns the payload contained in $msg
$msg
Returns the size of payload contained in $msg
Copies contents of $src to $dst.
$src
$dst
Moves contents of $src to $dst
Closes, cleans up the message.
@pollitems are list of hash references containing the following elements:
@pollitems
One of either fd or socket key must exist. fd should contain a UNIX file descriptor. socket should contain a ZMQ::LibZMQ2::Socket socket object.
fd
socket
ZMQ::LibZMQ2::Socket
A bit mask containing ZMQ_POLLOUT, ZMQ_POLLIN, ZMQ_POLLERR or combination there of.
ZMQ_POLLOUT
ZMQ_POLLIN
ZMQ_POLLERR
A subroutine reference, which will be called without arguments when the socket or descriptor is available.
Returns the version of the underlying zeromq library that is being linked. In scalar context, returns a dotted version string. In list context, returns a 3-element list of the version numbers:
my $version_string = ZMQ::LibZMQ3::zmq_version(); my ($major, $minor, $patch) = ZMQ::LibZMQ3::zmq_version();
Creates a new "device". See zmq_device for details. zmq_device() will only return if/when the current context is closed. Therefore, the return value is always -1, and errno is always ETERM
zmq_device
This function does not work on some versions, as certain early versions of libzmq3.x do not implement it.
These functions are provided by ZMQ::LibZMQ2 to make some operations easier in the Perl binding. They are not part of the official libzmq interface.
If you see segmentation faults, and such, you need to figure out where the error is occuring in order for the maintainers to figure out what happened. Here's a very very brief explanation of steps involved.
First, make sure to compile ZMQ::LibZMQ3 with debugging on by specifying -g:
perl Makefile.PL -g make
Then fire gdb:
gdb perl (gdb) R -Mblib /path/to/your/script.pl
When you see the crash, get a backtrace:
(gdb) bt
This is an early release. Proceed with caution, please report (or better yet: fix) bugs you encounter.
This module has been tested againt zeromq 3.1.1. Semantics of this module rely heavily on the underlying zeromq version. Make sure you know which version of zeromq you're working with.
http://zeromq.org
http://github.com/lestrrat/p5-ZMQ
Daisuke Maki <daisuke@endeworks.jp>
<daisuke@endeworks.jp>
Steffen Mueller, <smueller@cpan.org>
<smueller@cpan.org>
The ZMQ::LibZMQ3 module is
Copyright (C) 2010 by Daisuke Maki
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.0 or, at your option, any later version of Perl 5 you may have available.
To install ZMQ::LibZMQ3, copy and paste the appropriate command in to your terminal.
cpanm
cpanm ZMQ::LibZMQ3
CPAN shell
perl -MCPAN -e shell install ZMQ::LibZMQ3
For more information on module installation, please visit the detailed CPAN module installation guide.