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.004;
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(
49 buffer => $self->json->encode({
50 event => 'pusher:ping',
51 data => { }
52 }),
53 masked => 1
54 );
55 0 ▶ if(my $timer = $self->{inactivity_timer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timer = $self->{"inactivity_timer"}) 0 0 0
56 0 ▶ $timer->stop if $timer->is_running;
Branch: 1 branch
BranchTrueFalseTotal
if $timer->is_running 0 0 0
57 0 $timer->reset;
58 0 $timer->start;
59 }
60 $self
61 0 }
62  
63 =head2 incoming_frame
64  
65 Deals with incoming frames.
66  
67 =cut
68  
69 sub incoming_frame {
70 0 ▶ my $self = shift;
Subroutine incoming_frame: not called CC=7 SCAR=40.3
Pod documented
71 0 my ($client, $frame) = @_;
72  
73 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
74  
75 try {
76 0 $log->tracef("Frame [%s]", $frame);
77 0 $self->{last_seen} = time;
78 0 my $info = $self->json->decode($frame);
79 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
80 0 return $self->{channel}{$info->{channel}}->incoming_message($info);
81 } elsif($info->{event} eq 'pusher:connection_established') {
82 0 my $data = $self->json->decode($info->{data});
83 0 $self->{socket_id} = $data->{socket_id};
84 0 $log->tracef('Setting inactivity timeout to %d seconds', $data->{activity_timeout});
85 $self->add_child(
86 $self->{inactivity_timer} = IO::Async::Timer::Countdown->new(
87 delay => $data->{activity_timeout},
88 0 on_expire => $self->curry::weak::send_ping,
89 )
90 );
91 0 $self->{inactivity_timer}->start;
92 0 return $self->connected->done;
93 } elsif($info->{event} eq 'pusher:ping') {
94 0 return $self->client->send_frame(
95 buffer => $self->json->encode({
96 event => 'pusher:pong',
97 data => { }
98 }),
99 masked => 1
100 );
101 } elsif($info->{event} eq 'pusher:pong') {
102 0 return $log->trace("Pong event received from pusher");
103 }
104 0 die "unhandled"
105 0 } catch {
106 0 my $err = $@;
107 0 $log->errorf("Unexpected frame (%s) [%s]", $err, $frame);
108 0 $self->bus->invoke_event(
109 error => $err, $frame
110 );
111 }
112 }
113  
114 0 ▶sub socket_id { shift->{socket_id} }
Subroutine socket_id: not called CC=1 SCAR=6.9
Pod undocumented
115  
116 =head2 client
117  
118 Returns the L<Net::Async::WebSocket::Client> instance.
119  
120 =cut
121  
122 0 ▶sub client { shift->{client} }
Subroutine client: not called CC=1 SCAR=6.9
Pod documented
123  
124 =head2 open_channel
125  
126 Opens a channel.
127  
128 my $ch = $conn->open_channel(
129 'xyz'
130 )->get;
131  
132 Resolves to a L<Net::Async::Pusher::Channel> instance.
133  
134 =cut
135  
136 sub open_channel {
137 0 ▶ my ($self, $name, %args) = @_;
Subroutine open_channel: not called CC=1 SCAR=6.9
Pod documented
138 $self->connected->then(sub {
139 0 ▶ $log->tracef("Subscribing to [%s]", $name);
Subroutine __ANON__: not called CC=2 SCAR=17.9
140 0 my $ch = $self->{channel}{$name} = Net::Async::Pusher::Channel->new(
141 name => $name,
142 );
143 0 $self->add_child($ch);
144 my $frame = $self->json->encode({
145 event => 'pusher:subscribe',
146 # double-encoded
147 data => {
148 0 ▶ (exists $args{auth} ? (auth => $args{auth}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'auth'} ? : 0 0 0
149 channel => $name
150 }
151 });
152 0 $log->tracef("Subscribing: %s", $frame);
153 0 $self->client->send_frame(
154 buffer => $frame,
155 masked => 1,
156 );
157 # We map the channel ourselves so that we don't end up with
158 # the channel's ->subscribed method holding a strong reference
159 # to itself
160 $self->{channel}{$name}->subscribed->transform(
161 0 ▶ done => sub { $ch }
Subroutine __ANON__: not called CC=1 SCAR=6.9
162 )
163 0 });
164 }
165  
166 =head2 connect
167  
168 (Re)connects to the feed.
169  
170 =cut
171  
172 sub connect {
173 0 ▶ my ($self) = @_;
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
174 $self->add_child(
175 0 $self->{client} = Net::Async::WebSocket::Client->new(
176 on_frame => $self->curry::weak::incoming_frame,
177 )
178 );
179 retain_future(
180 $self->client->connect(
181 # all lovely hardcoded magic here
182 host => 'ws.pusherapp.com',
183 service => 443,
184 url => 'wss://ws.pusherapp.com/app/' . $self->key . '?protocol=7&client=perl-net-async-pusher&version=' . ($self->VERSION // '0.001'),
185 extensions => [ qw(SSL) ],
186 )->then(sub {
187 # don't seem to get any response until we send something first
188 0 ▶ $self->send_ping;
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 Future->done($self)
190 })->on_fail(sub {
191 0 ▶ $log->errorf('Failed to connect - %s', join ',', @_)
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 })
193 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
194 }
195  
196 =head2 connected
197  
198 L<Future> representing current connection state.
199  
200 =cut
201  
202 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
203  
204 sub configure {
205 0 ▶ my ($self, %args) = @_;
Subroutine configure: not called CC=2 SCAR=17.9
Pod documented
206 0 for(grep exists $args{$_}, qw(key)) {
207 0 $self->{$_} = delete $args{$_};
208 }
209 0 $self->SUPER::configure(%args);
210 }
211  
212 =head2 key
213  
214 The key.
215  
216 =cut
217  
218 0 ▶sub key { shift->{key} }
Subroutine key: not called CC=1 SCAR=6.9
Pod documented
219  
220 2 1;
221  
222 __END__
↑ Summary Pusher/Channel.pm »