| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | Net::SNMP::EV - adaptor to integrate Net::SNMP into the EV event loop. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | use EV; | ||||||||||||||||||||||||||||
| 8 | use Net::SNMP; | ||||||||||||||||||||||||||||
| 9 | use Net::SNMP::EV; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # just use Net::SNMP and EV as you like: | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | ... start non-blocking snmp request(s)... | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | EV::loop; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | This module coerces the Net::SNMP scheduler to use the EV high performance | ||||||||||||||||||||||||||||
| 20 | event loop as underlying event loop, i.e. EV will be used by Net::SNMP for | ||||||||||||||||||||||||||||
| 21 | all events. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | This integrates Net::SNMP into EV: You can make non-blocking Net::SNMP | ||||||||||||||||||||||||||||
| 24 | calls and as long as your main program uses the EV event loop, they will | ||||||||||||||||||||||||||||
| 25 | run in parallel to anything else that uses EV or AnyEvent. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | This module does not export anything and does not require you to do | ||||||||||||||||||||||||||||
| 28 | anything special apart from loading it. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | The module is quite short, you can use it as example to implement a | ||||||||||||||||||||||||||||
| 31 | similar integration into e.g. Event or other event loops. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | package Net::SNMP::EV; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use Net::SNMP (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use EV (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | our @W; | |||||||||||||||||||||||||||
| 46 | 1 | our $DISPATCHER = $Net::SNMP::DISPATCHER; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # handle as many snmp events as possible | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub drain { | ||||||||||||||||||||||||||
|
Subroutine
drain: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 0 | while () { | |||||||||||||||||||||||||||
| 51 | 0 | $DISPATCHER->one_event; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | my $next = $DISPATCHER->{_event_queue_h} | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return $next | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $$next[Net::SNMP::Dispatcher::_ACTIVE()] or $$next[Net::SNMP::Dispatcher::_TIME()] > &EV::now()
MC/DC0%0/2
not $$next[Net::SNMP::Dispatcher::_ACTIVE()] or $$next[Net::SNMP::Dispatcher::_TIME()] > &EV::now()
not $$next[Net::SNMP::Dispatcher::_ACTIVE()]
$$next[Net::SNMP::Dispatcher::_TIME()] > &EV::now()
Truth table0%0/3
not(A) or B
A= not $$next[Net::SNMP::Dispatcher::_ACTIVE()]B= $$next[Net::SNMP::Dispatcher::_TIME()] > &EV::now()
| |||||||||||||||||||||||||||||
| 57 | if !$next->[Net::SNMP::Dispatcher::_ACTIVE] | ||||||||||||||||||||||||||||
| 58 | || $next->[Net::SNMP::Dispatcher::_TIME] > EV::now; | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | our $PREPARE = EV::prepare sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 63 | # has any events? | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ($DISPATCHER->{_event_queue_h}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | # do some work | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my $next = drain | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | or return; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # register io watchers for all fds and a single timout | ||||||||||||||||||||||||||||
| 70 | @W = ( | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | (map { EV::io $_, EV::READ, sub { } } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | keys %{ $DISPATCHER->{_descriptors} }), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | (EV::timer +($next->[Net::SNMP::Dispatcher::_ACTIVE] | ||||||||||||||||||||||||||||
| 75 | ? $next->[Net::SNMP::Dispatcher::_TIME] - EV::now | ||||||||||||||||||||||||||||
| 76 | : 0), | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | 0, sub { }), | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 1 | }; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | our $CHECK = EV::check sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 83 | # nuke the watchers again (usually their callbacks will not even be called) | ||||||||||||||||||||||||||||
| 84 | 0 | @W = (); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | drain | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if $DISPATCHER->{_event_queue_h}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | }; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head1 BUGS | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Net::SNMP has no (documented or otherwise) API to do what this module | ||||||||||||||||||||||||||||
| 93 | does. As such, this module rummages around in the internals of Net::SNMP | ||||||||||||||||||||||||||||
| 94 | in a rather inacceptable way, and as thus might be very sensitive to the | ||||||||||||||||||||||||||||
| 95 | version of Net::SNMP used (it has been tested with some 5.x versions | ||||||||||||||||||||||||||||
| 96 | only, YMMV). | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | L<EV>, L<Net::SNMP>, L<AnyEvent>, L<Glib::EV>. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Marc Lehmann <schmorp@schmorp.de> | ||||||||||||||||||||||||||||
| 105 | http://home.schmorp.de/ | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | 1 | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||