SNMP/Dispatcher.pm

filter: Statementstmt 20.8% 25 / 120 Branchbran 2.6% 1 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 31.8% 7 / 22 Podpod 100% 8 / 8 totaltotal 20.3% 41 / 202 CC 23 SCAR 32.6
CC
23
Cov
16%
_send_pdu
72.0
_transport_response_received
56.0
register
20.0
CRAP 26.0
↑ Summary SNMP.pm »
1 package Mojo::SNMP::Dispatcher;
2 12 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Mojo::IOLoop::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Net::SNMP::MessageProcessing ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Net::SNMP::Message qw( TRUE FALSE );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use constant DEBUG => $ENV{MOJO_SNMP_DEBUG} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'MOJO_SNMP_DEBUG'} ? : 0 12 12
9  
10 0 ▶has ioloop => sub { Mojo::IOLoop->singleton };
Subroutine __ANON__: not called CC=1 SCAR=1.2
11 0 ▶has message_processing => sub { Net::SNMP::MessageProcessing->instance };
Subroutine __ANON__: not called CC=1 SCAR=1.2
12 12 has debug => 0; # Use MOJO_SNMP_DEBUG=1 instead
13  
14 0 ▶sub connections { int values %{$_[0]->{descriptors}} }
Subroutine connections: not called CC=1 SCAR=6.9
Pod documented
15  
16 0 ▶sub error {
Subroutine error: not called CC=3 SCAR=24.8
Pod documented
17 0 my ($self, $format, @args) = @_;
18  
19 0 ▶ return $self->{error} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
20 0 ▶ $self->{error} = defined $format ? sprintf $format, @args : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $format ? : 0 0 0
21 0 warn "[Mojo::SNMP::Dispatcher] error: $self->{error}\n" if DEBUG and defined $format;
22 0 return $self;
23 }
24  
25 0 ▶sub send_pdu {
Subroutine send_pdu: not called CC=2 SCAR=17.9
Pod documented
26 0 my ($self, $pdu, $delay) = @_;
27  
28 0 ▶ unless (ref $pdu) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $pdu) 0 0 0
29 0 $self->error('The required PDU object is missing or invalid');
30 0 return FALSE;
31 }
32  
33 0 $self->error(undef);
34 0 $self->schedule($delay, [_send_pdu => $pdu, $pdu->retries]);
35  
36 0 return TRUE;
37 }
38  
39 0 ▶sub return_response_pdu {
Subroutine return_response_pdu: not called CC=1 SCAR=6.9
Pod documented
40 0 $_[0]->send_pdu($_[1], -1);
41 }
42  
43 0 ▶sub msg_handle_alloc {
Subroutine msg_handle_alloc: not called CC=1 SCAR=6.9
Pod documented
44 0 $_[0]->message_processing->msg_handle_alloc;
45 }
46  
47 0 ▶sub schedule {
Subroutine schedule: not called CC=1 SCAR=6.9
Pod documented
48 0 my ($self, $time, $callback) = @_;
49 0 my $code = shift @$callback;
50  
51 0 warn "[Mojo::SNMP::Dispatcher] Schedule $time $code(@$callback)\n" if DEBUG;
52  
53 0 Scalar::Util::weaken($self);
54 0 ▶ $self->ioloop->timer($time => sub { $self->$code(@$callback) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 }
56  
57 0 ▶sub register {
Subroutine register: not called CC=4 SCAR=30.0
Pod documented
58 0 my ($self, $transport) = @_;
59 0 my $reactor = $self->ioloop->reactor;
60 0 my $fileno;
61  
62 0 ▶ unless (defined $transport and defined($fileno = $transport->fileno)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $transport and defined($fileno = $transport->fileno)) 0 0 0
Condition0%0/3
defined $transport and defined($fileno = $transport->fileno)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $transport and defined($fileno = $transport->fileno)
defined $transport defined($fileno = $transport->fileno)
Truth table0%0/3
A and B
A= defined $transportB= defined($fileno = $transport->fileno)
ABresult
0X0
100
111
63 0 $self->error('The Transport Domain object is invalid');
64 0 return FALSE;
65 }
66  
67 0 ▶ if ($self->{descriptors}{$fileno}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"descriptors"}{$fileno}++) 0 0 0
68 0 return $transport;
69 }
70  
71 0 Scalar::Util::weaken($self);
72 $reactor->io(
73 $transport->socket,
74 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 $self->_transport_response_received($transport);
76 }
77 0 );
78  
79 0 $reactor->watch($transport->socket, 1, 0);
80 0 warn "[Mojo::SNMP::Dispatcher] Add handler for descriptor $fileno\n" if DEBUG;
81 0 return $transport;
82 }
83  
84 0 ▶sub deregister {
Subroutine deregister: not called CC=2 SCAR=17.9
Pod documented
85 0 my ($self, $transport) = @_;
86 0 my $fileno = $transport->fileno;
87 0 ▶ return if --$self->{descriptors}{$fileno} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$self->{"descriptors"}{$fileno} > 0 0 0 0
88 0 delete $self->{descriptors}{$fileno};
89 0 warn "[Mojo::SNMP::Dispatcher] Remove handler for descriptor $fileno\n" if DEBUG;
90 0 $self->ioloop->reactor->remove($transport->socket);
91 }
92  
93 0 ▶sub _send_pdu {
Subroutine _send_pdu: not called CC=8 SCAR=42.8
94 0 my ($self, $pdu, $retries) = @_;
95 0 my $mp = $self->message_processing;
96 0 my $msg = $mp->prepare_outgoing_msg($pdu);
97  
98 0 ▶ unless (defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 0 0
99 0 warn "[Mojo::SNMP::Dispatcher] prepare_outgoing_msg: @{[$mp->error]}\n" if DEBUG;
100 0 $pdu->status_information($mp->error);
101 0 return;
102 }
103 0 ▶ unless (defined $msg->send) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->send) 0 0 0
104 0 ▶ if ($pdu->expect_response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdu->expect_response) 0 0 0
105 0 $mp->msg_handle_delete($msg->msg_id);
106 }
107 0 ▶ if ($retries-- > 0 and $!{EAGAIN} or $!{EWOULDBLOCK}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retries-- > 0 and $!{"EAGAIN"} or $!{"EWOULDBLOCK"}) 0 0 0
Condition0%0/6
$retries-- > 0 and $!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
000
$retries-- > 0 and $!{"EAGAIN"}
!ll&&!rl&&r
000
MC/DC0%0/3
$retries-- > 0 and $!{"EAGAIN"} or $!{"EWOULDBLOCK"}
$retries-- > 0 $!{"EAGAIN"} $!{"EWOULDBLOCK"}
Truth table0%0/5
A and B or C
A= $retries-- > 0B= $!{"EAGAIN"}C= $!{"EWOULDBLOCK"}
ABCresult
11X1
0X11
1011
0X00
1000
108 0 warn "[Mojo::SNMP::Dispatcher] Attempt to recover from temporary failure: $!\n" if DEBUG;
109 0 $self->schedule($pdu->timeout, [_send_pdu => $pdu, $retries]);
110 0 return FALSE;
111 }
112  
113 0 $pdu->status_information($msg->error);
114 0 return;
115 }
116  
117 0 ▶ if ($pdu->expect_response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdu->expect_response) 0 0 0
118 0 $self->register($msg->transport);
119 0 $msg->timeout_id($self->schedule($pdu->timeout, ['_transport_timeout', $pdu, $retries, $msg->msg_id,]));
120 }
121  
122 0 return TRUE;
123 }
124  
125 0 ▶sub _transport_timeout {
Subroutine _transport_timeout: not called CC=2 SCAR=17.9
126 0 my ($self, $pdu, $retries, $handle) = @_;
127  
128 0 $self->deregister($pdu->transport);
129 0 $self->message_processing->msg_handle_delete($handle);
130  
131 0 ▶ if ($retries-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retries-- > 0) { } 0 0 0
132 0 warn "[Mojo::SNMP::Dispatcher] Retries left: $retries\n" if DEBUG;
133 0 return $self->_send_pdu($pdu, $retries);
134 }
135 else {
136 0 warn "[Mojo::SNMP::Dispatcher] No response from remote host @{[ $pdu->hostname ]}\n" if DEBUG;
137 0 $pdu->status_information(q{No response from remote host "%s"}, $pdu->hostname);
138 0 return;
139 }
140 }
141  
142 0 ▶sub _transport_response_received {
Subroutine _transport_response_received: not called CC=7 SCAR=40.3
143 0 my ($self, $transport) = @_;
144 0 my $mp = $self->message_processing;
145 0 my ($msg, $error) = Net::SNMP::Message->new(-transport => $transport);
146  
147 0 $self->error(undef);
148  
149 0 ▶ if (not defined $msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 0 0
150 0 die sprintf 'Failed to create Message object: %s', $error;
151 }
152 0 ▶ if (not defined $msg->recv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg->recv) 0 0 0
153 0 $self->error($msg->error);
154 0 ▶ $self->deregister($transport) unless $transport->connectionless;
Branch: 1 branch
BranchTrueFalseTotal
unless $transport->connectionless 0 0 0
155 0 return;
156 }
157 0 ▶ if (not $msg->length) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->length) 0 0 0
158 0 warn "[Mojo::SNMP::Dispatcher] Ignoring zero length message\n" if DEBUG;
159 0 return;
160 }
161 0 ▶ if (not defined $mp->prepare_data_elements($msg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mp->prepare_data_elements($msg)) 0 0 0
162 0 $self->error($mp->error);
163 0 return;
164 }
165 0 ▶ if ($mp->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mp->error) 0 0 0
166 0 $msg->error($mp->error);
167 }
168  
169 0 warn "[Mojo::SNMP::Dispatcher] Processing pdu\n" if DEBUG;
170 0 $self->ioloop->remove($msg->timeout_id);
171 0 $self->deregister($transport);
172 0 $msg->process_response_pdu;
173 }
174  
175 12 1;
176  
177 =encoding utf8
178  
179 =head1 NAME
180  
181 Mojo::SNMP::Dispatcher - Instead of Net::SNMP::Dispatcher
182  
183 =head1 DESCRIPTION
184  
185 This module works better with L<Mojo::IOLoop> since it register the
186 L<IO::Socket::INET> sockets in with the mojo reactor.
187  
188 =head1 ATTRIBUTES
189  
190 =head2 ioloop
191  
192 Holds a L<Mojo::IOLoop> object. Same as L<Mojo::SNMP/ioloop>.
193  
194 =head2 message_processing
195  
196 Holds an instance of L<Net::SNMP::MessageProcessing>.
197  
198 =head2 debug
199  
200 Does nothing. Use C<MOJO_SNMP_DEBUG=1> instead to get debug information.
201  
202 =head2 error
203  
204 Holds the last error.
205  
206 =head2 connections
207  
208 Holds the number of active sockets.
209  
210 =head1 METHODS
211  
212 =head2 send_pdu
213  
214 This method will send a PDU to the SNMP server.
215  
216 =head2 return_response_pdu
217  
218 No idea what this does (?)
219  
220 =head2 msg_handle_alloc
221  
222 No idea what this does (?)
223  
224 =head2 schedule
225  
226 Used to schedule events at a given time. Use L<Mojo::IOLoop/timer> to
227 do the heavy lifting.
228  
229 =head2 register
230  
231 Register a new transport object with L<Mojo::IOLoop::Reactor>.
232  
233 =head2 deregister
234  
235 The opposite of L</register>.
236  
237 =head1 COPYRIGHT & LICENSE
238  
239 This library is free software. You can redistribute it and/or modify
240 it under the same terms as Perl itself.
241  
242 =head1 AUTHOR
243  
244 Jan Henning Thorsen - C<jhthorsen@cpan.org>
245  
246 =cut
↑ Summary SNMP.pm »