Sprocket/Plugin.pm

filter: Statementstmt 51.5% 67 / 130 Branchbran 21.8% 7 / 32 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 53.1% 17 / 32 Podpod 85.0% 17 / 20 totaltotal 49.3% 113 / 229 CC 20 SCAR 17.5
CC
20
Cov
46%
con_list
20.0
spread_subscribe
12.0
spread_unsubscribe
12.0
CRAP 5.8
« Sprocket/AIO.pm ↑ Summary Sprocket/Util/Observable.pm »
1 package Sprocket::Plugin;
2  
3 5 ▶use Class::Accessor::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Scalar::Util qw( weaken blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Sprocket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Sprocket::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Errno qw( EADDRINUSE );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 __PACKAGE__->mk_accessors( qw( uuid _uuid name parent_id ID ) );
12  
13 5 ▶use overload '""' => sub { shift->ID() };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Sprocket::Spread import will replace this
19 # when imported on demand
20 5 our $sprocket_spread;
21  
22 5 our %plugin_event_list = map { $_ => 1 } qw(
23 local_accept
24 local_connected
25 local_receive
26 local_disconnected
27 local_time_out
28 local_error
29  
30 remote_accept
31 remote_connected
32 remote_receive
33 remote_disconnected
34 remote_connect_error
35 remote_time_out
36  
37 plugin_start_aio
38 add_plugin
39 );
40  
41 5 our %plugin_event_discon = map { $_ => 1 } qw(
42 local_disconnected
43  
44 remote_disconnected
45 remote_connect_error
46 );
47  
48 10 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
49 10 my $class = shift;
50
51 10 ▶ my $self = bless( {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
52 __conlist__ => {},
53 &adjust_params,
54 }, ref $class || $class );
55  
56 # unique uuid, different for each instance
57 10 $self->uuid( new_uuid() );
58
59 10 $self->ID( $class.'/'.$self->uuid );
60
61 # uuid based off of the plugin's ref
62 10 $self->_uuid( gen_uuid( $self ) );
63  
64 10 $sprocket->add_plugin( $self );
65
66 10 return $self;
67 }
68  
69 61 ▶sub handle_event {
Subroutine handle_event: called CC=6 SCAR=18.6
Pod undocumented
70 61 my ( $self, $event ) = ( shift, shift );
71
72 delete $self->{__conlist__}->{ $_[ 1 ]->ID }
73 61 ▶ if ( $self->{__conlist__} && exists( $plugin_event_discon{ $event } ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__conlist__"} and exists $Sprocket::Plugin::plugin_event_discon{$event} 8 53 61
Condition66%2/3
$self->{"__conlist__"} and exists $Sprocket::Plugin::plugin_event_discon{$event}
!ll&&!rl&&r
0538
MC/DC50%1/2
$self->{"__conlist__"} and exists $Sprocket::Plugin::plugin_event_discon{$event}
$self->{"__conlist__"} exists $Sprocket::Plugin::plugin_event_discon{$event}
Truth table66%2/3
A and B
A= $self->{"__conlist__"}B= exists $Sprocket::Plugin::plugin_event_discon{$event}
ABresult
0X0
100
111
74
75 61 ▶ if ( $self->can( $event ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($event)) { } 40 21 61
76 40 $self->$event( @_ );
77 } else {
78 $self->_log( v => $self->{log_unhandled_events}, msg => "unhandled plugin event: $event" )
79 21 ▶ if ( $self->{log_unhandled_events} && !exists( $plugin_event_list{ $event } ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"log_unhandled_events"} and not exists $Sprocket::Plugin::plugin_event_list{$event} 0 21 21
Condition33%1/3
$self->{"log_unhandled_events"} and not exists $Sprocket::Plugin::plugin_event_list{$event}
!ll&&!rl&&r
2100
MC/DC0%0/2
$self->{"log_unhandled_events"} and not exists $Sprocket::Plugin::plugin_event_list{$event}
$self->{"log_unhandled_events"} not exists $Sprocket::Plugin::plugin_event_list{$event}
Truth table33%1/3
A and not(B)
A= $self->{"log_unhandled_events"}B= not exists $Sprocket::Plugin::plugin_event_list{$event}
ABresult
0X0
100
111
80 }
81
82 61 return 1;
83 }
84  
85 0 ▶sub _log {
Subroutine _log: not called CC=1 SCAR=6.9
86 0 $poe_kernel->call( shift->parent_id => _log => ( call => ( caller(1) )[ 3 ], @_ ) );
87 0 return;
88 }
89  
90 # ==========================================
91 # Events
92 # ==========================================
93  
94 4 ▶sub local_accept {
Subroutine local_accept: called CC=2 SCAR=7.4
Pod documented
95 4 my ( $self, $server, $con, $socket ) = @_;
96 4 ▶ if ( $server->shutting_down ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server->shutting_down) { } 0 4 4
97 0 $con->reject();
98 } else {
99 4 $con->accept();
100 }
101 4 return;
102 }
103  
104 0 ▶sub local_connected {
Subroutine local_connected: not called CC=1 SCAR=6.9
Pod documented
105 0 my ( $self, $server, $con, $socket ) = @_;
106 0 $server->_log( v => 4, msg => 'Rejecting connection because plugin:'
107 .$self.' did not define a local_connected event' );
108 0 $con->reject();
109 0 return;
110 }
111  
112 0 ▶sub local_error {
Subroutine local_error: not called CC=2 SCAR=17.9
Pod documented
113 0 my ( $self, $server, $operation, $errnum, $errstr ) = @_;
114 # note that this has no $con, it's a server wheel error
115 0 ▶ $server->shutdown() if ( $errnum == EADDRINUSE );
Branch: 1 branch
BranchTrueFalseTotal
if $errnum == 98 0 0 0
116 0 return;
117 }
118  
119 0 ▶sub local_time_out {
Subroutine local_time_out: not called CC=1 SCAR=6.9
Pod documented
120 0 my ( $self, $server, $con, $time ) = @_;
121 0 $server->_log( v => 4, msg => 'Timeout for connection ' );
122 0 $con->close();
123 0 return;
124 }
125  
126 0 ▶sub local_shutdown {
Subroutine local_shutdown: not called CC=1 SCAR=6.9
Pod documented
127 0 my ( $self, $server, $con ) = @_;
128 0 $server->_log( v => 4, msg => 'Closing connection, shutting down' );
129 0 $con->close( 1 );
130 0 return;
131 }
132  
133 0 ▶sub remote_connected {
Subroutine remote_connected: not called CC=1 SCAR=6.9
Pod documented
134 0 my ( $self, $client, $con, $socket ) = @_;
135 0 $client->_log( v => 4, msg => 'Rejecting connection because plugin:'
136 .$self.' did not define a remote_connected event' );
137 0 $con->reject();
138 0 return;
139 }
140  
141 4 ▶sub remote_accept {
Subroutine remote_accept: called CC=1 SCAR=0.0
Pod documented
142 4 my ( $self, $client, $con, $socket ) = @_;
143 # XXX shutting_down?
144 4 $con->accept();
145 4 return;
146 }
147  
148 0 ▶sub remote_disconnected {
Subroutine remote_disconnected: not called CC=1 SCAR=6.9
Pod documented
149 0 my ( $self, $client, $con ) = @_;
150 0 $con->close();
151 0 return;
152 }
153  
154 0 ▶sub remote_connect_error {
Subroutine remote_connect_error: not called CC=1 SCAR=6.9
Pod documented
155 0 my ( $self, $client, $con, $res_err, $res_obj ) = @_;
156 0 $con->close();
157 0 return;
158 }
159  
160 0 ▶sub remote_time_out {
Subroutine remote_time_out: not called CC=1 SCAR=6.9
Pod documented
161 0 my ( $self, $client, $con, $time ) = @_;
162 0 $client->_log( v => 4, msg => 'Timeout for connection' );
163 0 $con->close();
164 0 return;
165 }
166  
167 1 ▶sub remote_shutdown {
Subroutine remote_shutdown: called CC=1 SCAR=0.0
Pod documented
168 1 my ( $self, $client, $con ) = @_;
169 1 $client->_log( v => 4, msg => 'Closing connection, shutting down' );
170 1 $con->close( 1 );
171 1 return;
172 }
173  
174 # ==========================================
175 # Methods
176 # ==========================================
177  
178 0 ▶sub get_plugin_connection {
Subroutine get_plugin_connection: not called CC=1 SCAR=6.9
Pod undocumented
179 0 my ( $self, $server, $id ) = @_;
180  
181 # Sprocket::Local singleton
182 0 return $sprocket_local->get_connection( $server, $id );
183 }
184  
185 8 ▶sub take_connection {
Subroutine take_connection: called CC=2 SCAR=7.0
Pod documented
186 8 my ( $self, $con ) = @_;
187
188 $self->{__conlist__}->{ $con->ID } = 1
189 8 ▶ if ( $self->{__conlist__} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__conlist__"} 8 0 8
190
191 8 $con->plugin( $self->uuid );
192 8 return;
193 }
194  
195 0 ▶sub release_connection {
Subroutine release_connection: not called CC=2 SCAR=17.9
Pod documented
196 0 my ( $self, $con ) = @_;
197
198 delete $self->{__conlist__}->{ $con->ID }
199 0 ▶ if ( $self->{__conlist__} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__conlist__"} 0 0 0
200
201 0 $con->plugin( undef );
202 0 return;
203 }
204  
205 0 ▶sub spread_subscribe {
Subroutine spread_subscribe: not called CC=3 SCAR=24.8
Pod documented
206 0 my ( $self, $groups ) = @_;
207  
208 0 ▶ if ( !defined( $sprocket_spread ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Sprocket::Plugin::sprocket_spread) 0 0 0
209 # XXX is there a better way?
210 0 require Sprocket::Spread;
211 0 import Sprocket::Spread;
212 }
213
214 0 ▶ $groups = [ $groups ] unless ( ref $groups );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $groups 0 0 0
215  
216 0 return $sprocket_spread->plugin_subscribe( $self, $groups );
217 }
218  
219 0 ▶sub spread_unsubscribe {
Subroutine spread_unsubscribe: not called CC=3 SCAR=24.8
Pod documented
220 0 my ( $self, $groups ) = @_;
221  
222 0 ▶ if ( !defined( $sprocket_spread ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Sprocket::Plugin::sprocket_spread) 0 0 0
223 # XXX is there a better way?
224 0 require Sprocket::Spread;
225 0 import Sprocket::Spread;
226 }
227 0 ▶ $groups = [ $groups ] unless ( ref $groups );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $groups 0 0 0
228  
229 0 return $sprocket_spread->plugin_unsubscribe( $self, $groups );
230 }
231  
232 0 ▶sub spread_publish {
Subroutine spread_publish: not called CC=3 SCAR=24.8
Pod documented
233 0 my $self = shift;
234 0 my $groups = shift;
235  
236 0 ▶ if ( !defined( $sprocket_spread ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Sprocket::Plugin::sprocket_spread) 0 0 0
237 # XXX is there a better way?
238 0 require Sprocket::Spread;
239 0 import Sprocket::Spread;
240 }
241
242 0 ▶ $groups = [ $groups ] unless ( ref $groups );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $groups 0 0 0
243  
244 0 return $sprocket_spread->plugin_publish( $self, $groups, @_ );
245 }
246  
247 0 ▶sub con_list {
Subroutine con_list: not called CC=4 SCAR=30.0
Pod undocumented
248 0 my $self = shift;
249
250 0 ▶ if ( $self->{__conlist__} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__conlist__"}) 0 0 0
251 0 my @ids = keys %{ $self->{__conlist__} };
252 0 ▶ return wantarray ? @ids : \@ids;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
253 }
254  
255 0 ▶ return wantarray ? () : [];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
256 }
257  
258 5 *con_id_list = *con_list;
259  
260 5 1;
261  
262 __END__
« Sprocket/AIO.pm ↑ Summary Sprocket/Util/Observable.pm »