File: README.rdoc

package info (click to toggle)
ruby-ffi-rzmq 2.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 404 kB
  • sloc: ruby: 2,967; sh: 21; makefile: 2
file content (223 lines) | stat: -rw-r--r-- 7,672 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
ffi-rzmq
    by Chuck Remes
    http://www.zeromq.org/bindings:ruby-ffi

== FUTURE SUPPORT

Most people writing Zeromq applications in Ruby should be using the rbczmq
project. It wraps the CZMQ binding which is a much higher-level library
for writing Zeromq code. Find the Ruby gem here:

    http://github.com/methodmissing/rbczmq
    
Few projects need to write the low-level zeromq code that this gem allows.
With the release of ffi-rzmq 2.0.3, this library is going into permanent 
maintenance mode. As new versions of libzmq are released, interested parties
should send pull requests to this project or its related project
ffi-rzmq-core to support new features.

The original README text follows...

== 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.

The Ruby API provided by this gem is *NOT* very Ruby-like. It very closely
tracks the libzmq C API. However, the contributors to this project have
done all of the hard work to wrap up all of libzmq and make it accessible
from Ruby. If you want it to be more Ruby-like (e.g. raise Exceptions instead
of returning integer codes) then *wrap this library* with your own and release
it as a gem. We will all be grateful!

This single gem supports 0mq 3.2.x and 4.x 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 2.x is no longer supported. 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.

Please read the History.txt file for a description of all changes, including
API changes, since the last release!

== PERFORMANCE:

Check out the latest performance results:

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

The short version is that the FFI bindings are a few microseconds slower
than using a C extension.

== FEATURES/PROBLEMS:

This gem needs more tests. This gem has been battle tested by myself
and others for years, 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.

The `Socket` and `Context` classes have finalizers which will
be called by the garbage collector when there are no more references.
However, these finalizers should not be relied upon to orderly close
your sockets and then the context. Finalizers run in a non-determinant
order, so the `Context` finalizer may run first which will hang the
program. To avoid this, make sure to close all sockets before the
program exits.

All features are implemented.

== BUILD STATUS:

{<img src="https://secure.travis-ci.org/chuckremes/ffi-rzmq.png?branch=master" alt="Build Status" />}[http://travis-ci.org/chuckremes/ffi-rzmq]

{<img src="https://codeclimate.com/badge.png" />}[https://codeclimate.com/github/chuckremes/ffi-rzmq]

== SYNOPSIS:

0mq API v3.2-4 client code:

  require 'rubygems'
  require 'ffi-rzmq'

  if ARGV.length < 3
    puts "usage: ruby local_lat.rb <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::SNDHWM, 100)
  rc  = s.setsockopt(ZMQ::RCVHWM, 100)
  rc  = s.bind(bind_to)

  roundtrip_count.times do
    msg = ""
    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 v3.2-4 server code:

  require 'rubygems'
  require 'ffi-rzmq'

  if ARGV.length < 3
    puts "usage: ruby remote_lat.rb <connect-to> <message-size> <roundtrip-count>"
    exit
  end
  
  def assert(rc)
    raise "Last API call failed at #{caller(1)}" unless rc >= 0
  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 }"

  time_start = Time.now

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

  time_end = Time.now
  puts "Time #{( time_end - time_start )}"

== 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 3.2.x, 4.x or later; 2.x, 3.0.x and 3.1.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.

  * ffi (>= 1.0.0)
	* ffi-rzmq-core

This is a requirement for MRI and Rubinius. JRuby has 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:

Make sure the ZeroMQ library is already installed on your system. We recommend 'brew' or 'macports' to get it.

 % 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. That DLL also requires that you copy libstdc++-6.dll and libgcc_s_sjlj-1.dll from DevKit MinGW into the same folder that you copied libzmq.dll.

== LICENSE:

(The MIT License)

Copyright (c) 2013-2017 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.