mqtt-sn-ruby 0.0.5 → 0.0.6
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 +9 -3
- data/lib/mqtt-sn-ruby.rb +43 -33
- 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: e34953b26fefce4f5e92f3d3b5ce9761fedac071
|
4
|
+
data.tar.gz: 97786a47fa00c49c902edc99fc30072aa00fbc03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e459aca2a8410edadf97935beec968a7fc59c9a9ee0275b05122a71d2c3ffbbf3e5db328528d1ce43c530d85df1f7b3777ee9051f85fae0fb83d8e52a595180
|
7
|
+
data.tar.gz: 934ebee4d63c118429b5b257d2698efd36761eb7a6cff7675b4b360bfd8a8b36e3e241b4ceae8b16bf8bb2df87faa8e30ed97d8119d89bf77b2c5adba31fc944
|
data/bin/mqtt-sn-forwarder.rb
CHANGED
@@ -22,17 +22,23 @@ 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
|
-
opts.on("-h", "--host host", "MQTT-SN Host to connect (localhost)") do |v|
|
25
|
+
opts.on("-h", "--host host", "MQTT-SN target Host to connect (localhost)") do |v|
|
26
26
|
options[:server] = v
|
27
27
|
end
|
28
|
-
opts.on("-p", "--port port", "MQTT-SN Port to connect (1883)") do |v|
|
28
|
+
opts.on("-p", "--port port", "MQTT-SN target Port to connect (1883)") do |v|
|
29
29
|
options[:port] = v.to_i
|
30
30
|
end
|
31
|
+
opts.on("-i", "--localip ip", "MQTT-SN Local ip to bind (127.0.0.1)") do |v|
|
32
|
+
options[:local_ip] = v
|
33
|
+
end
|
34
|
+
opts.on("-l", "--localport port", "MQTT-SN local port to listen (1882)") do |v|
|
35
|
+
options[:local_port] = v.to_i
|
36
|
+
end
|
31
37
|
end.parse!
|
32
38
|
|
33
39
|
puts "MQTT-SN-FORWARDER: #{options.to_json}"
|
34
40
|
begin
|
35
|
-
MqttSN.forwarder
|
41
|
+
MqttSN.forwarder options
|
36
42
|
rescue SystemExit, Interrupt
|
37
43
|
puts "\nExiting after Disconnect\n"
|
38
44
|
rescue => e
|
data/lib/mqtt-sn-ruby.rb
CHANGED
@@ -256,7 +256,7 @@ class MqttSN
|
|
256
256
|
|
257
257
|
end
|
258
258
|
|
259
|
-
def self.forwarder
|
259
|
+
def self.forwarder hash={}
|
260
260
|
@options=hash #save these
|
261
261
|
@server=hash[:server]||"127.0.0.1"
|
262
262
|
@port=hash[:port]||1883
|
@@ -264,8 +264,7 @@ class MqttSN
|
|
264
264
|
@verbose=hash[:verbose]
|
265
265
|
|
266
266
|
socket = UDPSocket.new
|
267
|
-
socket.bind(
|
268
|
-
puts "listening #{listen_ip}:#{listen_port}"
|
267
|
+
socket.bind(hash[:local_ip]||"127.0.0.1",hash[:local_port]||0)
|
269
268
|
clients={}
|
270
269
|
begin
|
271
270
|
while true
|
@@ -279,8 +278,8 @@ class MqttSN
|
|
279
278
|
clients[key]={ip:client_ip, port:client_port, socket: UDPSocket.new }
|
280
279
|
end
|
281
280
|
clients[key][:socket].send(r, 0, @server, @port)
|
282
|
-
|
283
|
-
|
281
|
+
m=MqttSN::parse_message r
|
282
|
+
puts ">#{m.to_json}"
|
284
283
|
rescue IO::WaitReadable
|
285
284
|
rescue => e
|
286
285
|
puts "Error: receive thread died: #{e}"
|
@@ -292,7 +291,8 @@ class MqttSN
|
|
292
291
|
puts "got packet from server to client #{key}:"
|
293
292
|
puts "sending to #{c[:ip]}:#{c[:port]}"
|
294
293
|
socket.send(r, 0, c[:ip], c[:port])
|
295
|
-
|
294
|
+
m=MqttSN::parse_message r
|
295
|
+
puts "<#{m.to_json}"
|
296
296
|
rescue IO::WaitReadable
|
297
297
|
rescue => e
|
298
298
|
puts "Error: receive thread died: #{e}"
|
@@ -355,7 +355,6 @@ class MqttSN
|
|
355
355
|
send :disconnect, duration: duration, expect: :disconnect do |status,message|
|
356
356
|
end
|
357
357
|
end
|
358
|
-
|
359
358
|
|
360
359
|
def subscribe topic,hash={},&block
|
361
360
|
send :subscribe, topic: topic, qos: hash[:qos],expect: :sub_ack do |s,m|
|
@@ -431,7 +430,7 @@ class MqttSN
|
|
431
430
|
end
|
432
431
|
end
|
433
432
|
|
434
|
-
def
|
433
|
+
def self.parse_message r
|
435
434
|
m=nil
|
436
435
|
len=r[0].ord
|
437
436
|
case r[len-1].ord
|
@@ -451,7 +450,6 @@ class MqttSN
|
|
451
450
|
case type_byte
|
452
451
|
when CONNACK_TYPE
|
453
452
|
m={type: :connect_ack,status: status}
|
454
|
-
@state=:connected
|
455
453
|
when SUBACK_TYPE
|
456
454
|
topic_id=(r[3].ord<<8)+r[4].ord
|
457
455
|
msg_id=(r[5].ord<<8)+r[6].ord
|
@@ -465,37 +463,17 @@ class MqttSN
|
|
465
463
|
msg=r[7,len-7]
|
466
464
|
flags=r[2].ord
|
467
465
|
qos=(flags>>5)&0x03
|
468
|
-
|
469
|
-
m={type: :publish, qos: qos, topic_id: topic_id, topic: topic,msg_id: msg_id, msg: msg,status: :ok}
|
470
|
-
@dataq<<m
|
471
|
-
if not @transfer
|
472
|
-
if qos==1
|
473
|
-
send :publish_ack,topic_id: topic_id, msg_id: msg_id, return_code: 0
|
474
|
-
elsif qos==2
|
475
|
-
send :pub_rec, msg_id: msg_id
|
476
|
-
end
|
477
|
-
done=true
|
478
|
-
end
|
466
|
+
m={type: :publish, qos: qos, topic_id: topic_id,msg_id: msg_id, msg: msg,status: :ok}
|
479
467
|
when PUBREL_TYPE
|
480
468
|
msg_id=(r[2].ord<<8)+r[3].ord
|
481
|
-
m={type: :pub_rel, status: :ok}
|
482
|
-
if not @transfer
|
483
|
-
send :pub_comp, msg_id: msg_id
|
484
|
-
done=true
|
485
|
-
end
|
469
|
+
m={type: :pub_rel, msg_id: msg_id, status: :ok}
|
486
470
|
when DISCONNECT_TYPE
|
487
471
|
m={type: :disconnect,status: :ok}
|
488
|
-
@state=:disconnected if not @transfer
|
489
472
|
when REGISTER_TYPE
|
490
473
|
topic_id=(r[2].ord<<8)+r[3].ord
|
491
474
|
msg_id=(r[4].ord<<8)+r[5].ord
|
492
475
|
topic=r[6,len-6]
|
493
476
|
m={type: :register, topic_id: topic_id, msg_id: msg_id, topic: topic,status: :ok}
|
494
|
-
@topics[topic]=m[:topic_id]
|
495
|
-
if not @transfer
|
496
|
-
send :register_ack,topic_id: topic_id, msg_id: msg_id, return_code: 0
|
497
|
-
done=true
|
498
|
-
end
|
499
477
|
when REGACK_TYPE
|
500
478
|
topic_id=(r[2].ord<<8)+r[3].ord
|
501
479
|
m={type: :register_ack,topic_id: topic_id,status: status}
|
@@ -525,11 +503,43 @@ class MqttSN
|
|
525
503
|
else
|
526
504
|
m={type: :unknown, type_byte: type_byte }
|
527
505
|
end
|
506
|
+
m
|
507
|
+
end
|
508
|
+
|
509
|
+
def process_message r
|
510
|
+
m=MqttSN::parse_message r
|
528
511
|
if @debug and m
|
529
512
|
m[:debug]=hexdump r
|
530
513
|
end
|
531
|
-
|
532
|
-
|
514
|
+
done=false
|
515
|
+
case m[:type]
|
516
|
+
when :register
|
517
|
+
@topics[m[:topic]]=m[:topic_id]
|
518
|
+
if not @transfer
|
519
|
+
send :register_ack,topic_id: m[:topic_id], msg_id: m[:msg_id], return_code: 0
|
520
|
+
done=true
|
521
|
+
end
|
522
|
+
when :disconnect
|
523
|
+
@state=:disconnected if not @transfer
|
524
|
+
when :pub_rel
|
525
|
+
if not @transfer
|
526
|
+
send :pub_comp, msg_id: m[:msg_id]
|
527
|
+
done=true
|
528
|
+
end
|
529
|
+
when :publish
|
530
|
+
m[:topic]=@topics.key(m[:topic_id])
|
531
|
+
|
532
|
+
if not @transfer
|
533
|
+
@dataq<<m
|
534
|
+
if m[:qos]==1
|
535
|
+
send :publish_ack,topic_id: m[:topic_id], msg_id: m[:msg_id], return_code: 0
|
536
|
+
elsif m[:qos]==2
|
537
|
+
send :pub_rec, msg_id: m[:msg_id]
|
538
|
+
end
|
539
|
+
done=true
|
540
|
+
end
|
541
|
+
when :connect_ack
|
542
|
+
@state=:connected
|
533
543
|
end
|
534
544
|
puts "got :#{@id} #{m.to_json}" if @verbose
|
535
545
|
if not done
|