Class: David::Exchange

Inherits:
Struct
  • Object
show all
Includes:
Registry
Defined in:
lib/david/exchange.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ancillaryObject

Returns the value of attribute ancillary

Returns:

  • (Object)

    the current value of ancillary


2
3
4
# File 'lib/david/exchange.rb', line 2

def ancillary
  @ancillary
end

#hostObject

Returns the value of attribute host

Returns:

  • (Object)

    the current value of host


2
3
4
# File 'lib/david/exchange.rb', line 2

def host
  @host
end

#messageObject

Returns the value of attribute message

Returns:

  • (Object)

    the current value of message


2
3
4
# File 'lib/david/exchange.rb', line 2

def message
  @message
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options


2
3
4
# File 'lib/david/exchange.rb', line 2

def options
  @options
end

#portObject

Returns the value of attribute port

Returns:

  • (Object)

    the current value of port


2
3
4
# File 'lib/david/exchange.rb', line 2

def port
  @port
end

Instance Method Details

#acceptObject


5
6
7
# File 'lib/david/exchange.rb', line 5

def accept
  message.options[:accept]
end

#ack?Boolean

Returns:

  • (Boolean)

9
10
11
# File 'lib/david/exchange.rb', line 9

def ack?
  message.tt == :ack
end

#blockObject


13
14
15
16
17
18
19
# File 'lib/david/exchange.rb', line 13

def block
  @block ||= if message.options[:block2].nil?
    CoAP::Block.new(0, false, 1024)
  else
    CoAP::Block.new(message.options[:block2]).decode
  end
end

#cbor?Boolean

Returns:

  • (Boolean)

21
22
23
# File 'lib/david/exchange.rb', line 21

def cbor?
  message.options[:content_format] == 60
end

#con?Boolean

Returns:

  • (Boolean)

25
26
27
# File 'lib/david/exchange.rb', line 25

def con?
  message.tt == :con
end

#delete?Boolean

Returns:

  • (Boolean)

29
30
31
# File 'lib/david/exchange.rb', line 29

def delete?
  message.mcode == :delete
end

#etagObject


33
34
35
# File 'lib/david/exchange.rb', line 33

def etag
  message.options[:etag]
end

#get?Boolean

Returns:

  • (Boolean)

41
42
43
# File 'lib/david/exchange.rb', line 41

def get?
  message.mcode == :get
end

#get_etag?Boolean

Returns:

  • (Boolean)

37
38
39
# File 'lib/david/exchange.rb', line 37

def get_etag?
  message.options[:etag].nil? && get?
end

#idempotent?Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/david/exchange.rb', line 45

def idempotent?
  get? || put? || delete?
end

#keyObject


49
50
51
# File 'lib/david/exchange.rb', line 49

def key
  [host, mid]
end

#midObject


53
54
55
# File 'lib/david/exchange.rb', line 53

def mid
  message.mid
end

#multicast?Boolean

Returns:

  • (Boolean)

57
58
59
60
61
62
63
64
65
66
# File 'lib/david/exchange.rb', line 57

def multicast?
  a = ancillary
  return false if a.nil?

  return @multicast unless @multicast.nil?

  @multicast =
    a.cmsg_is?(:IP, :PKTINFO) && a.ip_pktinfo[0].ipv4_multicast? ||
    a.cmsg_is?(:IPV6, :PKTINFO) && a.ipv6_pktinfo[0].ipv6_multicast?
end

#non?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'lib/david/exchange.rb', line 68

def non?
  message.tt == :non
end

#observe?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'lib/david/exchange.rb', line 72

def observe?
  !message.options[:observe].nil?
end

#ping?Boolean

Returns:

  • (Boolean)

76
77
78
# File 'lib/david/exchange.rb', line 76

def ping?
  con? && message.mcode == [0, 0]
end

#post?Boolean

Returns:

  • (Boolean)

80
81
82
# File 'lib/david/exchange.rb', line 80

def post?
  message.mcode == :post
end

#proxy?Boolean

Returns:

  • (Boolean)

84
85
86
# File 'lib/david/exchange.rb', line 84

def proxy?
  !(message.options[:proxy_uri].nil? && message.options[:proxy_scheme].nil?)
end

#put?Boolean

Returns:

  • (Boolean)

88
89
90
# File 'lib/david/exchange.rb', line 88

def put?
  message.mcode == :put
end

#reliable?Boolean

Returns:

  • (Boolean)

92
93
94
# File 'lib/david/exchange.rb', line 92

def reliable?
  con? || ack?
end

#request?Boolean

Returns:

  • (Boolean)

96
97
98
# File 'lib/david/exchange.rb', line 96

def request?
  con? || non?
end

#response?Boolean

Returns:

  • (Boolean)

100
101
102
# File 'lib/david/exchange.rb', line 100

def response?
  ack? || rst?
end

#rst?Boolean

Returns:

  • (Boolean)

104
105
106
# File 'lib/david/exchange.rb', line 104

def rst?
  message.tt == :rst
end

#separate?Boolean

Returns:

  • (Boolean)

108
109
110
# File 'lib/david/exchange.rb', line 108

def separate?
  ack? && message.payload.empty? && message.mcode == [0, 0]
end

#to_sObject


112
113
114
# File 'lib/david/exchange.rb', line 112

def to_s
  "[#{host}]:#{port}: #{message} (block #{block.num})"
end

#tokenObject


116
117
118
# File 'lib/david/exchange.rb', line 116

def token
  message.options[:token]
end

#valid_method?Boolean

Returns:

  • (Boolean)

120
121
122
# File 'lib/david/exchange.rb', line 120

def valid_method?
  CoAP::METHODS.include?(message.mcode)
end