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 cna use it to do a similar integration into |
31 |
e.g. Event or other event loops. |
32 |
|
33 |
=cut |
34 |
|
35 |
package Net::SNMP::EV; |
36 |
|
37 |
no warnings; |
38 |
use strict; |
39 |
|
40 |
use Net::SNMP (); |
41 |
use EV (); |
42 |
|
43 |
our $VERSION = '0.1'; |
44 |
|
45 |
our @W; |
46 |
our $DISPATCHER = $Net::SNMP::DISPATCHER; |
47 |
|
48 |
# handle as many snmp events as possible |
49 |
sub drain { |
50 |
while () { |
51 |
$DISPATCHER->one_event; |
52 |
|
53 |
my $next = $DISPATCHER->{_event_queue_h} |
54 |
or return; |
55 |
|
56 |
return $next |
57 |
if !$next->[Net::SNMP::Dispatcher::_ACTIVE] |
58 |
|| $next->[Net::SNMP::Dispatcher::_TIME] > EV::now; |
59 |
} |
60 |
} |
61 |
|
62 |
our $PREPARE = EV::prepare sub { |
63 |
# has any events? |
64 |
if ($DISPATCHER->{_event_queue_h}) { |
65 |
# do some work |
66 |
my $next = drain |
67 |
or return; |
68 |
|
69 |
# register io watchers for all fds and a single timout |
70 |
@W = ( |
71 |
(map { EV::io $_, EV::READ, sub { } } |
72 |
keys %{ $DISPATCHER->{_descriptors} }), |
73 |
|
74 |
(EV::timer +($next->[Net::SNMP::Dispatcher::_ACTIVE] |
75 |
? $next->[Net::SNMP::Dispatcher::_TIME] - EV::now |
76 |
: 0), |
77 |
0, sub { }), |
78 |
); |
79 |
} |
80 |
}; |
81 |
|
82 |
our $CHECK = EV::check sub { |
83 |
# nuke the watchers again (usually their callbacks will not even be called) |
84 |
@W = (); |
85 |
|
86 |
drain |
87 |
if $DISPATCHER->{_event_queue_h}; |
88 |
}; |
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 |
110 |
|