mqtt-sn-ruby 0.0.7 → 0.0.8
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-forwarder.rb +3 -5
- data/bin/mqtt-sn-pub.rb +5 -6
- data/bin/mqtt-sn-sub.rb +4 -6
- data/lib/mqtt-sn-ruby.rb +84 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc027ab8d8f4c272b23e33ac92f95fb5fdddab86
|
4
|
+
data.tar.gz: 776240a0b678bad8016f081fcb773d75b9f4ca38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3bbfea7e20154be77e89c484edfcbd4b561cf0ff4b54d6df29ba59611c81c640bdfb8810b49f9294339deeef4217d476bf5da2b89606836581fab2a0a5675afa
|
7
|
+
data.tar.gz: dc1f8be26135951a7295e587967013a53986979f4f83069c6611d6232c78e40c38000a8dec310cf1a1a2723e290c4214319aee3574e57fb089d9d0a9963f997c
|
data/bin/mqtt-sn-forwarder.rb
CHANGED
@@ -22,11 +22,9 @@ OptionParser.new do |opts|
|
|
22
22
|
opts.on("-d", "--[no-]debug", "Produce Debug dump on console (false)") do |v|
|
23
23
|
options[:debug] = v
|
24
24
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
opts.on("-p", "--port port", "MQTT-SN target Port to connect (1883)") do |v|
|
29
|
-
options[:port] = v.to_i
|
25
|
+
options[:server_uri] = "udp://localhost:1883"
|
26
|
+
opts.on("-s", "--server uri", "URI of the MQTT-SN Server to connect to (udp://localhost:1883)") do |v|
|
27
|
+
options[:server_uri] = v
|
30
28
|
end
|
31
29
|
opts.on("-i", "--localip ip", "MQTT-SN Local ip to bind (127.0.0.1)") do |v|
|
32
30
|
options[:local_ip] = v
|
data/bin/mqtt-sn-pub.rb
CHANGED
@@ -22,11 +22,9 @@ OptionParser.new do |opts|
|
|
22
22
|
opts.on("-d", "--[no-]debug", "Produce Debug dump on console (false)") do |v|
|
23
23
|
options[:debug] = v
|
24
24
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
opts.on("-p", "--port port", "MQTT-SN Port to connect (1883)") do |v|
|
29
|
-
options[:port] = v.to_i
|
25
|
+
options[:server_uri] = "udp://localhost:1883"
|
26
|
+
opts.on("-s", "--server uri", "URI of the MQTT-SN Server to connect to (udp://localhost:1883)") do |v|
|
27
|
+
options[:server_uri] = v
|
30
28
|
end
|
31
29
|
opts.on("-q", "--qos level", "QoS level (0)") do |v|
|
32
30
|
options[:qos] = v.to_i
|
@@ -55,8 +53,9 @@ rescue SystemExit, Interrupt
|
|
55
53
|
puts "\nExiting after Disconnect\n"
|
56
54
|
rescue => e
|
57
55
|
puts "\nError: '#{e}' -- Quit after Disconnect\n"
|
56
|
+
pp e.backtrace
|
58
57
|
end
|
59
|
-
sn.disconnect
|
58
|
+
sn.disconnect if sn
|
60
59
|
|
61
60
|
puts "MQTT-SN-PUB Done."
|
62
61
|
|
data/bin/mqtt-sn-sub.rb
CHANGED
@@ -22,11 +22,9 @@ OptionParser.new do |opts|
|
|
22
22
|
opts.on("-d", "--[no-]debug", "Produce Debug dump on console (false)") do |v|
|
23
23
|
options[:debug] = v
|
24
24
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
opts.on("-p", "--port port", "MQTT-SN Port to connect (1883)") do |v|
|
29
|
-
options[:port] = v.to_i
|
25
|
+
options[:server_uri] = "udp://localhost:1883"
|
26
|
+
opts.on("-s", "--server uri", "URI of the MQTT-SN Server to connect to (udp://localhost:1883)") do |v|
|
27
|
+
options[:server_uri] = v
|
30
28
|
end
|
31
29
|
opts.on("-q", "--qos level", "QoS level (0)") do |v|
|
32
30
|
options[:qos] = v.to_i
|
@@ -56,7 +54,7 @@ rescue SystemExit, Interrupt
|
|
56
54
|
rescue => e
|
57
55
|
puts "\nError: '#{e}' -- Quit after Disconnect\n"
|
58
56
|
end
|
59
|
-
sn.disconnect
|
57
|
+
sn.disconnect if sn
|
60
58
|
|
61
59
|
puts "MQTT-SN-SUB Done."
|
62
60
|
|
data/lib/mqtt-sn-ruby.rb
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
require "pp"
|
5
5
|
require 'socket'
|
6
6
|
require 'json'
|
7
|
+
require 'uri'
|
7
8
|
|
8
9
|
class MqttSN
|
9
10
|
|
@@ -12,6 +13,7 @@ class MqttSN
|
|
12
13
|
|
13
14
|
SEARCHGW_TYPE =0x01
|
14
15
|
GWINFO_TYPE =0x02
|
16
|
+
ADVERTISE_TYPE =0x03
|
15
17
|
CONNECT_TYPE =0x04
|
16
18
|
CONNACK_TYPE =0x05
|
17
19
|
WILLTOPICREQ_TYPE=0x06
|
@@ -47,10 +49,28 @@ class MqttSN
|
|
47
49
|
|
48
50
|
@@msg_id=1
|
49
51
|
|
52
|
+
def open_port uri_s
|
53
|
+
begin
|
54
|
+
uri = URI.parse(uri_s)
|
55
|
+
pp uri
|
56
|
+
if uri.scheme== 'udp'
|
57
|
+
@server=uri.host
|
58
|
+
@port=uri.port
|
59
|
+
puts "server: #{@server}:#{@port}"
|
60
|
+
return UDPSocket.new
|
61
|
+
else
|
62
|
+
raise "Error: Cannot open socket for '#{uri_s}', unsupported scheme: '#{uri.scheme}'"
|
63
|
+
end
|
64
|
+
rescue => e
|
65
|
+
pp e.backtrace
|
66
|
+
raise "Error: Cannot open socket for '#{uri_s}': #{e}"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
50
70
|
def initialize(hash={})
|
71
|
+
#BasicSocket.do_not_reverse_lookup = true
|
51
72
|
@options=hash #save these
|
52
|
-
@
|
53
|
-
@port=hash[:port]||1883
|
73
|
+
@server_uri=hash[:server_uri]||"udp://localhost:1883"
|
54
74
|
@debug=hash[:debug]
|
55
75
|
@verbose=hash[:verbose]
|
56
76
|
@state=:inited
|
@@ -62,10 +82,24 @@ class MqttSN
|
|
62
82
|
@topics={} #hash of registered topics is stored here
|
63
83
|
@iq = Queue.new
|
64
84
|
@dataq = Queue.new
|
65
|
-
@s =
|
85
|
+
@s = open_port @server_uri
|
86
|
+
pp @s
|
66
87
|
@t=Thread.new do
|
67
88
|
recv_thread
|
68
89
|
end
|
90
|
+
@bcast=nil
|
91
|
+
if true
|
92
|
+
begin
|
93
|
+
@bcast = UDPSocket.open
|
94
|
+
@bcast.bind('0.0.0.0', 1882)
|
95
|
+
@roam_t=Thread.new do
|
96
|
+
roam_thread
|
97
|
+
end
|
98
|
+
rescue => e
|
99
|
+
puts "Error: Could not open bcast port!"
|
100
|
+
@bcast=nil
|
101
|
+
end
|
102
|
+
end
|
69
103
|
end
|
70
104
|
|
71
105
|
def self.hexdump data
|
@@ -88,8 +122,6 @@ class MqttSN
|
|
88
122
|
msg
|
89
123
|
end
|
90
124
|
|
91
|
-
|
92
|
-
|
93
125
|
def send type,hash={},&block
|
94
126
|
#puts "" if @verbose
|
95
127
|
if @state!=:connected and type!=:connect and type!=:will_topic and type!=:will_msg
|
@@ -267,7 +299,6 @@ class MqttSN
|
|
267
299
|
_,port,_,_ = socket.addr
|
268
300
|
src=":#{port}"
|
269
301
|
printf "< %-18.18s <- %-18.18s | %s\n",dest,src,parse_message(msg).to_json
|
270
|
-
|
271
302
|
end
|
272
303
|
|
273
304
|
|
@@ -297,20 +328,34 @@ class MqttSN
|
|
297
328
|
@debug=hash[:debug]
|
298
329
|
@verbose=hash[:verbose]
|
299
330
|
|
331
|
+
socket_b = UDPSocket.new
|
332
|
+
socket_b.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
|
333
|
+
#socket_b.bind("0.0.0.0",1882)
|
334
|
+
|
300
335
|
socket = UDPSocket.new
|
301
336
|
socket.bind(hash[:local_ip]||"127.0.0.1",hash[:local_port]||0)
|
337
|
+
|
302
338
|
clients={}
|
303
339
|
begin
|
340
|
+
last=0
|
341
|
+
stime=Time.now.to_i
|
304
342
|
while true
|
343
|
+
now=Time.now.to_i
|
344
|
+
if now>last+10
|
345
|
+
MqttSN::send_packet [ADVERTISE_TYPE,0xAB,0,30],socket_b,"255.255.255.255",1882
|
346
|
+
last=now
|
347
|
+
end
|
348
|
+
#periodically kill disconnected clients -- and those timed out..
|
305
349
|
#periodically broadcast :advertize
|
306
350
|
if pac=poll_packet(socket)
|
307
351
|
r,client_ip,client_port=pac
|
308
352
|
key="#{client_ip}:#{client_port}"
|
309
353
|
if not clients[key]
|
310
|
-
clients[key]={ip:client_ip, port:client_port, socket: UDPSocket.new }
|
354
|
+
clients[key]={ip:client_ip, port:client_port, socket: UDPSocket.new, state: :active }
|
311
355
|
dest="#{client_ip}:#{client_port}"
|
312
356
|
printf "+ %s\n",dest
|
313
357
|
end
|
358
|
+
clients[key][:stamp]=Time.now.to_i
|
314
359
|
m=MqttSN::parse_message r
|
315
360
|
done=false
|
316
361
|
case m[:type]
|
@@ -341,6 +386,7 @@ class MqttSN
|
|
341
386
|
case m[:type]
|
342
387
|
when :disconnect
|
343
388
|
puts "disco -- kill me!"
|
389
|
+
clients[key][:state]=:disconnected
|
344
390
|
end
|
345
391
|
end
|
346
392
|
end
|
@@ -492,6 +538,7 @@ class MqttSN
|
|
492
538
|
end
|
493
539
|
type_byte=r[1].ord
|
494
540
|
done=false
|
541
|
+
|
495
542
|
case type_byte
|
496
543
|
when CONNECT_TYPE
|
497
544
|
duration=(r[4].ord<<8)+r[5].ord
|
@@ -555,9 +602,13 @@ class MqttSN
|
|
555
602
|
m={type: :searchgw, radius: r[2].ord, status: :ok}
|
556
603
|
when GWINFO_TYPE
|
557
604
|
m={type: :gwinfo, gw_id: r[2].ord, status: :ok}
|
605
|
+
when ADVERTISE_TYPE
|
606
|
+
duration=(r[3].ord<<8)+r[4].ord
|
607
|
+
m={type: :advertise, gw_id: r[2].ord, duration: duration, status: :ok}
|
558
608
|
|
559
609
|
when PINGRESP_TYPE
|
560
610
|
m={type: :pong, status: :ok}
|
611
|
+
|
561
612
|
else
|
562
613
|
m={type: :unknown, type_byte: type_byte }
|
563
614
|
end
|
@@ -594,6 +645,12 @@ class MqttSN
|
|
594
645
|
end
|
595
646
|
when :connect_ack
|
596
647
|
@state=:connected
|
648
|
+
when :advertise
|
649
|
+
puts "hey, we have router there!"
|
650
|
+
done=true
|
651
|
+
when :gwinfo
|
652
|
+
puts "hey, we have router there!"
|
653
|
+
#done=true
|
597
654
|
end
|
598
655
|
# puts "got :#{@id} #{m.to_json}" if @verbose
|
599
656
|
if not done
|
@@ -605,6 +662,20 @@ class MqttSN
|
|
605
662
|
def recv_thread
|
606
663
|
while true do
|
607
664
|
begin
|
665
|
+
if @bcast
|
666
|
+
if pac=MqttSN::poll_packet(@bcast)
|
667
|
+
r,client_ip,client_port=pac
|
668
|
+
m=MqttSN::parse_message r
|
669
|
+
if @debug and m
|
670
|
+
m[:debug]=MqttSN::hexdump r
|
671
|
+
end
|
672
|
+
dest="#{client_ip}:#{client_port}"
|
673
|
+
_,port,_,_ = @bcast.addr
|
674
|
+
src=port
|
675
|
+
printf "R %-18.18s <- %-18.18s | %s\n",dest,":#{port}",m.to_json
|
676
|
+
process_message m
|
677
|
+
end
|
678
|
+
end
|
608
679
|
if pac=MqttSN::poll_packet(@s)
|
609
680
|
r,client_ip,client_port=pac
|
610
681
|
m=MqttSN::parse_message r
|
@@ -616,7 +687,6 @@ class MqttSN
|
|
616
687
|
src=port
|
617
688
|
printf "> %-18.18s <- %-18.18s | %s\n",dest,":#{port}",m.to_json
|
618
689
|
process_message m
|
619
|
-
|
620
690
|
end
|
621
691
|
rescue => e
|
622
692
|
puts "Error: receive thread died: #{e}"
|
@@ -624,4 +694,10 @@ class MqttSN
|
|
624
694
|
end
|
625
695
|
end
|
626
696
|
end
|
697
|
+
|
698
|
+
def roam_thread
|
699
|
+
while true do
|
700
|
+
sleep 1
|
701
|
+
end
|
702
|
+
end
|
627
703
|
end
|