Sha256: 462b8c5053bee07d45f0f8b17670937d6574cae9cbf92af6ca7e57b0cb95fea4

Contents?: true

Size: 994 Bytes

Versions: 1

Compression:

Stored size: 994 Bytes

Contents

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

class Snmpjr
  class Session

    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)
      end
      if result.response.nil?
        raise Snmpjr::TargetTimeoutError.new('Request timed out')
      else
        result.response.variable_bindings.map{|vb|
          construct_response(vb)
        }
      end
    end

    def close
      @snmp.close
    end

    private

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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
snmpjr-0.1.7-java lib/snmpjr/session.rb