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.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +13 -2
- data/Gemfile +0 -1
- data/LICENSE +2 -2
- data/README.md +37 -33
- data/Rakefile +12 -3
- data/bin/coap +111 -0
- data/coap.gemspec +34 -29
- data/lib/coap.rb +3 -34
- data/lib/core.rb +11 -0
- data/lib/core/coap.rb +42 -0
- data/lib/core/coap/block.rb +98 -0
- data/lib/core/coap/client.rb +314 -0
- data/lib/core/coap/coap.rb +26 -0
- data/lib/core/coap/coding.rb +146 -0
- data/lib/core/coap/fsm.rb +82 -0
- data/lib/core/coap/message.rb +203 -0
- data/lib/core/coap/observer.rb +40 -0
- data/lib/core/coap/options.rb +44 -0
- data/lib/core/coap/registry.rb +32 -0
- data/lib/core/coap/registry/content_formats.yml +7 -0
- data/lib/core/coap/resolver.rb +17 -0
- data/lib/core/coap/transmission.rb +165 -0
- data/lib/core/coap/types.rb +69 -0
- data/lib/core/coap/utility.rb +34 -0
- data/lib/core/coap/version.rb +5 -0
- data/lib/core/core_ext/socket.rb +19 -0
- data/lib/core/hexdump.rb +18 -0
- data/lib/core/link.rb +97 -0
- data/lib/core/os.rb +15 -0
- data/spec/block_spec.rb +160 -0
- data/spec/client_spec.rb +86 -0
- data/spec/fixtures/coap.me.link +1 -0
- data/spec/link_spec.rb +98 -0
- data/spec/registry_spec.rb +39 -0
- data/spec/resolver_spec.rb +19 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/transmission_spec.rb +70 -0
- data/test/helper.rb +15 -0
- data/test/test_client.rb +99 -228
- data/test/test_message.rb +99 -71
- metadata +140 -37
- data/bin/client +0 -42
- data/lib/coap/block.rb +0 -45
- data/lib/coap/client.rb +0 -364
- data/lib/coap/coap.rb +0 -273
- data/lib/coap/message.rb +0 -187
- data/lib/coap/mysocket.rb +0 -81
- data/lib/coap/observer.rb +0 -41
- data/lib/coap/version.rb +0 -3
- data/lib/misc/hexdump.rb +0 -17
- data/test/coap_test_helper.rb +0 -2
- 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
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
|
data/test/coap_test_helper.rb
DELETED
@@ -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
|