Hi Everyone,

        I'm having a few problems with POE 0.26 reading files on ActivePerl
5.61 (build 635),
on WinXP/2K.  

        If I use a ReadWrite wheel to read the file, I never get any
Input events at all -- it basically stops, and the CPU starts rising
steadily.  From what I gather, using ReadWrite to read files on Win32
is unsupported because you cant set filehandles to non-blocking on
Win32, right?

        Well, I noticed that FollowTail works fine on Win32.  So, just to
temporarily fix my problem, I hacked up a little Wrapper around 
FollowTail... it sets the SeekBack parameter to larger than the file's
size so it can read the entire file, then goes on processing the input
event's until it notices the entire file has been read.  The code is
attached
below.

        It works, but I'm not really satisfied with this solution.  I'd
like to use Readwrite features such as input throttling with highMark
and lowMark. A few questions:

        - Why is FollowTail able to read files in Win32 while ReadWrite 
isn't?
        - What are the main problems in this type of approach i've taken?
I know I'm generating twice the number of input events here (followtail's,
and for whoever's using my Reader object), but from my tests, that 
doesn't seem to slow things down so much.
        - How could I implement input throttling decently on top of 
FolloTail ?
        - Would the best thing to do be writing a new object, using POE's
internal calls for select and so on, forgetting FollowTail entirely?

        Thank you ver much!!

use strict;

package Reader;

use POE;
use POE::Kernel;
use POE::Session;
use POE::Wheel::FollowTail;

sub new {
        my ($class, $hashOptions) = @_;
        my $self = {};
        bless ($self, $class);

        my $session = POE::Session->create(
                                        object_states => 
                                                [ $self => [ qw(_start _stop
initRead input error) ] ],
                                        );

        $self->{options} = $hashOptions;
        
        return $self;
}

sub getSession {
        my $self = shift;
        return $self->{session};
}

sub _start {
        my ($self, $session, $heap, $kernel) = @_[OBJECT, SESSION, HEAP,
KERNEL];
        $kernel->yield("initRead");
}

sub initRead {
        my ($self, $session, $heap, $kernel) = @_[OBJECT, SESSION, HEAP,
KERNEL];
        
        return if ( ! -e $self->{options}{File} );
        $self->{filesize} = -s $self->{options}{File};

        my $fd;
        open ($fd, $self->{options}{File});
        binmode($fd);
        
        my $wheel = new POE::Wheel::FollowTail(
                        Handle => $fd,
                        Driver => $self->{options}{Driver},
                        Filter => $self->{options}{Filter},
                        PollInterval => 1,
                        InputEvent => "input",
                        ErrorEvent => "error",
                        SeekBack => $self->{filesize}+10
                );
        $self->{wheel} = $wheel;
}

sub _stop {
        my ($self, $session, $heap, $kernel) = @_[OBJECT, SESSION, HEAP,
KERNEL];
        undef @{$self->{session}}; 
}

sub error {
        my ($self, $session, $heap, $kernel) = @_[OBJECT, SESSION, HEAP,
KERNEL];
        my ($errFunc, $errCode, $errMsg) = @_[ARG0 .. ARG2];

        my $sess = $self->{options}{Session};
        $kernel->post($sess, $self->{options}{ErrorEvent}, $errFunc,
$errCode, $errMsg);
}

sub input {
        my ($self, $session, $heap, $kernel) = @_[OBJECT, SESSION, HEAP,
KERNEL];
        my ($input) = @_[ARG0];
        my $sess = $self->{options}{Session};
        my $ended = 0;
        $self->{count} = $self->{count} + length($input);
        
        $ended = 1 if ($self->{count} eq $self->{filesize});
        $kernel->post($sess, $self->{options}{InputEvent}, $input, $ended);
#       print " -- $self->{count} = $self->{filesize}\n";
        if ($self->{count} eq $self->{filesize}) {
                delete($self->{wheel});
        }
}

1;


Cristiano Lincoln Mattos
Tempest Security Technologies - www.tempest.com.br 


 

Reply via email to