Class: Takagi::Message::Request

Inherits:
Base
  • Object
show all
Defined in:
lib/takagi/message/request.rb,
sig/takagi/message/request.rbs

Overview

Encodes outbound CoAP request envelopes used when observing remote peers.

Instance Attribute Summary collapse

Attributes inherited from Base

#code, #payload, #token, #version

Instance Method Summary collapse

Methods inherited from Base

#coap_code_to_method, #coap_method_to_code, #coerce_option_value, #decode_extended_value, #extract_payload, #parse, #parse_options, #store_option

Constructor Details

#initialize(method:, uri:, payload: nil, token: nil, type: nil, options: {}, **kwargs) ⇒ Request

Returns a new instance of Request.

Parameters:

  • method: (Object)
  • uri: (Object)
  • payload: (Object, nil) (defaults to: nil)
  • token: (Object, nil) (defaults to: nil)
  • options (Object) (defaults to: {})


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/takagi/message/request.rb', line 17

def initialize(method:, uri:, payload: nil, token: nil, type: nil, options: {}, **kwargs)
  super()
  @method = method
  @uri = uri
  @code = CoAP::CodeHelpers.to_numeric(method)
  @token = token || SecureRandom.hex(4)
  @type = type || CoAP::Registries::MessageType::CON
  @message_id = kwargs[:message_id] || rand(0..0xFFFF)
  @payload = payload
  @options = build_options(options, kwargs)
end

Instance Attribute Details

#message_idObject (readonly)

Returns the value of attribute message_id.

Returns:

  • (Object)


15
16
17
# File 'lib/takagi/message/request.rb', line 15

def message_id
  @message_id
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/takagi/message/request.rb', line 15

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/takagi/message/request.rb', line 15

def type
  @type
end

Instance Method Details

#encode_extendedObject

Parameters:

  • val (Object)

Returns:

  • (Object)


29
# File 'sig/takagi/message/request.rbs', line 29

def encode_extended: (untyped val) -> untyped

#encode_optionObject

Parameters:

  • option_number (Object)
  • value (Object)
  • last_option_number (Object)

Returns:

  • (Object)


27
# File 'sig/takagi/message/request.rbs', line 27

def encode_option: (untyped option_number, untyped value, untyped last_option_number) -> untyped

#encode_optionsObject

Returns:

  • (Object)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/takagi/message/request.rb', line 84

def encode_options
  return ''.b if @options.empty?

  last_option_number = 0
  encoded = +''.b

  # CoAP options must be encoded in ascending order
  sorted_entries = @options.flat_map do |number, values|
    Array(values).map { |v| [number, v] }
  end
  sorted_entries.sort_by.with_index { |(number, _), idx| [number, idx] }.each do |number, value|
    value_bytes = encode_option_value(value)
    length = value_bytes.bytesize
    delta = number - last_option_number

    delta_nibble, delta_ext = encode_extended_nibble(delta)
    length_nibble, length_ext = encode_extended_nibble(length)

    encoded << ((delta_nibble << 4) | length_nibble).chr
    encoded << delta_ext if delta_ext
    encoded << length_ext if length_ext
    encoded << value_bytes

    last_option_number = number
  end

  encoded
end

#to_bytesObject

Returns:

  • (Object)


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/takagi/message/request.rb', line 29

def to_bytes
  return ''.b unless @code

  version = Takagi::CoAP::VERSION
  token_length = @token.bytesize
  ver_type_token = ((version << 6) | (@type << 4) | token_length)
  header = [ver_type_token, CoAP::CodeHelpers.to_numeric(@method), @message_id].pack('CCn')

  packet = (header + @token.b + encode_options + build_payload_section).b
  Takagi.logger.debug "Generated Request packet: #{packet.inspect}"
  packet
end