Class: SNMP::VarBind

Inherits:
Object
  • Object
show all
Extended by:
BER::Decode
Includes:
BER::Encode
Defined in:
lib/snmp/varbind.rb

Constant Summary collapse

ValueDecoderMap =
{
  BER::INTEGER_TAG           => Integer,
  BER::OCTET_STRING_TAG      => OctetString,
  BER::NULL_TAG              => Null,
  BER::OBJECT_IDENTIFIER_TAG => ObjectId,
  BER::IpAddress_TAG         => IpAddress,
  BER::Counter32_TAG         => Counter32,
  BER::Gauge32_TAG           => Gauge32,
  # note Gauge32 tag same as Unsigned32
  BER::TimeTicks_TAG         => TimeTicks,
  BER::Opaque_TAG            => Opaque,
  BER::Counter64_TAG         => Counter64,
  BER::NoSuchObject_TAG      => NoSuchObject,
  BER::NoSuchInstance_TAG    => NoSuchInstance,
  BER::EndOfMibView_TAG      => EndOfMibView
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BER::Decode

assert_no_remainder, build_integer, decode_integer, decode_integer_value, decode_ip_address, decode_object_id, decode_object_id_value, decode_octet_string, decode_sequence, decode_timeticks, decode_tlv, decode_uinteger_value

Methods included from BER::Encode

#encode_exception, #encode_integer, #encode_length, #encode_null, #encode_object_id, #encode_octet_string, #encode_sequence, #encode_tagged_integer, #encode_tlv, #integer_to_octets

Constructor Details

#initialize(name, value = Null) ⇒ VarBind

Returns a new instance of VarBind.



603
604
605
606
607
608
609
610
# File 'lib/snmp/varbind.rb', line 603

def initialize(name, value=Null)
  if name.kind_of? ObjectId
    @name = name
  else
    @name = ObjectName.new(name)
  end
  @value = value
end

Instance Attribute Details

#nameObject Also known as: oid

Returns the value of attribute name.



560
561
562
# File 'lib/snmp/varbind.rb', line 560

def name
  @name
end

#valueObject

Returns the value of attribute value.



561
562
563
# File 'lib/snmp/varbind.rb', line 561

def value
  @value
end

Class Method Details

.decode(data, mib = nil) ⇒ Object



566
567
568
569
570
571
572
# File 'lib/snmp/varbind.rb', line 566

def decode(data, mib=nil)
  varbind_data, remaining_varbind_data = decode_sequence(data)
  name, remainder = decode_object_id(varbind_data)
  value, remainder = decode_value(remainder)
  assert_no_remainder(remainder)
  return VarBind.new(name, value).with_mib(mib), remaining_varbind_data
end

.decode_value(data) ⇒ Object



591
592
593
594
595
596
597
598
599
600
# File 'lib/snmp/varbind.rb', line 591

def decode_value(data)
  value_tag, value_data, remainder = decode_tlv(data)
  decoder_class = ValueDecoderMap[value_tag]
  if decoder_class
    value = decoder_class.decode(value_data)
  else
    raise UnsupportedValueTag, value_tag.to_s
  end
  return value, remainder
end

Instance Method Details

#asn1_typeObject



621
622
623
# File 'lib/snmp/varbind.rb', line 621

def asn1_type
  "VarBind"
end

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (SNMP::VarBind)

    the object that the method was called on



633
634
635
# File 'lib/snmp/varbind.rb', line 633

def each
  yield self
end

#encodeObject



637
638
639
640
# File 'lib/snmp/varbind.rb', line 637

def encode
  data = encode_object_id(@name) << value.encode
  encode_sequence(data)
end

#to_sObject



629
630
631
# File 'lib/snmp/varbind.rb', line 629

def to_s
  "[name=#{@name.to_s}, value=#{@value.to_s} (#{@value.asn1_type})]"
end

#to_varbindObject



625
626
627
# File 'lib/snmp/varbind.rb', line 625

def to_varbind
  self
end

#with_mib(mib) ⇒ Object

Adds MIB information to this varbind for use with to_s.



615
616
617
618
619
# File 'lib/snmp/varbind.rb', line 615

def with_mib(mib)
  @name.with_mib(mib) if @name
  @value.with_mib(mib) if @value.respond_to? :with_mib
  self
end