mqtt-sub_handler 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/mqtt/sub_handler.rb +49 -58
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 18671e62878aafdf463e797c5e89da3ea1c20929
4
- data.tar.gz: a3d320eb0eb210c1e5826d54e9b835a5cf9b7faf
2
+ SHA256:
3
+ metadata.gz: 8d47218ce7ba4ae48cf8b7b44ecd807889111f520a86d5eaa5d458590d75ee5b
4
+ data.tar.gz: ba2fdf1f66a85f0de62c8c3b0238aa8ddf43df4a8e396440df92daf3323286ee
5
5
  SHA512:
6
- metadata.gz: f4a469aa5608c1283ff0daf5e5946086b1ed249052aa12d1284264a450d12b6e30cc217241cd0b582002d7f5ae26c5ad4ff3a0a58b83d843b6765522b5155f11
7
- data.tar.gz: f2fb74a355bdf2079381fec5278d76ebf7b55f38d339df0d14735041448b55bfbcadc8754d67fa5c9ea45c4af0fe08505ae79586b4f7cf1ea468aa6272d16d59
6
+ metadata.gz: 44c1ab1959a9d930ff9ffa18ee5f665d0ac76ae66da132c57dc12ccba36f7228b759d1b3eb24986b40160c2376569d33129ef127433a15f5f1aec6a3af5c4e2e
7
+ data.tar.gz: 609f907b547a2a78a08a2f2cdcf1f241cc2db70f8dc17c124682aac3423375f1f55399bfc2a376582fcd93fbc2fcf7d7648768d2bd14ad3c03878e5d671326c5
@@ -68,7 +68,7 @@ class SubHandler
68
68
  tMatch = SubHandler.getTopicMatch(topic, h.topic_split);
69
69
  if tMatch
70
70
  begin
