Sha256: bf876b1b76d9aab285a6fe7c6df6be9807680b8024f1e35ce43ffac691b0d03a

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

require 'snmpjr/wrappers/transport'
require 'snmpjr/response'

class Snmpjr
  class SessionV2C
    attr_reader :snmp

    def initialize
      @snmp = Snmpjr::Wrappers::Snmp.new(Snmpjr::Wrappers::Transport::DefaultUdpTransportMapping.new)
    end

    def start
      @snmp.listen
    end

    def send pdu, target
      begin
        result = @snmp.send(pdu, target)
      rescue Exception => error
        raise RuntimeError.new("#{error.inspect}: #{error_information pdu, target}")
      end
      if result.response.nil?
        raise Snmpjr::TargetTimeoutError.new("Request timed out: #{error_information pdu, target}")
      else
        result.response.variable_bindings.map{|vb|
          construct_response(vb)
        }
      end
    end

    def error_information pdu, target
      "#{target.address}, OIDs: #{pdu.to_array.map {|binding| binding.oid.to_s }.inspect}"
    end

    def close
      @snmp.close
    end

    private

    def construct_response variable_binding
      if variable_binding.is_exception
        Snmpjr::Response.new(oid: variable_binding.oid.to_s, error: variable_binding.variable.to_s)
      else
        Snmpjr::Response.new(oid: variable_binding.oid.to_s, value: variable_binding.variable.to_s)
      end
    end

  end

  class TargetTimeoutError < StandardError
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
snmpjr-0.3.2-java lib/snmpjr/session_v2c.rb