coap 0.0.16 → 0.1.0

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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +13 -2
  4. data/Gemfile +0 -1
  5. data/LICENSE +2 -2
  6. data/README.md +37 -33
  7. data/Rakefile +12 -3
  8. data/bin/coap +111 -0
  9. data/coap.gemspec +34 -29
  10. data/lib/coap.rb +3 -34
  11. data/lib/core.rb +11 -0
  12. data/lib/core/coap.rb +42 -0
  13. data/lib/core/coap/block.rb +98 -0
  14. data/lib/core/coap/client.rb +314 -0
  15. data/lib/core/coap/coap.rb +26 -0
  16. data/lib/core/coap/coding.rb +146 -0
  17. data/lib/core/coap/fsm.rb +82 -0
  18. data/lib/core/coap/message.rb +203 -0
  19. data/lib/core/coap/observer.rb +40 -0
  20. data/lib/core/coap/options.rb +44 -0
  21. data/lib/core/coap/registry.rb +32 -0
  22. data/lib/core/coap/registry/content_formats.yml +7 -0
  23. data/lib/core/coap/resolver.rb +17 -0
  24. data/lib/core/coap/transmission.rb +165 -0
  25. data/lib/core/coap/types.rb +69 -0
  26. data/lib/core/coap/utility.rb +34 -0
  27. data/lib/core/coap/version.rb +5 -0
  28. data/lib/core/core_ext/socket.rb +19 -0
  29. data/lib/core/hexdump.rb +18 -0
  30. data/lib/core/link.rb +97 -0
  31. data/lib/core/os.rb +15 -0
  32. data/spec/block_spec.rb +160 -0
  33. data/spec/client_spec.rb +86 -0
  34. data/spec/fixtures/coap.me.link +1 -0
  35. data/spec/link_spec.rb +98 -0
  36. data/spec/registry_spec.rb +39 -0
  37. data/spec/resolver_spec.rb +19 -0
  38. data/spec/spec_helper.rb +17 -0
  39. data/spec/transmission_spec.rb +70 -0
  40. data/test/helper.rb +15 -0
  41. data/test/test_client.rb +99 -228
  42. data/test/test_message.rb +99 -71
  43. metadata +140 -37
  44. data/bin/client +0 -42
  45. data/lib/coap/block.rb +0 -45
  46. data/lib/coap/client.rb +0 -364
  47. data/lib/coap/coap.rb +0 -273
  48. data/lib/coap/message.rb +0 -187
  49. data/lib/coap/mysocket.rb +0 -81
  50. data/lib/coap/observer.rb +0 -41
  51. data/lib/coap/version.rb +0 -3
  52. data/lib/misc/hexdump.rb +0 -17
  53. data/test/coap_test_helper.rb +0 -2
  54. data/test/disabled_econotag_blck.rb +0 -33
