mqtt-sn-ruby 0.0.1 → 0.0.2

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/mqtt-sn-ruby.rb +86 -50
  3. data/test.rb +8 -2
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a8cea07a29fac1f0ee317f5f8b1923227a26e55
4
- data.tar.gz: 7382ccd3bb7087a54e681dc00c0f4b7face5ca27
3
+ metadata.gz: 552d49593797c45d53598fe3d5787e3ffd692b56
4
+ data.tar.gz: 6c955790706011b9690186974084418488733af0
5
5
  SHA512:
6
- metadata.gz: 345a18e8b8f90de15cbeeb57e887a34319cf74efaac95a2292f7eda74e2adc2fcb989c53276a099b125dfa8958535c390b97a227728bb62ae8853096d61dcbab
7
- data.tar.gz: 1d5f9f86870762d83c8b73946b93816583c5f024bd02ebde54a9118b08a62d8b9668a50e00e8e9def99f3a92d828dd9336d5e0448ac91627a926db4d0dc2a45b
6
+ metadata.gz: 6edfcee73ea914ed1e4272e0c38eae30954b5ae3c76ff2fd0143a00b990879226efffb5a702826b4c90b17a49b04159fe7350fb536bb046506fa6b09903f88ae
7
+ data.tar.gz: 82aa807b9ff23d63aa707aea3937251a0ecc296717bf982a485d5c30b1d0f241f5c5e5f90b46e80085c679ffaaf7284cdd26ffc5d4e6b3afcb25423c25b99b1c
data/lib/mqtt-sn-ruby.rb CHANGED
@@ -53,8 +53,10 @@ class MqttSN
53
53
  @will_topic=nil
54
54
  @will_msg=nil
55
55
  @id="?"
56
+ @sem=Mutex.new
56
57
  @topics={} #hash of registered topics is stored here
57
58
  @iq = Queue.new
59
+ @dataq = Queue.new
58
60
  @s = UDPSocket.new
59
61
  @t=Thread.new do
60
62
  recv_thread
@@ -88,6 +90,7 @@ class MqttSN
88
90
  hexdump msg
89
91
  end
90
92
 
93
+
91
94
  def send type,hash={},&block
92
95
  puts ""
93
96
  if @state!=:connected and type!=:connect and type!=:will_topic and type!=:will_msg
@@ -160,6 +163,13 @@ class MqttSN
160
163
  end
161
164
  @@msg_id+=1
162
165
 
166
+ when :unsubscribe
167
+ raise "Need :topic to :unsubscribe" if not hash[:topic]
168
+ p=[UNSUBSCRIBE_TYPE,0,@@msg_id >>8 ,@@msg_id & 0xff]
169
+ hash[:topic].each_byte do |b|
170
+ p<<b
171
+ end
172
+ @@msg_id+=1
163
173
 
164
174
  when :publish
165
175
  raise "Need :topic_id to Publish!" if not hash[:topic_id]
@@ -191,49 +201,51 @@ class MqttSN
191
201
  puts "Error: Strange send?? #{type}"
192
202
  return nil
193
203
  end
194
- if hash[:expect]
195
- while not @iq.empty?
196
- mp=@iq.pop
197
- puts "WARN:#{@id} ************** Purged message: #{mp}"
198
- end
199
- @iq.clear
200
- end
201
- raw=send_packet p
202
- hash[:raw]=raw if @debug
203
- puts "send:#{@id} #{type},#{hash.to_json}"
204
- timeout=hash[:timeout]||Tretry
205
204
  status=:timeout
206
- retries=0
207
205
  m={}
