Async-Group

 view release on metacpan or  search on metacpan

Group.pm  view on Meta::CPAN

############################################################
#
# $Header: /mnt/barrayar/d06/home/domi/Tools/perlDev/Async_Group/RCS/Group.pm,v 1.2 1998/11/20 12:31:02 domi Exp $
#
# $Source: /mnt/barrayar/d06/home/domi/Tools/perlDev/Async_Group/RCS/Group.pm,v $
# $Revision: 1.2 $
# $Locker:  $
# 
############################################################

package Async::Group ;

use Carp ;
#use AutoLoader 'AUTOLOAD' ;

use strict ;
use vars qw($VERSION) ;
$VERSION =  substr q$Revision: 1.2 $, 10;

# see loadspecs for other names
sub new 
  {
    my $type = shift ;
    my $self = {} ;
    my %args = @_ ;

    $self->{name}='' ;
    foreach (qw/name test/)
      {
        $self->{$_}= $args{$_} ;
      }
    
    bless $self,$type ;
  }

sub getCbRef
  {
    my $self = shift;
    return sub{$self->callDone(@_)} ;
  }

sub printEvent
  {
    my $self = shift ;
    warn "$self->{name} ",shift if $self->{test} ;
  }

# Call a set of asynchronous functions which MUST have their set of user
# callbacks.. Note that the user call-back invoked when the function MUST
# call the asyncDone function with a result.
#
# When all function calls are over (i.e. all call-back were performed)
# all the returned results are logically 'anded' and the resulting result
# is passed to the main user call-back function
sub run
  {
    my $self = shift ;

    # 'set'    => [ sub { } ,... ]
    # 'callback' => 'method_name'
    my %args = @_ ; 

    foreach (qw/set callback/)
      {
        croak( "No $_ passed to Async::Group::run\n") unless 
          defined $args{$_};
      }
    
    croak( "$self->name:Async::Group: set parameter is not an array ref\n")
      unless ref($args{set}) eq 'ARRAY' ;

    # initialize 
    $self->{result} = 1 ;
    $self->{out} = '' ;

    # compute nb of asynchronous calls that will be done
    $self->{onGoing} = scalar (@{$args{set}}) ;

    # make up some log message
    $self->printEvent("asynCall called for ".
                      $self->{onGoing}." calls");

    # store what to do when the asyncGroup is all done
    $self->{callback} = $args{'callback'} ;

    # call them
    foreach my $func ( @{$args{set}} )
      {
        &$func ;
      }
  }

# expects asyncGroup id and a result as parameter
sub callDone
  {
    my $self= shift ;
    my $result = shift ;
    my $str = shift ;

    # no more info can be passed back, since we don't know what to do with
    # the results until all applied function are finished 
    #store results
    $self->{out} .= $str if defined $str ;
    $self->{result} &&= $result ;
    $self->printEvent("Async::Group call done ($result), ". 
                      -- $self->{onGoing}
                      ." left to do\n");
	
    unless ($self->{onGoing})



( run in 0.833 second using v1.01-cache-2.11-cpan-39bf76dae61 )