Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
FFI bindings for ZeroMQ so the library can be used under JRuby and other FFI-compliant ruby runtimes

This branch is 152 commits behind chuckremes:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
examples
ext
lib
spec
.bnsignore
.gitignore
AUTHORS.txt
Gemfile
History.txt
README.rdoc
Rakefile
ffi-rzmq.gemspec

README.rdoc

ffi-rzmq

by Chuck Remes
http://www.zeromq.org/bindings:ruby-ffi

DESCRIPTION:

This gem wraps the ZeroMQ networking library using the ruby FFI (foreign function interface). It's a pure ruby wrapper so this gem can be loaded and run by any ruby runtime that supports FFI. That's all of them: MRI 1.9.x, Rubinius and JRuby.

This single gem supports 0mq 2.x and 3.1.x 0mq APIs. The 0mq project started making backward-incompatible changes to the API with the 3.1.x release. The gem auto-configures itself to expose the API conforming to the loaded C library. 0mq API 3.0 is not supported; the 0mq community voted to abandon it.

The impetus behind this library was to provide support for ZeroMQ in JRuby which has native threads. Unlike MRI, which has a GIL, JRuby and Rubinius allow for threaded access to Ruby code from outside extensions. ZeroMQ is heavily threaded, so until the MRI runtime removes its GIL, JRuby and Rubinius will likely be the best environments to run this library.

Breaking API Changes from 0.8.x to 0.9.0

There has been a major change in API from the 0.8.x series to 0.9.0. Previously the Socket#send and Socket#recv methods could raise exceptions when there was an error. They also returned true or false depending on the success or failure of the operation. The exceptions were also used for managing flow control when doing non-blocking send/recv.

Mixing and matching return codes and exceptions is a terrible idea in practice. Using them for flow control is an even worse sin. So all of the exception code from Socket has been removed.

The Poller and Message class have also had their exceptions removed for returning errors on instance methods.

Unfortunately, it isn't possible to indicate that a call to #new has failed by returning nil. A call to #new that fails needs to raise an exception to indicate failure. (This isn't strictly true; this behavior can be changed but it is not a standard Ruby idiom and is therefore a surprising, and poor, thing to do.) Classes that formerly raised exceptions during allocation now offer a factory method #create that will either return a successfully constructed object or nil. Code should check for nil returns before using the object.

This is a breaking change. I'm sorry but the old API's inconsistency was causing too many problems. It now more closely mimics the 0mq C API. IF YOU PREFER THE ORIGINAL API WHICH WAS CLOSER TO IDIOMATIC RUBY, IT SHOULD BE EASY TO WRAP THESE CLASSES UP TO PROVIDE A MORE IDIOMATIC API. I SUGGEST YOU CREATE A PATCH OR CREATE A SEPARATE GEM TO WRAP THIS ONE. Sorry for shouting but I wanted to make sure this stood out.

All example code has been updated to use the new Ruby API.

Breaking API Change from 0.9.3 to 0.9.5

The 0mq library functions zmq_send() and zmq_recv() were changed to zmq_sendmsg() and zmq_recvmsg() with the 0mq 3.x API change. I have changed the binding to always use #sendmsg and #recvmsg. This breaks existing code. This should be the last breaking change heading into the 1.0 release of these bindings.

PERFORMANCE

Check out the latest performance results:

http://www.zeromq.org/bindings:ruby-ffi

FEATURES/PROBLEMS:

This gem needs more tests. This gem has been battle tested by myself and others for over a year, so I am fairly confident that it is solid. However, it is inevitable that there will be bugs, so please open issues for them here or fork this project, fix them, and send me a pull request.

The 'ffi' gem has dropped support for MRI 1.8.x. Since this project relies on that gem to load and run this code, then this project also no longer supports MRI 1.8.x. I recommend JRuby for the best performance and stability.

All features are implemented.

SYNOPSIS:

0mq API v2 client code:

require 'rubygems'
require 'ffi-rzmq'

if ARGV.length < 3
  puts "usage: local_lat <connect-to> <message-size> <roundtrip-count>"
  exit
end

bind_to = ARGV[0]
message_size = ARGV[1].to_i
roundtrip_count = ARGV[2].to_i

ctx = ZMQ::Context.new
s = ctx.socket ZMQ::REP
rc = s.setsockopt(ZMQ::HWM, 100)
rc = s.bind(bind_to)

msg = ""
roundtrip_count.times do
  rc = s.recv_string msg
  raise "Message size doesn't match, expected [#{message_size}] but received [#{msg.size}]" if message_size != msg.size
  rc = s.send_string msg, 0
end

0mq API v2 server code:

require 'rubygems'
require 'ffi-rzmq'

if ARGV.length < 3
  puts "usage: remote_lat <connect-to> <message-size> <roundtrip-count>"
  exit
end

connect_to = ARGV[0]
message_size = ARGV[1].to_i
roundtrip_count = ARGV[2].to_i

ctx = ZMQ::Context.new
s = ctx.socket ZMQ::REQ
rc = s.connect(connect_to)

msg = "#{ '3' * message_size }"

start_time = Time.now

msg = ""
roundtrip_count.times do
  rc = s.send_string msg, 0
  rc = s.recv_string msg
  raise "Message size doesn't match, expected [#{message_size}] but received [#{msg.size}]" if message_size != msg.size
end

Better Examples

I highly recommend visiting the Learn Ruby 0mq project for a bunch of good code examples.

http://github.com/andrewvc/learn-ruby-zeromq

REQUIREMENTS:

* 0mq 2.1.x, 3.1.x or later; 2.0.x and 3.0.x are no longer supported

The ZeroMQ library must be installed on your system in a well-known location like /usr/local/lib. This is the default for new ZeroMQ installs.

If you have installed ZeroMQ using brew, you need to `brew link zeromq` before installing this gem.

Future releases may include the library as a C extension built at time of installation.

* ffi (>= 1.0.0)

This is a requirement for MRI only. Both Rubinius and JRuby have FFI support built in as a standard component. Do not run this gem under MRI with an old 'ffi' gem. It will crash randomly and you will be sad.

INSTALL:

A full gem has been released to rubygems.org as of release 0.5.0. Make sure the ZeroMQ library is already installed on your system.

% gem install ffi-rzmq # should grab the latest release

To build from git master:

% git clone git://github.com/chuckremes/ffi-rzmq
% cd ffi-rzmq
% gem build ffi-rzmq.gemspec
% gem install ffi-rzmq-*.gem

NOTE for Windows users! In order for this gem to find the libzmq.dll, it must be on the Windows PATH. Google for “modify windows path” for instructions on how to do that if you are unfamiliar with that activity.

LICENSE:

(The MIT License)

Copyright © 2011 Chuck Remes

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.