Class: SNMP4JR::TrapListener
- Inherits:
-
Object
- Object
- SNMP4JR::TrapListener
- Defined in:
- lib/snmp4jr/trap_listener.rb
Instance Method Summary collapse
- #close ⇒ Object (also: #exit, #kill, #terminate)
-
#initialize(options = {}) ⇒ TrapListener
constructor
A new instance of TrapListener.
- #join ⇒ Object
- #on_trap(&block) ⇒ Object
- #process_pdu(event) ⇒ Object (also: #processPdu)
Constructor Details
#initialize(options = {}) ⇒ TrapListener
Returns a new instance of TrapListener.
3 4 5 6 7 8 9 10 11 |
# File 'lib/snmp4jr/trap_listener.rb', line 3 def initialize( = {}) @protocol = [:protocol] || 'udp' @host = [:host] || '127.0.0.1' @port = [:port] || 1162 @done = false @lock = Mutex.new @trap_handler = Proc.new {} @handler_thread = Thread.new { process_traps } end |
Instance Method Details
#close ⇒ Object Also known as: exit, kill, terminate
34 35 36 37 38 |
# File 'lib/snmp4jr/trap_listener.rb', line 34 def close @snmp.close if @snmp @done = true @handler_thread.join end |
#join ⇒ Object
18 19 20 |
# File 'lib/snmp4jr/trap_listener.rb', line 18 def join @handler_thread.join end |
#on_trap(&block) ⇒ Object
13 14 15 16 |
# File 'lib/snmp4jr/trap_listener.rb', line 13 def on_trap(&block) raise ArgumentError, 'a block must be provided' unless block @lock.synchronize { @trap_handler = block } end |
#process_pdu(event) ⇒ Object Also known as: processPdu
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/snmp4jr/trap_listener.rb', line 22 def process_pdu(event) begin @trap_handler.call(Message.new(event)) rescue => e puts "Error handling trap: #{e.}" puts e.backtrace.join("\n") puts "Event:" p event end end |