Skip to content

Commit

Permalink
redesigned Mojo::IOWatcher api
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 4, 2012
1 parent 5dd09f8 commit e292cc7
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 213 deletions.
6 changes: 5 additions & 1 deletion Changes
@@ -1,6 +1,6 @@
This file documents the revision history for Perl extension Mojolicious.

2.47 2012-02-04 00:00:00
2.47 2012-02-05 00:00:00
- Deprecated Mojo::Server::Daemon->prepare_ioloop in favor of
Mojo::Server::Daemon->start.
- Deprecated Mojo::Headers->x_forwarded_for.
Expand All @@ -11,6 +11,10 @@ This file documents the revision history for Perl extension Mojolicious.
- Added lock_timeout parameter to Hypnotoad.
- Removed experimental status from JSON Pointer support.
- Removed Cygwin exception from Hypnotoad.
- Replaced drop_handle and drop_timer methods in Mojo::IOWatcher with
drop method.
- Renamed change and watch methods in Mojo::IOWatcher to watch and
io.
- Renamed resume and pause methods in Mojo::IOLoop::Server to start
and stop.
- Renamed resume and pause methods in Mojo::IOLoop::Stream to start
Expand Down
8 changes: 0 additions & 8 deletions lib/Mojo/Content.pm
Expand Up @@ -121,14 +121,6 @@ sub header_size { length shift->build_headers }

sub is_chunked { (shift->headers->transfer_encoding || '') =~ /chunked/i }

# DEPRECATED in Leaf Fluttering In Wind!
sub is_done {
warn <<EOF;
Mojo::Content->is_done is DEPRECATED in favor of Mojo::Content->is_finished!
EOF
shift->is_finished;
}

