Lambda/SNMP.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 188 CC 21 SCAR 44.4
CC
21
Cov
0%
reshuffle_fds
210.0
wrapper
30.0
yield
6.0
CRAP 85.1
1 # $Id: SNMP.pm,v 1.20 2009/09/18 09:38:00 dk Exp $
2 package IO::Lambda::SNMP;
3 use vars qw(
4 $DEBUG
5 @ISA @EXPORT_OK %EXPORT_TAGS
6 $MASTER %ACTIVE_FDS %PASSIVE_FDS
7 @TIMER $TIMER_ACTIVE
8 );
9 @ISA = qw(Exporter);
10 my @methods = qw(get fget getnext fgetnext set bulkwalk);
11 @EXPORT_OK = map { "snmp$_" } @methods;
12 %EXPORT_TAGS = ( all => \@EXPORT_OK);
13 $DEBUG = $IO::Lambda::DEBUG{snmp} || 0;
14
15 use strict;
16 use warnings;
17 use SNMP;
18 use IO::Handle;
19 use Exporter;
20 use Time::HiRes qw(time);
21 use IO::Lambda qw(:all :dev);
22
23 # $DEBUG = 1;
24
25 #
26 # Part I: Lower-level event loop interactions
27 #
28 # Create a singleton object that will receive yield notification
29 # and that will be passed as WATCH_OBJ for all lower-level events
30 # to not involve upper-level IO::Lambda event mechanisms. In this
31 # part, we talk to the loop directly because SNMP has its own
32 # event loop.
33 #
34 # Also note that this implementation allows for use of SNMP with native
35 # callbacks together with lambdas.
36 $MASTER = bless {}, __PACKAGE__;
37
38 # register yield handler
39 IO::Lambda::add_loop($MASTER);
40 END { IO::Lambda::remove_loop($MASTER) };
41
42 sub remove {}
43
44 sub empty { 0 == keys %ACTIVE_FDS and 0 == keys %PASSIVE_FDS }
45
46 sub yield
47 {
48 warn "snmp.yield\n" if $DEBUG;
49 SNMP::MainLoop(1e-6);
50 reshuffle_fds();
51 }
52 # Use the same $MASTER for the lambda emulator and do not call anything in the handler,
53 # but do that in yield()
54 sub io_handler
55 {
56 my ( undef, $rec) = @_;
57 my $fileno = fileno($rec->[WATCH_IO_HANDLE]);
58 warn "snmp.io_handler[$fileno]\n" if $DEBUG;
59 $PASSIVE_FDS{$fileno} = delete $ACTIVE_FDS{$fileno};
60 }
61
62 # There'll also be a single timer as SNMP loop needs timeouts
63 $TIMER[WATCH_OBJ] = bless {}, "IO::Lambda::Loop::SNMP::Timer";
64 sub IO::Lambda::Loop::SNMP::Timer::io_handler { $TIMER_ACTIVE = 0 }
65
66 # Get all fds monitored by SNMP, and monitor these by ourselves.
67 # Return number of events passed (and therefore resubmitted)
68 sub reshuffle_fds
69 {
70 my $resubmitted = 0;
71
72 my ( $timeout, @fds) = SNMP::select_info;
73 @fds = grep { defined } @fds;
74 if ( @fds) {
75 $timeout = 1e-6 if defined($timeout) && $timeout == 0;
76 } else {
77 undef $timeout;
78 }
79
80 # kill old handles
81 my %all = map { $_ => 1 } @fds;
82 for my $old ( grep { not exists $all{$_} } keys %ACTIVE_FDS) {
83 $IO::Lambda::LOOP-> remove_event( delete $ACTIVE_FDS{$old});
84 warn "snmp.remove: $old\n" if $DEBUG;
85 }
86
87 # resubmit handles that were fired off
88 for my $passive ( grep { exists $all{$_} } keys %PASSIVE_FDS) {
89 $resubmitted++;
90 $IO::Lambda::LOOP-> watch( $ACTIVE_FDS{$passive} = $PASSIVE_FDS{$passive} );
91 warn "snmp.resubmit: $passive\n" if $DEBUG;
92 }
93 %PASSIVE_FDS = ();
94
95 # register new handles
96 for my $new ( grep { not exists $ACTIVE_FDS{$_} } @fds) {
97
98 warn "snmp.listen: $new\n" if $DEBUG;
99
100 my $fh = IO::Handle-> new;
101 unless ( open( $fh, "<&=$new")) {
102 warn "cannot dup($new):$!\n";
103 next;
104 }
105
106 # construct a fake IO::Lambda event record
107 my @rec;
108 $rec[WATCH_OBJ] = $MASTER;
109 $rec[WATCH_IO_HANDLE] = $fh;
110 $rec[WATCH_IO_FLAGS] = IO_READ;
111
112 $IO::Lambda::LOOP-> watch( $ACTIVE_FDS{$new} = \@rec);
113 }
114
115 # timer
116 if ( $timeout) {
117 my $deadline = time + $timeout;
118 if ( $TIMER_ACTIVE) {
119 if ( abs( $deadline - $TIMER[WATCH_DEADLINE]) > 0.001) {
120 # restart the active timer
121 warn "snmp.timer restart $timeout $deadline/$TIMER[WATCH_DEADLINE]\n"
122 if $DEBUG;
123 $IO::Lambda::LOOP-> remove_event( \@TIMER);
124 $TIMER[WATCH_DEADLINE] = $deadline;
125 $IO::Lambda::LOOP-> after( \@TIMER);
126 }
127 # else, same timeout, on already active timer - do nothing
128 } else {
129 # resubmit
130 warn "snmp.timer resubmit $timeout\n" if $DEBUG;
131 $TIMER[WATCH_DEADLINE] = $deadline;
132 $IO::Lambda::LOOP-> after( \@TIMER);
133 $TIMER_ACTIVE = 1;
134 $resubmitted++;
135 }
136 } elsif ( $TIMER_ACTIVE) {
137 warn "snmp.timer stop\n" if $DEBUG;
138 # stop timer
139 $IO::Lambda::LOOP-> remove_event( \@TIMER);
140 $TIMER_ACTIVE = 0;
141 }
142
143 return $resubmitted;
144 }
145
146 # Part II - building on SNMP callback mechanism, provide lambda interface
147
148 sub snmpcallback
149 {
150 my ($q, $c) = (shift, shift);
151
152 warn "snmp.cb: $q\n" if $DEBUG;
153 $q-> resolve($c);
154 $q-> terminate(@_);
155 undef $c;
156 undef $q;
157 }
158
159
160 sub wrapper
161 {
162 my ( $cb, $method, $caller) = @_;
163
164 return this-> override_handler( $method, $caller, $cb)
165 if this-> {override}->{$method};
166
167 my ( $session, @param ) = context;
168 _subname( $method, $cb, 1) if $cb;
169
170 # the caller will listen to a new lambda
171 my $q = IO::Lambda-> new;
172 my $c = $q-> bind;
173 this-> add_tail( $cb, $caller, $q, context);
174
175 # fire an snmp request
176 my $ok = $session-> $method(
177 @param,
178 [ \&snmpcallback, $q, $c ]
179 );
180
181 return $q-> resolve($c) unless $ok;
182
183 reshuffle_fds();
184
185 # don't set up timers and fd listeners yet, yield() will do that
186 warn "snmp.call: $method($q)\n" if $DEBUG;
187 }
188
189 for ( @methods) {
190 eval "sub snmp$_(&) { wrapper( shift, '$_', \\&snmp$_ ) }";
191 die $@ if $@;
192 }
193
194 1;
195
196 __DATA__