mqtt-ccutrer 1.0.0 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mqtt/client.rb +34 -16
- data/lib/mqtt/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c95449b66faaba1f857dbb64e535b2f4a8565a91a8611aa3b53d1b1a3c802f7d
|
4
|
+
data.tar.gz: 053ce243061cf20b3326d53e7fda9afaa82972d4029631e6e0857efc465e7bcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e465141738a3352c562474041f199a260496d9d499d81899613381de8521a9fb215bdde351b608ac9630a463b3fa2e2dd4cfe22046ab179926d0c657ffebaa08
|
7
|
+
data.tar.gz: 0b87b975e3c9a792f17dcc663c9aa0c0be764a6866eeeedb1a88ba9d38511f661995c412920d6798e580929317d4e633fdd744f33177fb7609c30542b4394e5d
|
data/lib/mqtt/client.rb
CHANGED
@@ -43,14 +43,17 @@ module MQTT
|
|
43
43
|
attr_accessor :resend_limit
|
44
44
|
|
45
45
|
# How many attempts to re-establish a connection after it drops before
|
46
|
-
# giving up (default 5)
|
46
|
+
# giving up (default 5); nil for unlimited retries
|
47
47
|
attr_accessor :reconnect_limit
|
48
48
|
|
49
49
|
# How long to wait between re-connection attempts (exponential - i.e.
|
50
50
|
# immediately after first drop, then 5s, then 25s, then 125s, etc.
|
51
|
-
# when
|
51
|
+
# when this value defaults to 5)
|
52
52
|
attr_accessor :reconnect_backoff
|
53
53
|
|
54
|
+
# the longest amount of time to wait before attempting a reconnect
|
55
|
+
attr_accessor :reconnect_backoff_max
|
56
|
+
|
54
57
|
# Username to authenticate to the server with
|
55
58
|
attr_accessor :username
|
56
59
|
|
@@ -80,7 +83,8 @@ module MQTT
|
|
80
83
|
ack_timeout: 5,
|
81
84
|
resend_limit: 5,
|
82
85
|
reconnect_limit: 5,
|
83
|
-
reconnect_backoff:
|
86
|
+
reconnect_backoff: 2,
|
87
|
+
reconnect_backoff_max: 30,
|
84
88
|
username: nil,
|
85
89
|
password: nil,
|
86
90
|
will_topic: nil,
|
@@ -130,8 +134,11 @@ module MQTT
|
|
130
134
|
# client = MQTT::Client.new('myserver.example.com', 18830)
|
131
135
|
# client = MQTT::Client.new(host: 'myserver.example.com')
|
132
136
|
# client = MQTT::Client.new(host: 'myserver.example.com', keep_alive: 30)
|
137
|
+
# client = MQTT::Client.new(uri: 'mqtt://myserver.example.com', keep_alive: 30)
|
133
138
|
#
|
134
139
|
def initialize(host = nil, port = nil, **attributes)
|
140
|
+
host = attributes.delete(:uri) if attributes.key?(:uri)
|
141
|
+
|
135
142
|
# Set server URI from environment if present
|
136
143
|
if host.nil? && port.nil? && attributes.empty? && ENV['MQTT_SERVER']
|
137
144
|
attributes.merge!(parse_uri(ENV['MQTT_SERVER']))
|
@@ -173,6 +180,9 @@ module MQTT
|
|
173
180
|
@wake_up_pipe = IO.pipe
|
174
181
|
|
175
182
|
@connected = false
|
183
|
+
@keep_alive_sent = false
|
184
|
+
@last_packet_id = 0
|
185
|
+
@batch_publish = false
|
176
186
|
end
|
177
187
|
|
178
188
|
# Get the OpenSSL context, that is used if SSL/TLS is enabled
|
@@ -514,6 +524,7 @@ module MQTT
|
|
514
524
|
|
515
525
|
# Send packet
|
516
526
|
@socket.write(packet.to_s)
|
527
|
+
@last_packet_sent_at = current_time
|
517
528
|
|
518
529
|
# Receive response
|
519
530
|
receive_connack
|
@@ -528,6 +539,7 @@ module MQTT
|
|
528
539
|
next
|
529
540
|
end
|
530
541
|
@socket.write(packet.to_s)
|
542
|
+
@last_packet_sent_at = current_time
|
531
543
|
end
|
532
544
|
rescue => e
|
533
545
|
@write_queue << packet if packet
|
@@ -555,9 +567,10 @@ module MQTT
|
|
555
567
|
rescue
|
556
568
|
@socket&.close
|
557
569
|
@socket = nil
|
570
|
+
retries += 1
|
558
571
|
|
559
|
-
if
|
560
|
-
sleep @reconnect_backoff ** retries
|
572
|
+
if @reconnect_limit.nil? || retries < @reconnect_limit
|
573
|
+
sleep [@reconnect_backoff ** retries, @reconnect_backoff_max].min
|
561
574
|
retry
|
562
575
|
end
|
563
576
|
end
|
@@ -643,7 +656,7 @@ module MQTT
|
|
643
656
|
# Add to queue
|
644
657
|
@read_queue.push(packet)
|
645
658
|
when MQTT::Packet::Pingresp
|
646
|
-
# do nothing; setting @
|
659
|
+
# do nothing; setting @keep_alive_sent already handled it
|
647
660
|
when MQTT::Packet::Puback,
|
648
661
|
MQTT::Packet::Suback,
|
649
662
|
MQTT::Packet::Unsuback
|
@@ -661,9 +674,9 @@ module MQTT
|
|
661
674
|
|
662
675
|
def handle_timeouts
|
663
676
|
@acks_mutex.synchronize do
|
664
|
-
|
677
|
+
current_time_local = current_time
|
665
678
|
@acks.each_value do |pending_ack|
|
666
|
-
break unless pending_ack.timeout_at <=
|
679
|
+
break unless pending_ack.timeout_at <= current_time_local
|
667
680
|
|
668
681
|
resend(pending_ack)
|
669
682
|
end
|
@@ -694,21 +707,26 @@ module MQTT
|
|
694
707
|
end
|
695
708
|
return nil if timeout_from_acks.nil? && @keep_alive.nil?
|
696
709
|
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
710
|
+
if @keep_alive
|
711
|
+
next_ping = if @keep_alive_sent
|
712
|
+
@last_packet_received_at + @keep_alive + @ack_timeout
|
713
|
+
else
|
714
|
+
@last_packet_sent_at + @keep_alive
|
715
|
+
end
|
716
|
+
end
|
717
|
+
current_time_local = current_time
|
718
|
+
[([timeout_from_acks, next_ping].compact.min || current_time_local) - current_time_local, 0].max
|
701
719
|
end
|
702
720
|
|
703
721
|
def handle_keep_alives
|
704
722
|
return unless @keep_alive && @keep_alive > 0
|
705
723
|
|
706
|
-
|
707
|
-
if
|
724
|
+
current_time_local = current_time
|
725
|
+
if current_time_local >= @last_packet_sent_at + @keep_alive && !@keep_alive_sent
|
708
726
|
packet = MQTT::Packet::Pingreq.new
|
709
727
|
send_packet(packet)
|
710
728
|
@keep_alive_sent = true
|
711
|
-
elsif
|
729
|
+
elsif current_time_local >= @last_packet_received_at + @keep_alive + @ack_timeout
|
712
730
|
raise KeepAliveTimeout
|
713
731
|
end
|
714
732
|
end
|
@@ -764,7 +782,7 @@ module MQTT
|
|
764
782
|
end
|
765
783
|
|
766
784
|
def next_packet_id
|
767
|
-
@last_packet_id =
|
785
|
+
@last_packet_id = @last_packet_id.next
|
768
786
|
@last_packet_id = 1 if @last_packet_id > 0xffff
|
769
787
|
@last_packet_id
|
770
788
|
end
|
data/lib/mqtt/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mqtt-ccutrer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cody Cutrer
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -159,7 +159,7 @@ homepage: http://github.com/ccutrer/ruby-mqtt
|
|
159
159
|
licenses:
|
160
160
|
- MIT
|
161
161
|
metadata: {}
|
162
|
-
post_install_message:
|
162
|
+
post_install_message:
|
163
163
|
rdoc_options: []
|
164
164
|
require_paths:
|
165
165
|
- lib
|
@@ -174,8 +174,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
174
|
- !ruby/object:Gem::Version
|
175
175
|
version: '0'
|
176
176
|
requirements: []
|
177
|
-
rubygems_version: 3.
|
178
|
-
signing_key:
|
177
|
+
rubygems_version: 3.3.5
|
178
|
+
signing_key:
|
179
179
|
specification_version: 4
|
180
180
|
summary: Implementation of the MQTT protocol
|
181
181
|
test_files:
|