sub is_dynamic {
my $self = shift;
return $self->{dynamic} && !defined $self->headers->content_length;
Expand Down
12 changes: 1 addition & 11 deletions lib/Mojo/DOM.pm
Expand Up @@ -33,17 +33,7 @@ sub DESTROY { }
sub new {
my $class = shift;
my $self = bless [Mojo::DOM::HTML->new], ref $class || $class;

# DEPRECATED in Leaf Fluttering In Wind!
my $input;
$input = shift if @_ % 2;
warn "Mojo::DOM->new with arguments is DEPRECATED!\n" if @_;
my %attrs = (@_);
exists($attrs{$_}) and $self->$_($attrs{$_}) for qw/tree charset xml/;

# Parse right away
$self->parse($input) if defined $input;

$self->parse(@_) if @_;
return $self;
}

Expand Down
8 changes: 0 additions & 8 deletions lib/Mojo/Headers.pm
Expand Up @@ -88,14 +88,6 @@ sub header {
return @$headers;
}

# DEPRECATED in Leaf Fluttering In Wind!
sub is_done {
warn <<EOF;
Mojo::Headers->is_done is DEPRECATED in favor of Mojo::Headers->is_finished!
EOF
shift->is_finished;
}

sub is_finished { (shift->{state} || '') eq 'finished' }

sub is_limit_exceeded { shift->{limit} }
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop.pm
Expand Up @@ -389,7 +389,7 @@ sub _drop {

# Timer
return unless my $watcher = $self->iowatcher;
return if $watcher->drop_timer($id);
return if $watcher->drop($id);

# Listen socket
if (delete $self->{servers}->{$id}) { delete $self->{listening} }
Expand Down
14 changes: 5 additions & 9 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -42,8 +42,8 @@ sub connect {
sub _cleanup {
my $self = shift;
return unless my $watcher = $self->{iowatcher};
$watcher->drop_timer($self->{timer}) if $self->{timer};
$watcher->drop_handle($self->{handle}) if $self->{handle};
$watcher->drop($self->{timer}) if $self->{timer};
$watcher->drop($self->{handle}) if $self->{handle};
}

sub _connect {
Expand Down Expand Up @@ -108,11 +108,7 @@ sub _connect {

# Start writing right away
$self->{handle} = $handle;
$watcher->watch(
$handle,
sub { $self->_connecting },
sub { $self->_connecting }
);
$watcher->io($handle => sub { $self->_connecting });
}

# "Have you ever seen that Blue Man Group? Total ripoff of the Smurfs.
Expand All @@ -125,8 +121,8 @@ sub _connecting {
my $watcher = $self->iowatcher;
if ($self->{tls} && !$handle->connect_SSL) {
my $err = $IO::Socket::SSL::SSL_ERROR;
if ($err == TLS_READ) { $watcher->change($handle, 1, 0) }
elsif ($err == TLS_WRITE) { $watcher->change($handle, 1, 1) }
if ($err == TLS_READ) { $watcher->watch($handle, 1, 0) }
elsif ($err == TLS_WRITE) { $watcher->watch($handle, 1, 1) }
return;
}

Expand Down
22 changes: 9 additions & 13 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -82,7 +82,7 @@ sub DESTROY {
if (my $key = $self->{key}) { unlink $key if -w $key }
return unless my $watcher = $self->{iowatcher};
$self->stop if $self->{handle};
$watcher->drop_handle($_) for values %{$self->{handles}};
$watcher->drop($_) for values %{$self->{handles}};
}

# "And I gave that man directions, even though I didn't know the way,
Expand Down Expand Up @@ -168,13 +168,13 @@ sub generate_port {
sub start {
my $self = shift;
weaken $self;
$self->iowatcher->watch($self->{handle},
sub { $self->_accept for 1 .. $self->accepts });
$self->iowatcher->io(
$self->{handle} => sub { $self->_accept for 1 .. $self->accepts });
}

sub stop {
my $self = shift;
$self->iowatcher->drop_handle($self->{handle});
$self->iowatcher->drop($self->{handle});
}

sub _accept {
Expand All @@ -192,15 +192,11 @@ sub _accept {
weaken $self;
$tls->{SSL_error_trap} = sub {
return unless my $handle = delete $self->{handles}->{shift()};
$self->iowatcher->drop_handle($handle);
$self->iowatcher->drop($handle);
close $handle;
};
$handle = IO::Socket::SSL->start_SSL($handle, %$tls);
$self->iowatcher->watch(
$handle,
sub { $self->_tls($handle) },
sub { $self->_tls($handle) }
);
$self->iowatcher->io($handle => sub { $self->_tls($handle) });
$self->{handles}->{$handle} = $handle;
}

Expand Down Expand Up @@ -245,15 +241,15 @@ sub _tls {

# Accepted
if ($handle->accept_SSL) {
$self->iowatcher->drop_handle($handle);
$self->iowatcher->drop($handle);
delete $self->{handles}->{$handle};
return $self->emit_safe(accept => $handle);
}

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

1;
Expand Down
22 changes: 10 additions & 12 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -26,9 +26,9 @@ sub close {

# Cleanup
return unless my $watcher = $self->{iowatcher};
$watcher->drop_timer(delete $self->{timer}) if $self->{timer};
$watcher->drop(delete $self->{timer}) if $self->{timer};
return unless my $handle = delete $self->{handle};
$watcher->drop_handle($handle);
$watcher->drop($handle);

# Close
close $handle;
Expand Down Expand Up @@ -62,28 +62,26 @@ sub start {
);

# Start streaming
return $watcher->watch(
$self->{handle},
sub { $self->_read },
sub { $self->_write }
) unless $self->{streaming}++;
my $handle = $self->{handle};
return $watcher->io($handle => sub { pop() ? $self->_write : $self->_read })
unless $self->{streaming}++;

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

sub stop {
my $self = shift;
return if $self->{paused}++;
$self->iowatcher->change($self->{handle}, 0, $self->is_writing);
$self->iowatcher->watch($self->{handle}, 0, $self->is_writing);
}

# "No children have ever meddled with the Republican Party and lived to tell
# about it."
sub steal_handle {
my $self = shift;
$self->iowatcher->drop_handle($self->{handle});
$self->iowatcher->drop($self->{handle});
return delete $self->{handle};
}

Expand All @@ -98,7 +96,7 @@ sub write {
else { return unless length $self->{buffer} }

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

Expand Down Expand Up @@ -162,7 +160,7 @@ sub _write {

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

1;
Expand Down

0 comments on commit e292cc7

Please sign in to comment.