mosq 0.0.2 → 0.1.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/ext/mosq/Rakefile +9 -3
  3. data/lib/mosq/client.rb +97 -2
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77871424981b16bc758d319dbabbb63958f96d61
4
- data.tar.gz: e61adcdefbc1238c1df36e54ca6e6f6b3dfba294
3
+ metadata.gz: b296db03c458067273327316bdb11e9e363d671e
4
+ data.tar.gz: 78c2ba4c4782f25a010398be2823161163b49411
5
5
  SHA512:
6
- metadata.gz: 8348f60d1caf81225fd7c7ae9b75f5e0030d514a6c0986ac4eef21ea036dad1fefc5977e1ae34d0d6d058092ce0d5c588a59bfa5807a5874edbbd2a81f48a820
7
- data.tar.gz: 3e7d5d2ddd4b732905ae73f43bd94bae371a537007065df1d0a1c753692c7731325e49a2dd837b962defa4d5443d5624c1cec9b7ce11ef11dddf791c673cf44d
6
+ metadata.gz: 07a7d0ab55446f767c6a81eba9eae3a1ea08524c55d2fc6f29a32a26e33e192a3bb01cd9e49ca4a9a0bef36b72d96f76c8f78466f1808d9acf7fb68504180a27
7
+ data.tar.gz: 41e4acb30e8ae4c775864a1f6df4545f4ebde02850a5c02b3bdd0d100fed4dff417f3232ddd29a1140d2001f75484b26f1ab44f595cc342743860388bd5fbe02
@@ -36,9 +36,15 @@ file_task 'mosquitto', :download => :download_tarball do
36
36
  end
37
37
 
38
38
  file_task "libmosquitto.#{::FFI::Platform::LIBSUFFIX}", :build => :download do
39
- mkflags = "WITH_THREADING=no WITH_SRV=no WITH_UUID=false"
40
- cmd "/usr/bin/env sh -c 'cd #{FILES[:download]} && make #{mkflags}'"
41
- cmd "cp #{FILES[:download]}/lib/#{FILES[:build]}* ./#{FILES[:build]}"
39
+ begin
40
+ mkflags = "WITH_THREADING=no WITH_SRV=no WITH_UUID=no"
41
+ cmd "/usr/bin/env sh -c 'cd #{FILES[:download]} && make #{mkflags}'"
42
+ cmd "cp #{FILES[:download]}/lib/#{FILES[:build]}* ./#{FILES[:build]}"
43
+ rescue RuntimeError # make won't work on OSX, asking to use cmake instead.
44
+ cmkflags = "-DWITH_THREADING=OFF -DWITH_SRV=OFF"
45
+ cmd "/usr/bin/env sh -c 'cd #{FILES[:download]} && cmake . #{cmkflags} && make'"
46
+ cmd "cp #{FILES[:download]}/lib/#{FILES[:build]} ./#{FILES[:build]}"
47
+ end
42
48
  end
43
49
 
44
50
  task :compact => FILES[:build] do
@@ -44,7 +44,8 @@ module Mosq
44
44
  def heartbeat; @options.fetch(:heartbeat); end
45
45
 
46
46
  # The maximum time interval the user application should wait between
47
- # yielding control back to the client object by calling run_loop!.
47
+ # yielding control back to the client object by calling methods like
48
+ # {#run_loop!} and {#run_immediate!}.
48
49
  def max_poll_interval
49
50
  @options.fetch(:heartbeat) / 2.0
50
51
  end
@@ -181,6 +182,70 @@ module Mosq
181
182
  self
182
183
  end
183
184
 