71
- Timeout.timeout(10) {
71
+ Timeout.timeout(5) {
72
72
  h.offer(tMatch, data)
73
73
  }
74
74
  rescue Timeout::Error
@@ -84,19 +84,8 @@ class SubHandler
84
84
 
85
85
  # Handle sending a subscription-message to the server
86
86
  def raw_subscribe_to(topic, qos: 1)
87
- begin
88
- @conChangeMutex.lock
89
- if not @connected
90
- @subscribeQueue << [topic, qos];
91
- @conChangeMutex.unlock
92
- else
93
- @conChangeMutex.unlock
94
- @mqtt.subscribe(topic => qos);
95
- end
96
- rescue MQTT::Exception, SocketError, SystemCallError
97
- sleep 0.05;
98
- retry
99
- end
87
+ @subscribeQueue << [topic, qos];
88
+ @publisherThread.run();
100
89
  end
101
90
  private :raw_subscribe_to
102
91
 
@@ -220,26 +209,8 @@ class SubHandler
220
209
  data = data.to_json
221
210
  end
222
211
 
223
- retryCount = 0;
224
- begin
225
- @conChangeMutex.lock
226
- if not @connected
227
- @publishQueue << {topic: topic, data: data, qos: qos, retain: retain} unless qos == 0
228
- @conChangeMutex.unlock
229
- else
230
- @conChangeMutex.unlock
231
- @mqtt.publish(topic, data, retain);
232
- end
233
- rescue MQTT::Exception, SocketError, SystemCallError
234
- retryCount += 1;
235
- if(qos == 0 && retryCount == 3)
236
- STDERR.puts "Publish to #{topic} dropped!"
237
- return;
238
- end
239
-
240
- sleep 0.05;
241
- retry
242
- end
212
+ @publishQueue << {topic: topic, data: data, qos: qos, retain: retain}
213
+ @publisherThread.run();
243
214
  end
244
215
  alias publishTo publish_to
245
216
 
@@ -265,13 +236,13 @@ class SubHandler
265
236
  if(@mqttWasStartedClean)
266
237
  print "Logging out of mqtt server... "
267
238
  begin
268
- Timeout.timeout(10) {
239
+ Timeout.timeout(3) {
269
240
  begin
270
241
  @mqtt.clean_session = true;
271
242
  @mqtt.disconnect();
272
243
  @mqtt.connect();
273
244
  rescue MQTT::Exception, SocketError, SystemCallError
274
- sleep 1
245
+ sleep 0.3
275
246
  retry;
276
247
  end
277
248
  }
@@ -284,35 +255,59 @@ class SubHandler
284
255
  end
285
256
  private :ensure_clean_exit
286
257
 
287
- def mqtt_resub_thread
288
- while(true)
258
+ def mqtt_push_thread
259
+ loop do
260
+ Thread.stop();
261
+
262
+ next unless @connected
263
+
289
264
  begin
290
- Timeout.timeout(10) {
291
- @mqtt.connect()
292
- }
293
- @conChangeMutex.synchronize {
294
- @connected = true;
295
- }
296
265
  until @subscribeQueue.empty? do
297
266
  h = @subscribeQueue[-1];
298
- @mqtt.subscribe(h[0] => h[1]);
267
+ Timeout.timeout(3) {
268
+ @mqtt.subscribe(h[0] => h[1]);
269
+ }
299
270
  @subscribeQueue.pop;
300
271
  sleep 0.01
301
272
  end
302
273
  until @publishQueue.empty? do
303
274
  h = @publishQueue[-1];
304
- @mqtt.publish(h[:topic], h[:data], h[:retain]);
275
+ Timeout.timeout(3) {
276
+ @mqtt.publish(h[:topic], h[:data], h[:retain], h[:qos]);
277
+ }
305
278
  @publishQueue.pop;
306
279
  sleep 0.01
307
280
  end
281
+ rescue MQTT::Exception, SocketError, SystemCallError, Timeout::Error
282
+ @conChangeMutex.synchronize {
283
+ @connected = false;
284
+ }
285
+ @mqtt.disconnect();
286
+ end
287
+ end
288
+ end
289
+ private :mqtt_push_thread
290
+
291
+ def mqtt_resub_thread
292
+ while(true)
293
+ begin
294
+ Timeout.timeout(4) {
295
+ @mqtt.connect()
296
+ }
297
+ STDERR.puts("MQTT #{@mqtt.host} connected!")
298
+ @conChangeMutex.synchronize {
299
+ @connected = true;
300
+ }
301
+ @publisherThread.run();
308
302
  @mqtt.get do |topic, message|
309
303
  call_interested(topic, message);
310
304
  end
311
305
  rescue MQTT::Exception, Timeout::Error, SocketError, SystemCallError
306
+ STDERR.puts("MQTT #{@mqtt.host} disconnected!");
312
307
  @connected = false;
313
308
 
314
309
  @conChangeMutex.unlock if @conChangeMutex.owned?
315
- @mqtt.clean_session=false;
310
+ @mqtt.clean_session = false;
316
311
  sleep 2
317
312
  end
318
313
  end
@@ -331,10 +326,7 @@ class SubHandler
331
326
  Thread.stop();
332
327
  end
333
328
  def flush_pubqueue()
334
- puts "\n";
335
- if @publishQueue.empty?
336
- puts "MQTT buffer empty, continuing."
337
- else
329
+ unless @publishQueue.empty?
338
330
  print "Finishing sending of MQTT messages ... "
339
331
  begin
340
332
  Timeout.timeout(10) {
@@ -388,18 +380,17 @@ class SubHandler
388
380
  end
389
381
  @listenerThread.abort_on_exception = true;
390
382
 
383
+ @publisherThread = Thread.new do
384
+ mqtt_push_thread();
385
+ end
386
+ @publisherThread.abort_on_exception = true;
387
+
391
388
  at_exit {
392
389
  flush_pubqueue();
393
390
  @listenerThread.kill();
391
+ @publisherThread.kill();
394
392
  ensure_clean_exit();
395
393
  }
396
-
397
- begin
398
- Timeout.timeout(10) {
399
- until(@connected) do sleep 0.1; end
400
- }
401
- rescue Timeout::Error
402
- end
403
394
  end
404
395
  end
405
396
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mqtt-sub_handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xasin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-01 00:00:00.000000000 Z
11
+ date: 2019-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mqtt
@@ -114,8 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  - !ruby/object:Gem::Version
115
115
  version: '0'
116
116
  requirements: []
117
- rubyforge_project:
118
- rubygems_version: 2.5.2.1
117
+ rubygems_version: 3.0.6
119
118
  signing_key:
120
119
  specification_version: 4
121
120
  summary: Asynchronous, topic-based MQTT gem