208
- if hash[:expect]
209
- while retries<Nretry do
210
- stime=Time.now.to_i
211
- while Time.now.to_i<stime+timeout
212
- if not @iq.empty?
213
- m=@iq.pop
214
- if Array(hash[:expect]).include? m[:type]
215
- status=:ok
216
- break
217
- else
218
- puts "WARN:#{@id} ************** Discarded message: #{m}"
206
+ @sem.synchronize do #one command at a time --
207
+ if hash[:expect]
208
+ while not @iq.empty?
209
+ mp=@iq.pop
210
+ puts "WARN:#{@id} ************** Purged message: #{mp}"
211
+ end
212
+ @iq.clear
213
+ end
214
+ raw=send_packet p
215
+ hash[:raw]=raw if @debug
216
+ puts "send:#{@id} #{type},#{hash.to_json}"
217
+ timeout=hash[:timeout]||Tretry
218
+ retries=0
219
+ if hash[:expect]
220
+ while retries<Nretry do
221
+ stime=Time.now.to_i
222
+ while Time.now.to_i<stime+timeout
223
+ if not @iq.empty?
224
+ m=@iq.pop
225
+ if Array(hash[:expect]).include? m[:type]
226
+ status=:ok
227
+ break
228
+ else
229
+ puts "WARN:#{@id} ************** Discarded message: #{m}"
230
+ end
219
231
  end
232
+ sleep 0.1
233
+ end
234
+ if status==:ok
235
+ break
236
+ else
237
+ retries+=1
238
+ send_packet p
239
+ puts "fail to get ack, retry #{retries} :#{@id} #{type},#{hash.to_json}"
240
+ #need to set DUP flag !
220
241
  end
221
- sleep 0.1
222
- end
223
- if status==:ok
224
- break
225
- else
226
- retries+=1
227
- send_packet p
228
- puts "fail to get ack, retry #{retries} :#{@id} #{type},#{hash.to_json}"
229
- #need to set DUP flag !
230
242
  end
231
243
  end
232
-
233
- if block
234
- block.call status,m
235
- end
244
+ end #sem
245
+ if block
246
+ block.call status,m
236
247
  end
248
+
237
249
  end
238
250
 
239
251
  def will_and_testament topic,msg
@@ -288,8 +300,33 @@ class MqttSN
288
300
  end
289
301
  end
290
302
 
291
- def subscribe topic,hash={}
292
- send :subscribe, topic: topic, qos: hash[:qos],expect: :sub_ack do |status,message|
303
+
304
+ def subscribe topic,hash={},&block
305
+ send :subscribe, topic: topic, qos: hash[:qos],expect: :sub_ack do |s,m|
306
+ if s==:ok
307
+ if m[:topic_id] and m[:topic_id]>0 #when subs topic has no wild cards, we get topic id here:
308
+ @topics[topic]=m[:topic_id]
309
+ end
310
+ end
311
+ if block
312
+ block.call :sub_ack,m
313
+ while true
314
+ if not @dataq.empty?
315
+ m=@dataq.pop
316
+ block.call :got_data,m
317
+ end
318
+ sleep 0.1
319
+ if @state!=:connected
320
+ block.call :disconnect,{}
321
+ break
322
+ end
323
+ end
324
+ end
325
+ end
326
+ end
327
+
328
+ def unsubscribe topic
329
+ send :unsubscribe, topic: topic, expect: :unsub_ack do |s,m|
293
330
  end
294
331
  end
295
332
 
@@ -322,7 +359,6 @@ class MqttSN
322
359
  when 1
323
360
  send :publish,msg: msg, retain: hash[:retain], topic_id: @topics[topic], qos: 1, expect: [:publish_ack] do |s,m|
324
361
  if s==:ok
325
- puts "got handshaken once! status=#{s}, message=#{m.to_json}"
326
362
  end
327
363
  end
328
364
  when 2
@@ -330,7 +366,6 @@ class MqttSN
330
366
  if s==:ok
331
367
  if m[:type]==:pubrec
332
368
  send :pubrel,msg_id: m[:msg_id], expect: :pubcomp do |s,m|
333
- puts "got handshaken twice! status=#{s}, message=#{m.to_json}"
334
369
  end
335
370
  end
336
371
  end
@@ -364,33 +399,37 @@ class MqttSN
364
399
  when CONNACK_TYPE
365
400
  m={type: :connect_ack,status: status}
366
401
  @state=:connected
