blib/lib/POE/Component/Client/NNTP/Tail.pm

filter: Statementstmt 76.4% 120 / 157 Branchbran 42.8% 24 / 56 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 78.5% 22 / 28 Podpod 100% 5 / 5 totaltotal 70.2% 177 / 252 CC 38 SCAR 15.1
CC
38
Cov
68%
_debug
20.0
_nntp_socketerr
6.0
_nntp_disconnected
6.0
CRAP 4.5
↑ Summary
1 package POE::Component::Client::NNTP::Tail;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Sends events for new articles posted to an NNTP newsgroup
7 # VERSION
8  
9 2 our $VERSION = '0.04';
10  
11 2 ▶use Carp::POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use POE qw(Component::Client::NNTP);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 my %spawn_args = (
16 # required
17 Group => 1,
18 NNTPServer => 1,
19 # optional with defaults
20 Interval => { default => 60 },
21 TimeOut => { default => 30 },
22 # purely optional
23 Port => 0,
24 LocalAddr => 0,
25 Alias => 0,
26 Debug => 0,
27 );
28  
29 2 ▶sub spawn {
Subroutine spawn: called CC=1 SCAR=0.0
Pod documented
30 2 my $class = shift;
31 2 my %opts = validate( @_, \%spawn_args );
32  
33 2 POE::Session->create(
34 heap => \%opts,
35 package_states => [
36 # nntp component events
37 $class => {
38 nntp_connected => '_nntp_connected',
39 nntp_registered => '_nntp_registered',
40 nntp_socketerr => '_nntp_socketerr',
41 nntp_disconnected => '_nntp_disconnected',
42 nntp_200 => '_nntp_server_ready',
43 nntp_201 => '_nntp_server_ready',
44 nntp_211 => '_nntp_group_selected',
45 nntp_220 => '_nntp_got_article',
46 nntp_221 => '_nntp_got_head',
47 nntp_411 => '_nntp_no_group',
48 nntp_423 => '_nntp_no_article',
49 nntp_430 => '_nntp_no_article',
50 nntp_503 => '_nntp_503_error',
51 },
52 # session events
53 $class => [ qw( _start _stop _child ) ],
54 # internal events
55 $class => [ qw( _poll _reconnect ) ],
56 # API events
57 $class => [ qw( register unregister get_article shutdown ) ],
58 ],
59 );
60 }
61  
62 0 ▶sub _debug {
Subroutine _debug: not called CC=4 SCAR=30.0
63 0 my $where = (caller(1))[3];
64 0 $where =~ s{.*::}{P::C::C::N::T::};
65 0 my @args = @_[ARG0 .. $#_];
66 0 for ( @args ) {
67 0 ▶ $_ = 'undef' if not defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
68 }
69 0 ▶ my $args = @args ? join( " " => "", (map { "'$_'" } @args), "" ) : "";
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
70 0 warn "$where->($args)\n";
71 0 return;
72 }
73  
74 #--------------------------------------------------------------------------#
75 # session events
76 #--------------------------------------------------------------------------#
77  
78 2 ▶sub _start {
Subroutine _start: called CC=5 SCAR=16.1
79 2 my ( $kernel, $session, $heap ) = @_[KERNEL, SESSION, HEAP];
80 2 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 2 2
81  
82 # alias defaults to group name if not otherwise set
83 2 ▶ $heap->{Alias} = $heap->{Group} unless exists $heap->{Alias};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $heap->{"Alias"} 1 1 2
84 2 $kernel->alias_set($heap->{Alias});
85  
86 # setup NNTP including optional args if defined;
87 2 my %nntp_args;
88 2 for my $k ( qw/NNTPServer Port LocalAddr TimeOut/ ) {
89 8 ▶ $nntp_args{$k} = $heap->{$k} if exists $heap->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $heap->{$k} 6 2 8
90 }
91 2 my $alias = "NNTP-Client-" . $session->ID;
92 2 $heap->{nntp} = POE::Component::Client::NNTP->spawn($alias,\%nntp_args);
93 2 $heap->{nntp_id} = $heap->{nntp}->session_id;
94  
95 # start NNTP connection
96 2 $kernel->yield( '_reconnect' );
97 2 return;
98 }
99  
100 # ignore these
101 4 ▶sub _child {
Subroutine _child: called CC=2 SCAR=7.2
102 4 my ( $kernel, $heap ) = @_[KERNEL, HEAP];
103 4 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 4 4
104 }
105  
106 2 ▶sub _stop {
Subroutine _stop: called CC=3 SCAR=11.4
107 2 my ( $kernel, $session, $heap ) = @_[KERNEL, SESSION, HEAP];
108 2 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 2 2
109 2 ▶ $kernel->call( $session, 'shutdown' ) if $heap->{nntp};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"nntp"} 0 2 2
110 2 return;
111 }
112  
113 #--------------------------------------------------------------------------#
114 # events from our clients
115 #--------------------------------------------------------------------------#
116  
117 #--------------------------------------------------------------------------#
118 # register -- [EVENT]
119 #
120 # EVENT - event to dispatch to the registered session on receipt of new
121 # headers; defaults to "new_header"
122 #--------------------------------------------------------------------------#
123  
124 2 ▶sub register {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
125 2 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
126 2 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 2 2
127 2 ▶ my ($event) = $_[ARG0] || 'new_header';
Condition100%2/2
$_[10] || 'new_header'
l!l
11
MC/DC100%1/1
$_[10] || 'new_header'
$_[10]
Truth table100%2/2
A
A= $_[10]
Aresult
11
00
128 2 $kernel->refcount_increment( $sender->ID, __PACKAGE__ );
129 2 $heap->{listeners}{$sender->ID} = $event;
130 2 return;
131 }
132  
133 #--------------------------------------------------------------------------#
134 # unregister --
135 #
136 # removes sender registration
137 #--------------------------------------------------------------------------#
138  
139 2 ▶sub unregister {
Subroutine unregister: called CC=2 SCAR=7.0
Pod documented
140 2 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
141 2 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 2 2
142 2 $kernel->refcount_decrement( $sender->ID, __PACKAGE__ );
143 2 delete $heap->{listeners}{$sender->ID};
144 2 return;
145 }
146  
147 #--------------------------------------------------------------------------#
148 # get_article -- ARTICLE_ID, EVENT
149 #
150 # request ARTICLE_ID be retrieved and returned via EVENT or 'got_article
151 # if not specified
152 #--------------------------------------------------------------------------#
153  
154 8 ▶sub get_article {
Subroutine get_article: called CC=3 SCAR=11.0
Pod documented
155 8 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
156 8 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 8 8
157 8 my ($article_id, $return_event) = @_[ARG0, ARG1];
158 8 ▶ $return_event ||= 'got_article';
Condition100%2/2
$return_event ||= "got_article"
l!l
44
MC/DC100%1/1
$return_event ||= "got_article"
$return_event
Truth table100%2/2
A
A= $return_event
Aresult
11
00
159 # store requesting session and desired return event
160 8 push @{$heap->{requests}{$article_id}}, [$sender, $return_event];
161 8 $kernel->post( $heap->{nntp_id} => article => $article_id );
162 8 return;
163 }
164  
165 #--------------------------------------------------------------------------#
166 # shudown
167 #--------------------------------------------------------------------------#
168  
169 2 ▶sub shutdown {
Subroutine shutdown: called CC=4 SCAR=14.2
Pod documented
170 2 my ( $kernel, $heap ) = @_[KERNEL, HEAP];
171 2 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 2 2
172 # unregister anyone that didn't do it themselves
173 2 for my $listener ( keys %{ $heap->{listeners} } ) {
174 0 $kernel->refcount_decrement( $listener, __PACKAGE__ );
175 0 delete $heap->{listeners}{$listener};
176 }
177 2 $kernel->alarm_remove_all();
178 2 $kernel->call( $heap->{nntp_id} => 'unregister' => 'all' );
179 2 $kernel->call( $heap->{nntp_id} => 'shutdown' );
180 2 delete $heap->{nntp};
181 2 $kernel->alias_remove($_) for $kernel->alias_list;
182 2 return;
183 }
184  
185 #--------------------------------------------------------------------------#
186 # our internal events
187 #--------------------------------------------------------------------------#
188  
189 # if connected, check for new messages, otherwise reconnect
190 10 ▶sub _poll {
Subroutine _poll: called CC=3 SCAR=11.6
191 10 my ( $kernel, $heap ) = @_[KERNEL, HEAP];
192 10 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 10 10
193 10 ▶ if ( $heap->{connected} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{'connected'}) { } 10 0 10
194 10 $kernel->post( $heap->{nntp_id} => group => $heap->{Group} );
195 }
196 else {
197 0 $kernel->yield( '_reconnect' );
198 }
199 10 $kernel->delay( '_poll' => $heap->{Interval} );
200 10 return;
201 }
202  
203 # connect to NNTP server
204 2 ▶sub _reconnect {
Subroutine _reconnect: called CC=2 SCAR=7.0
205 2 my ( $kernel, $heap ) = @_[KERNEL, HEAP];
206 2 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 2 2
207 2 $kernel->post( $heap->{nntp_id} => 'connect' );
208 2 return;
209 }
210  
211 #--------------------------------------------------------------------------#
212 # events from NNTP client
213 #--------------------------------------------------------------------------#
214  
215 # ignore event
216 2 ▶sub _nntp_registered {
Subroutine _nntp_registered: called CC=2 SCAR=7.1
217 2 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
218 2 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 2 2
219 2 return;
220 }
221  
222 # ignore event
223 2 ▶sub _nntp_connected {
Subroutine _nntp_connected: called CC=2 SCAR=7.1
224 2 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
225 2 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 2 2
226 2 return;
227 }
228  
229 # if connection can't be made, wait for next poll period to try again
230 0 ▶sub _nntp_socketerr {
Subroutine _nntp_socketerr: not called CC=2 SCAR=17.9
231 0 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
232 0 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 0 0
233 0 my ($error) = $_[ARG0];
234 0 warn "Socket error: $error\n";
235 0 $heap->{connected} = 0;
236 0 $kernel->delay( '_reconnect' => $heap->{Interval} );
237 0 return;
238 }
239  
240 # if we time-out, just note it and wait for next poll to reconnect
241 0 ▶sub _nntp_disconnected {
Subroutine _nntp_disconnected: not called CC=2 SCAR=17.9
242 0 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
243 0 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 0 0
244 0 $heap->{connected} = 0;
245 0 return;
246 }
247  
248 # once connected, start polling loop
249 2 ▶sub _nntp_server_ready {
Subroutine _nntp_server_ready: called CC=2 SCAR=7.0
250 2 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
251 2 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 2 2
252 2 $heap->{connected} = 1;
253 2 $kernel->yield( '_poll' );
254 2 undef;
255 }
256  
257 # if the group doesn't exist, then we shut ourselves down
258 0 ▶sub _nntp_no_group {
Subroutine _nntp_no_group: not called CC=2 SCAR=17.9
259 0 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
260 0 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 0 0
261 0 warn "No such newsgroup $heap->{Group}";
262 0 $kernel->yield( 'shutdown' );
263 0 return;
264 }
265  
266 # if the article doesn't exist, warn about it
267 0 ▶sub _nntp_no_article {
Subroutine _nntp_no_article: not called CC=2 SCAR=17.9
268 0 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
269 0 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 0 0
270 0 warn "Couldnt find article in $heap->{Group}\n";
271 0 return;
272 }
273  
274 # 503 error in response to group query reconnect
275 0 ▶sub _nntp_503_error {
Subroutine _nntp_503_error: not called CC=2 SCAR=17.9
276 0 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
277 0 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 0 0
278 0 $heap->{connected} = 0;
279 0 $kernel->delay( '_reconnect' => $heap->{Interval} );
280 0 return;
281 }
282  
283 # if there are new articles, request their headers
284 # also schedules the next check
285 10 ▶sub _nntp_group_selected {
Subroutine _nntp_group_selected: called CC=5 SCAR=16.2
286 10 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
287 10 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 10 10
288 10 my ($estimate,$first,$last,$group) = split( /\s+/, $_[ARG0] );
289  
290 # first time, we won't know last_article, so skip to the end
291 10 ▶ if ( exists $heap->{last_article} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $heap->{"last_article"}) 8 2 10
292 # fetch new headers or articles only if people are listening
293 8 for my $article_id ( $heap->{last_article} + 1 .. $last ) {
294 8 ▶ if ( scalar keys %{ $heap->{listeners} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$heap{"listeners"};}) 8 0 8
295 8 $kernel->post( $sender => head => $article_id );
296 }
297 }
298 }
299 10 $heap->{last_article} = $last;
300 10 $kernel->delay( '_poll' => $heap->{Interval} );
301 10 return;
302 }
303  
304 # notify listeners of new header
305 8 ▶sub _nntp_got_head {
Subroutine _nntp_got_head: called CC=3 SCAR=11.0
306 8 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
307 8 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 8 8
308 8 my ($response, $lines) = @_[ARG0, ARG1];
309 8 my ($article_id) = split " ", $response;
310 8 for my $who ( keys %{ $heap->{listeners} } ) {
311 8 $kernel->post( $who => $heap->{listeners}{$who} => $article_id, $lines );
312 }
313 8 return;
314 }
315  
316 # return article to request queue
317 8 ▶sub _nntp_got_article {
Subroutine _nntp_got_article: called CC=3 SCAR=11.0
318 8 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
319 8 ▶ &_debug if $heap->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"Debug"} 0 8 8
320 8 my ($response, $lines) = @_[ARG0, ARG1];
321 8 my ($article_id) = split " ", $response;
322 # dispatch for all entries in the request queue for this article
323 8 for my $request ( @{$heap->{requests}{$article_id}} ) {
324 8 my ($who, $event) = @$request;
325 8 $kernel->post( $who, $event, $article_id, $lines );
326 }
327 # clear the request queue
328 8 delete $heap->{requests}{$article_id};
329 8 return;
330 }
331  
332 2 1;
333  
334 __END__
↑ Summary