Class: Takagi::Message::Request
- 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
-
#message_id ⇒ Object
readonly
Returns the value of attribute message_id.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from Base
#code, #payload, #token, #version
Instance Method Summary collapse
- #encode_extended ⇒ Object
- #encode_option ⇒ Object
- #encode_options ⇒ Object
-
#initialize(method:, uri:, payload: nil, token: nil, type: nil, options: {}, **kwargs) ⇒ Request
constructor
A new instance of Request.
- #to_bytes ⇒ Object
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.
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 = (, kwargs) end |
Instance Attribute Details
#message_id ⇒ Object (readonly)
Returns the value of attribute message_id.
15 16 17 |
# File 'lib/takagi/message/request.rb', line 15 def @message_id end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/takagi/message/request.rb', line 15 def @options end |
#type ⇒ Object (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_extended ⇒ Object
29 |
# File 'sig/takagi/message/request.rbs', line 29
def encode_extended: (untyped val) -> untyped
|
#encode_option ⇒ Object
27 |
# File 'sig/takagi/message/request.rbs', line 27
def encode_option: (untyped option_number, untyped value, untyped last_option_number) -> untyped
|
#encode_options ⇒ 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 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_bytes ⇒ 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 + + build_payload_section).b Takagi.logger.debug "Generated Request packet: #{packet.inspect}" packet end |