Pusher/Connection.pm

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