paho-mqtt 1.0.11 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/paho-mqtt.rb +1 -5
- data/lib/paho_mqtt/client.rb +20 -22
- data/lib/paho_mqtt/connection_helper.rb +6 -6
- data/lib/paho_mqtt/exception.rb +3 -0
- data/lib/paho_mqtt/publisher.rb +18 -18
- data/lib/paho_mqtt/sender.rb +12 -7
- data/lib/paho_mqtt/ssl_helper.rb +25 -6
- data/lib/paho_mqtt/subscriber.rb +5 -1
- data/lib/paho_mqtt/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b12b2ca9e37d486f259e998a800d9bb792aaf32bade2d542bfb1f65cddb7eed
|
4
|
+
data.tar.gz: d2526058c6647dcc29b694240074c17f1ba23153a4834002620da00fe0ca8618
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e406280a8134c6ba1f6dfe4a308300bccfd5f724d6b9b1f6dcdee0dc39b038855ba49d9d8c810c552d3563f2af6f2e141e246579307819dcaaec39128fa8f79
|
7
|
+
data.tar.gz: 3d2733b63c3665ebf1c81636f7e056a72034b8dc3016413df9c3f4175f5a57b102b10edc680ab83283cc88d0fa2e361c9d1698bf754d608781730632642d5658
|
data/lib/paho-mqtt.rb
CHANGED
@@ -32,12 +32,8 @@ module PahoMqtt
|
|
32
32
|
# MAX size of queue
|
33
33
|
MAX_SUBACK = 10
|
34
34
|
MAX_UNSUBACK = 10
|
35
|
-
MAX_PUBACK = 100
|
36
|
-
MAX_PUBREC = 100
|
37
|
-
MAX_PUBREL = 100
|
38
35
|
MAX_PUBLISH = 1000
|
39
|
-
|
40
|
-
MAX_QUEUE = MAX_PUBACK + MAX_PUBREC + MAX_PUBREL + MAX_PUBCOMP
|
36
|
+
MAX_QUEUE = 1000
|
41
37
|
|
42
38
|
# Connection states values
|
43
39
|
MQTT_CS_NEW = 0
|
data/lib/paho_mqtt/client.rb
CHANGED
@@ -112,9 +112,10 @@ module PahoMqtt
|
|
112
112
|
end
|
113
113
|
@mqtt_thread.kill unless @mqtt_thread.nil?
|
114
114
|
|
115
|
-
init_connection
|
115
|
+
init_connection unless reconnect?
|
116
116
|
@connection_helper.send_connect(session_params)
|
117
117
|
begin
|
118
|
+
init_pubsub
|
118
119
|
@connection_state = @connection_helper.do_connect(reconnect?)
|
119
120
|
if connected?
|
120
121
|
build_pubsub
|
@@ -168,6 +169,8 @@ module PahoMqtt
|
|
168
169
|
result = @handler.receive_packet
|
169
170
|
break if result.nil?
|
170
171
|
end
|
172
|
+
rescue FullQueueException
|
173
|
+
PahoMqtt.logger.warn("Early exit in reading loop. The maximum packets have been reach for #{packet.type_name}") if PahoMqtt.logger?
|
171
174
|
rescue ReadingException
|
172
175
|
if check_persistence
|
173
176
|
reconnect
|
@@ -213,11 +216,14 @@ module PahoMqtt
|
|
213
216
|
end
|
214
217
|
|
215
218
|
def disconnect(explicit=true)
|
216
|
-
@last_packet_id = 0 if explicit
|
217
219
|
@connection_helper.do_disconnect(@publisher, explicit, @mqtt_thread)
|
218
220
|
@connection_state_mutex.synchronize do
|
219
221
|
@connection_state = MQTT_CS_DISCONNECT
|
220
222
|
end
|
223
|
+
if explicit && @clean_session
|
224
|
+
@last_packet_id = 0
|
225
|
+
@subscriber.clear_queue
|
226
|
+
end
|
221
227
|
MQTT_ERR_SUCCESS
|
222
228
|
end
|
223
229
|
|
@@ -368,30 +374,22 @@ module PahoMqtt
|
|
368
374
|
end
|
369
375
|
end
|
370
376
|
|
371
|
-
def
|
372
|
-
|
373
|
-
|
374
|
-
else
|
375
|
-
@subscriber.sender = @sender
|
376
|
-
@subscriber.config_subscription(next_packet_id)
|
377
|
-
end
|
378
|
-
if @publisher.nil?
|
379
|
-
@publisher = Publisher.new(@sender)
|
380
|
-
else
|
381
|
-
@publisher.sender = @sender
|
382
|
-
@sender.flush_waiting_packet
|
383
|
-
@publisher.config_all_message_queue
|
384
|
-
end
|
377
|
+
def init_pubsub
|
378
|
+
@subscriber.nil? ? @subscriber = Subscriber.new(@sender) : @subscriber.sender = @sender
|
379
|
+
@publisher.nil? ? @publisher = Publisher.new(@sender) : @publisher.sender = @sender
|
385
380
|
@handler.config_pubsub(@publisher, @subscriber)
|
386
381
|
end
|
387
382
|
|
383
|
+
def build_pubsub
|
384
|
+
@subscriber.config_subscription(next_packet_id)
|
385
|
+
@sender.flush_waiting_packet
|
386
|
+
@publisher.config_all_message_queue
|
387
|
+
end
|
388
|
+
|
388
389
|
def init_connection
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
@sender = @connection_helper.sender
|
393
|
-
end
|
394
|
-
@connection_helper.setup_connection
|
390
|
+
@connection_helper = ConnectionHelper.new(@host, @port, @ssl, @ssl_context, @ack_timeout)
|
391
|
+
@connection_helper.handler = @handler
|
392
|
+
@sender = @connection_helper.sender
|
395
393
|
end
|
396
394
|
|
397
395
|
def session_params
|
@@ -63,7 +63,7 @@ module PahoMqtt
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def explicit_disconnect(publisher, mqtt_thread)
|
66
|
-
@sender.flush_waiting_packet
|
66
|
+
@sender.flush_waiting_packet(false)
|
67
67
|
send_disconnect
|
68
68
|
mqtt_thread.kill if mqtt_thread && mqtt_thread.alive?
|
69
69
|
publisher.flush_publisher unless publisher.nil?
|
@@ -81,14 +81,14 @@ module PahoMqtt
|
|
81
81
|
PahoMqtt.logger.debug("Attempt to connect to host: #{@host}...") if PahoMqtt.logger?
|
82
82
|
begin
|
83
83
|
tcp_socket = TCPSocket.new(@host, @port)
|
84
|
+
if @ssl
|
85
|
+
encrypted_socket(tcp_socket, @ssl_context)
|
86
|
+
else
|
87
|
+
@socket = tcp_socket
|
88
|
+
end
|
84
89
|
rescue StandardError
|
85
90
|
PahoMqtt.logger.warn("Could not open a socket with #{@host} on port #{@port}.") if PahoMqtt.logger?
|
86
91
|
end
|
87
|
-
if @ssl
|
88
|
-
encrypted_socket(tcp_socket, @ssl_context)
|
89
|
-
else
|
90
|
-
@socket = tcp_socket
|
91
|
-
end
|
92
92
|
end
|
93
93
|
|
94
94
|
def encrypted_socket(tcp_socket, ssl_context)
|
data/lib/paho_mqtt/exception.rb
CHANGED
data/lib/paho_mqtt/publisher.rb
CHANGED
@@ -39,28 +39,28 @@ module PahoMqtt
|
|
39
39
|
:retain => retain,
|
40
40
|
:qos => qos
|
41
41
|
)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
begin
|
43
|
+
case qos
|
44
|
+
when 1
|
45
|
+
push_queue(@waiting_puback, @puback_mutex, MAX_QUEUE, packet, new_id)
|
46
|
+
when 2
|
47
|
+
push_queue(@waiting_pubrec, @pubrec_mutex, MAX_QUEUE, packet, new_id)
|
48
|
+
end
|
49
|
+
rescue FullQueueException
|
50
|
+
PahoMqtt.logger.warn("PUBLISH queue is full, waiting for publishing #{packet.inspect}") if PahoMqtt.logger?
|
51
|
+
sleep SELECT_TIMEOUT
|
52
|
+
retry
|
47
53
|
end
|
48
54
|
@sender.append_to_writing(packet)
|
49
55
|
MQTT_ERR_SUCCESS
|
50
56
|
end
|
51
57
|
|
52
58
|
def push_queue(waiting_queue, queue_mutex, max_packet, packet, new_id)
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
waiting_queue.push(:id => new_id, :packet => packet, :timestamp => Time.now)
|
59
|
-
end
|
60
|
-
rescue FullQueueException
|
61
|
-
PahoMqtt.logger.error("#{packet.type_name} queue is full") if PahoMqtt.logger?
|
62
|
-
sleep SELECT_TIMEOUT
|
63
|
-
retry
|
59
|
+
if waiting_queue.length >= max_packet
|
60
|
+
raise FullQueueException
|
61
|
+
end
|
62
|
+
queue_mutex.synchronize do
|
63
|
+
waiting_queue.push(:id => new_id, :packet => packet, :timestamp => Time.now)
|
64
64
|
end
|
65
65
|
MQTT_ERR_SUCCESS
|
66
66
|
end
|
@@ -98,7 +98,7 @@ module PahoMqtt
|
|
98
98
|
packet = PahoMqtt::Packet::Pubrec.new(
|
99
99
|
:id => packet_id
|
100
100
|
)
|
101
|
-
push_queue(@waiting_pubrel, @pubrel_mutex,
|
101
|
+
push_queue(@waiting_pubrel, @pubrel_mutex, MAX_QUEUE, packet, packet_id)
|
102
102
|
@sender.append_to_writing(packet)
|
103
103
|
MQTT_ERR_SUCCESS
|
104
104
|
end
|
@@ -114,7 +114,7 @@ module PahoMqtt
|
|
114
114
|
packet = PahoMqtt::Packet::Pubrel.new(
|
115
115
|
:id => packet_id
|
116
116
|
)
|
117
|
-
push_queue(@waiting_pubcomp, @pubcomp_mutex,
|
117
|
+
push_queue(@waiting_pubcomp, @pubcomp_mutex, MAX_QUEUE, packet, packet_id)
|
118
118
|
@sender.append_to_writing(packet)
|
119
119
|
MQTT_ERR_SUCCESS
|
120
120
|
end
|
data/lib/paho_mqtt/sender.rb
CHANGED
@@ -61,8 +61,7 @@ module PahoMqtt
|
|
61
61
|
|
62
62
|
def append_to_writing(packet)
|
63
63
|
begin
|
64
|
-
|
65
|
-
when packet.is_a?(PahoMqtt::Packet::Publish)
|
64
|
+
if packet.is_a?(PahoMqtt::Packet::Publish)
|
66
65
|
prepare_sending(@publish_queue, @publish_mutex, MAX_PUBLISH, packet)
|
67
66
|
else
|
68
67
|
prepare_sending(@writing_queue, @writing_mutex, MAX_QUEUE, packet)
|
@@ -95,13 +94,18 @@ module PahoMqtt
|
|
95
94
|
def flush_waiting_packet(sending=true)
|
96
95
|
if sending
|
97
96
|
@writing_mutex.synchronize do
|
98
|
-
@writing_queue.each do |
|
99
|
-
send_packet(
|
97
|
+
@writing_queue.each do |packet|
|
98
|
+
send_packet(packet)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
@publish_mutex.synchronize do
|
102
|
+
@publish_queue.each do |packet|
|
103
|
+
send_packet(packet)
|
100
104
|
end
|
101
105
|
end
|
102
|
-
else
|
103
|
-
@writing_queue = []
|
104
106
|
end
|
107
|
+
@writing_queue = []
|
108
|
+
@publish_queue = []
|
105
109
|
end
|
106
110
|
|
107
111
|
def check_ack_alive(queue, mutex)
|
@@ -110,7 +114,8 @@ module PahoMqtt
|
|
110
114
|
queue.each do |pck|
|
111
115
|
if now >= pck[:timestamp] + @ack_timeout
|
112
116
|
pck[:packet].dup ||= true unless pck[:packet].class == PahoMqtt::Packet::Subscribe || pck[:packet].class == PahoMqtt::Packet::Unsubscribe
|
113
|
-
|
117
|
+
PahoMqtt.logger.info("Acknowledgement timeout is over, resending #{pck[:packet].inspect}") if PahoMqtt.logger?
|
118
|
+
send_packet(pck[:packet])
|
114
119
|
pck[:timestamp] = now
|
115
120
|
end
|
116
121
|
end
|
data/lib/paho_mqtt/ssl_helper.rb
CHANGED
@@ -18,21 +18,40 @@ module PahoMqtt
|
|
18
18
|
module SSLHelper
|
19
19
|
extend self
|
20
20
|
|
21
|
-
def config_ssl_context(cert_path, key_path, ca_path=nil)
|
21
|
+
def config_ssl_context(cert_path=nil, key_path=nil, ca_path=nil)
|
22
22
|
ssl_context = OpenSSL::SSL::SSLContext.new
|
23
23
|
set_cert(cert_path, ssl_context)
|
24
24
|
set_key(key_path, ssl_context)
|
25
25
|
set_root_ca(ca_path, ssl_context)
|
26
|
-
#ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER unless ca_path.nil?
|
26
|
+
# ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER unless ca_path.nil?
|
27
27
|
ssl_context
|
28
28
|
end
|
29
29
|
|
30
|
-
def set_cert(cert_path, ssl_context)
|
31
|
-
|
30
|
+
def set_cert(cert_path=nil, ssl_context)
|
31
|
+
unless cert_path.nil?
|
32
|
+
ssl_context.cert = OpenSSL::X509::Certificate.new(File.read(cert_path))
|
33
|
+
end
|
32
34
|
end
|
33
35
|
|
34
|
-
def set_key(key_path, ssl_context)
|
35
|
-
|
36
|
+
def set_key(key_path=nil, ssl_context)
|
37
|
+
unless key_path.nil?
|
38
|
+
return MQTT_ERR_SUCCESS if try_rsa_key(key_path, ssl_context) == MQTT_ERR_SUCCESS
|
39
|
+
begin
|
40
|
+
ssl_context.key = OpenSSL::PKey::EC.new(File.read(key_path))
|
41
|
+
return MQTT_ERR_SUCCESS
|
42
|
+
rescue OpenSSL::PKey::ECError
|
43
|
+
raise NotSupportedEncryptionException.new("Could not support the type of the provided key (supported: RSA and EC)")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def try_rsa_key(key_path, ssl_context)
|
49
|
+
begin
|
50
|
+
ssl_context.key = OpenSSL::PKey::RSA.new(File.read(key_path))
|
51
|
+
return MQTT_ERR_SUCCESS
|
52
|
+
rescue OpenSSL::PKey::RSAError
|
53
|
+
return MQTT_ERR_FAIL
|
54
|
+
end
|
36
55
|
end
|
37
56
|
|
38
57
|
def set_root_ca(ca_path, ssl_context)
|
data/lib/paho_mqtt/subscriber.rb
CHANGED
@@ -47,7 +47,7 @@ module PahoMqtt
|
|
47
47
|
end
|
48
48
|
@waiting_suback.push(:id => new_id, :packet => packet, :timestamp => Time.now)
|
49
49
|
end
|
50
|
-
@sender.
|
50
|
+
@sender.append_to_writing(packet)
|
51
51
|
end
|
52
52
|
MQTT_ERR_SUCCESS
|
53
53
|
end
|
@@ -143,6 +143,10 @@ module PahoMqtt
|
|
143
143
|
@sender.check_ack_alive(@waiting_unsuback, @unsuback_mutex)
|
144
144
|
end
|
145
145
|
|
146
|
+
def clear_queue
|
147
|
+
@waiting_suback = []
|
148
|
+
end
|
149
|
+
|
146
150
|
def valid_topics?(topics)
|
147
151
|
unless topics.length == 0
|
148
152
|
topics.map do |topic|
|
data/lib/paho_mqtt/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paho-mqtt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pierre Goudet
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|