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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 705eb97fc06ebe058eb3b7aded29ab274e50d5d5
4
- data.tar.gz: 38ae9662ffc544634d14130e4d776a2eaf7daf52
3
+ metadata.gz: 1e495c4d45fd7179f188f83894e048a83432cfd6
4
+ data.tar.gz: 90152bb81bdca776b0b74001bf11f3423cb89c5d
5
5
  SHA512:
6
- metadata.gz: c2874cc9d625439ce68d33414aa9efccad6d67b15775f578e035e320df388fbbabaa1b61e83d670b67ec673d49c123e28d79a1b331358d8cc09913310c036dd9
7
- data.tar.gz: b4b54ab175b0fc6856d99dfde89ddbca500cee574cb7344043c216f67f74a11e18a86f39a1484a87cbe61dcb8721af68bf1d976a985f008a7406ff80564fdf39
6
+ metadata.gz: 4ee91fee12ff5a12dd11e64dd9f8416274ef6f0496de213a3370a55d09e1111c48a5f49bca1145841a75c8727a21ec5f3a772b9314e2c9e271bdf08fcf1be81e
7
+ data.tar.gz: 40c2d2404d968cc3d5c2c84e541dbfa4656f35779824488de94769440e1a6cca8f17165a8a78197738b121193ef58b9390ec6f2117ee04a2c8884012a647f3b6
@@ -37,6 +37,7 @@ OptionParser.new do |opts|
37
37
  opts.on("-t", "--topic topic", "Topic to use (test/message/123)") do |topic|
38
38
  options[:topic] = topic
39
39
  end
40
+
40
41
  end.parse!
41
42
 
42
43
  puts "MQTT-SN-PUB: #{options.to_json}"
@@ -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
@@ -103,19 +103,22 @@ class MqttSN
103
103
  end
104
104
  end
105
105
 
106
- def self.open_multicast_send_port port
107
- ip = IPAddr.new(MULTICAST_ADDR).hton + IPAddr.new("0.0.0.0").hton
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 self.open_multicast_recv_port port
114
- ip = IPAddr.new(MULTICAST_ADDR).hton + IPAddr.new("0.0.0.0").hton
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=MqttSN::open_multicast_send_port @bcast_port
167
- @bcast=MqttSN::open_multicast_recv_port @bcast_port
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
- @client_t=Thread.new do
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 "so %-24.24s <- %-24.24s | %s",@clients[my_key][:uri],@gateways[@active_gw_id][:uri],mm.to_json
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 "ci %-24.24s -> %-24.24s | %s", @clients[key][:uri],@gateways[@active_gw_id][:uri],m.to_json
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,duration=30
556
- send :connect,id: id, clean: false, duration: duration, expect: [:connect_ack,:will_topic_req] do |s,m| #add will here!
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
- while true do
796
- begin
797
- if @active_gw_id and @gateways[@active_gw_id] and @gateways[@active_gw_id][:socket] #if we are connected...
798
- pac=MqttSN::poll_packet_block(@gateways[@active_gw_id][:socket])
799
- r,client_ip,client_port=pac
800
- m=MqttSN::parse_message r
801
- if @debug and m
802
- m[:debug]=MqttSN::hexdump r
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
- dest="#{client_ip}:#{client_port}"
805
- _,port,_,_= @gateways[@active_gw_id][:socket].addr
806
- src="udp://0.0.0.0:#{port}"
807
- logger "id %-24.24s -> %-24.24s | %s",@gateways[@active_gw_id][:uri],src,m.to_json
808
- process_message m
809
- else
810
- sleep 0.01
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.13
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-19 00:00:00.000000000 Z
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