blib/lib/POE/Component/SpreadClient.pm

filter: Statementstmt 90.9% 20 / 22 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.3
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/POE/Driver/SpreadClient.pm ↑ Summary
1 #
2 # This file is part of POE-Component-SpreadClient
3 #
4 # This software is copyright (c) 2011 by Apocalypse.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 package POE::Component::SpreadClient;
11 BEGIN {
12 1 ▶ $POE::Component::SpreadClient::VERSION = '1.002';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14 BEGIN {
15 1 ▶ $POE::Component::SpreadClient::AUTHORITY = 'cpan:APOCAL';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 }
17  
18 # ABSTRACT: Handle Spread communications in POE
19  
20 # Load our stuff
21 1 ▶use 5.006; # to silence Perl::Critic's Compatibility::ProhibitThreeArgumentOpen
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use POE::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use POE::Wheel::ReadWrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use POE::Driver::SpreadClient;
Subroutine BEGIN: called CC=1 SCAR=2.6
26 use POE::Filter::SpreadClient;
27 use Spread 3.017 qw( :MESS :ERROR );
28  
29 # Generate our states!
30 use base 'POE::Session::AttributeBased';
31  
32 # Set some constants
33 BEGIN {
34 if ( ! defined &DEBUG ) { *DEBUG = sub () { 0 } }
35 }
36  
37 # Create our instance!
38 sub spawn {
39 # Get the OOP's type
40 my $type = shift;
41  
42 # Our own options
43 my $ALIAS = shift;
44  
45 # Get the session alias
46 if ( ! defined $ALIAS ) {
47 # Debugging info...
48 if ( DEBUG ) {
49 warn 'Using default ALIAS = SpreadClient';
50 }
51  
52 # Set the default
53 $ALIAS = 'SpreadClient';
54 }
55  
56 # Okay, create our session!
57 my $sess = POE::Session->create(
58 __PACKAGE__->inline_states(), ## no critic ( RequireExplicitInclusion )
59 'heap' => {
60 'ALIAS' => $ALIAS,
61 },
62 );
63  
64 # return the session's ID in case the caller needs it
65 return $sess->ID;
66 }
67  
68 sub _start : State {
69 # Debugging
70 if ( DEBUG ) {
71 warn "SpreadClient was started!";
72 }
73  
74 # Set our own alias
75 if ( $_[KERNEL]->alias_set( $_[HEAP]->{'ALIAS'} ) != 0 ) {
76 die "unable to set alias: " . $_[HEAP]->{'ALIAS'};
77 }
78  
79 return;
80 }
81  
82 sub _stop : State {
83 # Debugging
84 if ( DEBUG ) {
85 warn "SpreadClient was stopped!";
86 }
87  
88 # Wow, go disconnect ourself!
89 $_[KERNEL]->call( $_[SESSION], 'disconnect' );
90  
91 return;
92 }
93  
94 sub connect : State {
95 # Server info, private name
96 my( $server, $priv ) = @_[ ARG0, ARG1 ];
97  
98 # Tack on the default port if needed
99 unless ( $server =~ /^\d+$/ or $server =~ /@/ ) {
100 # Debugging
101 if ( DEBUG ) {
102 warn "using default port 4803";
103 }
104  
105 $server = '4803@' . $server;
106 }
107  
108 # Automatically set private name
109 if ( ! defined $priv ) {
110 # Debugging
111 if ( DEBUG ) {
112 warn "using default priv-name: spread-PID";
113 }
114  
115 $priv = 'spread-' . $$;
116 }
117  
118 # Automatically add the sender session to listeners
119 if ( ! exists $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } ) {
120 $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } = 1;
121 }
122  
123 # Fire up Spread itself
124 my( $mbox, $priv_group );
125 eval {
126 ( $mbox, $priv_group ) = Spread::connect( {
127 'private_name' => $priv,
128 'spread_name' => $server,
129 } );
130 };
131 if ( $@ ) {
132 # Inform our registered listeners
133 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
134 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'CONNECT', $@, $server, $priv );
135 }
136  
137 # We're not connected...
138 $_[HEAP]->{'DISCONNECTED'} = 1;
139 } else {
140 # Sanity
141 if ( ! defined $mbox ) {
142 # Inform our registered listeners
143 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
144 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'CONNECT', $sperrno, $server, $priv );
145 }
146  
147 # We're not connected...
148 $_[HEAP]->{'DISCONNECTED'} = 1;
149 } else {
150 # Debugging
151 if ( DEBUG ) {
152 warn "creating RW wheel for Spread";
153 }
154  
155 # Set our data
156 $_[HEAP]->{'SERVER'} = $server;
157 $_[HEAP]->{'PRIV_NAME'} = $priv;
158 $_[HEAP]->{'PRIV_GROUP'} = $priv_group;
159 $_[HEAP]->{'MBOX'} = $mbox;
160  
161 # Create a FH to feed into Wheel::ReadWrite
162 # we retry because... there seems to be several microseconds until fileno() works!
163 # TODO we need to investigate the underlying cause of this...
164 my $retries = 0;
165 until ( ++$retries == 10 || ( $_[HEAP]->{'FH'} && fileno( $_[HEAP]->{'FH'} ) ) ) {
166 open $_[HEAP]->{'FH'}, '<&=', $mbox;
167 if ( DEBUG ) {
168 warn "SpreadClient: bad fh!!! retrying" if ! fileno( $_[HEAP]->{'FH'} );
169 }
170 }
171 if ( $retries == 10 ) {
172 if ( DEBUG ) {
173 warn "SpreadClient: UNABLE to create FH from mbox!";
174 }
175  
176 # Inform our registered listeners
177 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
178 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'CONNECT', 'BADFH', $server, $priv );
179 }
180  
181 # We're not connected...
182 $_[HEAP]->{'DISCONNECTED'} = 1;
183 } else {
184 # Finally, create the wheel!
185 $_[HEAP]->{'WHEEL'} = POE::Wheel::ReadWrite->new(
186 'Handle' => $_[HEAP]->{'FH'},
187 'Driver' => POE::Driver::SpreadClient->new( $mbox ),
188 'Filter' => POE::Filter::SpreadClient->new(),
189  
190 'InputEvent' => 'RW_GotPacket',
191 'ErrorEvent' => 'RW_Error'
192 );
193  
194 # Inform our registered listeners
195 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
196 $_[KERNEL]->post( $l, '_sp_connect', $priv, $priv_group );
197 }
198  
199 # We're connected...
200 delete $_[HEAP]->{'DISCONNECTED'} if exists $_[HEAP]->{'DISCONNECTED'};
201 }
202 }
203 }
204  
205 # All done!
206 return;
207 }
208  
209 sub disconnect : State {
210 # Sanity
211 if ( exists $_[HEAP]->{'WHEEL'} and defined $_[HEAP]->{'WHEEL'} ) {
212 # Debugging
213 if ( DEBUG ) {
214 warn "SpreadClient is disconnecting!";
215 }
216  
217 # Shutdown the input/output
218 $_[HEAP]->{'WHEEL'}->shutdown_input();
219 $_[HEAP]->{'WHEEL'}->shutdown_output();
220  
221 # Get rid of it!
222 undef $_[HEAP]->{'WHEEL'};
223 }
224  
225 # Sanity
226 if ( ! exists $_[HEAP]->{'DISCONNECTED'} ) {
227 if ( DEBUG ) {
228 warn "calling sp_disconnect";
229 }
230  
231 # Set it in our heap that we've disconnected
232 $_[HEAP]->{'DISCONNECTED'} = 1;
233  
234 # Inform our registered listeners
235 # FIXME Should I use POST here instead?
236 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
237 $_[KERNEL]->call( $l, '_sp_disconnect', $_[HEAP]->{'PRIV_NAME'} );
238 }
239 }
240  
241 # All done!
242 return;
243 }
244  
245 sub destroy : State {
246 # Okay, destroy ourself!
247 $_[KERNEL]->call( $_[SESSION], 'disconnect' );
248  
249 # Get rid of our alias
250 $_[KERNEL]->alias_remove( $_[HEAP]->{'ALIAS'} );
251  
252 # All done!
253 return;
254 }
255  
256 sub publish : State {
257 my( $groups, $message, $mess_type, $flag ) = @_[ ARG0 .. ARG3 ];
258  
259 # Shortcut
260 if ( ! defined $_[HEAP]->{'WHEEL'} ) {
261 # Inform our registered listeners
262 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
263 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'PUBLISH', CONNECTION_CLOSED, $groups, $message );
264 }
265  
266 # All done!
267 return;
268 }
269  
270 # Sanity
271 if ( ! defined $mess_type ) {
272 $mess_type = 0;
273 }
274 if ( ! defined $flag ) {
275 $flag = SAFE_MESS;
276 }
277  
278 # Spread.pm doesn't like one-member group via arrayref...
279 if ( defined $groups ) {
280 if ( ref $groups and ref( $groups ) eq 'ARRAY' and scalar @$groups == 1 ) {
281 $groups = $groups->[0];
282 }
283 } else {
284 # Inform our registered listeners
285 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
286 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'PUBLISH', ILLEGAL_GROUP, undef, $message );
287 }
288  
289 # All done!
290 return;
291 }
292  
293 # Send it!
294 my $rtn;
295 eval {
296 $rtn = Spread::multicast( $_[HEAP]->{'MBOX'}, $flag, $groups, $mess_type, $message );
297 };
298 if ( $@ or ! defined $rtn or $rtn < 0 ) {
299 # Check for disconnect
300 if ( defined $sperrno and $sperrno == CONNECTION_CLOSED ) {
301 $_[KERNEL]->call( $_[SESSION], 'disconnect' );
302 }
303  
304 # Inform our registered listeners
305 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
306 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'PUBLISH', $sperrno, $groups, $message );
307 }
308 }
309  
310 # All done!
311 return;
312 }
313  
314 sub subscribe : State {
315 # The groups to join
316 my $groups = $_[ARG0];
317  
318 # Automatically add the sender session to listeners
319 if ( ! exists $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } ) {
320 $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } = 1;
321 }
322  
323 # Shortcut
324 if ( ! defined $_[HEAP]->{'WHEEL'} ) {
325 # Inform our registered listeners
326 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
327 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'SUBSCRIBE', CONNECTION_CLOSED, $groups );
328 }
329  
330 # All done!
331 return;
332 }
333  
334 # sanity check
335 if ( ! defined $groups ) {
336 # Inform our registered listeners
337 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
338 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'SUBSCRIBE', ILLEGAL_GROUP, undef );
339 }
340  
341 # All done!
342 return;
343 }
344  
345 eval {
346 # try to join each group
347 foreach my $g ( ref $groups ? @$groups : $groups ) {
348 if ( ! Spread::join( $_[HEAP]->{'MBOX'}, $g ) ) {
349 # Check for disconnect
350 if ( defined $sperrno and $sperrno == CONNECTION_CLOSED ) {
351 die "disconnected";
352 }
353  
354 # Inform our registered listeners
355 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
356 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'SUBSCRIBE', $sperrno, $g );
357 }
358 }
359 }
360 };
361 if ( $@ ) {
362 # Inform our registered listeners
363 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
364 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'SUBSCRIBE', $sperrno, $groups );
365 }
366  
367 if ( $@ eq "disconnected" ) {
368 $_[KERNEL]->call( $_[SESSION], 'disconnect' );
369 }
370 }
371  
372 # All done!
373 return;
374 }
375  
376 sub unsubscribe : State {
377 # The groups to unsub
378 my $groups = $_[ARG0];
379  
380 # Shortcut
381 if ( ! defined $_[HEAP]->{'WHEEL'} ) {
382 # Inform our registered listeners
383 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
384 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'UNSUBSCRIBE', CONNECTION_CLOSED, $groups );
385 }
386  
387 # All done!
388 return;
389 }
390  
391 # sanity
392 if ( ! defined $groups ) {
393 # Inform our registered listeners
394 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
395 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'UNSUBSCRIBE', ILLEGAL_GROUP, undef );
396 }
397  
398 # All done!
399 return;
400 }
401  
402 eval {
403 # try to leave each group
404 foreach my $g ( ref $groups ? @$groups : $groups ) {
405 if ( ! Spread::leave( $_[HEAP]->{'MBOX'}, $g ) ) {
406 # Check for disconnect
407 if ( defined $sperrno and $sperrno == CONNECTION_CLOSED ) {
408 die "disconnected";
409 }
410  
411 # Inform our registered listeners
412 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
413 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'UNSUBSCRIBE', $sperrno, $g );
414 }
415 }
416 }
417 };
418 if ( $@ ) {
419 # Inform our registered listeners
420 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
421 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'UNSUBSCRIBE', $sperrno, $groups );
422 }
423  
424 if ( $@ eq "disconnected" ) {
425 $_[KERNEL]->call( $_[SESSION], 'disconnect' );
426 }
427 }
428  
429 # All done!
430 return;
431 }
432  
433 # Registers interest in the client
434 sub register : State {
435 # Automatically add the sender session to listeners
436 if ( ! exists $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } ) {
437 $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } = 1;
438 }
439  
440 # All done!
441 return;
442 }
443  
444 # Unregisters interest in the client
445 sub unregister : State {
446 # Automatically add the sender session to listeners
447 if ( exists $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID } ) {
448 delete $_[HEAP]->{'LISTEN'}->{ $_[SENDER]->ID };
449 }
450  
451 # All done!
452 return;
453 }
454  
455 sub RW_Error : State {
456 # ARG0 = operation, ARG1 = error number, ARG2 = error string, ARG3 = wheel ID
457 my ( $operation, $errnum, $errstr, $id ) = @_[ ARG0 .. ARG3 ];
458  
459 # Debugging
460 if ( DEBUG ) {
461 warn "ReadWrite wheel($id) got error $errnum - $errstr doing $operation";
462 }
463  
464 # Disconnect now!
465 $_[KERNEL]->call( $_[SESSION], 'disconnect' );
466  
467 return;
468 }
469  
470 sub RW_GotPacket : State {
471 # we might get multiple packets per read
472 for my $packet ( @{ $_[ARG0] } ) {
473 my( $type, $sender, $groups, $mess_type, $endian, $message ) = @$packet;
474  
475 # Check for disconnect
476 if ( ! defined $type ) {
477 # Disconnect now!
478 $_[KERNEL]->call( $_[SESSION], 'disconnect' );
479 } else {
480 # Logic mostly taken from http://www.spread.org/docs/spread_docs_3/docs/sp_receive.html
481 # Check the type
482 if ( $type & REGULAR_MESS ) {
483 # Do we have an endian problem?
484 if ( defined $endian and $endian ) {
485 # FIXME Argh!
486 if ( DEBUG ) {
487 warn "endian mis-match detected!";
488 }
489 }
490  
491 # Regular message
492 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
493 $_[KERNEL]->post( $l, '_sp_message', $_[HEAP]->{'PRIV_NAME'}, $sender, $groups, $mess_type, $message );
494 }
495 } else {
496 # Okay, figure out the type
497 if ( $type & TRANSITION_MESS ) {
498 # Transitional Message
499 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
500 $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'TRANSITIONAL', 'GROUP' => $sender } );
501 }
502 } elsif ( $type & CAUSED_BY_LEAVE and ! ( $type & REG_MEMB_MESS ) ) {
503 # Self leave
504 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
505 $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'SELF_LEAVE', 'GROUP' => $sender } );
506 }
507 } elsif ( $type & REG_MEMB_MESS ) {
508 # Parse the message!
509 my ( $gid1, $gid2, $gid3, $num_memb, $member );
510 eval {
511 # Code copied from Spread::Message v0.21, thanks!
512 #($gid[0],$gid[1],$gid[2],$numg,$who) = unpack("IIIIa*",$msg);
513 #$who =~ s/[[:cntrl:]]+/ /go; # Just to clean it up
514 #$who =~ s/\s+$/ /go; # No space at end thanks
515 # changed from a to Z thanks RT#65795
516 ( $gid1, $gid2, $gid3, $num_memb, $member ) = unpack( "IIIIZ*", $message );
517 };
518 if ( $@ ) {
519 # Inform our registered listeners
520 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
521 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'RECEIVE', $@ );
522 }
523 } else {
524 # Okay, what was it?
525 if ( $type & CAUSED_BY_JOIN ) {
526 # Inform our registered listeners
527 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
528 $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'JOIN', 'GROUP' => $sender, 'MEMBERS' => $groups, 'WHO' => $member, 'GID' => [ $gid1, $gid2, $gid3 ], 'INDEX' => $mess_type } );
529 }
530 } elsif ( $type & CAUSED_BY_LEAVE ) {
531 # Inform our registered listeners
532 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
533 $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'LEAVE', 'GROUP' => $sender, 'MEMBERS' => $groups, 'WHO' => $member, 'GID' => [ $gid1, $gid2, $gid3 ], 'INDEX' => $mess_type } );
534 }
535 } elsif ( $type & CAUSED_BY_DISCONNECT ) {
536 # Inform our registered listeners
537 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
538 $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'DISCONNECT', 'GROUP' => $sender, 'MEMBERS' => $groups, 'WHO' => $member, 'GID' => [ $gid1, $gid2, $gid3 ], 'INDEX' => $mess_type } );
539 }
540 } elsif ( $type & CAUSED_BY_NETWORK ) {
541 # FIXME Unpack the full nodelist
542 #my @nodes = unpack( "a32" x ( length( $member ) / 32 + 1 ), $member );
543  
544 # Network failure
545 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
546 $_[KERNEL]->post( $l, '_sp_admin', $_[HEAP]->{'PRIV_NAME'}, { 'TYPE' => 'NETWORK', 'GROUP' => $sender, 'MEMBERS' => $groups, 'GID' => [ $gid1, $gid2, $gid3 ], 'INDEX' => $mess_type, 'MESSAGE' => $message } );
547 }
548 } else {
549 # Unknown?
550 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
551 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'RECEIVE', 'UNKNOWN PACKET TYPE' );
552 }
553 }
554 }
555 # TODO REJECT_MESS ???
556 } else {
557 # Unknown?
558 foreach my $l ( keys %{ $_[HEAP]->{'LISTEN'} } ) {
559 $_[KERNEL]->post( $l, '_sp_error', $_[HEAP]->{'PRIV_NAME'}, 'RECEIVE', 'UNKNOWN PACKET TYPE' );
560 }
561 }
562 }
563 }
564 }
565  
566 # All done!
567 return;
568 }
569  
570 1;
571  
572  
573 __END__
« blib/lib/POE/Driver/SpreadClient.pm ↑ Summary