Sprocket.pm

filter: Statementstmt 90.1% 137 / 152 Branchbran 52.5% 21 / 40 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 96.8% 31 / 32 Podpod 30.7% 4 / 13 totaltotal 79.7% 197 / 247 CC 27 SCAR 11.6
CC
27
Cov
81%
get_connection
20.0
get_plugin
6.0
_resolve_session
5.2
CRAP 3.2
« Sprocket/Local.pm ↑ Summary Sprocket/Logger/Log4perl.pm »
1 package Sprocket;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 our $VERSION = '0.07';
7  
8 13 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Sprocket::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 our $sprocket;
13 8 our $sprocket_aio;
14  
15 13 ▶use Sprocket::AIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 ▶use Sprocket::Util::Observable;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use base qw( Sprocket::Util::Observable );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # weak list of all sprocket components
22 8 our %COMPONENTS;
23 8 our %PLUGINS;
24  
25 # events sent to process_plugins
26 sub EVENT_NAME() { 0 }
27 sub SERVER() { 1 }
28 sub CONNECTION() { 2 }
29  
30  
31 52 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
32 52 shift;
33  
34 52 my @modules = @_;
35  
36 52 unshift( @modules, 'Common' );
37 52 @modules = map { 'Sprocket::'.$_ } @modules;
38
39 52 unshift( @modules, 'POE' );
40  
41 52 my $package = caller();
42 52 my @failed;
43  
44 52 foreach my $module ( @modules ) {
45 143 my $code = "package $package; use $module;";
46 13 ▶ eval( $code );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
47 143 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 138 138
48 5 warn $@;
49 5 push( @failed, $module );
50 }
51 }
52  
53 52 ▶ unless ( defined( $sprocket ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Sprocket::sprocket) 8 39 47
54 13 Sprocket->new();
55 }
56  
57 {
58 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 52 *{ $package . '::sprocket' } = \$sprocket;
60 }
61  
62 52 ▶ @failed and croak 'could not import (' . join( ' ', @failed ) . ')';
Branch: 1 branch
BranchTrueFalseTotal
@failed and croak('could not import (' . join(" ", @failed) . ')') 0 47 47
63 }
64  
65 13 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
66 13 my $class = shift;
67 13 ▶ croak "$class requires an even number of parameters" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 8 8
68 13 ▶ return $sprocket if ( defined( $sprocket ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $Sprocket::sprocket 0 8 8
69  
70 13 my $self = $sprocket = $class->SUPER::new( @_ );
71 13 $self->{_uuid} = gen_uuid( $self );
72
73 13 $self->register_hook( [qw(
74 sprocket.component.add
75 sprocket.component.remove
76 sprocket.plugin.add
77 sprocket.plugin.remove
78 sprocket.shutdown
79 )] );
80  
81 13 return $self;
82 }
83  
84 15 ▶sub add_plugin {
Subroutine add_plugin: called CC=1 SCAR=0.0
Pod undocumented
85 14 my $self = shift;
86 14 my $uuid = $_[ 0 ]->uuid;
87
88 14 $PLUGINS{ $uuid } = $_[ 0 ];
89 14 weaken( $PLUGINS{ $uuid } );
90
91 14 $self->broadcast( 'sprocket.plugin.add', {
92 source => $self,
93 target => $_[ 0 ],
94 } );
95
96 14 return;
97 }
98  
99 5 ▶sub remove_plugin {
Subroutine remove_plugin: called CC=2 SCAR=10.1
Pod undocumented
100 4 my ( $self, $uuid ) = @_;
101  
102 4 $self->broadcast( 'sprocket.plugin.remove', {
103 source => $self,
104 target => $uuid,
105 } );
106
107 # supplied the object, get the uuid from it
108 4 ▶ $uuid = $uuid->uuid if ( ref( $uuid ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $uuid 0 0 0
109  
110 0 delete $PLUGINS{ $uuid };
111
112 0 return;
113 }
114  
115 15 ▶sub add_component {
Subroutine add_component: called CC=1 SCAR=0.0
Pod undocumented
116 10 my $self = shift;
117 10 my $uuid = $_[ 0 ]->uuid;
118
119 10 $COMPONENTS{ $uuid } = $_[ 0 ];
120 10 weaken( $COMPONENTS{ $uuid } );
121
122 10 $self->broadcast( 'sprocket.component.add', {
123 source => $self,
124 target => $_[ 0 ],
125 } );
126
127 10 return;
128 }
129  
130 17 ▶sub remove_component {
Subroutine remove_component: called CC=5 SCAR=16.2
Pod undocumented
131 12 my ( $self, $uuid ) = @_;
132  
133 12 $self->broadcast( 'sprocket.component.remove', {
134 source => $self,
135 target => $uuid,
136 } );
137
138 12 ▶ $uuid = $uuid->uuid if ( ref( $uuid ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $uuid 12 0 12
139
140 12 my $count = 0;
141 12 delete $COMPONENTS{ $uuid };
142 12 foreach my $id ( keys %COMPONENTS ) {
143 5 ▶ next unless defined( $COMPONENTS{ $id } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Sprocket::COMPONENTS{$id} 0 5 5
144 5 $count++;
145 }
146  
147 12 ▶ $self->finalize_shutdown() if ( $count == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 7 5 12
148  
149 12 return $count;
150 }
151  
152 12 ▶sub finalize_shutdown {
Subroutine finalize_shutdown: called CC=3 SCAR=11.0
Pod undocumented
153 7 my $self = shift;
154
155 # this will self elimiate double calls
156 7 ▶ return if ( $self->{__SHUTDOWN__}++ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__SHUTDOWN__"}++ 2 5 7
157
158 5 ▶ $sprocket_aio->shutdown()
Branch: 1 branch
BranchTrueFalseTotal
if $Sprocket::sprocket_aio 0 5 5
159 if ( $sprocket_aio );
160  
161 5 $self->broadcast( 'sprocket.shutdown', {
162 source => $self,
163 } );
164  
165 5 $self->clear_hooks();
166
167 5 return;
168 }
169  
170 5 ▶sub get_components {
Subroutine get_components: called CC=1 SCAR=6.9
Pod undocumented
171 # XXX does this make our refs strong again?
172 0 return [ values %COMPONENTS ];
173 }
174  
175 5 ▶sub get_connection {
Subroutine get_connection: called CC=4 SCAR=30.0
Pod undocumented
176 0 my $uuid = $_[ 1 ];
177  
178 0 foreach my $id ( keys %COMPONENTS ) {
179 0 ▶ next unless ( defined( $COMPONENTS{ $id } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Sprocket::COMPONENTS{$id} 0 0 0
180 0 ▶ if ( my $con = $COMPONENTS{ $id }->get_connection( $uuid, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $con = $Sprocket::COMPONENTS{$id}->get_connection($uuid, 1)) 0 0 0
181 0 return $con;
182 }
183 }
184  
185 0 return undef;
186 }
187  
188 7 ▶sub shutdown_all {
Subroutine shutdown_all: called CC=4 SCAR=14.0
Pod documented
189 3 my $self = shift;
190
191 3 my $count = 0;
192 3 foreach my $id ( keys %COMPONENTS ) {
193 5 ▶ next unless ( defined( $COMPONENTS{ $id } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Sprocket::COMPONENTS{$id} 0 5 5
194 5 $COMPONENTS{ $id }->shutdown( @_ );
195 5 $count++;
196 }
197
198 3 ▶ $self->finalize_shutdown() if ( $count == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 0 3 3
199
200 3 return $count;
201 }
202  
203 4 ▶sub get_plugin {
Subroutine get_plugin: called CC=2 SCAR=17.9
Pod undocumented
204 0 my $uuid = $_[ 1 ];
205
206 0 ▶ return defined( $PLUGINS{ $uuid } ) ? $PLUGINS{ $uuid } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $Sprocket::PLUGINS{$uuid} ? : 0 0 0
207 }
208  
209 9 ▶sub callback {
Subroutine callback: called CC=1 SCAR=0.0
Pod documented
210 5 my ( $self, $ses, $event, @etc ) = @_;
211
212 5 my $id = $self->_resolve_session( $ses );
213  
214 12 ▶ return Sprocket::AnonCallback->new( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
215 12 $poe_kernel->call( $id => $event => @etc => @_ );
216 5 }, $id );
217 }
218  
219 2 ▶sub postback {
Subroutine postback: called CC=1 SCAR=0.0
Pod documented
220 2 my ( $self, $ses, $event, @etc ) = @_;
221
222 2 my $id = $self->_resolve_session( $ses );
223  
224 2 ▶ return Sprocket::AnonCallback->new( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
225 2 $poe_kernel->post( $id => $event => @etc => @_ );
226 2 return;
227 2 }, $id );
228 }
229  
230 7 ▶sub _resolve_session {
Subroutine _resolve_session: called CC=5 SCAR=16.4
231 7 my ( $self, $ses ) = @_;
232  
233 7 ▶ if ( defined( $ses ) && $ses =~ m/^\d+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ses and $ses =~ /^\d+$/) { } 4 3 7
elsif (UNIVERSAL::can($ses, 'ID')) { } 2 1 3
Condition66%2/3
defined $ses and $ses =~ /^\d+$/
!ll&&!rl&&r
034
MC/DC50%1/2
defined $ses and $ses =~ /^\d+$/
defined $ses $ses =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $sesB= $ses =~ /^\d+$/
ABresult
0X0
100
111
234 4 return $ses;
235 } elsif ( UNIVERSAL::can( $ses, 'ID' ) ) {
236 2 return $ses->ID();
237 } else {
238 1 my $s = $poe_kernel->alias_resolve( $ses );
239 1 ▶ return $s->ID() if ( $s );
Branch: 1 branch
BranchTrueFalseTotal
if $s 1 0 1
240 }
241
242 0 return $poe_kernel->get_active_session()->ID();
243 }
244  
245 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
246 0 shift;
247 0 return $poe_kernel->run( @_ );
248 }
249  
250 8 1;
251  
252 package Sprocket::AnonCallback;
253  
254 13 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
255  
256 8 our %callback_ids;
257  
258 7 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
259 7 my ( $class, $cb, $id ) = @_;
260
261 7 ▶ my $self = bless( $cb, ref $class || $class );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
262  
263 $poe_kernel->refcount_increment(
264 7 $Sprocket::AnonCallback::callback_ids{$self} = $id,
265 __PACKAGE__
266 );
267  
268 7 return $self;
269 }
270  
271 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
272 7 my $self = shift;
273 7 my $id = delete $Sprocket::AnonCallback::callback_ids{"$self"};
274  
275 7 ▶ if ( defined( $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) { } 7 0 7
276 7 $poe_kernel->refcount_decrement( $id, __PACKAGE__ );
277 } else {
278 0 warn "connection callback DESTROY without session_id to refcount_decrement";
279 }
280  
281 7 return;
282 }
283  
284 8 1;
285  
286 __END__
« Sprocket/Local.pm ↑ Summary Sprocket/Logger/Log4perl.pm »