data/lib/coap/mysocket.rb DELETED
@@ -1,81 +0,0 @@
1
- module CoAP
2
- class MySocket
3
-
4
- attr_writer :socket_type, :ack_timeout
5
-
6
- def initialize
7
- @logger = CoAP.logger
8
-
9
- end
10
-
11
- def connect(hostname, port)
12
- # hostname or ipv4/ipv6 address?
13
- address = IPAddr.new(hostname)
14
- return connect_socket(address, port)
15
-
16
- rescue IPAddr::InvalidAddressError
17
- # got a hostname, trying to resolv
18
-
19
- addresses = IPv6FavorResolv.getaddresses(hostname)
20
-
21
- raise Resolv::ResolvError if addresses.empty?
22
-
23
- addresses.each do |address|
24
-
25
- begin
26
-
27
- # transform to IPAddr object
28
- address = IPAddr.new(address.to_s)
29
- return connect_socket(address, port)
30
-
31
- rescue Errno::EHOSTUNREACH
32
-
33
- @logger.fatal 'Address unreachable: ' + address.to_s if $DEBUG
34
- # try next one if exists
35
-
36
- rescue Errno::ENETUNREACH
37
-
38
- @logger.fatal 'Net unreachable: ' + address.to_s if $DEBUG
39
- # try next one if exists
40
-
41
- end
42
-
43
- end
44
- end
45
-
46
- def send(data, flags)
47
- @socket.send(data, flags)
48
- end
49
-
50
- def receive(timeout = nil, retry_count = 0)
51
- timeout = @ack_timeout**(retry_count + 1) if timeout.nil? # timeout doubles
52
-
53
- @logger.debug @socket.peeraddr.inspect
54
- @logger.debug @socket.addr.inspect
55
- @logger.debug 'AAA Timeout: ' + timeout.to_s
56
-
57
- recv_data = nil
58
- status = Timeout.timeout(timeout) do
59
- recv_data = @socket.recvfrom(1024)
60
- end
61
-
62
- # pp recv_data
63
- recv_data
64
- end
65
-
66
- private
67
-
68
- def connect_socket(address, port)
69
- if address.ipv6?
70
- @socket = @socket_type.new(Socket::AF_INET6)
71
- else
72
- @socket = @socket_type.new
73
- end
74
-
75
- # TODO: Error handling connection
76
- @socket.connect(address.to_s, port)
77
-
78
- @socket
79
- end
80
- end
81
- end
data/lib/coap/observer.rb DELETED
@@ -1,41 +0,0 @@
1
- module CoAP
2
- class Observer
3
- MAX_OBSERVE_OPTION_VALUE = 8_388_608
4
-
5
- def initialize
6
- @logger = CoAP.logger
7
-
8
- @retry_count = 0
9
- end
10
-
11
- def observe(recv_parsed, recv_data, observe_callback, mySocket)
12
- observe_number = recv_parsed.options[:observe]
13
- observe_callback.call(recv_parsed, recv_data)
14
-
15
- loop do
16
- begin # todo fix this
17
- recv_data = mySocket.receive(60, @retry_count)
18
- rescue Timeout::Error
19
- @retry_count = 0
20
- @logger.error 'Observe Timeout'
21
- end
22
-
23
- recv_parsed = CoAP.parse(recv_data[0].force_encoding('BINARY'))
24
-
25
- if recv_parsed.tt == :con
26
- message = Message.new(:ack, 0, recv_parsed.mid, nil, {})
27
- mySocket.send message.to_wire, 0
28
- end
29
-
30
- if recv_parsed.options[:observe] &&
31
- ((recv_parsed.options[:observe] > observe_number && recv_parsed.options[:observe] - observe_number < MAX_OBSERVE_OPTION_VALUE) ||
32
- (recv_parsed.options[:observe] < observe_number && observe_number - recv_parsed.options[:observe] > MAX_OBSERVE_OPTION_VALUE))
33
-
34
- observe_callback.call(recv_parsed, recv_data)
35
-
36
- end
37
-
38
- end
39
- end
40
- end
41
- end
data/lib/coap/version.rb DELETED
@@ -1,3 +0,0 @@
1
- module Coap
2
- VERSION = "0.0.16"
3
- end
data/lib/misc/hexdump.rb DELETED
@@ -1,17 +0,0 @@
1
- # hexdump.rb
2
- # Copyright (C) 2010..2013 Carsten Bormann <cabo@tzi.org>
3
-
4
- class String
5
- def hexdump(prefix = '', out = STDOUT)
6
- i = 0
7
- while i < length
8
- slice = byteslice(i, 16)
9
- out.puts '%s%-48s |%-16s|' %
10
- [prefix,
11
- slice.bytes.map { |b| '%02x' % b.ord }.join(' '),
12
- slice.gsub(/[^ -~]/mn, '.')]
13
- i += 16
14
- end
15
- out
16
- end
17
- end
@@ -1,2 +0,0 @@
1
- require 'test/unit'
2
- require 'coap.rb'
@@ -1,33 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require_relative 'coap_test_helper'
3
-
4
- ECONOTAG_PAYLOAD = Random.rand(999).to_s + 'TESTLorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligul'
5
- ECONOTAG_SHORT_PAYLOAD = Random.rand(999).to_s + 'TESTLorem ipsum'
6
-
7
- class TestMessage < Test::Unit::TestCase
8
- def observe_tester(data, socket)
9
- $observe_count += 1
10
- end
11
-
12
- def test_econotag_pr_b1_b2
13
-
14
- client = CoAP::Client.new
15
- client.max_payload = 48
16
- answer = client.post('aaaa::60b1:10', 5683, '/r', ECONOTAG_PAYLOAD)
17
- assert_equal(2, answer.mcode[0])
18
- assert_equal(1, answer.mcode[1])
19
- assert_equal(ECONOTAG_PAYLOAD, answer.payload)
20
-
21
- client = CoAP::Client.new
22
- client.max_payload = 48
23
- answer = client.post('aaaa::60b1:10', 5683, '/r', ECONOTAG_SHORT_PAYLOAD)
24
- assert_equal(ECONOTAG_SHORT_PAYLOAD, answer.payload)
25
-
26
- end
27
-
28
- end
29
-
30
- # post
31
- # mit payload und block1 oder ohne
32
- # seperate
33
- # block2 antwort