mqtt-sn-ruby 0.0.13 → 0.0.14
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/bin/mqtt-sn-pub.rb +1 -0
- data/bin/mqtt-sn-sub.rb +3 -1
- data/lib/mqtt-sn-ruby.rb +57 -32
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e495c4d45fd7179f188f83894e048a83432cfd6
|
4
|
+
data.tar.gz: 90152bb81bdca776b0b74001bf11f3423cb89c5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ee91fee12ff5a12dd11e64dd9f8416274ef6f0496de213a3370a55d09e1111c48a5f49bca1145841a75c8727a21ec5f3a772b9314e2c9e271bdf08fcf1be81e
|
7
|
+
data.tar.gz: 40c2d2404d968cc3d5c2c84e541dbfa4656f35779824488de94769440e1a6cca8f17165a8a78197738b121193ef58b9390ec6f2117ee04a2c8884012a647f3b6
|
data/bin/mqtt-sn-pub.rb
CHANGED
data/bin/mqtt-sn-sub.rb
CHANGED
@@ -37,7 +37,9 @@ OptionParser.new do |opts|
|
|
37
37
|
opts.on("-h", "--http port", "Http port for debug/status JSON server (false)") do |v|
|
38
38
|
options[:http_port] = v.to_i
|
39
39
|
end
|
40
|
-
|
40
|
+
opts.on("-k", "--keepalive dur", "Keepalive timer, in seconds. Will ping with this interval. (25)") do |topic|
|
41
|
+
options[:keepalive] = topic
|
42
|
+
end
|
41
43
|
end.parse!
|
42
44
|
|
43
45
|
$sn=MqttSN.new options
|
data/lib/mqtt-sn-ruby.rb
CHANGED
@@ -103,19 +103,22 @@ class MqttSN
|
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
|
-
def
|
107
|
-
|
106
|
+
def open_multicast_send_port
|
107
|
+
uri = URI.parse(@broadcast_uri)
|
108
|
+
|
109
|
+
ip = IPAddr.new(uri.host).hton + IPAddr.new("0.0.0.0").hton
|
108
110
|
socket_b = UDPSocket.new
|
109
111
|
socket_b.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, [1].pack('i'))
|
110
112
|
socket_b
|
111
113
|
end
|
112
114
|
|
113
|
-
def
|
114
|
-
|
115
|
+
def open_multicast_recv_port
|
116
|
+
uri = URI.parse(@broadcast_uri)
|
117
|
+
ip = IPAddr.new(uri.host).hton + IPAddr.new("0.0.0.0").hton
|
115
118
|
s = UDPSocket.new
|
116
119
|
s.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, ip)
|
117
120
|
s.setsockopt(:SOL_SOCKET, :SO_REUSEPORT, 1)
|
118
|
-
s.bind(Socket::INADDR_ANY, port)
|
121
|
+
s.bind(Socket::INADDR_ANY, uri.port)
|
119
122
|
s
|
120
123
|
end
|
121
124
|
|
@@ -131,6 +134,7 @@ class MqttSN
|
|
131
134
|
@verbose=hash[:verbose]
|
132
135
|
@state=:inited
|
133
136
|
@bcast_port=5000
|
137
|
+
@keepalive=hash[:keepalive]||25
|
134
138
|
@forwarder=hash[:forwarder] #flag to indicate forward mode
|
135
139
|
@will_topic=nil
|
136
140
|
@will_msg=nil
|
@@ -142,6 +146,7 @@ class MqttSN
|
|
142
146
|
@gateways={}
|
143
147
|
@autodiscovery=false
|
144
148
|
@broadcast_uri="udp://225.4.5.6:5000"
|
149
|
+
|
145
150
|
if @server_uri
|
146
151
|
puts "adding default gateway #{@server_uri}"
|
147
152
|
@gateways[0]={stamp: Time.now.to_i,uri: @server_uri, duration: 0, source: 'default', status: :ok}
|
@@ -163,8 +168,8 @@ class MqttSN
|
|
163
168
|
@iq = Queue.new
|
164
169
|
@dataq = Queue.new
|
165
170
|
|
166
|
-
@bcast_s=
|
167
|
-
@bcast=
|
171
|
+
@bcast_s=open_multicast_send_port
|
172
|
+
@bcast=open_multicast_recv_port
|
168
173
|
|
169
174
|
@roam_t=Thread.new do
|
170
175
|
roam_thread @bcast
|
@@ -176,9 +181,7 @@ class MqttSN
|
|
176
181
|
@s.bind("0.0.0.0",@local_port)
|
177
182
|
@bcast_period=60
|
178
183
|
else
|
179
|
-
|
180
|
-
client_thread
|
181
|
-
end
|
184
|
+
client_thread
|
182
185
|
end
|
183
186
|
end
|
184
187
|
|
@@ -223,7 +226,7 @@ class MqttSN
|
|
223
226
|
mm=MqttSN::parse_message rr
|
224
227
|
_,port,_,_ = @clients[my_key][:socket].addr
|
225
228
|
dest="#{@server}:#{port}"
|
226
|
-
logger "
|
229
|
+
logger "sc %-24.24s <- %-24.24s | %s",@clients[my_key][:uri],@gateways[@active_gw_id][:uri],mm.to_json
|
227
230
|
case mm[:type]
|
228
231
|
when :disconnect
|
229
232
|
@clients[my_key][:state]=:disconnected
|
@@ -239,7 +242,7 @@ class MqttSN
|
|
239
242
|
sbytes=@clients[key][:socket].send(r, 0, @server, @port) # to rsmb -- ok as is
|
240
243
|
_,port,_,_ = @clients[key][:socket].addr
|
241
244
|
dest="#{@server}:#{port}"
|
242
|
-
logger "
|
245
|
+
logger "cs %-24.24s -> %-24.24s | %s", @clients[key][:uri],@gateways[@active_gw_id][:uri],m.to_json
|
243
246
|
|
244
247
|
end
|
245
248
|
end
|
@@ -552,8 +555,8 @@ class MqttSN
|
|
552
555
|
@will_msg=msg
|
553
556
|
end
|
554
557
|
|
555
|
-
def connect id
|
556
|
-
send :connect,id: id, clean: false, duration:
|
558
|
+
def connect id
|
559
|
+
send :connect,id: id, clean: false, duration: @keepalive, expect: [:connect_ack,:will_topic_req] do |s,m| #add will here!
|
557
560
|
if s==:ok
|
558
561
|
if m[:type]==:will_topic_req
|
559
562
|
puts "will topic!"
|
@@ -738,6 +741,8 @@ class MqttSN
|
|
738
741
|
duration=(r[3].ord<<8)+r[4].ord
|
739
742
|
m={type: :advertise, gw_id: r[2].ord, duration: duration, status: :ok}
|
740
743
|
|
744
|
+
when PINGREQ_TYPE
|
745
|
+
m={type: :ping, status: :ok}
|
741
746
|
when PINGRESP_TYPE
|
742
747
|
m={type: :pong, status: :ok}
|
743
748
|
|
@@ -792,26 +797,46 @@ class MqttSN
|
|
792
797
|
end
|
793
798
|
|
794
799
|
def client_thread
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
800
|
+
Thread.new do #ping thread
|
801
|
+
while true do
|
802
|
+
begin
|
803
|
+
sleep @keepalive
|
804
|
+
if @active_gw_id and @gateways[@active_gw_id] and @gateways[@active_gw_id][:socket] #if we are connected...
|
805
|
+
send :ping, timeout: 2,expect: :pong do |status,message|
|
806
|
+
if status!=:ok
|
807
|
+
note "Error:#{@id} no pong! -- need to switch gateway & restart comms"
|
808
|
+
end
|
809
|
+
end
|
803
810
|
end
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
+
rescue => e
|
812
|
+
puts "Error: receive thread died: #{e}"
|
813
|
+
pp e.backtrace
|
814
|
+
end
|
815
|
+
end
|
816
|
+
end
|
817
|
+
|
818
|
+
Thread.new do #work thread
|
819
|
+
while true do
|
820
|
+
begin
|
821
|
+
if @active_gw_id and @gateways[@active_gw_id] and @gateways[@active_gw_id][:socket] #if we are connected...
|
822
|
+
pac=MqttSN::poll_packet_block(@gateways[@active_gw_id][:socket])
|
823
|
+
r,client_ip,client_port=pac
|
824
|
+
m=MqttSN::parse_message r
|
825
|
+
if @debug and m
|
826
|
+
m[:debug]=MqttSN::hexdump r
|
827
|
+
end
|
828
|
+
dest="#{client_ip}:#{client_port}"
|
829
|
+
_,port,_,_= @gateways[@active_gw_id][:socket].addr
|
830
|
+
src="udp://0.0.0.0:#{port}"
|
831
|
+
logger "id %-24.24s -> %-24.24s | %s",@gateways[@active_gw_id][:uri],src,m.to_json
|
832
|
+
process_message m
|
833
|
+
else
|
834
|
+
sleep 0.01
|
835
|
+
end
|
836
|
+
rescue => e
|
837
|
+
puts "Error: receive thread died: #{e}"
|
838
|
+
pp e.backtrace
|
811
839
|
end
|
812
|
-
rescue => e
|
813
|
-
puts "Error: receive thread died: #{e}"
|
814
|
-
pp e.backtrace
|
815
840
|
end
|
816
841
|
end
|
817
842
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mqtt-sn-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ari Siitonen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby toolkit for MQTT-SN, compatible with RSMB, command line tools and
|
14
14
|
API
|