ezmq 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +96 -2
- data/UPGRADING.md +12 -0
- data/doc/EZMQ.html +1 -1
- data/doc/EZMQ/Client.html +61 -32
- data/doc/EZMQ/Publisher.html +44 -18
- data/doc/EZMQ/Server.html +77 -127
- data/doc/EZMQ/Socket.html +301 -59
- data/doc/EZMQ/Subscriber.html +72 -62
- data/doc/_index.html +1 -1
- data/doc/file.README.html +89 -3
- data/doc/index.html +89 -3
- data/doc/method_list.html +16 -22
- data/doc/top-level-namespace.html +1 -1
- data/ezmq.gemspec +1 -1
- data/lib/ezmq.rb +120 -83
- metadata +3 -2
data/doc/method_list.html
CHANGED
@@ -88,14 +88,14 @@
|
|
88
88
|
|
89
89
|
|
90
90
|
<li class="r1 ">
|
91
|
-
<span class='object_link'><a href="EZMQ/
|
92
|
-
<small>EZMQ::
|
91
|
+
<span class='object_link'><a href="EZMQ/Publisher.html#initialize-instance_method" title="EZMQ::Publisher#initialize (method)">#initialize</a></span>
|
92
|
+
<small>EZMQ::Publisher</small>
|
93
93
|
</li>
|
94
94
|
|
95
95
|
|
96
96
|
<li class="r2 ">
|
97
|
-
<span class='object_link'><a href="EZMQ/
|
98
|
-
<small>EZMQ::
|
97
|
+
<span class='object_link'><a href="EZMQ/Socket.html#initialize-instance_method" title="EZMQ::Socket#initialize (method)">#initialize</a></span>
|
98
|
+
<small>EZMQ::Socket</small>
|
99
99
|
</li>
|
100
100
|
|
101
101
|
|
@@ -106,14 +106,14 @@
|
|
106
106
|
|
107
107
|
|
108
108
|
<li class="r2 ">
|
109
|
-
<span class='object_link'><a href="EZMQ/
|
110
|
-
<small>EZMQ::
|
109
|
+
<span class='object_link'><a href="EZMQ/Server.html#initialize-instance_method" title="EZMQ::Server#initialize (method)">#initialize</a></span>
|
110
|
+
<small>EZMQ::Server</small>
|
111
111
|
</li>
|
112
112
|
|
113
113
|
|
114
114
|
<li class="r1 ">
|
115
|
-
<span class='object_link'><a href="EZMQ/
|
116
|
-
<small>EZMQ::
|
115
|
+
<span class='object_link'><a href="EZMQ/Client.html#initialize-instance_method" title="EZMQ::Client#initialize (method)">#initialize</a></span>
|
116
|
+
<small>EZMQ::Client</small>
|
117
117
|
</li>
|
118
118
|
|
119
119
|
|
@@ -130,20 +130,20 @@
|
|
130
130
|
|
131
131
|
|
132
132
|
<li class="r2 ">
|
133
|
-
<span class='object_link'><a href="EZMQ/
|
134
|
-
<small>EZMQ::
|
133
|
+
<span class='object_link'><a href="EZMQ/Socket.html#receive-instance_method" title="EZMQ::Socket#receive (method)">#receive</a></span>
|
134
|
+
<small>EZMQ::Socket</small>
|
135
135
|
</li>
|
136
136
|
|
137
137
|
|
138
138
|
<li class="r1 ">
|
139
|
-
<span class='object_link'><a href="EZMQ/
|
140
|
-
<small>EZMQ::
|
139
|
+
<span class='object_link'><a href="EZMQ/Client.html#request-instance_method" title="EZMQ::Client#request (method)">#request</a></span>
|
140
|
+
<small>EZMQ::Client</small>
|
141
141
|
</li>
|
142
142
|
|
143
143
|
|
144
144
|
<li class="r2 ">
|
145
|
-
<span class='object_link'><a href="EZMQ/
|
146
|
-
<small>EZMQ::
|
145
|
+
<span class='object_link'><a href="EZMQ/Socket.html#send-instance_method" title="EZMQ::Socket#send (method)">#send</a></span>
|
146
|
+
<small>EZMQ::Socket</small>
|
147
147
|
</li>
|
148
148
|
|
149
149
|
|
@@ -154,24 +154,18 @@
|
|
154
154
|
|
155
155
|
|
156
156
|
<li class="r2 ">
|
157
|
-
<span class='object_link'><a href="EZMQ/Socket.html#send-instance_method" title="EZMQ::Socket#send (method)">#send</a></span>
|
158
|
-
<small>EZMQ::Socket</small>
|
159
|
-
</li>
|
160
|
-
|
161
|
-
|
162
|
-
<li class="r1 ">
|
163
157
|
<span class='object_link'><a href="EZMQ/Socket.html#socket-instance_method" title="EZMQ::Socket#socket (method)">#socket</a></span>
|
164
158
|
<small>EZMQ::Socket</small>
|
165
159
|
</li>
|
166
160
|
|
167
161
|
|
168
|
-
<li class="
|
162
|
+
<li class="r1 ">
|
169
163
|
<span class='object_link'><a href="EZMQ/Subscriber.html#subscribe-instance_method" title="EZMQ::Subscriber#subscribe (method)">#subscribe</a></span>
|
170
164
|
<small>EZMQ::Subscriber</small>
|
171
165
|
</li>
|
172
166
|
|
173
167
|
|
174
|
-
<li class="
|
168
|
+
<li class="r2 ">
|
175
169
|
<span class='object_link'><a href="EZMQ/Subscriber.html#unsubscribe-instance_method" title="EZMQ::Subscriber#unsubscribe (method)">#unsubscribe</a></span>
|
176
170
|
<small>EZMQ::Subscriber</small>
|
177
171
|
</li>
|
@@ -103,7 +103,7 @@
|
|
103
103
|
</div>
|
104
104
|
|
105
105
|
<div id="footer">
|
106
|
-
Generated on
|
106
|
+
Generated on Fri Jan 9 13:06:26 2015 by
|
107
107
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
108
108
|
0.8.7.6 (ruby-2.0.0).
|
109
109
|
</div>
|
data/ezmq.gemspec
CHANGED
data/lib/ezmq.rb
CHANGED
@@ -14,10 +14,16 @@ module EZMQ
|
|
14
14
|
#
|
15
15
|
# @option options [ZMQ::Context] context a context to use for this socket
|
16
16
|
# (one will be created if not provided).
|
17
|
-
# @option options [lambda] encode how to encode messages.
|
18
|
-
# @option options [lambda] decode how to decode messages.
|
19
|
-
# @option options [String]
|
20
|
-
#
|
17
|
+
# @option options [lambda] encode how to encode messages.
|
18
|
+
# @option options [lambda] decode how to decode messages.
|
19
|
+
# @option options [String] protocol protocol to use for transport.
|
20
|
+
# Default: 'tcp'
|
21
|
+
# @option options [String] address address to use for endpoint.
|
22
|
+
# Default: '127.0.0.1'
|
23
|
+
# @note 'localhost' does not always work as expected. Prefer '127.0.0.1'
|
24
|
+
# @option options [Fixnum] port port to use for endpoint.
|
25
|
+
# Default: 5555
|
26
|
+
# @note `port` is ignored unless protocol is either 'tcp' or 'udp'.
|
21
27
|
#
|
22
28
|
# @return [Socket] a new instance of Socket.
|
23
29
|
#
|
@@ -27,116 +33,144 @@ module EZMQ
|
|
27
33
|
@socket = @context.socket type
|
28
34
|
@encode = options[:encode] || -> m { m }
|
29
35
|
@decode = options[:decode] || -> m { m }
|
30
|
-
|
36
|
+
endpoint = options.select { |k, _| [:protocol, :address, :port].include? k }
|
37
|
+
method(mode).call endpoint
|
31
38
|
end
|
32
39
|
|
33
|
-
#
|
40
|
+
# Sends a message on the socket.
|
34
41
|
#
|
35
|
-
# @note
|
42
|
+
# @note If `message` is not a String, `encode` must convert it to one.
|
36
43
|
#
|
37
|
-
# @param [
|
44
|
+
# @param [String] message the message to send.
|
45
|
+
# @param [Hash] options optional parameters.
|
46
|
+
# @option options [lambda] encode how to encode the message.
|
38
47
|
#
|
39
|
-
# @return [
|
48
|
+
# @return [Fixnum] the size of the message.
|
40
49
|
#
|
41
|
-
def
|
42
|
-
|
43
|
-
@socket.
|
44
|
-
decode.call message
|
50
|
+
def send(message = '', **options)
|
51
|
+
encoded = (options[:encode] || @encode).call message
|
52
|
+
@socket.send_string encoded
|
45
53
|
end
|
46
54
|
|
47
|
-
#
|
55
|
+
# Receive a message from the socket.
|
48
56
|
#
|
49
|
-
# @note
|
57
|
+
# @note This method blocks until a message arrives.
|
50
58
|
#
|
51
|
-
# @param [
|
52
|
-
# @
|
59
|
+
# @param [Hash] options optional parameters.
|
60
|
+
# @option options [lambda] decode how to decode the message.
|
53
61
|
#
|
54
|
-
# @
|
62
|
+
# @yield message passes the message received to the block.
|
63
|
+
# @yieldparam [Object] message the message received (decoded).
|
55
64
|
#
|
56
|
-
|
57
|
-
|
65
|
+
# @return [Object] the message received (decoded).
|
66
|
+
#
|
67
|
+
def receive(**options)
|
68
|
+
message = ''
|
69
|
+
@socket.recv_string message
|
70
|
+
decoded = (options[:decode] || @decode).call message
|
71
|
+
if block_given?
|
72
|
+
yield decoded
|
73
|
+
else
|
74
|
+
decoded
|
75
|
+
end
|
58
76
|
end
|
59
77
|
|
60
78
|
# Binds the socket to the given address.
|
61
79
|
#
|
80
|
+
# @param [String] protocol protocol to use for transport. Default: 'tcp'
|
81
|
+
# @param [String] address address to use for endpoint. Default: '127.0.0.1'
|
62
82
|
# @note 'localhost' does not always work as expected. Prefer '127.0.0.1'
|
63
|
-
#
|
64
|
-
# @
|
65
|
-
# Default is 'tcp://127.0.0.1:5555'
|
83
|
+
# @param [Fixnum] port port to use for endpoint. Default: 5555
|
84
|
+
# @note `port` is ignored unless protocol is either 'tcp' or 'udp'.
|
66
85
|
#
|
67
86
|
# @return [Boolean] was binding successful?
|
68
87
|
#
|
69
|
-
def bind(address: '
|
70
|
-
|
88
|
+
def bind(protocol: 'tcp', address: '127.0.0.1', port: 5555)
|
89
|
+
endpoint = "#{ protocol }://#{ address }"
|
90
|
+
endpoint = "#{ endpoint }:#{ port }" if %w(tcp udp).include? protocol
|
91
|
+
@socket.bind(endpoint) == 0
|
71
92
|
end
|
72
93
|
|
73
94
|
# Connects the socket to the given address.
|
74
95
|
#
|
75
|
-
# @param [String]
|
76
|
-
#
|
96
|
+
# @param [String] protocol protocol to use for transport. Default: 'tcp'
|
97
|
+
# @param [String] address address to use for endpoint. Default: '127.0.0.1'
|
98
|
+
# @param [Fixnum] port port to use for endpoint. Default: 5555
|
99
|
+
# @note `port` is ignored unless protocol is either 'tcp' or 'udp'.
|
77
100
|
#
|
78
101
|
# @return [Boolean] was connection successful?
|
79
102
|
#
|
80
|
-
def connect(address: '
|
81
|
-
|
103
|
+
def connect(protocol: 'tcp', address: '127.0.0.1', port: 5555)
|
104
|
+
endpoint = "#{ protocol }://#{ address }"
|
105
|
+
endpoint = "#{ endpoint }:#{ port }" if %w(tcp udp).include? protocol
|
106
|
+
@socket.connect(endpoint) == 0
|
82
107
|
end
|
83
108
|
end
|
84
109
|
|
85
|
-
#
|
86
|
-
class
|
87
|
-
|
88
|
-
|
89
|
-
# Creates a new Server socket.
|
90
|
-
#
|
91
|
-
# @param [lambda] provides the service provided by this server.
|
92
|
-
# @param [Hash] options optional parameters
|
110
|
+
# Request socket that sends messages and receives replies.
|
111
|
+
class Client < EZMQ::Socket
|
112
|
+
# Creates a new Client socket.
|
93
113
|
#
|
94
|
-
# @
|
114
|
+
# @param [Hash] options optional parameters.
|
115
|
+
# @see EZMQ::Socket EZMQ::Socket for optional parameters.
|
95
116
|
#
|
96
|
-
# @return [
|
117
|
+
# @return [Client] a new instance of Client.
|
97
118
|
#
|
98
|
-
def initialize(
|
99
|
-
|
100
|
-
super :bind, ZMQ::REP, options
|
119
|
+
def initialize(**options)
|
120
|
+
super :connect, ZMQ::REQ, options
|
101
121
|
end
|
102
122
|
|
103
|
-
#
|
104
|
-
# with the result, then loops.
|
123
|
+
# Sends a message and waits to receive a response.
|
105
124
|
#
|
106
|
-
# @param [
|
125
|
+
# @param [String] message the message to send.
|
126
|
+
# @param [Hash] options optional parameters.
|
127
|
+
# @option options [lambda] encode how to encode the message.
|
128
|
+
# @option options [lambda] decode how to decode the message.
|
107
129
|
#
|
108
|
-
# @return [void] the
|
130
|
+
# @return [void] the decoded response message.
|
109
131
|
#
|
110
|
-
def
|
111
|
-
|
132
|
+
def request(message = '', **options)
|
133
|
+
send message, options
|
134
|
+
if block_given?
|
135
|
+
yield receive options
|
136
|
+
else
|
137
|
+
receive options
|
138
|
+
end
|
112
139
|
end
|
113
140
|
end
|
114
141
|
|
115
|
-
#
|
116
|
-
class
|
117
|
-
# Creates a new
|
142
|
+
# Reply socket that listens for and replies to requests.
|
143
|
+
class Server < EZMQ::Socket
|
144
|
+
# Creates a new Server socket.
|
118
145
|
#
|
119
146
|
# @param [Hash] options optional parameters
|
120
147
|
#
|
121
|
-
# @see EZMQ::Socket EZMQ::Socket for
|
148
|
+
# @see EZMQ::Socket EZMQ::Socket for optional parameters.
|
122
149
|
#
|
123
|
-
# @return [
|
150
|
+
# @return [Server] a new instance of Server
|
124
151
|
#
|
125
152
|
def initialize(**options)
|
126
|
-
super :
|
153
|
+
super :bind, ZMQ::REP, options
|
127
154
|
end
|
128
155
|
|
129
|
-
#
|
156
|
+
# Listens for a request, and responds to it.
|
130
157
|
#
|
131
|
-
#
|
132
|
-
# @param [lambda] encode how to encode the message.
|
133
|
-
# @param [lambda] decode how to decode the message.
|
158
|
+
# If no block is given, responds with the request message.
|
134
159
|
#
|
135
|
-
# @
|
160
|
+
# @yield message passes the message received to the block.
|
161
|
+
# @yieldparam [String] message the message received.
|
162
|
+
# @yieldreturn [void] the message to reply with.
|
163
|
+
#
|
164
|
+
# @return [void] the return from handler.
|
136
165
|
#
|
137
|
-
def
|
138
|
-
|
139
|
-
|
166
|
+
def listen
|
167
|
+
loop do
|
168
|
+
if block_given?
|
169
|
+
send yield receive
|
170
|
+
else
|
171
|
+
send receive
|
172
|
+
end
|
173
|
+
end
|
140
174
|
end
|
141
175
|
end
|
142
176
|
|
@@ -144,9 +178,8 @@ module EZMQ
|
|
144
178
|
class Publisher < EZMQ::Socket
|
145
179
|
# Creates a new Publisher socket.
|
146
180
|
#
|
147
|
-
# @param [Hash] options optional parameters
|
148
|
-
#
|
149
|
-
# @see EZMQ::Socket EZMQ::Socket for a list of optional parameters.
|
181
|
+
# @param [Hash] options optional parameters.
|
182
|
+
# @see EZMQ::Socket EZMQ::Socket for optional parameters.
|
150
183
|
#
|
151
184
|
# @return [Publisher] a new instance of Publisher.
|
152
185
|
#
|
@@ -158,12 +191,13 @@ module EZMQ
|
|
158
191
|
#
|
159
192
|
# @param [String] message the message to send.
|
160
193
|
# @param [String] topic an optional topic for the message.
|
161
|
-
# @param [
|
194
|
+
# @param [Hash] options optional parameters.
|
195
|
+
# @option options [lambda] encode how to encode the message.
|
162
196
|
#
|
163
197
|
# @return [Fixnum] the size of the message.
|
164
198
|
#
|
165
|
-
def send(message = '', topic: '',
|
166
|
-
@socket.send_string "#{ topic } #{ encode.call message }"
|
199
|
+
def send(message = '', topic: '', **options)
|
200
|
+
@socket.send_string "#{ topic } #{ (options[:encode] || @encode).call message }"
|
167
201
|
end
|
168
202
|
end
|
169
203
|
|
@@ -172,20 +206,16 @@ module EZMQ
|
|
172
206
|
attr_accessor :action
|
173
207
|
|
174
208
|
# Creates a new Subscriber socket.
|
175
|
-
#
|
176
|
-
# @note The default behaviour is to output and messages received to STDOUT.
|
177
209
|
#
|
178
|
-
# @
|
179
|
-
# @param [Hash] options optional parameters
|
210
|
+
# @note The default behaviour is to output and messages received to STDOUT.
|
180
211
|
#
|
212
|
+
# @param [Hash] options optional parameters.
|
181
213
|
# @option options [String] topic a topic to subscribe to.
|
182
|
-
#
|
183
|
-
# @see EZMQ::Socket EZMQ::Socket for a list of optional parameters.
|
214
|
+
# @see EZMQ::Socket EZMQ::Socket for optional parameters.
|
184
215
|
#
|
185
216
|
# @return [Publisher] a new instance of Publisher.
|
186
217
|
#
|
187
|
-
def initialize(
|
188
|
-
@action = action
|
218
|
+
def initialize(**options)
|
189
219
|
super :connect, ZMQ::SUB, options
|
190
220
|
subscribe options[:topic] if options[:topic]
|
191
221
|
end
|
@@ -202,7 +232,7 @@ module EZMQ
|
|
202
232
|
# @return [Boolean] was subscription successful?
|
203
233
|
#
|
204
234
|
def subscribe(topic)
|
205
|
-
@socket.setsockopt(ZMQ::SUBSCRIBE, topic) == 0
|
235
|
+
@socket.setsockopt(ZMQ::SUBSCRIBE, topic) == 0
|
206
236
|
end
|
207
237
|
|
208
238
|
# Removes a message filter (as set with subscribe) from the socket.
|
@@ -213,17 +243,24 @@ module EZMQ
|
|
213
243
|
# @return [Boolean] was unsubscription successful?
|
214
244
|
#
|
215
245
|
def unsubscribe(topic)
|
216
|
-
@socket.setsockopt(ZMQ::UNSUBSCRIBE, topic) == 0
|
246
|
+
@socket.setsockopt(ZMQ::UNSUBSCRIBE, topic) == 0
|
217
247
|
end
|
218
248
|
|
219
|
-
# By default, waits for a message and
|
249
|
+
# By default, waits for a message and prints it to STDOUT.
|
220
250
|
#
|
221
|
-
# @
|
251
|
+
# @yield message passes the message received to the block.
|
252
|
+
# @yieldparam [String] message the message received.
|
222
253
|
#
|
223
|
-
# @return [void]
|
254
|
+
# @return [void]
|
224
255
|
#
|
225
|
-
def listen
|
226
|
-
loop
|
256
|
+
def listen
|
257
|
+
loop do
|
258
|
+
if block_given?
|
259
|
+
yield receive
|
260
|
+
else
|
261
|
+
puts receive
|
262
|
+
end
|
263
|
+
end
|
227
264
|
end
|
228
265
|
end
|
229
266
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ezmq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Olstrom
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi-rzmq
|
@@ -162,6 +162,7 @@ files:
|
|
162
162
|
- LICENSE
|
163
163
|
- README.md
|
164
164
|
- Rakefile
|
165
|
+
- UPGRADING.md
|
165
166
|
- doc/EZMQ.html
|
166
167
|
- doc/EZMQ/Client.html
|
167
168
|
- doc/EZMQ/Publisher.html
|