paho-mqtt 1.0.7 → 1.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -10
- data/lib/paho-mqtt.rb +44 -62
- data/lib/paho_mqtt/client.rb +51 -43
- data/lib/paho_mqtt/connection_helper.rb +15 -15
- data/lib/paho_mqtt/exception.rb +43 -0
- data/lib/paho_mqtt/handler.rb +21 -33
- data/lib/paho_mqtt/packet/base.rb +27 -19
- data/lib/paho_mqtt/packet/connack.rb +9 -7
- data/lib/paho_mqtt/packet/connect.rb +24 -19
- data/lib/paho_mqtt/packet/disconnect.rb +2 -1
- data/lib/paho_mqtt/packet/pingresp.rb +2 -1
- data/lib/paho_mqtt/packet/puback.rb +2 -1
- data/lib/paho_mqtt/packet/pubcomp.rb +2 -1
- data/lib/paho_mqtt/packet/publish.rb +15 -11
- data/lib/paho_mqtt/packet/pubrec.rb +2 -1
- data/lib/paho_mqtt/packet/pubrel.rb +2 -1
- data/lib/paho_mqtt/packet/suback.rb +6 -4
- data/lib/paho_mqtt/packet/subscribe.rb +10 -7
- data/lib/paho_mqtt/packet/unsuback.rb +2 -1
- data/lib/paho_mqtt/packet/unsubscribe.rb +7 -5
- data/lib/paho_mqtt/publisher.rb +73 -62
- data/lib/paho_mqtt/sender.rb +18 -22
- data/lib/paho_mqtt/subscriber.rb +52 -41
- data/lib/paho_mqtt/version.rb +1 -1
- metadata +3 -2
data/lib/paho_mqtt/sender.rb
CHANGED
@@ -16,13 +16,13 @@ module PahoMqtt
|
|
16
16
|
class Sender
|
17
17
|
|
18
18
|
attr_accessor :last_ping_req
|
19
|
-
|
19
|
+
|
20
20
|
def initialize(ack_timeout)
|
21
|
-
@socket
|
21
|
+
@socket = nil
|
22
22
|
@writing_queue = []
|
23
23
|
@writing_mutex = Mutex.new
|
24
24
|
@last_ping_req = -1
|
25
|
-
@ack_timeout
|
25
|
+
@ack_timeout = ack_timeout
|
26
26
|
end
|
27
27
|
|
28
28
|
def socket=(socket)
|
@@ -39,52 +39,48 @@ module PahoMqtt
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
def append_to_writing(packet)
|
43
|
-
@writing_mutex.synchronize
|
44
|
-
@writing_queue.push(packet)
|
45
|
-
|
42
|
+
def append_to_writing(packet)
|
43
|
+
@writing_mutex.synchronize do
|
44
|
+
@writing_queue.push(packet) unless @writing_queue.length >= MAX_WRITING
|
45
|
+
end
|
46
46
|
MQTT_ERR_SUCCESS
|
47
47
|
end
|
48
48
|
|
49
49
|
def writing_loop(max_packet)
|
50
|
-
@writing_mutex.synchronize
|
50
|
+
@writing_mutex.synchronize do
|
51
51
|
cnt = 0
|
52
52
|
while !@writing_queue.empty? && cnt < max_packet do
|
53
53
|
packet = @writing_queue.shift
|
54
54
|
send_packet(packet)
|
55
55
|
cnt += 1
|
56
56
|
end
|
57
|
-
|
57
|
+
end
|
58
58
|
MQTT_ERR_SUCCESS
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
def flush_waiting_packet(sending=true)
|
62
62
|
if sending
|
63
|
-
@writing_mutex.synchronize
|
63
|
+
@writing_mutex.synchronize do
|
64
64
|
@writing_queue.each do |m|
|
65
65
|
send_packet(m)
|
66
66
|
end
|
67
|
-
|
67
|
+
end
|
68
68
|
else
|
69
69
|
@writing_queue = []
|
70
70
|
end
|
71
71
|
end
|
72
|
-
|
73
|
-
def check_ack_alive(queue, mutex
|
74
|
-
mutex.synchronize
|
72
|
+
|
73
|
+
def check_ack_alive(queue, mutex)
|
74
|
+
mutex.synchronize do
|
75
75
|
now = Time.now
|
76
|
-
cnt = 0
|
77
76
|
queue.each do |pck|
|
78
77
|
if now >= pck[:timestamp] + @ack_timeout
|
79
78
|
pck[:packet].dup ||= true unless pck[:packet].class == PahoMqtt::Packet::Subscribe || pck[:packet].class == PahoMqtt::Packet::Unsubscribe
|
80
|
-
|
81
|
-
|
82
|
-
pck[:timestamp] = now
|
83
|
-
cnt += 1
|
84
|
-
end
|
79
|
+
append_to_writing(pck[:packet])
|
80
|
+
pck[:timestamp] = now
|
85
81
|
end
|
86
82
|
end
|
87
|
-
|
83
|
+
end
|
88
84
|
end
|
89
85
|
end
|
90
86
|
end
|
data/lib/paho_mqtt/subscriber.rb
CHANGED
@@ -18,13 +18,13 @@ module PahoMqtt
|
|
18
18
|
attr_reader :subscribed_topics
|
19
19
|
|
20
20
|
def initialize(sender)
|
21
|
-
@waiting_suback
|
22
|
-
@waiting_unsuback
|
23
|
-
@subscribed_mutex
|
21
|
+
@waiting_suback = []
|
22
|
+
@waiting_unsuback = []
|
23
|
+
@subscribed_mutex = Mutex.new
|
24
24
|
@subscribed_topics = []
|
25
|
-
@suback_mutex
|
26
|
-
@unsuback_mutex
|
27
|
-
@sender
|
25
|
+
@suback_mutex = Mutex.new
|
26
|
+
@unsuback_mutex = Mutex.new
|
27
|
+
@sender = sender
|
28
28
|
end
|
29
29
|
|
30
30
|
def sender=(sender)
|
@@ -34,24 +34,28 @@ module PahoMqtt
|
|
34
34
|
def config_subscription(new_id)
|
35
35
|
unless @subscribed_topics == [] || @subscribed_topics.nil?
|
36
36
|
packet = PahoMqtt::Packet::Subscribe.new(
|
37
|
-
:id
|
37
|
+
:id => new_id,
|
38
38
|
:topics => @subscribed_topics
|
39
39
|
)
|
40
|
-
@subscribed_mutex.synchronize
|
40
|
+
@subscribed_mutex.synchronize do
|
41
41
|
@subscribed_topics = []
|
42
|
-
|
43
|
-
@suback_mutex.synchronize
|
44
|
-
@waiting_suback.
|
45
|
-
|
42
|
+
end
|
43
|
+
@suback_mutex.synchronize do
|
44
|
+
if @waiting_suback.length >= MAX_SUBACK
|
45
|
+
PahoMqtt.logger.error('SUBACK queue is full, could not send subscribe') if PahoMqtt.logger?
|
46
|
+
return MQTT_ERR_FAILURE
|
47
|
+
end
|
48
|
+
@waiting_suback.push(:id => new_id, :packet => packet, :timestamp => Time.now)
|
49
|
+
end
|
46
50
|
@sender.send_packet(packet)
|
47
51
|
end
|
48
52
|
MQTT_ERR_SUCCESS
|
49
53
|
end
|
50
54
|
|
51
55
|
def add_subscription(max_qos, packet_id, adjust_qos)
|
52
|
-
@suback_mutex.synchronize
|
56
|
+
@suback_mutex.synchronize do
|
53
57
|
adjust_qos, @waiting_suback = @waiting_suback.partition { |pck| pck[:id] == packet_id }
|
54
|
-
|
58
|
+
end
|
55
59
|
if adjust_qos.length == 1
|
56
60
|
adjust_qos = adjust_qos.first[:packet].topics
|
57
61
|
adjust_qos.each do |t|
|
@@ -60,51 +64,54 @@ module PahoMqtt
|
|
60
64
|
elsif max_qos[0] == 128
|
61
65
|
adjust_qos.delete(t)
|
62
66
|
else
|
63
|
-
|
64
|
-
|
65
|
-
raise PacketException
|
67
|
+
PahoMqtt.logger.error("The QoS value is invalid in subscribe.") if PahoMqtt.logger?
|
68
|
+
raise PacketException.new('Invalid suback QoS value')
|
66
69
|
end
|
67
70
|
end
|
68
71
|
else
|
69
|
-
|
70
|
-
raise PacketException
|
72
|
+
PahoMqtt.logger.error("The packet id is invalid, already used.") if PahoMqtt.logger?
|
73
|
+
raise PacketException.new("Invalid suback packet id: #{packet_id}")
|
71
74
|
end
|
72
|
-
@subscribed_mutex.synchronize
|
75
|
+
@subscribed_mutex.synchronize do
|
73
76
|
@subscribed_topics.concat(adjust_qos)
|
74
|
-
|
77
|
+
end
|
75
78
|
return adjust_qos
|
76
79
|
end
|
77
80
|
|
78
81
|
def remove_subscription(packet_id, to_unsub)
|
79
|
-
@unsuback_mutex.synchronize
|
82
|
+
@unsuback_mutex.synchronize do
|
80
83
|
to_unsub, @waiting_unsuback = @waiting_unsuback.partition { |pck| pck[:id] == packet_id }
|
81
|
-
|
82
|
-
|
84
|
+
end
|
85
|
+
|
83
86
|
if to_unsub.length == 1
|
84
87
|
to_unsub = to_unsub.first[:packet].topics
|
85
88
|
else
|
86
|
-
|
87
|
-
raise PacketException
|
89
|
+
PahoMqtt.logger.error("The packet id is invalid, already used.") if PahoMqtt.logger?
|
90
|
+
raise PacketException.new("Invalid unsuback packet id: #{packet_id}")
|
88
91
|
end
|
89
92
|
|
90
|
-
@subscribed_mutex.synchronize
|
93
|
+
@subscribed_mutex.synchronize do
|
91
94
|
to_unsub.each do |filter|
|
92
95
|
@subscribed_topics.delete_if { |topic| PahoMqtt.match_filter(topic.first, filter) }
|
93
96
|
end
|
94
|
-
|
97
|
+
end
|
95
98
|
return to_unsub
|
96
99
|
end
|
97
|
-
|
100
|
+
|
98
101
|
def send_subscribe(topics, new_id)
|
99
102
|
unless valid_topics?(topics) == MQTT_ERR_FAIL
|
100
103
|
packet = PahoMqtt::Packet::Subscribe.new(
|
101
|
-
:id
|
104
|
+
:id => new_id,
|
102
105
|
:topics => topics
|
103
|
-
)
|
106
|
+
)
|
104
107
|
@sender.append_to_writing(packet)
|
105
|
-
@suback_mutex.synchronize
|
106
|
-
@waiting_suback.
|
107
|
-
|
108
|
+
@suback_mutex.synchronize do
|
109
|
+
if @waiting_suback.length >= MAX_SUBACK
|
110
|
+
PahoMqtt.logger.error('SUBACK queue is full, could not send subscribe') if PahoMqtt.logger?
|
111
|
+
return MQTT_ERR_FAILURE
|
112
|
+
end
|
113
|
+
@waiting_suback.push(:id => new_id, :packet => packet, :timestamp => Time.now)
|
114
|
+
end
|
108
115
|
MQTT_ERR_SUCCESS
|
109
116
|
else
|
110
117
|
raise ProtocolViolation
|
@@ -114,14 +121,18 @@ module PahoMqtt
|
|
114
121
|
def send_unsubscribe(topics, new_id)
|
115
122
|
unless valid_topics?(topics) == MQTT_ERR_FAIL
|
116
123
|
packet = PahoMqtt::Packet::Unsubscribe.new(
|
117
|
-
:id
|
124
|
+
:id => new_id,
|
118
125
|
:topics => topics
|
119
126
|
)
|
120
|
-
|
127
|
+
|
121
128
|
@sender.append_to_writing(packet)
|
122
|
-
@unsuback_mutex.synchronize
|
123
|
-
@
|
124
|
-
|
129
|
+
@unsuback_mutex.synchronize do
|
130
|
+
if @waiting_suback.length >= MAX_UNSUBACK
|
131
|
+
PahoMqtt.logger.error('UNSUBACK queue is full, could not send unbsubscribe') if PahoMqtt.logger?
|
132
|
+
return MQTT_ERR_FAIL
|
133
|
+
end
|
134
|
+
@waiting_unsuback.push(:id => new_id, :packet => packet, :timestamp => Time.now)
|
135
|
+
end
|
125
136
|
MQTT_ERR_SUCCESS
|
126
137
|
else
|
127
138
|
raise ProtocolViolation
|
@@ -129,8 +140,8 @@ module PahoMqtt
|
|
129
140
|
end
|
130
141
|
|
131
142
|
def check_waiting_subscriber
|
132
|
-
@sender.check_ack_alive(@waiting_suback, @suback_mutex
|
133
|
-
@sender.check_ack_alive(@waiting_unsuback, @unsuback_mutex
|
143
|
+
@sender.check_ack_alive(@waiting_suback, @suback_mutex)
|
144
|
+
@sender.check_ack_alive(@waiting_unsuback, @unsuback_mutex)
|
134
145
|
end
|
135
146
|
|
136
147
|
def valid_topics?(topics)
|
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.9
|
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-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/paho-mqtt.rb
|
72
72
|
- lib/paho_mqtt/client.rb
|
73
73
|
- lib/paho_mqtt/connection_helper.rb
|
74
|
+
- lib/paho_mqtt/exception.rb
|
74
75
|
- lib/paho_mqtt/handler.rb
|
75
76
|
- lib/paho_mqtt/packet.rb
|
76
77
|
- lib/paho_mqtt/packet/base.rb
|