| 1 | package POE::Component::DirWatch::Object; | ||||||||||
| 2 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 8 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 8 | our $VERSION = "0.1200"; | |||||||||
| 7 | 8 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 8 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 8 | ▶ | use POE; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | #use IO::AIO; | ||||||||||
| 11 | |||||||||||
| 12 | #--------#---------#---------#---------#---------#---------#---------#---------# | ||||||||||
| 13 | 8 | has 'alias' => (is => 'rw', isa => 'Str', required => 1, | |||||||||
| 14 | default => 'dirwatch'); | ||||||||||
| 15 | |||||||||||
| 16 | 8 | has 'next_poll' => (is => 'rw', isa => 'Int', required => 0, | |||||||||
| 17 | clearer => 'clear_next_poll', predicate => 'has_next_poll'); | ||||||||||
| 18 | 8 | has 'callback' => (is => 'rw', isa => 'Ref', required => 1); | |||||||||
| 19 | 8 | has 'directory' => (is => 'rw', isa => 'Str', required => 1); | |||||||||
| 20 | 8 | has 'interval' => (is => 'rw', isa => 'Int', required => 1, default => 1); | |||||||||
| 21 | has 'filter' => (is => 'rw', isa => 'CodeRef', required => 1, | ||||||||||
| 22 | 5 | ▶ | default => sub { sub{ -f $_[1]; } }); #holler | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 5 | ▶ | sub BUILD{ | ||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 25 | 5 | my ($self, $args) = @_; | |||||||||
| 26 | |||||||||||
| 27 | #my $s = | ||||||||||
| 28 | 5 | POE::Session->create | |||||||||
| 29 | ( | ||||||||||
| 30 | object_states => | ||||||||||
| 31 | [ | ||||||||||
| 32 | $self, { | ||||||||||
| 33 | _start => '_start', | ||||||||||
| 34 | _pause => '_pause', | ||||||||||
| 35 | _resume => '_resume', | ||||||||||
| 36 | shutdown => '_shutdown', | ||||||||||
| 37 | poll => '_poll', | ||||||||||
| 38 | callback => '_callback', | ||||||||||
| 39 | dispatch => '_dispatch', | ||||||||||
| 40 | }, | ||||||||||
| 41 | ] | ||||||||||
| 42 | ); | ||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | sub session{ | ||||||||
|
Subroutine
session: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 46 | 0 | my $self = shift; | |||||||||
| 47 | 0 | return $poe_kernel->alias_resolve( $self->alias ); | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | #--------#---------#---------#---------#---------#---------#---------#---------# | ||||||||||
| 51 | |||||||||||
| 52 | 5 | ▶ | sub _start{ | ||||||||
|
Subroutine
_start: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 5 | my ($self, $kernel) = @_[OBJECT, KERNEL]; | |||||||||
| 54 | |||||||||||
| 55 | # set alias for ourselves and remember it | ||||||||||
| 56 | 5 | $kernel->alias_set($self->alias); | |||||||||
| 57 | # set up polling | ||||||||||
| 58 | 5 | $self->next_poll( $kernel->delay_set(poll => $self->interval) ); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 3 | ▶ | sub _pause{ | ||||||||
|
Subroutine
_pause: called CC=4 SCAR=14.0
| |||||||||||
| 62 | 3 | my ($self, $kernel, $until) = @_[OBJECT, KERNEL, ARG0]; | |||||||||
| 63 | 3 | ▶ | $kernel->alarm_remove($self->next_poll) if $self->has_next_poll; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 3 | $self->clear_next_poll; | |||||||||
| 65 | 3 | ▶ | return unless defined $until; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | |||||||||||
| 67 | 1 | my $t = time; | |||||||||
| 68 | 1 | ▶ | $until += $t if $t > $until; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 1 | $self->next_poll( $kernel->alarm_set(poll => $until) ); | |||||||||
| 70 | |||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 2 | ▶ | sub _resume{ | ||||||||
|
Subroutine
_resume: called CC=4 SCAR=14.3
| |||||||||||
| 74 | 2 | my ($self, $kernel, $when) = @_[OBJECT, KERNEL, ARG0]; | |||||||||
| 75 | 2 | ▶ | $kernel->alarm_remove($self->next_poll) if $self->has_next_poll; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 2 | $self->clear_next_poll; | |||||||||
| 77 | 2 | ▶ | $when = 0 unless defined $when; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | 2 | my $t = time; | |||||||||
| 80 | 2 | ▶ | $when += $t if $t > $when; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 2 | $self->next_poll( $kernel->alarm_set(poll => $when) ); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | #--------#---------#---------#---------#---------#---------#---------#---------# | ||||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | sub pause{ | ||||||||
|
Subroutine
pause: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 87 | 0 | my ($self, $until) = @_; | |||||||||
| 88 | 0 | $poe_kernel->call($self->alias, _pause => $until); | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | sub resume{ | ||||||||
|
Subroutine
resume: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 92 | 0 | my ($self, $when) = @_; | |||||||||
| 93 | 0 | $poe_kernel->call($self->alias, _resume => $when); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 1 | ▶ | sub shutdown{ | ||||||||
|
Subroutine
shutdown: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 97 | 1 | my ($self) = @_; | |||||||||
| 98 | 1 | ▶ | $poe_kernel->alarm_remove($self->next_poll) if $self->has_next_poll; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 1 | $self->clear_next_poll; | |||||||||
| 100 | 1 | $poe_kernel->post($self->alias, 'shutdown'); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | #--------#---------#---------#---------#---------#---------#---------#---------# | ||||||||||
| 104 | 18 | ▶ | sub _poll{ | ||||||||
|
Subroutine
_poll: called CC=3 SCAR=11.1
| |||||||||||
| 105 | 18 | my ($self, $kernel) = @_[OBJECT, KERNEL]; | |||||||||
| 106 | 18 | $self->clear_next_poll; | |||||||||
| 107 | |||||||||||
| 108 | #AIO?? maybe one day... | ||||||||||
| 109 | #aio_readdir($self->directory, sub{ $self->_aio_callback(@_) } ); | ||||||||||
| 110 | |||||||||||
| 111 | #until i figure out AIO this will have to be good enough | ||||||||||
| 112 | 18 | my @files; | |||||||||
| 113 | 18 | ▶ | eval { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 18 | ▶ | opendir(DIR, $self->directory) || | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | die "Failed to open '".$self->directory."': $!"; | ||||||||||
| 116 | 18 | @files = grep { $_ !~ /^\.\.?$/ } readdir(DIR); | |||||||||
| 117 | 18 | closedir DIR; | |||||||||
| 118 | } || carp($@); | ||||||||||
| 119 | |||||||||||
| 120 | 18 | $self->_aio_callback(\@files); | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 18 | ▶ | sub _aio_callback{ | ||||||||
|
Subroutine
_aio_callback: called CC=3 SCAR=11.1
| |||||||||||
| 124 | 18 | my ($self, $files) = @_; | |||||||||
| 125 | |||||||||||
| 126 | 18 | $self->next_poll( $poe_kernel->delay_set(poll => $self->interval) ); | |||||||||
| 127 | 18 | ▶ | return unless ref $files; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | |||||||||||
| 129 | $poe_kernel->yield(dispatch => $_, File::Spec->catfile($self->directory, $_)) | ||||||||||
| 130 | 18 | foreach (@$files); | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 10 | ▶ | sub _dispatch { | ||||||||
|
Subroutine
_dispatch: called CC=2 SCAR=7.2
| |||||||||||
| 134 | 10 | my ($self, $kernel, $fname, $fpath) = @_[OBJECT, KERNEL, ARG0, ARG1]; | |||||||||
| 135 | 10 | ▶ | $kernel->yield(callback => [$fname, $fpath]) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | if $self->filter->($fname,$fpath); | ||||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 13 | ▶ | sub _callback{ | ||||||||
|
Subroutine
_callback: called CC=3 SCAR=11.0
| |||||||||||
| 140 | 13 | my ($self, $args) = @_[OBJECT, ARG0]; | |||||||||
| 141 | 13 | my $cb = $self->callback; | |||||||||
| 142 | |||||||||||
| 143 | 13 | ▶ | if( ref $cb eq 'ARRAY' ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 2 | my ($obj, $method) = @$cb; | |||||||||
| 145 | 2 | $obj->$method(@$args); | |||||||||
| 146 | 2 | return; | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 11 | ▶ | $cb->(@$args) if( ref $cb eq 'CODE'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | 11 | return; | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | #--------#---------#---------#---------#---------#---------#---------#---------# | ||||||||||
| 154 | 5 | ▶ | sub _shutdown { | ||||||||
|
Subroutine
_shutdown: called CC=1 SCAR=0.0
| |||||||||||
| 155 | 5 | my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP]; | |||||||||
| 156 | |||||||||||
| 157 | #cleaup heap, alias, alarms (no lingering refs n ish) | ||||||||||
| 158 | 5 | %$heap = (); | |||||||||
| 159 | 5 | $kernel->alias_remove($self->alias); | |||||||||
| 160 | 5 | $kernel->alarm_remove_all(); | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | #--------#---------#---------#---------#---------#---------#---------#---------# | ||||||||||
| 164 | |||||||||||
| 165 | 8 | 1; | |||||||||
| 166 | |||||||||||
| 167 | __END__; | ||||||||||