POE::Component::DirWatch::Role::AIO - Make poll calls asynchronous


POE-Component-DirWatch documentation Contained in the POE-Component-DirWatch distribution.

Index


Code Index:

NAME

Top

POE::Component::DirWatch::Role::AIO - Make poll calls asynchronous

DESCRIPTION

Top

POE::Component::DirWatch::Role::AIO adds support for non-blocking polling operations using POE::Component::AIO to interface with IO::AIO. The POE::Component::AIO object is stored in the aio attribute, so you can access it in your own applications.

ATTRIBUTES

Top

aio

A read-only instance of POE::Component::AIO that is automatically created at instantiation time.

METHODS

Top

start

after '_start' Create the aio_callback event and event handler.

_poll

around '_poll' Replaces the original _poll method with one that reads the contents of the target directory asynchronously. The original sub is never called.

SEE ALSO

Top

POE::Component::DirWatch, Moose

COPYRIGHT

Top


POE-Component-DirWatch documentation Contained in the POE-Component-DirWatch distribution.

package POE::Component::DirWatch::Role::AIO;

our $VERSION = "0.300000";

use POE;
use IO::AIO qw/2/;
use POE::Component::AIO { no_auto_export => 1, no_auto_bootstrap => 1 };
use Moose::Role;
use Path::Class qw(file dir);

has aio => (
  is => 'ro',
  isa => 'POE::Component::AIO',
  required => 1,
  clearer => 'clear_aio',
  default => sub { POE::Component::AIO->new }
);

after _start => sub {
  my $self = $_[OBJECT];
  my $aio_cb = sub {
    my ($kernel, $dirs, $nondirs) = @_[KERNEL, ARG0, ARG1];
    my $filter = $self->has_filter ? $self->filter : undef;
    if( $self->has_dir_callback ){
      foreach my $child (@$dirs){
        $child = dir($self->directory, $child);
        next if ref $filter && !$filter->($child);
        $kernel->yield(dir_callback => $child);
      }
    }
    if( $self->has_file_callback ){
      foreach my $child (@$nondirs){
        $child = file($self->directory, $child);
        next if ref $filter && !$filter->($child);
        $poe_kernel->yield(file_callback => $child);
      }
    }
    $self->next_poll( $kernel->delay_set(poll => $self->interval) );
  };

  $_[KERNEL]->state(aio_callback => $aio_cb);
};

around _poll => sub {
  my $super = shift;
  my ($self, $kernel) = @_[OBJECT, KERNEL];
  $self->clear_next_poll;
  my $dir = $self->directory->stringify;
  aio_scandir $dir, 0, $self->aio->postback('aio_callback');
};

before _shutdown => sub {
  my ($self, $kernel) = @_[OBJECT, KERNEL];
  $kernel->state('aio_callback');
  $self->aio->shutdown;
  $self->clear_aio;
};

1;

__END__;

#--------#---------#---------#---------#---------#---------#---------#---------