Analysis/Dispatcher.pm

filter: Statementstmt 92.5% 62 / 67 Branchbran 72.2% 13 / 18 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 12 / 12 Podpod 75.0% 3 / 4 totaltotal 87.5% 91 / 104 CC 12 SCAR 12.5
CC
12
Cov
87%
add_listener
5.9
emit_event
5.0
_invoke_callbacks
4.0
CRAP 3.5
« Analysis/Listener/Base.pm ↑ Summary Analysis/Listener/Example1.pm »
1 package Net::Analysis::Dispatcher;
2 # $Id: Dispatcher.pm 131 2005-10-02 17:24:31Z abworrall $
3  
4 8 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 our $VERSION = '0.01';
6 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use overload q("") => sub { $_[0]->as_string() }; # OO style stringify
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
9 8 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # {{{ POD
14  
15 =head1 NAME
16  
17 Net::Analysis::Dispatcher - handle the event stuff for the proto analysers
18  
19 =head1 SYNOPSIS
20  
21 use Net::Analysis::Dispatcher;
22  
23 my $d = Net::Analysis::Dispatcher->new();
24 my $listener = Net::Analysis::Listener::TCP->new();
25 $d->add_listener (listener => $listener);
26  
27 =head1 DESCRIPTION
28  
29 This class is used to register listener objects. Whenever any of the objects
30 emit an event, the dispatcher is used to make sure other interested listeners
31 receive the event.
32  
33 =cut
34  
35 # }}}
36  
37 # {{{ new
38  
39 # {{{ POD
40  
41 =head2 new ()
42  
43 Takes no arguments, tells no lies.
44  
45 =cut
46  
47 # }}}
48  
49 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
50 4 my ($class) = shift;
51  
52 4 my %h;
53  
54 4 $h{listeners} = []; # List of objects that are listening to events
55  
56 4 my ($self) = bless (\%h, $class);
57  
58 4 return $self;
59 }
60  
61 # }}}
62  
63 # {{{ add_listener
64  
65 =head2 add_listener (listener => $obj, config => $hash)
66  
67 This method adds a new listener to the list of things to be notified of each
68 event.
69  
70 If the listener object has a field C<pos>, then we attempt to put the listener
71 in that position in the event queue. Valid values are C<first> and C<last>, to
72 receive events first and last. Listener::TCP likes to be first, since it adds
73 extra info to the C<tcp_packet> that other modules might like to see.
74  
75 If a listener has already claimed the first or last spot, then we croak with an
76 error.
77  
78 =cut
79  
80 9 ▶sub add_listener {
Subroutine add_listener: called CC=5 SCAR=17.8
Pod documented
81 9 my ($self) = shift;
82  
83 9 my %h = validate (@_, { listener => 1, #{ can => "emit" }, <-- broken :(
84 config => { default => {} },
85 });
86  
87 # XXXX workaround issue where Params::Validate rejects mocked methods
88 9 ▶ if (!$h{listener}->can('emit')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($h{"listener"}->can("emit")) 0 9 9
89 0 carp "add_listener needs an object that can ->emit() !\n";
90 0 return undef;
91 }
92  
93 9 ▶ if (exists $h{listener}{pos}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h{'listener'}{'pos'}) { } 2 7 9
94 2 ▶ if ($h{listener}{pos} !~ /^(first|last)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($h{"listener"}{"pos"} =~ /^(first|last)$/) 0 2 2
95 0 croak "$h{listener} has invalid pos; $h{listener}{pos}\n";
96 }
97 2 ▶ if (exists $self->{pos}{$h{listener}{pos}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"pos"}{$h{"listener"}{"pos"}}) 0 2 2
98 0 croak "position '$h{listener}{pos}' taken; bad $h{listener}\n";
99 }
100 2 $self->{pos}{$h{listener}{pos}} = $h{listener};
101  
102 } else {
103 7 push (@{$self->{listeners}}, $h{listener});
104 }
105 }
106  
107 # }}}
108 # {{{ emit_event
109  
110 =head2 emit_event (name => 'event_name', args => $hash)
111  
112 The name must be a valid Perl function name. By convention, it should start
113 with the name of the module that is emitting the event (e.g.
114 C<http_transaction>).
115  
116 Where your code is emitting events, it must must document the args in detail,
117 so that listeners will know what to do with them.
118  
119 This method runs through the listener list, and if appropriate, invokes the
120 listening method for each listener.
121  
122 A listener gets the event if it has a method which has the same name as the
123 C<event_name>.
124  
125 =cut
126  
127 4 ▶sub emit_event {
Subroutine emit_event: called CC=5 SCAR=16.2
Pod documented
128 4 my $self = shift;
129  
130 4 my %h = @_;
131 4 ▶ $h{args} ||= {};
Condition50%1/2
$h{"args"} ||= {}
l!l
40
MC/DC0%0/1
$h{"args"} ||= {}
$h{"args"}
Truth table50%1/2
A
A= $h{"args"}
Aresult
11
00
132  
133 4 ▶ if ($self->{_i_am_invoking}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_i_am_invoking"}) 0 4 4
134 # warn "Argh, circular mayhem ($h{name})\n"; exit;
135 }
136  
137 ## Adverse performance impacts, so commented out
138 # my %h = validate (@_, { name => { regex => qr/^[a-z][a-z0-9_]+$/ },
139 # args => { default => {} },
140 # });
141  
142 # If we have any listeners that wanted a special place in the queue, then
143 # give it to them. This stuff will only trigger on the very first event.
144 4 ▶ if (exists $self->{pos}{first}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"pos"}{"first"}) 1 3 4
145 1 unshift (@{$self->{listeners}}, delete ($self->{pos}{first}));
146 }
147 4 ▶ if (exists $self->{pos}{last}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"pos"}{"last"}) 1 3 4
148 1 push (@{$self->{listeners}}, delete ($self->{pos}{last}));
149 }
150  
151 4 $self->_invoke_callbacks (\%h);
152 }
153  
154 # }}}
155  
156 # {{{ as_string
157  
158 2 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
159 2 my ($self) = @_;
160 2 my $s = '';
161  
162 2 $s .= "Dispatching to [".join(',', map {"$_"} @{$self->{listeners}})."]";
163  
164 2 return $s;
165 }
166  
167 # }}}
168  
169 # {{{ _invoke_callbacks
170  
171 4 ▶sub _invoke_callbacks {
Subroutine _invoke_callbacks: called CC=4 SCAR=13.9
172 4 my $self = shift;
173 4 my ($h) = @_;
174  
175 4 $self->{_i_am_invoking} = 1;
176  
177 # Memoise this iteration & 'can' call ? Results won't change !
178 4 foreach my $l (@{$self->{listeners}}) {
179 11 my $method = $h->{name};
180 11 ▶ if ($l->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->can($method)) 10 1 11
181 10 eval {
182 10 $l->$method($h->{args});
183 10 ▶ }; if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 10 10
184 0 carp ("Listener '$l' die()d on method $h->{name}:\n$@");
185 }
186 }
187 }
188  
189 4 delete ($self->{_i_am_invoking});
190 }
191  
192 # }}}
193  
194  
195 8 1;
196 __END__
« Analysis/Listener/Base.pm ↑ Summary Analysis/Listener/Example1.pm »