| 1 | package Net::Async::Pusher::Connection; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.005'; # VERSION | |||||||||||||||||||||||||||
| 7 | 2 | our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Net::Async::Pusher::Connection - represents one L<Net::Async::Pusher> server connection | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Provides basic integration with the L<https://pusher.com|Pusher> API. This implements | ||||||||||||||||||||||||||||
| 18 | the protocol as documented in L<https://pusher.com/docs/pusher_protocol>. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Mixin::Event::Dispatch::Bus; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Net::Async::WebSocket::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use IO::Async::SSL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use HTML::Entities (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use JSON::MaybeUTF8 qw(:v1); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | use curry::weak; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use Variable::Disposition qw(retain_future); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use Net::Async::Pusher::Channel; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub bus { shift->{bus} //= Mixin::Event::Dispatch::Bus->new } | ||||||||||||||||||||||||||
|
Subroutine
bus: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/3
+(shift())->{"bus"} //= "Mixin::Event::Dispatch::Bus"->new
MC/DC0%0/2
+(shift())->{"bus"} //= "Mixin::Event::Dispatch::Bus"->new
+(shift())->{"bus"}
"Mixin::Event::Dispatch::Bus"->new
Truth table0%0/3
A //= B
A= +(shift())->{"bus"}B= "Mixin::Event::Dispatch::Bus"->new
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub json { shift->{json} //= JSON::MaybeXS->new( allow_nonref => 1) } | ||||||||||||||||||||||||||
|
Subroutine
json: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/3
+(shift())->{"json"} //= "JSON::MaybeXS"->new("allow_nonref", 1)
MC/DC0%0/2
+(shift())->{"json"} //= "JSON::MaybeXS"->new("allow_nonref", 1)
+(shift())->{"json"}
"JSON::MaybeXS"->new("allow_nonref", 1)
Truth table0%0/3
A //= B
A= +(shift())->{"json"}B= "JSON::MaybeXS"->new("allow_nonref", 1)
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head2 send_ping | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Sends a ping request on this connection. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub send_ping { | ||||||||||||||||||||||||||
|
Subroutine
send_ping: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 52 | 0 | $self->client->send_frame( | |||||||||||||||||||||||||||
| 53 | buffer => encode_json_utf8({ | ||||||||||||||||||||||||||||
| 54 | event => 'pusher:ping', | ||||||||||||||||||||||||||||
| 55 | data => { } | ||||||||||||||||||||||||||||
| 56 | }), | ||||||||||||||||||||||||||||
| 57 | masked => 1, | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if(my $timer = $self->{inactivity_timer}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $timer->stop if $timer->is_running; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | $timer->reset; | |||||||||||||||||||||||||||
| 62 | 0 | $timer->start; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | $self | ||||||||||||||||||||||||||||
| 65 | 0 | } | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head2 incoming_frame | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Deals with incoming frames. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub incoming_frame { | ||||||||||||||||||||||||||
|
Subroutine
incoming_frame: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 0 | my ($client, $frame) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | return unless defined($frame) && length($frame); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $frame and length $frame
MC/DC0%0/2
defined $frame and length $frame
defined $frame
length $frame
Truth table0%0/3
A and B
A= defined $frameB= length $frame
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | try { | ||||||||||||||||||||||||||||
| 80 | 0 | $log->tracef("Frame [%s]", $frame); | |||||||||||||||||||||||||||
| 81 | 0 | $self->{last_seen} = time; | |||||||||||||||||||||||||||
| 82 | 0 | my $info = $self->json->decode($frame); | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | if(exists $info->{channel}) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 84 | 0 | return $self->{channel}{$info->{channel}}->incoming_message($info); | |||||||||||||||||||||||||||
| 85 | } elsif($info->{event} eq 'pusher:connection_established') { | ||||||||||||||||||||||||||||
| 86 | 0 | my $data = $self->json->decode($info->{data}); | |||||||||||||||||||||||||||
| 87 | 0 | $self->{socket_id} = $data->{socket_id}; | |||||||||||||||||||||||||||
| 88 | 0 | $log->tracef('Setting inactivity timeout to %d seconds', $data->{activity_timeout}); | |||||||||||||||||||||||||||
| 89 | $self->add_child( | ||||||||||||||||||||||||||||
| 90 | $self->{inactivity_timer} = IO::Async::Timer::Countdown->new( | ||||||||||||||||||||||||||||
| 91 | delay => $data->{activity_timeout}, | ||||||||||||||||||||||||||||
| 92 | 0 | on_expire => $self->curry::weak::send_ping, | |||||||||||||||||||||||||||
| 93 | ) | ||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||
| 95 | 0 | $self->{inactivity_timer}->start; | |||||||||||||||||||||||||||
| 96 | 0 | return $self->connected->done; | |||||||||||||||||||||||||||
| 97 | } elsif($info->{event} eq 'pusher:ping') { | ||||||||||||||||||||||||||||
| 98 | 0 | return $self->client->send_frame( | |||||||||||||||||||||||||||
| 99 | buffer => encode_json_utf8({ | ||||||||||||||||||||||||||||
| 100 | event => 'pusher:pong', | ||||||||||||||||||||||||||||
| 101 | data => { } | ||||||||||||||||||||||||||||
| 102 | }), | ||||||||||||||||||||||||||||
| 103 | masked => 1, | ||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||
| 105 | } elsif($info->{event} eq 'pusher:pong') { | ||||||||||||||||||||||||||||
| 106 | 0 | return $log->trace("Pong event received from pusher"); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | die "unhandled" | |||||||||||||||||||||||||||
| 109 | 0 | } catch { | |||||||||||||||||||||||||||
| 110 | 0 | my $err = $@; | |||||||||||||||||||||||||||
| 111 | 0 | $log->errorf("Unexpected frame (%s) [%s]", $err, $frame); | |||||||||||||||||||||||||||
| 112 | 0 | $self->bus->invoke_event( | |||||||||||||||||||||||||||
| 113 | error => $err, $frame | ||||||||||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub incoming_ping_frame { | ||||||||||||||||||||||||||
|
Subroutine
incoming_ping_frame: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 0 | my ($client, $frame) = @_; | |||||||||||||||||||||||||||
| 121 | 0 | $log->debugf('Received ping frame'); | |||||||||||||||||||||||||||
| 122 | 0 | $self->client->send_pong_frame( | |||||||||||||||||||||||||||
| 123 | '', | ||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub socket_id { shift->{socket_id} } | ||||||||||||||||||||||||||
|
Subroutine
socket_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 client | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Returns the L<Net::Async::WebSocket::Client> instance. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub client { shift->{client} } | ||||||||||||||||||||||||||
|
Subroutine
client: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head2 open_channel | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Opens a channel. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | my $ch = $conn->open_channel( | ||||||||||||||||||||||||||||
| 142 | 'xyz' | ||||||||||||||||||||||||||||
| 143 | )->get; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Resolves to a L<Net::Async::Pusher::Channel> instance. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =cut | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | sub open_channel { | ||||||||||||||||||||||||||
|
Subroutine
open_channel: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 0 | my ($self, $name, %args) = @_; | |||||||||||||||||||||||||||
| 151 | 0 | ▶ | $self->connected->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 152 | 0 | $log->tracef("Subscribing to [%s]", $name); | |||||||||||||||||||||||||||
| 153 | 0 | my $ch = $self->{channel}{$name} = Net::Async::Pusher::Channel->new( | |||||||||||||||||||||||||||
| 154 | name => $name, | ||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||
| 156 | 0 | $self->add_child($ch); | |||||||||||||||||||||||||||
| 157 | my $frame = encode_json_utf8({ | ||||||||||||||||||||||||||||
| 158 | event => 'pusher:subscribe', | ||||||||||||||||||||||||||||
| 159 | # double-encoded | ||||||||||||||||||||||||||||
| 160 | data => { | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | (exists $args{auth} ? (auth => $args{auth}) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | channel => $name | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | }); | ||||||||||||||||||||||||||||
| 165 | 0 | $log->tracef("Subscribing: %s", $frame); | |||||||||||||||||||||||||||
| 166 | 0 | $self->client->send_frame( | |||||||||||||||||||||||||||
| 167 | buffer => $frame, | ||||||||||||||||||||||||||||
| 168 | masked => 1, | ||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||
| 170 | # We map the channel ourselves so that we don't end up with | ||||||||||||||||||||||||||||
| 171 | # the channel's ->subscribed method holding a strong reference | ||||||||||||||||||||||||||||
| 172 | # to itself | ||||||||||||||||||||||||||||
| 173 | $self->{channel}{$name}->subscribed->transform( | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | done => sub { $ch } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 175 | ) | ||||||||||||||||||||||||||||
| 176 | 0 | }); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 connect | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | (Re)connects to the feed. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub connect { | ||||||||||||||||||||||||||
|
Subroutine
connect: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 187 | $self->add_child( | ||||||||||||||||||||||||||||
| 188 | 0 | $self->{client} = Net::Async::WebSocket::Client->new( | |||||||||||||||||||||||||||
| 189 | on_frame => $self->curry::weak::incoming_frame, | ||||||||||||||||||||||||||||
| 190 | on_ping_frame => $self->curry::weak::incoming_ping_frame, | ||||||||||||||||||||||||||||
| 191 | ) | ||||||||||||||||||||||||||||
| 192 | ); | ||||||||||||||||||||||||||||
| 193 | 0 | ▶ | my $uri = URI->new('wss://ws-mt1.pusher.com/app/' . $self->key . '?protocol=7&client=perl-net-async-pusher&version=' . ($self->VERSION || '1.0')); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->VERSION || '1.0'
MC/DC0%0/1
$self->VERSION || '1.0'
$self->VERSION
Truth table0%0/2
A
A= $self->VERSION
| |||||||||||||||||||||||||||||
| 194 | 0 | $log->tracef('Connecting to %s', $uri); | |||||||||||||||||||||||||||
| 195 | $self->client->connect( | ||||||||||||||||||||||||||||
| 196 | url => $uri, | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | )->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 198 | 0 | $log->tracef('Connected to %s', $uri); | |||||||||||||||||||||||||||
| 199 | # don't seem to get any response until we send something first | ||||||||||||||||||||||||||||
| 200 | 0 | $self->send_ping; | |||||||||||||||||||||||||||
| 201 | 0 | Future->done($self) | |||||||||||||||||||||||||||
| 202 | 0 | ▶ | })->on_fail(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 203 | 0 | $log->errorf('Failed to connect - %s', join ',', @_) | |||||||||||||||||||||||||||
| 204 | 0 | })->retain; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head2 connected | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | L<Future> representing current connection state. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | sub connected { $_[0]->{connected} ||= $_[0]->loop->new_future } | ||||||||||||||||||||||||||
|
Subroutine
connected: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/3
$_[0]{"connected"} ||= $_[0]->loop->new_future
MC/DC0%0/2
$_[0]{"connected"} ||= $_[0]->loop->new_future
$_[0]{"connected"}
$_[0]->loop->new_future
Truth table0%0/3
A ||= B
A= $_[0]{"connected"}B= $_[0]->loop->new_future
| |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 0 | ▶ | sub configure { | ||||||||||||||||||||||||||
|
Subroutine
configure: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 217 | 0 | for(grep exists $args{$_}, qw(key)) { | |||||||||||||||||||||||||||
| 218 | 0 | $self->{$_} = delete $args{$_}; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 0 | $self->SUPER::configure(%args); | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head2 key | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | The key. | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =cut | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 0 | ▶ | sub key { shift->{key} } | ||||||||||||||||||||||||||
|
Subroutine
key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 2 | 1; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | __END__ | ||||||||||||||||||||||||||||