mqtt-ccutrer 1.0.0 → 1.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fbcc93715636fd1a6f3491005dccf8c9805c519145145154a9895d5fb3df0670
4
- data.tar.gz: f978d8185287327b475f69881546dde182fbbd3c9d665253d9625bc48df1f9a9
3
+ metadata.gz: c95449b66faaba1f857dbb64e535b2f4a8565a91a8611aa3b53d1b1a3c802f7d
4
+ data.tar.gz: 053ce243061cf20b3326d53e7fda9afaa82972d4029631e6e0857efc465e7bcf
5
5
  SHA512:
6
- metadata.gz: 53cb56f81cd0eed1d41d6a0c5a7b7fcb88e53f5fcea755cdf544715d38fd698b931c7b5d23bbf161709a0f46fe72ce14a0f0107f2bfe5f59e92225c72d8d0556
7
- data.tar.gz: 845ec674c88c7be5b7835adeb1d6b52397661f3e02ae775240cb2621058eba72956327ca961c0ce82172a13485faee720d9b0c9c37e99f03643f1918d5d0d203
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 theis value defaults to 5)
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: 5,
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 (retries += 1) < @reconnect_limit
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 @last_packet_received_at already handled it
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
- current_time = self.current_time
677
+ current_time_local = current_time
665
678
  @acks.each_value do |pending_ack|
666
- break unless pending_ack.timeout_at <= current_time
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
- next_ping = @last_packet_received_at + @keep_alive if @keep_alive && !@keep_alive_sent
698
- next_ping = @last_packet_received_at + @keep_alive + @ack_timeout if @keep_alive && @keep_alive_sent
699
- current_time = self.current_time
700
- [([timeout_from_acks, next_ping].compact.min || current_time) - current_time, 0].max
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
- current_time = self.current_time
707
- if current_time >= @last_packet_received_at + @keep_alive && !@keep_alive_sent
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 current_time >= @last_packet_received_at + @keep_alive + @ack_timeout
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 = (@last_packet_id || 0).next
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
@@ -2,5 +2,5 @@
2
2
 
3
3
  module MQTT
4
4
  # The version number of the MQTT gem
5
- VERSION = '1.0.0'
5
+ VERSION = '1.0.3'
6
6
  end
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.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: 2021-02-20 00:00:00.000000000 Z
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.1.4
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: