mqtt-sn-ruby 0.0.14 → 0.0.15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e495c4d45fd7179f188f83894e048a83432cfd6
4
- data.tar.gz: 90152bb81bdca776b0b74001bf11f3423cb89c5d
3
+ metadata.gz: c5ba209ea7715436c359933ae41662a04868ad43
4
+ data.tar.gz: c326fc2d3ac00c24bb8df4c4b0ed34cdf44bfe70
5
5
  SHA512:
6
- metadata.gz: 4ee91fee12ff5a12dd11e64dd9f8416274ef6f0496de213a3370a55d09e1111c48a5f49bca1145841a75c8727a21ec5f3a772b9314e2c9e271bdf08fcf1be81e
7
- data.tar.gz: 40c2d2404d968cc3d5c2c84e541dbfa4656f35779824488de94769440e1a6cca8f17165a8a78197738b121193ef58b9390ec6f2117ee04a2c8884012a647f3b6
6
+ metadata.gz: fd5d904ad1a174fb9d46d53d8c1281583659efccd23cf8c153ce22579dd3d005de21470bf86b17b077bfd59bc9b2780cad8236685eb1b393399f97dfd7f12b33
7
+ data.tar.gz: 01612dd211b3e2b2305433e29454d92c703cffe194b5ff7ed8264a773aab517048e8afbeb55a74d91091dd054fd844b6ea5163a37c2c187fa7cccfcf26efa3c8
data/bin/mqtt-sn-pub.rb CHANGED
@@ -43,9 +43,18 @@ end.parse!
43
43
  puts "MQTT-SN-PUB: #{options.to_json}"
44
44
  begin
45
45
  sn=MqttSN.new options
46
- sn.connect options[:id]
47
- sn.publish options[:topic]||"test/message/123", options[:msg]||"test_value", qos: options[:qos]
48
- puts "Sent ok."
46
+ sent=false
47
+ while not sent
48
+ sn.connect options[:id] do |s,m|
49
+ if s==:ok
50
+ sn.publish options[:topic]||"test/message/123", options[:msg]||"test_value", qos: options[:qos]
51
+ puts "Sent ok."
52
+ sent=true
53
+ else
54
+ sn.disconnect
55
+ end
56
+ end
57
+ end
49
58
  sn.log_flush
50
59
  rescue SystemExit, Interrupt
51
60
  puts "\nExiting after Disconnect\n"
data/bin/mqtt-sn-sub.rb CHANGED
@@ -77,16 +77,23 @@ end
77
77
 
78
78
  puts "MQTT-SN-SUB: #{options.to_json}"
79
79
  begin
80
-
81
- $sn.connect options[:id]
82
- $sn.subscribe options[:topic]||"test/message/123", qos: options[:qos] do |s,m|
83
- if s==:sub_ack
84
- puts "Subscribed Ok! Waiting for Messages!"
85
- else
86
- puts "Got Message: #{m}"
80
+ loop do
81
+ puts "Connecting.."
82
+ $sn.connect options[:id] do |cs,cm|
83
+ puts "connect result: #{cs} #{cm}"
84
+ if cs==:ok
85
+ puts "Subscribing.."
86
+ $sn.subscribe options[:topic]||"test/message/123", qos: options[:qos] do |s,m|
87
+ if s==:sub_ack
88
+ puts "Subscribed Ok! Waiting for Messages!"
89
+ else
90
+ puts "Got Message: #{m}"
91
+ end
92
+ end
93
+ end
87
94
  end
95
+ puts "Disconnected..."
88
96
  end
89
- puts "Disconnected..."
90
97
  rescue SystemExit, Interrupt
91
98
  puts "\nExiting after Disconnect\n"
92
99
  rescue => e
data/lib/mqtt-sn-ruby.rb CHANGED
@@ -141,6 +141,8 @@ class MqttSN
141
141
  @active_gw_id=nil
142
142
  @id="?"
143
143
 
144
+ @sem=Mutex.new
145
+ @gsem=Mutex.new
144
146
  @msg_id=1
145
147
  @clients={}
146
148
  @gateways={}
@@ -163,7 +165,6 @@ class MqttSN
163
165
  log_thread
164
166
  end
165
167
 
166
- @sem=Mutex.new
167
168
  @topics={} #hash of registered topics is stored here
168
169
  @iq = Queue.new
169
170
  @dataq = Queue.new
@@ -242,8 +243,11 @@ class MqttSN
242
243
  sbytes=@clients[key][:socket].send(r, 0, @server, @port) # to rsmb -- ok as is
243
244
  _,port,_,_ = @clients[key][:socket].addr
244
245
  dest="#{@server}:#{port}"
245
- logger "cs %-24.24s -> %-24.24s | %s", @clients[key][:uri],@gateways[@active_gw_id][:uri],m.to_json
246
-
246
+ if @active_gw_id
247
+ logger "cs %-24.24s -> %-24.24s | %s", @clients[key][:uri],@gateways[@active_gw_id][:uri],m.to_json
248
+ else
249
+ logger "cs %-24.24s -> %-24.24s | %s", @clients[key][:uri],"??",m.to_json
250
+ end
247
251
  end
248
252
  end
249
253
  end
@@ -555,7 +559,7 @@ class MqttSN
555
559
  @will_msg=msg
556
560
  end
557
561
 
558
- def connect id
562
+ def connect id,&block
559
563
  send :connect,id: id, clean: false, duration: @keepalive, expect: [:connect_ack,:will_topic_req] do |s,m| #add will here!
560
564
  if s==:ok
561
565
  if m[:type]==:will_topic_req
@@ -567,7 +571,13 @@ class MqttSN
567
571
  end
568
572
  end
569
573
  end
574
+ elsif m[:type]==:connect_ack
575
+ puts "connected!---------------"
576
+ block.call :ok,m if block
570
577
  end
578
+ else
579
+ puts "failed to connect"
580
+ block.call :fail,m if block
571
581
  end
572
582
  end
573
583
  end
@@ -800,11 +810,16 @@ class MqttSN
800
810
  Thread.new do #ping thread
801
811
  while true do
802
812
  begin
813
+ while @state!=:connected
814
+ sleep 1
815
+ end
803
816
  sleep @keepalive
804
817
  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|
818
+ send :ping, timeout: 2,expect: :pong do |status,message|
806
819
  if status!=:ok
807
- note "Error:#{@id} no pong! -- need to switch gateway & restart comms"
820
+ note "Error:#{@id} no pong! -- sending disconnect to app"
821
+ @state=:disconnected
822
+ gateway_close :timeout
808
823
  end
809
824
  end
810
825
  end
@@ -819,17 +834,19 @@ class MqttSN
819
834
  while true do
820
835
  begin
821
836
  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
837
+ if pac=MqttSN::poll_packet(@gateways[@active_gw_id][:socket]) #cannot block -- gateway may change...
838
+ r,client_ip,client_port=pac
839
+ m=MqttSN::parse_message r
840
+ if @debug and m
841
+ m[:debug]=MqttSN::hexdump r
842
+ end
843
+ _,port,_,_= @gateways[@active_gw_id][:socket].addr
844
+ src="udp://0.0.0.0:#{port}"
845
+ logger "id %-24.24s -> %-24.24s | %s",@gateways[@active_gw_id][:uri],src,m.to_json
846
+ process_message m
847
+ else
848
+ sleep 0.01
827
849
  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
850
  else
834
851
  sleep 0.01
835
852
  end
@@ -854,7 +871,7 @@ class MqttSN
854
871
  duration=m[:duration]||180
855
872
  uri="udp://#{client_ip}:1882"
856
873
  if not @gateways[gw_id]
857
- @gateways[gw_id]={stamp: Time.now.to_i,uri: uri, duration: duration, source: m[:type], status: :ok}
874
+ @gateways[gw_id]={stamp: Time.now.to_i,uri: uri, duration: duration, source: m[:type], status: :ok, last_use: 0}
858
875
  else
859
876
  if @gateways[gw_id][:uri]!=uri
860
877
  note "conflict -- gateway has moved? or duplicate"
@@ -868,21 +885,44 @@ class MqttSN
868
885
  end
869
886
 
870
887
 
888
+ def gateway_close cause
889
+ @gsem.synchronize do #one command at a time --
890
+
891
+ if @active_gw_id # if using one, mark it used, so it will be last reused
892
+ note "closing gw #{@active_gw_id} cause: #{cause}"
893
+ @gateways[@active_gw_id][:last_use]=Time.now.to_i
894
+ if @gateways[@active_gw_id][:socket]
895
+ @gateways[@active_gw_id][:socket].close
896
+ @gateways[@active_gw_id][:socket]=nil
897
+ end
898
+ @active_gw_id=nil
899
+ end
900
+ end
901
+ end
902
+
871
903
  def pick_new_gateway
872
904
  begin
873
- use_gw=nil
874
- @gateways.each do |gw_id,data|
875
- if data[:uri]
876
- use_gw=gw_id
905
+ gateway_close nil
906
+ @gsem.synchronize do #one command at a time --
907
+ pick=nil
908
+ pick_t=0
909
+ @gateways.each do |gw_id,data|
910
+ if data[:uri] and data[:status]==:ok
911
+ if not pick or data[:last_use]==0 or pick_t>data[:last_use]
912
+ pick=gw_id
913
+ pick_t=data[:last_use]
914
+ end
915
+ end
916
+ end
917
+ if pick
918
+ @active_gw_id=pick
919
+ note "using gateway #{@active_gw_id} #{@gateways[@active_gw_id][:uri]}"
920
+ @s,@server,@port = MqttSN::open_port @gateways[@active_gw_id][:uri]
921
+ @gateways[@active_gw_id][:socket]=@s
922
+ @gateways[@active_gw_id][:last_use]=Time.now.to_i
923
+ else
924
+ #note "Error: no usable gw found !!"
877
925
  end
878
- end
879
- if use_gw
880
- @active_gw_id=use_gw
881
- puts "using gateway #{@active_gw_id} #{@gateways[@active_gw_id][:uri]}"
882
- @s,@server,@port = MqttSN::open_port @gateways[@active_gw_id][:uri]
883
- @gateways[use_gw][:socket]=@s
884
- else
885
- #note "Error: no usable gw found !!"
886
926
  end
887
927
  rescue => e
888
928
  puts "Error: receive thread died: #{e}"
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.14
4
+ version: 0.0.15
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-20 00:00:00.000000000 Z
11
+ date: 2014-11-21 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