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.
@@ -88,14 +88,14 @@
88
88
 
89
89
 
90
90
  <li class="r1 ">
91
- <span class='object_link'><a href="EZMQ/Socket.html#initialize-instance_method" title="EZMQ::Socket#initialize (method)">#initialize</a></span>
92
- <small>EZMQ::Socket</small>
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/Publisher.html#initialize-instance_method" title="EZMQ::Publisher#initialize (method)">#initialize</a></span>
98
- <small>EZMQ::Publisher</small>
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/Client.html#initialize-instance_method" title="EZMQ::Client#initialize (method)">#initialize</a></span>
110
- <small>EZMQ::Client</small>
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/Server.html#initialize-instance_method" title="EZMQ::Server#initialize (method)">#initialize</a></span>
116
- <small>EZMQ::Server</small>
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/Server.html#provides-instance_method" title="EZMQ::Server#provides (method)">#provides</a></span>
134
- <small>EZMQ::Server</small>
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/Socket.html#receive-instance_method" title="EZMQ::Socket#receive (method)">#receive</a></span>
140
- <small>EZMQ::Socket</small>
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/Client.html#request-instance_method" title="EZMQ::Client#request (method)">#request</a></span>
146
- <small>EZMQ::Client</small>
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="r2 ">
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="r1 ">
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 Tue Jan 6 12:58:18 2015 by
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>
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'ezmq'
3
- gem.version = '0.1.2'
3
+ gem.version = '0.2.0'
4
4
  gem.licenses = 'MIT'
5
5
  gem.authors = ['Chris Olstrom']
6
6
  gem.email = 'chris@olstrom.com'
@@ -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. Default unaltered.
18
- # @option options [lambda] decode how to decode messages. Default unaltered.
19
- # @option options [String] address specifies protocol, address and port (if
20
- # needed). Default is 'tcp://127.0.0.1:5555'
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
- method(mode).call address: options[:address] || 'tcp://127.0.0.1:5555'
36
+ endpoint = options.select { |k, _| [:protocol, :address, :port].include? k }
37
+ method(mode).call endpoint
31
38
  end
32
39
 
33
- # Receive a message from the socket.
40
+ # Sends a message on the socket.
34
41
  #
35
- # @note This method blocks until a message arrives.
42
+ # @note If `message` is not a String, `encode` must convert it to one.
36
43
  #
37
- # @param [lambda] decode how to decode the message.
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 [void] the decoded message.
48
+ # @return [Fixnum] the size of the message.
40
49
  #
41
- def receive(decode: @decode)
42
- message = ''
43
- @socket.recv_string message
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
- # Sends a message on the socket.
55
+ # Receive a message from the socket.
48
56
  #
49
- # @note If `message` is not a String, `encode` must convert it to one.
57
+ # @note This method blocks until a message arrives.
50
58
  #
51
- # @param [String] message the message to send.
52
- # @param [lambda] encode how to encode the message.
59
+ # @param [Hash] options optional parameters.
60
+ # @option options [lambda] decode how to decode the message.
53
61
  #
54
- # @return [Fixnum] the size of the message.
62
+ # @yield message passes the message received to the block.
63
+ # @yieldparam [Object] message the message received (decoded).
55
64
  #
56
- def send(message = '', encode: @encode)
57
- @socket.send_string encode.call message
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
- # @param [String] address specifies protocol, address and port (if needed).
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: 'tcp://127.0.0.1:5555')
70
- @socket.bind(address) == 0 ? true : false
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] address specifies protocol, address and port (if needed).
76
- # Default is 'tcp://127.0.0.1:5555'
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: 'tcp://127.0.0.1:5555')
81
- @socket.connect(address) == 0 ? true : false
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
- # Reply socket that listens for and replies to requests.
86
- class Server < EZMQ::Socket
87
- attr_accessor :provides
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
- # @see EZMQ::Socket EZMQ::Socket for a list of optional parameters.
114
+ # @param [Hash] options optional parameters.
115
+ # @see EZMQ::Socket EZMQ::Socket for optional parameters.
95
116
  #
96
- # @return [Server] a new instance of Server
117
+ # @return [Client] a new instance of Client.
97
118
  #
98
- def initialize(provides: -> m { m }, **options)
99
- @provides = provides
100
- super :bind, ZMQ::REP, options
119
+ def initialize(**options)
120
+ super :connect, ZMQ::REQ, options
101
121
  end
102
122
 
103
- # By default, waits to receive a message, calls @action with it, replies
104
- # with the result, then loops.
123
+ # Sends a message and waits to receive a response.
105
124
  #
106
- # @param [lambda] handler how requests are handled.
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 return from handler.
130
+ # @return [void] the decoded response message.
109
131
  #
110
- def listen(handler: -> { send @provides.call(receive) })
111
- loop { handler.call }
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
- # Request socket that sends messages and receives replies.
116
- class Client < EZMQ::Socket
117
- # Creates a new Client socket.
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 a list of optional parameters.
148
+ # @see EZMQ::Socket EZMQ::Socket for optional parameters.
122
149
  #
123
- # @return [Client] a new instance of Client.
150
+ # @return [Server] a new instance of Server
124
151
  #
125
152
  def initialize(**options)
126
- super :connect, ZMQ::REQ, options
153
+ super :bind, ZMQ::REP, options
127
154
  end
128
155
 
129
- # Sends a message and waits to receive a response.
156
+ # Listens for a request, and responds to it.
130
157
  #
131
- # @param [String] message the message to send.
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
- # @return [void] the decoded response message.
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 request(message = '', encode: @encode, decode: @decode)
138
- send message, encode: encode
139
- receive decode: decode
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 [lambda] encode how to encode the message.
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: '', encode: @encode)
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
- # @param [lambda] action the action to perform when a message is received.
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(action: -> m { puts m }, **options)
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 ? true : false
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 ? true : false
246
+ @socket.setsockopt(ZMQ::UNSUBSCRIBE, topic) == 0
217
247
  end
218
248
 
219
- # By default, waits for a message and calls @action with the message.
249
+ # By default, waits for a message and prints it to STDOUT.
220
250
  #
221
- # @param [lambda] handler how requests are handled.
251
+ # @yield message passes the message received to the block.
252
+ # @yieldparam [String] message the message received.
222
253
  #
223
- # @return [void] the return from handler.
254
+ # @return [void]
224
255
  #
225
- def listen(handler: -> { @action.call(receive) })
226
- loop { handler.call }
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.1.2
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-06 00:00:00.000000000 Z
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