Class: Net::SNMP::Dispatcher
- Inherits:
-
Object
- Object
- Net::SNMP::Dispatcher
- Defined in:
- lib/net/snmp/dispatcher.rb
Class Method Summary collapse
-
.em_loop(options = {}) ⇒ Object
Start a loop in eventmachine (no fibers)
options
* :sleep Number of seconds to sleep if no data available. -
.fiber_loop(options = {}) ⇒ Object
Start a loop using eventmachine and fibers
options
* :sleep Number of seconds to sleep if no data available, so we don’t peg the reactor. -
.run_loop(options = {}) ⇒ Object
Start a poller loop.
-
.select(timeout = nil) ⇒ Object
(also: poll)
Loop through all sessions, calling select on each.
-
.thread_loop(options = {}) ⇒ Object
Start a poller loop in a seperate thread.
Class Method Details
.em_loop(options = {}) ⇒ Object
Start a loop in eventmachine (no fibers) options
-
:sleep Number of seconds to sleep if no data available. So we don’t peg the reactor. Default 0.2
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/net/snmp/dispatcher.rb', line 70 def em_loop( = {}) timeout = [:timeout] sleep_time = [:sleep] || 0.2 myproc = Proc.new do EM.next_tick do while(true) do num_ready = select(timeout) break if num_ready == 0 end EM.add_timer(sleep_time) do myproc.call end end end myproc.call end |
.fiber_loop(options = {}) ⇒ Object
Start a loop using eventmachine and fibers options
-
:sleep Number of seconds to sleep if no data available, so we don’t peg the reactor. Default 0.2
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/net/snmp/dispatcher.rb', line 90 def fiber_loop( = {}) timeout = [:timeout] sleep_time = [:sleep] || 0.2 fib = Fiber.new { loop do num_handled = poll(timeout) if num_handled == 0 f = Fiber.current EM.add_timer(sleep_time) do f.resume end Fiber.yield end end } fib.resume end |
.run_loop(options = {}) ⇒ Object
Start a poller loop. Behavior depends upon whether you are running under eventmachine and whether fibers are available. options
-
:timeout Number of seconds to block on select. nil effects a poll. false blocks forever (probably not what you want).
-
:sleep Number of seconds to sleep if no data is available. Gives other fibers/threads a chance to run.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/net/snmp/dispatcher.rb', line 37 def run_loop( = {}) if defined?(EM) && EM.reactor_running? if defined?(Fiber) fiber_loop() else em_loop() end else thread_loop() end end |
.select(timeout = nil) ⇒ Object Also known as: poll
Loop through all sessions, calling select on each.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/net/snmp/dispatcher.rb', line 5 def select(timeout = nil) total = 0 t = timeout t = nil if t == false catch :got_data do loop do if Net::SNMP.thread_safe Net::SNMP::Session.lock.synchronize { Net::SNMP::Session.sessions.each do |k, sess| total += sess.select(t) end } else Net::SNMP::Session.sessions.each do |k, sess| total += sess.select(t) end end throw :got_data if total > 0 throw :got_data unless timeout == false end end total end |
.thread_loop(options = {}) ⇒ Object
Start a poller loop in a seperate thread. You should first call Net::SNMP.thread_safe = true. options
-
:timeout Number of seconds to block on select. Will not block other threads.
-
:sleep Number of seconds to sleep if no data is available. Allows other threads to run. Default 0.2
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/net/snmp/dispatcher.rb', line 54 def thread_loop( = {}) timeout = [:timeout] || 0.2 sleep_time = [:sleep] || 0.2 Thread.new do loop do num_ready = select(timeout) if num_ready == 0 sleep(sleep_time) if sleep_time end end end end |