185
+ # Subscribe to many topics. This is more performant than many calls
186
+ # to {#subscribe}, as the transactions occur concurrently.
187
+ #
188
+ # @param topics [Array<String>] The topic patterns to subscribe to.
189
+ # @param qos [Integer] The QoS level to use for all publish transaction.
190
+ # @return [Client] This client.
191
+ #
192
+ def subscribe_many(topics, qos: 0)
193
+ packet_ids = []
194
+ topics.each do |topic|
195
+ Util.error_check "subscribing to many topics",
196
+ FFI.mosquitto_subscribe(ptr, @packet_id_ptr, topic, qos)
197
+
198
+ packet_ids << @packet_id_ptr.read_int
199
+ end
200
+
201
+ fetch_responses(:subscribe, packet_ids)
202
+
203
+ self
204
+ end
205
+
206
+ # Unsubscribe from many topics. This is more performant than many calls
207
+ # to {#unsubscribe}, as the transactions occur concurrently.
208
+ #
209
+ # @param topics [Array<String>] The topic patterns to unsubscribe from.
210
+ # @return [Client] This client.
211
+ #
212
+ def unsubscribe_many(topics)
213
+ packet_ids = []
214
+ topics.each do |topic|
215
+ Util.error_check "subscribing to many topics",
216
+ FFI.mosquitto_unsubscribe(ptr, @packet_id_ptr, topic)
217
+
218
+ packet_ids << @packet_id_ptr.read_int
219
+ end
220
+
221
+ fetch_responses(:unsubscribe, packet_ids)
222
+
223
+ self
224
+ end
225
+
226
+ # Publish many pairs of topic/payload as messages. This is more performant
227
+ # than many calls to {#publish}, as the transactions occur concurrently.
228
+ #
229
+ # @param pairs [Array<Array(String, String)>] The topic/payload pairs.
230
+ # @param qos [Integer] The QoS level to use for all publish transaction.
231
+ # @param retain [Boolean] Whether the broker should retain the messages.
232
+ # @return [Client] This client.
233
+ #
234
+ def publish_many(pairs, qos: 0, retain: false)
235
+ packet_ids = []
236
+ pairs.each do |topic, payload|
237
+ Util.error_check "publishing many messages",
238
+ FFI.mosquitto_publish(ptr, @packet_id_ptr,
239
+ topic, payload.bytesize, payload, qos, retain)
240
+
241
+ packet_ids << @packet_id_ptr.read_int
242
+ end
243
+
244
+ fetch_responses(:publish, packet_ids)
245
+
246
+ self
247
+ end
248
+
184
249
  # Fetch and handle events in a loop that blocks the calling thread.
185
250
  # The loop will continue until the {#break!} method is called from within
186
251
  # an event handler, or until the given timeout duration has elapsed.
@@ -302,7 +367,8 @@ module Mosq
302
367
  end
303
368
 
304
369
  # Internal implementation of synchronous responses.
305
- def fetch_response(expected_type, expected_packet_id, timeout=protocol_timeout, start=Time.now)
370
+ def fetch_response(expected_type, expected_packet_id,
371
+ timeout=protocol_timeout, start=Time.now)
306
372
  unwanted_events = []
307
373
 
308
374
  while (event = fetch_next_event(timeout, start))
@@ -320,5 +386,34 @@ module Mosq
320
386
 
321
387
  raise FFI::Error::Timeout, "waiting for #{expected_type} response"
322
388
  end
389
+
390
+ # Internal implementation of multiple outstanding synchronous responses.
391
+ def fetch_responses(expected_type, expected_packet_ids,
392
+ timeout=protocol_timeout, start=Time.now)
393
+ return [] if expected_packet_ids.empty?
394
+ unwanted_events = []
395
+ wanted_events = []
396
+
397
+ while (event = fetch_next_event(timeout, start))
398
+ if (event.fetch(:type) == expected_type) && (
399
+ expected_packet_ids.include?(event.fetch(:packet_id))
400
+ )
401
+ wanted_events.push(event)
402
+ else
403
+ unwanted_events.push(event)
404
+ end
405
+
406
+ if wanted_events.size >= expected_packet_ids.size
407
+ unwanted_events.reverse_each { |e| @bucket.events.unshift(e) }
408
+ wanted_events.each do |event|
409
+ handle_incoming_event(event)
410
+ end
411
+
412
+ return wanted_events
413
+ end
414
+ end
415
+
416
+ raise FFI::Error::Timeout, "waiting for #{expected_type} responses"
417
+ end
323
418
  end
324
419
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mosq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe McIlvain