mqtt-sub_handler 0.1.6 → 0.1.6.2
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/lib/mqtt/sub_handler.rb +63 -36
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfe901d34d1a891840006f77467ef0929bfb9ff1363ccc3ece47cf35454178b0
|
4
|
+
data.tar.gz: 1e601e44349b5e360068d3e07a4a59caa0254cfc2aaee1e1f870fe1069041e96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 589b2c0307aa5ec6aa0e253805d8b095ce73997bd9461bc7abc060d306ac6f1b5c8ff18ddee861a1c2d43d6b7e3685bacb9b4994360b106c1ad19791c91bd511
|
7
|
+
data.tar.gz: 4d5d90483bfa7ce80c4c6359d5fb38faad29a08f447d987cebf1779597190c4e7d692950a0bafa1d63b98d5d76d5fcdedd696b6385ab170a5c917da0c4ef0ac0
|
data/lib/mqtt/sub_handler.rb
CHANGED
@@ -74,7 +74,7 @@ class SubHandler
|
|
74
74
|
h.offer(tMatch, data)
|
75
75
|
}
|
76
76
|
rescue Timeout::Error
|
77
|
-
STDERR.puts "MQTT Callback Timeout #{h}"
|
77
|
+
STDERR.puts "MQTT: Callback Timeout #{h}".red
|
78
78
|
end
|
79
79
|
topicHasReceivers = true;
|
80
80
|
end
|
@@ -84,10 +84,18 @@ class SubHandler
|
|
84
84
|
end
|
85
85
|
private :call_interested
|
86
86
|
|
87
|
+
def queue_packet(data)
|
88
|
+
@packetQueueMutex.synchronize {
|
89
|
+
@packetQueue << data;
|
90
|
+
@packetQueue.shift if @packetQueue.size > 100
|
91
|
+
|
92
|
+
@publisherThread.run() if @publisherThreadWaiting;
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
87
96
|
# Handle sending a subscription-message to the server
|
88
97
|
def raw_subscribe_to(topic, qos: 1)
|
89
|
-
|
90
|
-
@publisherThread.run();
|
98
|
+
queue_packet({topic: topic, qos: qos, type: :sub});
|
91
99
|
end
|
92
100
|
private :raw_subscribe_to
|
93
101
|
|
@@ -211,8 +219,14 @@ class SubHandler
|
|
211
219
|
data = data.to_json
|
212
220
|
end
|
213
221
|
|
214
|
-
|
215
|
-
|
222
|
+
if(qos > 1)
|
223
|
+
qos = 1
|
224
|
+
STDERR.puts("MQTT push with QOS > 1 was attempted, this is not supported yet!".yellow) unless $MQTTPubQOSWarned
|
225
|
+
|
226
|
+
$MQTTPubQOSWarned = true;
|
227
|
+
end
|
228
|
+
|
229
|
+
queue_packet({type: :pub, topic: topic, data: data, qos: qos, retain: retain});
|
216
230
|
end
|
217
231
|
alias publishTo publish_to
|
218
232
|
|
@@ -259,40 +273,47 @@ class SubHandler
|
|
259
273
|
|
260
274
|
def mqtt_push_thread
|
261
275
|
loop do
|
276
|
+
@packetQueueMutex.synchronize {
|
277
|
+
@publisherThreadWaiting = true;
|
278
|
+
}
|
262
279
|
Thread.stop();
|
280
|
+
@packetQueueMutex.synchronize {
|
281
|
+
@publisherThreadWaiting = false;
|
282
|
+
}
|
263
283
|
|
264
284
|
next unless @connected
|
265
285
|
|
266
286
|
begin
|
267
|
-
until @
|
268
|
-
h =
|
269
|
-
|
270
|
-
|
287
|
+
until @packetQueue.empty? do
|
288
|
+
h = nil;
|
289
|
+
@packetQueueMutex.synchronize {
|
290
|
+
h = @packetQueue[0];
|
271
291
|
}
|
272
|
-
@subscribeQueue.pop;
|
273
|
-
sleep 0.01
|
274
|
-
end
|
275
|
-
until @publishQueue.empty? do
|
276
|
-
h = @publishQueue[-1];
|
277
292
|
Timeout.timeout(3) {
|
278
|
-
|
293
|
+
if(h[:type] == :sub)
|
294
|
+
@mqtt.subscribe(h[:topic] => h[:qos]);
|
295
|
+
elsif(h[:type] == :pub)
|
296
|
+
@mqtt.publish(h[:topic], h[:data], h[:retain], h[:qos]);
|
297
|
+
end
|
279
298
|
}
|
280
|
-
@
|
281
|
-
|
282
|
-
end
|
283
|
-
rescue MQTT::Exception, SocketError, SystemCallError, Timeout::Error
|
284
|
-
@conChangeMutex.synchronize {
|
285
|
-
@connected = false;
|
299
|
+
@packetQueueMutex.synchronize {
|
300
|
+
@packetQueue.shift();
|
286
301
|
}
|
287
|
-
|
302
|
+
end
|
303
|
+
rescue MQTT::Exception, SocketError, SystemCallError, Timeout::Error => e
|
304
|
+
STDERR.puts("MQTT: #{@mqtt.host} push error, disconnecting!".red) if @connected
|
305
|
+
STDERR.puts(e.inspect);
|
306
|
+
|
307
|
+
sleep 1
|
288
308
|
end
|
289
309
|
end
|
290
310
|
end
|
291
311
|
private :mqtt_push_thread
|
292
312
|
|
293
313
|
def mqtt_resub_thread
|
294
|
-
|
314
|
+
loop do
|
295
315
|
begin
|
316
|
+
STDERR.puts("MQTT: #{@mqtt.host} trying reconnect...".yellow)
|
296
317
|
Timeout.timeout(4) {
|
297
318
|
@mqtt.connect()
|
298
319
|
}
|
@@ -300,12 +321,16 @@ class SubHandler
|
|
300
321
|
@conChangeMutex.synchronize {
|
301
322
|
@connected = true;
|
302
323
|
}
|
303
|
-
|
324
|
+
|
325
|
+
@packetQueueMutex.synchronize {
|
326
|
+
@publisherThread.run() if (@publisherThread && @publisherThreadWaiting)
|
327
|
+
}
|
328
|
+
|
304
329
|
@mqtt.get do |topic, message|
|
305
330
|
call_interested(topic, message);
|
306
331
|
end
|
307
332
|
rescue MQTT::Exception, Timeout::Error, SocketError, SystemCallError
|
308
|
-
STDERR.puts("MQTT #{@mqtt.host} disconnected!".red)
|
333
|
+
STDERR.puts("MQTT: #{@mqtt.host} disconnected!".red) if @connected
|
309
334
|
@connected = false;
|
310
335
|
|
311
336
|
@conChangeMutex.unlock if @conChangeMutex.owned?
|
@@ -328,11 +353,11 @@ class SubHandler
|
|
328
353
|
Thread.stop();
|
329
354
|
end
|
330
355
|
def flush_pubqueue()
|
331
|
-
unless @
|
356
|
+
unless @packetQueue.empty?
|
332
357
|
print "Finishing sending of MQTT messages ... "
|
333
358
|
begin
|
334
359
|
Timeout.timeout(4) {
|
335
|
-
until @
|
360
|
+
until @packetQueue.empty? do
|
336
361
|
sleep 0.05;
|
337
362
|
end
|
338
363
|
}
|
@@ -370,8 +395,10 @@ class SubHandler
|
|
370
395
|
|
371
396
|
@mqtt.client_id ||= MQTT::Client.generate_client_id("MQTT_Sub_", 8);
|
372
397
|
|
373
|
-
@
|
374
|
-
@
|
398
|
+
@packetQueue = Array.new();
|
399
|
+
@packetQueueMutex = Mutex.new();
|
400
|
+
@publisherThreadWaiting = false;
|
401
|
+
|
375
402
|
@subscribedTopics = Hash.new();
|
376
403
|
|
377
404
|
@trackerHash = Hash.new();
|
@@ -382,13 +409,8 @@ class SubHandler
|
|
382
409
|
end
|
383
410
|
@listenerThread.abort_on_exception = true;
|
384
411
|
|
385
|
-
@publisherThread = Thread.new do
|
386
|
-
mqtt_push_thread();
|
387
|
-
end
|
388
|
-
@publisherThread.abort_on_exception = true;
|
389
|
-
|
390
412
|
begin
|
391
|
-
Timeout.timeout(
|
413
|
+
Timeout.timeout(5) {
|
392
414
|
until(@connected)
|
393
415
|
sleep 0.1;
|
394
416
|
end
|
@@ -397,10 +419,15 @@ class SubHandler
|
|
397
419
|
STDERR.puts "MQTT: #{@mqtt.host} did not connect!".red
|
398
420
|
end
|
399
421
|
|
422
|
+
@publisherThread = Thread.new do
|
423
|
+
mqtt_push_thread();
|
424
|
+
end
|
425
|
+
@publisherThread.abort_on_exception = true;
|
426
|
+
|
400
427
|
at_exit {
|
401
428
|
flush_pubqueue();
|
429
|
+
@connected = false;
|
402
430
|
@listenerThread.kill();
|
403
|
-
@publisherThread.kill();
|
404
431
|
ensure_clean_exit();
|
405
432
|
}
|
406
433
|
end
|