Permalink
Browse files

updating as a bundler style gem

  • Loading branch information...
mixtli committed Mar 22, 2011
1 parent 1d3f9fa commit 87f91718a7e21df54e46b50498658fde6ff4c0d9
Showing with 103 additions and 114 deletions.
  1. +4 −21 .gitignore
  2. +1 −0 .rspec
  3. +4 −0 Gemfile
  4. +2 −45 Rakefile
  5. +5 −20 lib/net/snmp.rb
  6. +1 −1 lib/net/snmp/error.rb
  7. +49 −24 lib/net/snmp/session.rb
  8. +5 −0 lib/net/snmp/version.rb
  9. +1 −1 lib/net/snmp/wrapper.rb
  10. +23 −0 net-snmp.gemspec
  11. +5 −0 spec/mib_spec.rb
  12. +3 −2 spec/net-snmp_spec.rb
View
@@ -1,21 +1,4 @@
-## MAC OS
-.DS_Store
-
-## TEXTMATE
-*.tmproj
-tmtags
-
-## EMACS
-*~
-\#*
-.\#*
-
-## VIM
-*.swp
-
-## PROJECT::GENERAL
-coverage
-rdoc
-pkg
-
-## PROJECT::SPECIFIC
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
View
1 .rspec
@@ -0,0 +1 @@
+--color
View
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in net-snmp.gemspec
+gemspec
View
@@ -1,45 +1,2 @@
-require 'rubygems'
-require 'rake'
-
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gem|
- gem.name = "net-snmp"
- gem.summary = %Q{SNMP library for ruby}
- gem.description = %Q{wrapper around net-snmp lib for ruby using ffi bindings}
- gem.email = "ronmcclain75@gmail.com"
- gem.homepage = "http://github.com/mixtli/net-snmp"
- gem.authors = ["mixtli"]
- gem.add_development_dependency "rspec", ">= 1.2.9"
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
- end
- Jeweler::GemcutterTasks.new
-rescue LoadError
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
-end
-
-#require 'spec/rake/spectask'
-#Spec::Rake::SpecTask.new(:spec) do |spec|
-# spec.libs << 'lib' << 'spec'
-# spec.spec_files = FileList['spec/**/*_spec.rb']
-#end
-
-#Spec::Rake::SpecTask.new(:rcov) do |spec|
-# spec.libs << 'lib' << 'spec'
-# spec.pattern = 'spec/**/*_spec.rb'
-# spec.rcov = true
-#end
-
-task :spec => :check_dependencies
-
-task :default => :spec
-
-require 'rake/rdoctask'
-Rake::RDocTask.new do |rdoc|
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
-
- rdoc.rdoc_dir = 'rdoc'
- rdoc.title = "net-snmp #{version}"
- rdoc.rdoc_files.include('README*')
- rdoc.rdoc_files.include('lib/**/*.rb')
-end
+require 'bundler'
+Bundler::GemHelper.install_tasks
View
@@ -2,33 +2,18 @@
module Net
module SNMP
include Net::SNMP::Constants
+
+ # Putting this here so there can be one global dispatcher for all sessions. need to think this through
REQUESTS = {}
def self.init(tag="snmp")
Wrapper.init_snmp(tag)
end
def self.dispatcher
- puts "in dispatcher"
- #while REQUESTS.size > 0
- puts REQUESTS.inspect
- fdset = Net::SNMP::Wrapper.get_fd_set
- fds = FFI::MemoryPointer.new(:int)
- tval = Net::SNMP::Wrapper::TimeVal.new
- block = FFI::MemoryPointer.new(:int)
- block.write_int(1)
- Net::SNMP::Wrapper.snmp_select_info(fds, fdset, tval.pointer, block )
- puts "selecting"
- if fds.read_int > 0
- puts "snmp_select returned #{fds.read_int}"
- zero = Wrapper::TimeVal.new(:tv_sec => 0, :tv_usec => 0)
- puts "block is #{block.read_int}"
- num_ready = Net::SNMP::Wrapper.select(fds.read_int, fdset, nil, nil, block.read_int == 1 ? nil : zero.pointer)
- puts "got #{num_ready} descriptors"
- Net::SNMP::Wrapper.snmp_read(fdset)
- end
- #end
- puts "exited dispatcher"
+ Net::SNMP::Session.all_sessions.each do |s|
+ s.dispatcher
+ end
end
def self._get_oid(name)
View
@@ -1,7 +1,7 @@
module Net
module SNMP
class Error < RuntimeError
- attr :status, :errno, :snmp_err, :snmp_msg
+ #attr :status, :errno, :snmp_err, :snmp_msg
def initialize(opts = {})
@status = opts[:status]
View
@@ -4,20 +4,25 @@ class Session
extend Forwardable
attr_accessor :struct, :callback
def_delegator :@struct, :pointer
+ @@sessions = []
def initialize(options)
options[:community] ||= "public"
options[:community_len] = options[:community].length
options[:version] ||= Constants::SNMP_VERSION_1
@callback = options[:callback]
- sess = Wrapper::SnmpSession.new
- Wrapper.snmp_sess_init(sess.pointer)
+ @requests = {}
+ @@sessions << self
+ @sess = Wrapper::SnmpSession.new
+ Wrapper.snmp_sess_init(@sess.pointer)
#options.each_pair {|k,v| ptr.send("#{k}=", v)}
- sess.community = FFI::MemoryPointer.new(options[:community].length + 1)
- sess.community.write_string(options[:community])
- sess.community_len = options[:community].length
- sess.peername = FFI::MemoryPointer.new(options[:peername].length + 1)
- sess.peername.write_string(options[:peername])
- sess.version = case options[:version].to_s
+ @sess.community = FFI::MemoryPointer.new(options[:community].length + 1)
+ @sess.community.write_string(options[:community])
+ @sess.community_len = options[:community].length
+ puts "options[:peername] = #{options[:peername]}"
+ @sess.peername = FFI::MemoryPointer.new(options[:peername].length + 1)
+ @sess.peername.write_string(options[:peername])
+ puts "peername now #{@sess.peername.read_string}"
+ @sess.version = case options[:version].to_s
when '1'
Constants::SNMP_VERSION_1
when '2', '2c'
@@ -28,23 +33,35 @@ def initialize(options)
Constants::SNMP_VERSION_1
end
- if sess.version == Constants::SNMP_VERSION_3
+ if @sess.version == Constants::SNMP_VERSION_3
puts "version 3"
end
- sess.callback = lambda do |operation, session, reqid, pdu_ptr, magic|
+ @sess.callback = lambda do |operation, session, reqid, pdu_ptr, magic|
puts "got snmp_session callback"
pdu = Net::SNMP::PDU.new(pdu_ptr)
- if Net::SNMP::REQUESTS[reqid]
- Net::SNMP::REQUESTS[reqid].call(pdu)
- Net::SNMP::REQUESTS.delete(reqid)
- end
+ run_callbacks(operation, reqid, pdu, magic)
0
end
- @struct = Wrapper.snmp_open(sess.pointer)
+ @handle = Wrapper.snmp_sess_open(@sess.pointer)
+ @struct = Wrapper.snmp_sess_session(@handle)
+ puts "final peername = #{@struct.peername.read_string}"
+ end
+
+ def self.all_sessions
+ @@sessions
end
+ def run_callbacks(operation, reqid, pdu, magic)
+ callback.call(operation, reqid, pdu, magic) if callback
+ if Net::SNMP::REQUESTS[reqid]
+ Net::SNMP::REQUESTS[reqid].call(pdu)
+ Net::SNMP::REQUESTS.delete(reqid)
+ end
+ end
+
+
def self.open(options)
session = new(options)
if block_given?
@@ -54,6 +71,7 @@ def self.open(options)
end
def get(oidlist, options = {}, &block)
+ puts "peername inside get #{@struct.peername.read_string}"
oidlist = [oidlist] unless oidlist.kind_of?(Array)
@@ -83,17 +101,27 @@ def set(oidlist, options = {}, &block)
def error(msg)
raise Net::SNMP::Error.new({:session => self}), msg
end
+
+ def dispatcher
+ fdset = Net::SNMP::Wrapper.get_fd_set
+ fds = FFI::MemoryPointer.new(:int)
+ tval = Net::SNMP::Wrapper::TimeVal.new
+ block = FFI::MemoryPointer.new(:int)
+ block.write_int(1)
+ Net::SNMP::Wrapper.snmp_sess_select_info(@handle, fds, fdset, tval.pointer, block )
+ if fds.read_int > 0
+ zero = Wrapper::TimeVal.new(:tv_sec => 0, :tv_usec => 0)
+ num_ready = Net::SNMP::Wrapper.select(fds.read_int, fdset, nil, nil, block.read_int == 1 ? nil : zero.pointer)
+ puts "num_ready = #{num_ready}"
+ Net::SNMP::Wrapper.snmp_sess_read(@handle, fdset)
+ end
+ end
private
def send_pdu(pdu, &block)
if block
REQUESTS[pdu.reqid] = block
- puts "sending pdu"
- puts pdu.inspect
- puts pdu.struct.inspect
- puts pdu.varbinds.first.struct.inspect
- if (status = Net::SNMP::Wrapper.snmp_send(@struct.pointer, pdu.pointer)) == 0
- puts "BIG FAT ERROR"
+ if (status = Net::SNMP::Wrapper.snmp_sess_send(@handle, pdu.pointer)) == 0
error("snmp_get async failed")
end
nil
@@ -107,10 +135,7 @@ def send_pdu(pdu, &block)
Net::SNMP::PDU.new(response_ptr.read_pointer)
end
end
-
end
-
-
end
end
end
View
@@ -0,0 +1,5 @@
+module Net
+ module Snmp
+ VERSION = "0.0.1"
+ end
+end
View
@@ -264,7 +264,7 @@ class Tree < NiceFFI::Struct
attach_function :snmp_sess_init, [ :pointer ], :void
attach_function :snmp_sess_open, [ :pointer ], :pointer
attach_function :snmp_sess_pointer, [ :pointer ], :pointer
- attach_function :snmp_sess_session, [ :pointer ], :pointer
+ attach_function :snmp_sess_session, [ :pointer ], SnmpSession.typed_pointer
attach_function :snmp_sess_transport, [ :pointer ], :pointer
attach_function :snmp_sess_transport_set, [ :pointer, :pointer ], :void
attach_function :snmp_sess_add_ex, [ :pointer, :pointer, callback([ :pointer, :pointer, :pointer, :int ], :int), callback([ :pointer, :pointer, :pointer, :uint ], :int), callback([ :pointer, :pointer, :int ], :int), callback([ :pointer, :pointer, :pointer, :pointer ], :int), callback([ :pointer, :pointer, :pointer, :pointer, :pointer ], :int), callback([ :pointer, :uint ], :int), callback([ :pointer, :pointer, :uint ], :pointer) ], :pointer
View
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "net/snmp/version"
+
+Gem::Specification.new do |s|
+ s.name = "net-snmp"
+ s.version = Net::Snmp::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["TODO: Write your name"]
+ s.email = ["TODO: Write your email address"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "net-snmp"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+ s.add_development_dependency "rspec"
+ s.add_dependency "nice-ffi"
+end
View
@@ -36,5 +36,10 @@
node.label.should eq("sysDescr")
end
+ it "should do stuff" do
+ node = Net::SNMP::MIB::Node.get_node("ipAdEntIfIndex")
+ puts node.inspect
+ puts node.struct.inspect
+ end
end
View
@@ -20,7 +20,7 @@
it "should get multiple requests asynchronously" do
did_callback = false
- Net::SNMP::Session.open(:peername => '127.0.0.1') do |s|
+ session = Net::SNMP::Session.open(:peername => '127.0.0.1') do |s|
s.get(["sysDescr.0", "sysName.0", "ifOutOctets.1"]) do |result|
did_callback = true
result.varbinds[0].value.should match(/Darwin/)
@@ -30,7 +30,8 @@
end
end
sleep 1
- Net::SNMP.dispatcher()
+ #session.dispatcher()
+ Net::SNMP.dispatcher
#2.times { sleep 1; Net::SNMP.dispatcher() }
did_callback.should be(true)
end

0 comments on commit 87f9171

Please sign in to comment.