Class: Nl::Protocols::Raw::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/nl/protocols/raw.rb

Direct Known Subclasses

Genl::Message

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, fixed_header = nil, attributes = self.class::ATTRIBUTE_SET.new) ⇒ Message

Returns a new instance of Message.



213
214
215
216
217
# File 'lib/nl/protocols/raw.rb', line 213

def initialize(header, fixed_header = nil, attributes = self.class::ATTRIBUTE_SET.new)
  @nlmsg_header = header
  @fixed_header = fixed_header
  @attributes = attributes
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



211
212
213
# File 'lib/nl/protocols/raw.rb', line 211

def attributes
  @attributes
end

#fixed_headerObject

Returns the value of attribute fixed_header.



211
212
213
# File 'lib/nl/protocols/raw.rb', line 211

def fixed_header
  @fixed_header
end

#nlmsg_headerObject

Returns the value of attribute nlmsg_header.



211
212
213
# File 'lib/nl/protocols/raw.rb', line 211

def nlmsg_header
  @nlmsg_header
end

Class Method Details

.decode(decoder, header, type: header.type) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/nl/protocols/raw.rb', line 249

def self.decode(decoder, header, type: header.type)
  unless self::TYPE == type
    raise "Expected message type #{self::TYPE}, got #{type}"
  end

  if fixed_header_class = self::FIXED_HEADER
    fixed_header = fixed_header_class.decode(decoder)
  end

  attributes = self::ATTRIBUTE_SET.decode(decoder)

  new(header, fixed_header, attributes)
end

.from_params(params) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/nl/protocols/raw.rb', line 219

def self.from_params(params)
  if self::FIXED_HEADER
    header_params = params.slice(*self::FIXED_HEADER.members)
    fixed_header = self::FIXED_HEADER.new(**header_params)
  end
  attribute_params = params.slice(*self::ATTRIBUTES)
  attributes = self::ATTRIBUTE_SET.build_attributes(**attribute_params)

  unknown = params.keys - attribute_params.keys
  unknown -= header_params.keys if header_params
  unless unknown.empty?
    raise ArgumentError, "unknown parameters: #{unknown.join(', ')}"
  end

  header = Core::NlMsgHdr.new(0, self::TYPE, nil, nil, nil)
  new(header, fixed_header, attributes)
end

Instance Method Details

#append_attribute(attribute) ⇒ Object



237
238
239
# File 'lib/nl/protocols/raw.rb', line 237

def append_attribute(attribute)
  @attributes << attribute
end

#encode(encoder) ⇒ Object



241
242
243
244
245
246
247
# File 'lib/nl/protocols/raw.rb', line 241

def encode(encoder)
  encoder.measure(Endian::Host::U16) do
    @nlmsg_header.encode(encoder)
    @fixed_header&.encode(encoder)
    @attributes.encode(encoder)
  end
end