367
-
368
402
  when SUBACK_TYPE
369
403
  topic_id=(r[3].ord<<8)+r[4].ord
370
404
  msg_id=(r[5].ord<<8)+r[6].ord
371
405
  m={type: :sub_ack, topic_id: topic_id, msg_id: msg_id, status: status}
406
+ when UNSUBACK_TYPE
407
+ msg_id=(r[2].ord<<8)+r[3].ord
408
+ m={type: :unsub_ack, msg_id: msg_id, status: :ok}
372
409
  when PUBLISH_TYPE
373
410
  topic_id=(r[3].ord<<8)+r[4].ord
374
411
  msg_id=(r[5].ord<<8)+r[6].ord
375
412
  msg=r[7,len-7]
376
413
  flags=r[2].ord
377
414
  qos=(flags>>5)&0x03
378
- m={type: :publish, qos: qos, topic_id: topic_id, msg_id: msg_id, msg: msg,status: :ok}
379
- done=true
380
- if qos==1 #send ack
415
+ topic=@topics.key(topic_id)
416
+ m={type: :publish, qos: qos, topic_id: topic_id, topic: topic,msg_id: msg_id, msg: msg,status: :ok}
417
+ @dataq<<m
418
+ if qos==1
381
419
  send :publish_ack,topic_id: topic_id, msg_id: msg_id, return_code: 0
382
- elsif qos==2 #send ack
420
+ elsif qos==2
383
421
  send :pub_rec, msg_id: msg_id
384
422
  end
423
+ done=true
385
424
  when PUBREL_TYPE
386
425
  msg_id=(r[2].ord<<8)+r[3].ord
426
+ m={type: :pub_rel, status: :ok}
387
427
  send :pub_comp, msg_id: msg_id
388
428
  done=true
389
429
  when DISCONNECT_TYPE
390
430
  m={type: :disconnect,status: :ok}
391
431
  @state=:disconnected
392
432
  when REGISTER_TYPE
393
- puts "registering... *************************"
394
433
  topic_id=(r[2].ord<<8)+r[3].ord
395
434
  msg_id=(r[4].ord<<8)+r[5].ord
396
435
  topic=r[6,len-6]
@@ -429,13 +468,10 @@ class MqttSN
429
468
  else
430
469
  m={type: :unknown, type_byte: type_byte }
431
470
  end
432
- if @debug
471
+ if @debug and m
433
472
  m[:raw]=hexdump r
434
473
  end
435
474
  puts "got :#{@id} #{m.to_json}"
436
- if m[:type]==:publish
437
- puts "**************************** PUBLISH"
438
- end
439
475
  if not done
440
476
  @iq<<m if m
441
477
  end
data/test.rb CHANGED
@@ -27,18 +27,24 @@ sn2.connect "toka"
27
27
 
28
28
  sn3.connect "kolmas"
29
29
 
30
- sn3.subscribe "eka/+",qos:2
30
+ Thread.new do
31
+ sn3.subscribe "eka/2",qos:2 do |s,m|
32
+ puts ">>>>>>>>>>>> subscribe got: #{s},#{m}"
33
+ end
34
+ end
35
+ sn3.unsubscribe "eka/2"
31
36
 
32
37
  topic_id=0
33
38
  sn.will_and_testament "top2","testamentti2"
34
39
 
35
40
  sn2.ping
36
-
41
+ sn3.subscribe "eka/3",qos:2
37
42
  #sn.register_topic "jeesus/perkele/toimii"
38
43
  sn.publish "eka/1","perkkule0",qos: 0
39
44
  sn.publish "eka/2","perkkule1",qos: 1
40
45
  sn.publish "eka/3","perkkule2",qos: 2
41
46
 
47
+
42
48
  sn2.publish "eka/4","2perkkule0",qos: 0
43
49
  sn2.publish "eka/5","2perkkule1rrrr",qos: 1, retain: true
44
50
  sn2.publish "eka/6","2perkkule2",qos: 2
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mqtt-sn-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Siitonen