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

#ancillary ⇒ Object

Returns the value of attribute ancillary

Returns:

  • the current value of ancillary



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

def ancillary
  @ancillary
end

#host ⇒ Object

Returns the value of attribute host

Returns:

  • the current value of host



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

def host
  @host
end

#message ⇒ Object

Returns the value of attribute message

Returns:

  • the current value of message



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

def message
  @message
end

#options ⇒ Object

Returns the value of attribute options

Returns:

  • the current value of options



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

def options
  @options
end

#port ⇒ Object

Returns the value of attribute port

Returns:

  • the current value of port



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

def port
  @port
end

Instance Method Details

#accept ⇒ Object



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

def accept
  message.options[:accept]
end

#ack? ⇒ Boolean

Returns:



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

def ack?
  message.tt == :ack
end

#block ⇒ Object



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:



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

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

#con? ⇒ Boolean

Returns:



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

def con?
  message.tt == :con
end

#delete? ⇒ Boolean

Returns:



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

def delete?
  message.mcode == :delete
end

#etag ⇒ Object



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

def etag
  message.options[:etag]
end

#get? ⇒ Boolean

Returns:



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

def get?
  message.mcode == :get
end

#get_etag? ⇒ Boolean

Returns:



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

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

#idempotent? ⇒ Boolean

Returns:



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

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

#key ⇒ Object



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

def key
  [host, mid]
end

#mid ⇒ Object



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

def mid
  message.mid
end

#multicast? ⇒ Boolean

Returns:



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:



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

def non?
  message.tt == :non
end

#observe? ⇒ Boolean

Returns:



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

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

#ping? ⇒ Boolean

Returns:



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

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

#post? ⇒ Boolean

Returns:



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

def post?
  message.mcode == :post
end

#proxy? ⇒ Boolean

Returns:



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:



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

def put?
  message.mcode == :put
end

#reliable? ⇒ Boolean

Returns:



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

def reliable?
  con? || ack?
end

#request? ⇒ Boolean

Returns:



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

def request?
  con? || non?
end

#response? ⇒ Boolean

Returns:



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

def response?
  ack? || rst?
end

#rst? ⇒ Boolean

Returns:



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

def rst?
  message.tt == :rst
end

#separate? ⇒ Boolean

Returns:



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

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

#to_s ⇒ Object



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

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

#token ⇒ Object



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

def token
  message.options[:token]
end

#valid_method? ⇒ Boolean

Returns:



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

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