Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
renamed io and watch methods in Mojo::IOWatcher
  • Loading branch information
kraih committed Oct 28, 2011
1 parent 6d31997 commit 1aa7665
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 92 deletions.
6 changes: 4 additions & 2 deletions Changes
Expand Up @@ -3,8 +3,10 @@ This file documents the revision history for Perl extension Mojolicious.
2.14 2011-10-29 00:00:00
- Deprecated Mojo::DOM->new with arguments.
- Renamed Mojo::IOLoop::Trigger to Mojo::IOLoop::Delay.
- Replaced cancel method in Mojo::IOWatcher with drop_timer method.
- Replaced remove method in Mojo::IOWatcher with drop_handle method.
- Renamed watch method in Mojo::IOWatcher to change.
- Renamed io method in Mojo::IOWatcher to watch.
- Renamed cancel method in Mojo::IOWatcher to drop_timer.
- Renamed remove method in Mojo::IOWatcher to drop_handle.
- Added EXPERIMENTAL --verbose flag to test command.

2.13 2011-10-28 00:00:00
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -121,7 +121,7 @@ sub _connect {

# Start writing right away
$self->{handle} = $handle;
$watcher->io(
$watcher->watch(
$handle,
on_readable => sub { $self->_connecting },
on_writable => sub { $self->_connecting }
Expand All @@ -138,8 +138,8 @@ sub _connecting {
my $watcher = $self->resolver->ioloop->iowatcher;
if ($self->{tls} && !$handle->connect_SSL) {
my $error = $IO::Socket::SSL::SSL_ERROR;
if ($error == TLS_READ) { $watcher->watch($handle, 1, 0) }
elsif ($error == TLS_WRITE) { $watcher->watch($handle, 1, 1) }
if ($error == TLS_READ) { $watcher->change($handle, 1, 0) }
elsif ($error == TLS_WRITE) { $watcher->change($handle, 1, 1) }
return;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -172,7 +172,7 @@ sub pause {
sub resume {
my $self = shift;
weaken $self;
$self->iowatcher->io($self->{handle},
$self->iowatcher->watch($self->{handle},
on_readable => sub { $self->_accept for 1 .. $self->accepts });
}

Expand All @@ -195,7 +195,7 @@ sub _accept {
close $handle;
};
$handle = IO::Socket::SSL->start_SSL($handle, %$tls);
$self->iowatcher->io(
$self->iowatcher->watch(
$handle,
on_readable => sub { $self->_tls($handle) },
on_writable => sub { $self->_tls($handle) }
Expand Down Expand Up @@ -251,8 +251,8 @@ sub _tls {

# Switch between reading and writing
my $error = $IO::Socket::SSL::SSL_ERROR;
if ($error == TLS_READ) { $self->iowatcher->watch($handle, 1, 0) }
elsif ($error == TLS_WRITE) { $self->iowatcher->watch($handle, 1, 1) }
if ($error == TLS_READ) { $self->iowatcher->change($handle, 1, 0) }
elsif ($error == TLS_WRITE) { $self->iowatcher->change($handle, 1, 1) }
}

1;
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -41,7 +41,7 @@ sub is_writing {
sub pause {
my $self = shift;
return if $self->{paused}++;
$self->iowatcher->watch($self->{handle}, 0, $self->is_writing);
$self->iowatcher->change($self->{handle}, 0, $self->is_writing);
}

sub resume {
Expand All @@ -50,7 +50,7 @@ sub resume {
# Start streaming
unless ($self->{streaming}++) {
weaken $self;
return $self->iowatcher->io(
return $self->iowatcher->watch(
$self->{handle},
on_readable => sub { $self->_read },
on_writable => sub { $self->_write }
Expand All @@ -59,7 +59,7 @@ sub resume {

# Resume streaming
return unless delete $self->{paused};
$self->iowatcher->watch($self->{handle}, 1, $self->is_writing);
$self->iowatcher->change($self->{handle}, 1, $self->is_writing);
}

# "No children have ever meddled with the Republican Party and lived to tell
Expand All @@ -81,7 +81,7 @@ sub write {
else { return unless length $self->{buffer} }

# Start writing
$self->iowatcher->watch($self->{handle}, !$self->{paused}, 1)
$self->iowatcher->change($self->{handle}, !$self->{paused}, 1)
if $self->{handle};
}

Expand Down Expand Up @@ -146,7 +146,7 @@ sub _write {

# Stop writing
return if $self->is_writing;
$self->iowatcher->watch($handle, !$self->{paused}, 0);
$self->iowatcher->change($handle, !$self->{paused}, 0);
}

1;
Expand Down
82 changes: 41 additions & 41 deletions lib/Mojo/IOWatcher.pm
Expand Up @@ -13,6 +13,18 @@ use constant DEBUG => $ENV{MOJO_IOWATCHER_DEBUG} || 0;
# I say the Pledge of Allegiance every day.
# You pledge allegiance to the flag.
# And the flag is made in China."
sub change {
my ($self, $handle, $read, $write) = @_;

my $poll = $self->_poll;
$poll->remove($handle);
if ($read && $write) { $poll->mask($handle, POLLIN | POLLOUT) }
elsif ($read) { $poll->mask($handle, POLLIN) }
elsif ($write) { $poll->mask($handle, POLLOUT) }

return $self;
}

sub detect {
my $try = $ENV{MOJO_IOWATCHER} || 'Mojo::IOWatcher::EV';
return $try unless Mojo::Loader->load($try);
Expand Down Expand Up @@ -40,15 +52,6 @@ sub is_readable {
return !!$result;
}

sub io {
my $self = shift;
my $handle = shift;
my $args = {@_, handle => $handle};
$self->{handles}->{fileno $handle} = $args;
$self->watch($handle, 1, $args->{on_writable});
return $self;
}

# "This was such a pleasant St. Patrick's Day until Irish people showed up."
sub recurring { shift->_timer(pop, after => pop, recurring => time) }

Expand All @@ -65,14 +68,11 @@ sub stop { delete shift->{running} }
sub timer { shift->_timer(pop, after => pop, started => time) }

sub watch {
my ($self, $handle, $read, $write) = @_;

my $poll = $self->_poll;
$poll->remove($handle);
if ($read && $write) { $poll->mask($handle, POLLIN | POLLOUT) }
elsif ($read) { $poll->mask($handle, POLLIN) }
elsif ($write) { $poll->mask($handle, POLLOUT) }

my $self = shift;
my $handle = shift;
my $args = {@_, handle => $handle};
$self->{handles}->{fileno $handle} = $args;
$self->change($handle, 1, $args->{on_writable});
return $self;
}

Expand Down Expand Up @@ -143,7 +143,7 @@ Mojo::IOWatcher - Non-blocking I/O watcher
# Watch if handle becomes readable
my $watcher = Mojo::IOWatcher->new;
$watcher->io($handle, on_readable => sub {
$watcher->watch($handle, on_readable => sub {
my ($watcher, $handle) = @_;
...
});
Expand Down Expand Up @@ -178,6 +178,14 @@ following new ones.
Detect and load the best watcher implementation available, will try the value
of the C<MOJO_IOWATCHER> environment variable or L<Mojo::IOWatcher::EV>.
=head2 C<change>
$watcher = $watcher->change($handle, $read, $write);
Change I/O events to watch handle for.
$watcher->change($handle, 0, 1);
=head2 C<drop_handle>
$watcher->drop_handle($handle);
Expand All @@ -190,26 +198,6 @@ Drop handle.
Drop timer.
=head2 C<io>
$watcher = $watcher->io($handle, on_readable => sub {...});
Watch handle for I/O events.
These options are currently available:
=over 2
=item C<on_readable>
Callback to be invoked once the handle becomes readable.
=item C<on_writable>
Callback to be invoked once the handle becomes writable.
=back
=head2 C<is_readable>
my $success = $watcher->is_readable($handle);
Expand Down Expand Up @@ -244,11 +232,23 @@ Create a new timer, invoking the callback after a given amount of seconds.
=head2 C<watch>
$watcher = $watcher->watch($handle, $read, $write);
$watcher = $watcher->watch($handle, on_readable => sub {...});
Change I/O events to watch handle for.
Watch handle for I/O events.
$watcher->watch($handle, 0, 1);
These options are currently available:
=over 2
=item C<on_readable>
Callback to be invoked once the handle becomes readable.
=item C<on_writable>
Callback to be invoked once the handle becomes writable.
=back
=head1 DEBUGGING
Expand Down
42 changes: 21 additions & 21 deletions lib/Mojo/IOWatcher/EV.pm
Expand Up @@ -11,19 +11,7 @@ sub DESTROY { undef $EV }
# We have to fall back to Mojo::IOWatcher, since EV is unique
sub new { $EV++ ? Mojo::IOWatcher->new : shift->SUPER::new }

sub drop_handle { delete shift->{handles}->{fileno shift} }

sub recurring { shift->_timer(shift, 1, @_) }

# "Wow, Barney. You brought a whole beer keg.
# Yeah... where do I fill it up?"
sub start {EV::run}

sub stop { EV::break(EV::BREAK_ONE) }

sub timer { shift->_timer(shift, 0, @_) }

sub watch {
sub change {
my ($self, $handle, $read, $write) = @_;

my $fd = fileno $handle;
Expand All @@ -42,6 +30,18 @@ sub watch {
return $self;
}

sub drop_handle { delete shift->{handles}->{fileno shift} }

sub recurring { shift->_timer(shift, 1, @_) }

# "Wow, Barney. You brought a whole beer keg.
# Yeah... where do I fill it up?"
sub start {EV::run}

sub stop { EV::break(EV::BREAK_ONE) }

sub timer { shift->_timer(shift, 0, @_) }

sub _io {
my ($self, $fd, $w, $revents) = @_;
my $handles = $self->{handles};
Expand Down Expand Up @@ -104,6 +104,14 @@ implements the following new ones.
Construct a new L<Mojo::IOWatcher::EV> object.
=head2 C<change>
$watcher = $watcher->change($handle, $read, $write);
Change I/O events to watch handle for.
$watcher->change($handle, 0, 0);
=head2 C<drop_handle>
$watcher->drop_handle($handle);
Expand Down Expand Up @@ -135,14 +143,6 @@ Stop watching for I/O and timer events.
Create a new timer, invoking the callback after a given amount of seconds.
=head2 C<watch>
$watcher = $watcher->watch($handle, $read, $write);
Change I/O events to watch handle for.
$watcher->watch($handle, 0, 0);
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
16 changes: 8 additions & 8 deletions t/mojo/iowatcher.t
Expand Up @@ -27,7 +27,7 @@ my $listen = IO::Socket::INET->new(
my $watcher = Mojo::IOWatcher->new;
isa_ok $watcher, 'Mojo::IOWatcher', 'right object';
my ($readable, $writable);
$watcher->io(
$watcher->watch(
$listen,
on_readable => sub { $readable++ },
on_writable => sub { $writable++ }
Expand All @@ -51,7 +51,7 @@ $watcher = undef;
$watcher = Mojo::IOWatcher->new;
isa_ok $watcher, 'Mojo::IOWatcher', 'right object';
($readable, $writable) = undef;
$watcher->io(
$watcher->watch(
$client,
on_readable => sub { $readable++ },
on_writable => sub { $writable++ }
Expand All @@ -66,33 +66,33 @@ $watcher = undef;
$watcher = Mojo::IOWatcher->new;
isa_ok $watcher, 'Mojo::IOWatcher', 'right object';
($readable, $writable) = undef;
$watcher->io(
$watcher->watch(
$server,
on_readable => sub { $readable++ },
on_writable => sub { $writable++ }
);
$watcher->watch($server, 1, 0);
$watcher->change($server, 1, 0);
$watcher->timer(0 => sub { shift->stop });
$watcher->start;
is $readable, 1, 'handle is readable';
is $writable, undef, 'handle is not writable';
$watcher->watch($server, 1, 1);
$watcher->change($server, 1, 1);
$watcher->timer(0 => sub { shift->stop });
$watcher->start;
is $readable, 2, 'handle is readable';
is $writable, 1, 'handle is writable';
$watcher->watch($server, 0, 0);
$watcher->change($server, 0, 0);
$watcher->timer(0 => sub { shift->stop });
$watcher->start;
is $readable, 2, 'handle is not readable';
is $writable, 1, 'handle is not writable';
$watcher->watch($server, 1, 0);
$watcher->change($server, 1, 0);
$watcher->timer(0 => sub { shift->stop });
$watcher->start;
is $readable, 3, 'handle is readable';
is $writable, 1, 'handle is not writable';
($readable, $writable) = undef;
$watcher->io(
$watcher->watch(
$server,
on_readable => sub { $readable++ },
on_writable => sub { $writable++ }
Expand Down

0 comments on commit 1aa7665

Please sign in to comment.