File Coverage

blib/lib/Async/Group.pm
Criterion Covered Total %
statement 45 45 100.0
branch 6 10 60.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 4 5 80.0
total 67 72 93.0


line stmt bran cond sub pod time code
1             ############################################################
2             #
3             # $Header: /mnt/barrayar/d06/home/domi/Tools/perlDev/Async_Group/RCS/Group.pm,v 1.2 1998/11/20 12:31:02 domi Exp $
4             #
5             # $Source: /mnt/barrayar/d06/home/domi/Tools/perlDev/Async_Group/RCS/Group.pm,v $
6             # $Revision: 1.2 $
7             # $Locker: $
8             #
9             ############################################################
10              
11             package Async::Group ;
12              
13 1     1   5833 use Carp ;
  1         2  
  1         276  
14             #use AutoLoader 'AUTOLOAD' ;
15              
16 1     1   6 use strict ;
  1         2  
  1         38  
17 1     1   6 use vars qw($VERSION) ;
  1         7  
  1         698  
18             $VERSION = substr q$Revision: 1.2 $, 10;
19              
20             # see loadspecs for other names
21             sub new
22             {
23 1     1 1 451 my $type = shift ;
24 1         2 my $self = {} ;
25 1         5 my %args = @_ ;
26              
27 1         4 $self->{name}='' ;
28 1         2 foreach (qw/name test/)
29             {
30 2         6 $self->{$_}= $args{$_} ;
31             }
32            
33 1         4 bless $self,$type ;
34             }
35              
36             sub getCbRef
37             {
38 1     1 1 5 my $self = shift;
39 1     4   6 return sub{$self->callDone(@_)} ;
  4         28  
40             }
41              
42             sub printEvent
43             {
44 6     6 0 7 my $self = shift ;
45 6 50       15 warn "$self->{name} ",shift if $self->{test} ;
46             }
47              
48             # Call a set of asynchronous functions which MUST have their set of user
49             # callbacks.. Note that the user call-back invoked when the function MUST
50             # call the asyncDone function with a result.
51             #
52             # When all function calls are over (i.e. all call-back were performed)
53             # all the returned results are logically 'anded' and the resulting result
54             # is passed to the main user call-back function
55             sub run
56             {
57 1     1 1 14 my $self = shift ;
58              
59             # 'set' => [ sub { } ,... ]
60             # 'callback' => 'method_name'
61 1         3 my %args = @_ ;
62              
63 1         6 foreach (qw/set callback/)
64             {
65 2 50       9 croak( "No $_ passed to Async::Group::run\n") unless
66             defined $args{$_};
67             }
68            
69 1 50       5 croak( "$self->name:Async::Group: set parameter is not an array ref\n")
70             unless ref($args{set}) eq 'ARRAY' ;
71              
72             # initialize
73 1         5 $self->{result} = 1 ;
74 1         3 $self->{out} = '' ;
75              
76             # compute nb of asynchronous calls that will be done
77 1         1 $self->{onGoing} = scalar (@{$args{set}}) ;
  1         2  
78              
79             # make up some log message
80 1         7 $self->printEvent("asynCall called for ".
81             $self->{onGoing}." calls");
82              
83             # store what to do when the asyncGroup is all done
84 1         2 $self->{callback} = $args{'callback'} ;
85              
86             # call them
87 1         2 foreach my $func ( @{$args{set}} )
  1         2  
88             {
89 4         11 &$func ;
90             }
91             }
92              
93             # expects asyncGroup id and a result as parameter
94             sub callDone
95             {
96 4     4 1 5 my $self= shift ;
97 4         5 my $result = shift ;
98 4         4 my $str = shift ;
99              
100             # no more info can be passed back, since we don't know what to do with
101             # the results until all applied function are finished
102             #store results
103 4 50       11 $self->{out} .= $str if defined $str ;
104 4   100     16 $self->{result} &&= $result ;
105 4         23 $self->printEvent("Async::Group call done ($result), ".
106             -- $self->{onGoing}
107             ." left to do\n");
108            
109 4 100       13 unless ($self->{onGoing})
110             {
111 1         5 $self->printEvent
112             ("Async::Group finished, global result is $self->{result}\n"
113             .$self->{out}) ;
114 1         1 my $cb = $self->{'callback'} ;
115 1         5 &$cb($self->{result},$self->{out});
116             }
117             }
118              
119             1;
120              
121             __END__