Permalink
Browse files

Moving towards KISS

  • Loading branch information...
1 parent b530bfc commit 6b4db2d7b5f5597055507c89fb80641e6b9a714d @vti committed Jan 21, 2012
View
@@ -30,6 +30,8 @@ sub new {
return $self;
}
+sub fh { $_[0]->{handle}->fh }
+
sub on_heartbeat {
my $self = shift;
my ($cb) = @_;
@@ -151,7 +153,7 @@ L<AnyEvent::Handle>.
=head2 C<new>
-=head2 C<heartbeat_timeout>
+=head2 C<fh>
=head2 C<on_heartbeat>
View
@@ -68,12 +68,10 @@ sub dispatch {
my $transport = $self->_build_transport(
$path->transport_type,
- env => $env,
- pool => $self->{pool},
- conn => $conn,
- heartbeat_timeout => $self->{heartbeat_timeout},
- close_timeout => $self->{close_timeout},
- on_disconnect => sub { $self->{pool}->remove_connection($conn) }
+ env => $env,
+ conn => $conn,
+ handle => $self->_build_handle($env),
+ on_disconnect => sub { $self->{pool}->remove_connection($conn) }
);
$conn->type($path->transport_type);
@@ -87,6 +85,16 @@ sub dispatch {
};
}
+sub _build_handle {
+ my $self = shift;
+ my ($env) = @_;
+
+ return PocketIO::Handle->new(
+ heartbeat_timeout => $self->{heartbeat_timeout},
+ fh => $env->{'psgix.io'}
+ );
+}
+
sub _dispatch_handshake {
my $self = shift;
my ($env, $cb) = @_;
@@ -9,7 +9,6 @@ use Try::Tiny;
use Scalar::Util qw(weaken);
use Plack::Request;
-use PocketIO::Handle;
sub new {
my $class = shift;
@@ -39,80 +38,30 @@ sub client_connected {
return if $conn->is_connected;
- $self->_log_client_connected($conn);
-
$conn->connected;
}
sub client_disconnected {
my $self = shift;
my ($conn) = @_;
- $self->_log_client_disconnected($conn);
-
$conn->disconnected;
$self->{on_disconnect}->($self);
return $self;
}
-sub _log_client_connected {
- my $self = shift;
- my ($conn) = @_;
-
- my $logger = $self->_get_logger;
- return unless $logger;
-
- $logger->(
- { level => 'debug',
- message => sprintf(
- "Client '%s' connected via '%s'",
- $conn->id, $conn->type
- )
- }
- );
-}
-
-sub _log_client_disconnected {
- my $self = shift;
- my ($conn) = @_;
-
- my $logger = $self->_get_logger;
- return unless $logger;
-
- $logger->(
- { level => 'debug',
- message => sprintf("Client '%s' disconnected", $conn->id)
- }
- );
-}
-
-sub _get_logger {
- my $self = shift;
-
- return $self->env->{'psgix.logger'};
-}
-
-sub _build_handle {
- my $self = shift;
-
- my $heartbeat_timeout = $self->{heartbeat_timeout};
- $heartbeat_timeout -= 5;
-
- return PocketIO::Handle->new(heartbeat_timeout => $heartbeat_timeout, @_);
-}
-
1;
__END__
=head1 NAME
-PocketIO::Base - Base class for transports
+PocketIO::Transport::Base - Base class for transports
=head1 DESCRIPTION
-L<PocketIO::Base> is a base class for the transports.
+L<PocketIO::Transport::Base> is a base class for the transports.
=head1 METHODS
@@ -22,7 +22,7 @@ sub _dispatch_stream {
my $conn = $self->conn;
- my $handle = $self->_build_handle(fh => $self->env->{'psgix.io'});
+ my $handle = $self->{handle};
return sub {
my $respond = shift;
@@ -105,16 +105,14 @@ __END__
=head1 NAME
-PocketIO::Polling - Basic class for polling transports
+PocketIO::Transport::BasePolling - Basic class for polling transports
=head1 DESCRIPTION
Basic class for polling transports.
=head1 METHODS
-=head2 name
-
=head2 dispatch
=cut
@@ -22,7 +22,7 @@ sub _dispatch_stream {
my $conn = $self->conn;
- my $handle = $self->_build_handle(fh => $self->env->{'psgix.io'});
+ my $handle = $self->{handle};
return sub {
my $close_cb =
@@ -90,18 +90,16 @@ __END__
=head1 NAME
-PocketIO::Htmlfile - Htmlfile transport
+PocketIO::Transport::Htmlfile - Htmlfile transport
=head1 DESCRIPTION
-L<PocketIO::Htmlfile> is a C<htmlfile> transport implementation.
+L<PocketIO::Transport::Htmlfile> is a C<htmlfile> transport implementation.
=head1 METHODS
=over
-=item name
-
=item dispatch
=back
@@ -22,17 +22,14 @@ __END__
=head1 NAME
-PocketIO::JSONPPolling - JSONPPolling transport
+PocketIO::Transport::JSONPPolling - JSONPPolling transport
=head1 DESCRIPTION
-L<PocketIO::JSONPPolling> is a C<jsonp-polling> transport
-implementation.
+L<PocketIO::Transport::JSONPPolling> is a C<jsonp-polling> transport implementation.
=head1 METHODS
-=head2 C<name>
-
-=head2 C<dispatch>
+Inherits all methods from L<PocketIO::Transport::BasePolling>.
=cut
@@ -14,19 +14,17 @@ use PocketIO::Handle;
sub dispatch {
my $self = shift;
- my $fh = $self->req->env->{'psgix.io'};
- PocketIO::Exception->throw(500 => 'No psgix.io available') unless $fh;
+ my $handle = $self->{handle};
my $hs =
Protocol::WebSocket::Handshake::Server->new_from_psgi($self->req->env);
PocketIO::Exception->throw('WebSocket failed: ' . $hs->error)
- unless $hs->parse($fh);
+ unless $hs->parse($handle->fh);
return unless $hs->is_done;
my $version = $hs->version;
- my $handle = $self->_build_handle(fh => $fh);
my $frame = Protocol::WebSocket::Frame->new(version => $version);
return sub {
@@ -96,16 +94,14 @@ __END__
=head1 NAME
-PocketIO::WebSocket - WebSocket transport
+PocketIO::Transport::WebSocket - WebSocket transport
=head1 DESCRIPTION
-L<PocketIO::WebSocket> is a WebSocket transport implementation.
+L<PocketIO::Transport::WebSocket> is a WebSocket transport implementation.
=head1 METHODS
-=head2 name
-
=head2 dispatch
=head1 SEE ALSO
@@ -28,12 +28,11 @@ sub dispatch {
sub _dispatch_stream {
my $self = shift;
- my $handle = $self->_build_handle($self->req->env->{'psgix.io'});
- return unless $handle;
-
return sub {
my $respond = shift;
+ my $handle = $self->{handle};
+
my $conn = $self->conn;
my $close_cb = sub { $handle->close; $self->client_disconnected($conn); };
@@ -91,19 +90,16 @@ __END__
=head1 NAME
-PocketIO::XHRMultipart - XHRMultipart transport
+PocketIO::Transport::XHRMultipart - XHRMultipart transport
=head1 DESCRIPTION
-L<PocketIO::XHRMultipart> is a C<xhr-multipart> transport
-implementation.
+L<PocketIO::Transport::XHRMultipart> is a C<xhr-multipart> transport implementation.
=head1 METHODS
=head2 C<new>
-=head2 C<name>
-
=head2 C<dispatch>
=cut
@@ -10,17 +10,14 @@ __END__
=head1 NAME
-PocketIO::XHRPolling - XHRPolling transport
+PocketIO::Transport::XHRPolling - XHRPolling transport
=head1 DESCRIPTION
-L<PocketIO::XHRPolling> is a C<xhr-polling> transport
-implementation.
+L<PocketIO::Transport::XHRPolling> is a C<xhr-polling> transport implementation.
=head1 METHODS
-=head2 C<name>
-
-=head2 C<dispatch>
+Inherict all methods from L<PocketIO::Transport::BasePolling>.
=cut

0 comments on commit 6b4db2d

Please sign in to comment.