BoardStreams/Client.pm

filter: Statementstmt 100% 162 / 162 Branchbran 90.3% 56 / 62 Conditioncond 68.5% 37 / 54 MC/DCmcdc 46.6% 14 / 30 Subroutinesub 100% 34 / 34 Podpod 100% 3 / 3 totaltotal 88.7% 306 / 345 CC 55 SCAR 18.5
CC
55
Cov
92%
__ANON__
16.3
_lost
11.0
_rest
6.0
CRAP 6.4
« BoardStreams/Context.pm ↑ Summary BoardStreams/Update.pm »
1 2 ▶use v5.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use experimental 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # A connection to a BoardStreams server, kept alive by reconnecting: the
5 # Perl counterpart of the JavaScript client, ported from it and meant to
6 # behave identically. See the POD at the end of this file.
7 2 class BoardStreams::Client {
8 2 our $VERSION = "v0.0.59";
9  
10 2 ▶ use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶ use RxPerl::Mojo qw(rx_behavior_subject rx_interval rx_observable rx_timer);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶ use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶ use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶ use BoardStreams ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ use BoardStreams::Client::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use BoardStreams::Client::Membership;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use BoardStreams::Client::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 field $url :param;
21 2 field $min_delay :param = 0.5; # seconds before the first reconnection attempt; doubles per attempt
22 2 field $max_delay :param = 30; # upper limit for that delay, in seconds
23 2 field $idle_delay :param = 30; # seconds the connection is kept with no channel joined; undef for ever
24 2 field $ua :param = undef; # a Mojo::UserAgent to connect with
25  
26 2 field $status_subject = rx_behavior_subject->new('connecting');
27 2 field %channels; # memberships by channel name
28 2 field $socket; # the current connection attempt: { tx => ... } once established
29 2 field $open = 0; # true once the server has greeted with the same protocol version
30 2 field $attempt = 0;
31 2 field $timer; # subscription of the pending reconnection
32 2 field $watchdog; # subscription of the periodic silence check
33 2 field $silence_limit = 0;
34 2 field $last_seen = 0;
35 2 field $fatal;
36 2 field $idle_timer; # subscription of the wait before going idle
37 2 field $idle = 0; # true while the connection is closed for lack of channels
38  
39 19 ▶ ADJUST {
Subroutine __ANON__: called CC=2 SCAR=7.6
40 # The server's heartbeats and this client's own check decide when a
41 # connection is dead, whatever the heartbeat interval is.
42 19 ▶ $ua //= Mojo::UserAgent->new->inactivity_timeout(0);
Condition33%1/3
$ua //= "Mojo::UserAgent"->new->inactivity_timeout(0)
l!l&&r!l&&!r
0190
MC/DC0%0/2
$ua //= "Mojo::UserAgent"->new->inactivity_timeout(0)
$ua "Mojo::UserAgent"->new->inactivity_timeout(0)
Truth table33%1/3
A //= B
A= $uaB= "Mojo::UserAgent"->new->inactivity_timeout(0)
ABresult
1X1
011
000
43 19 $self->_connect;
44 19 $self->_rest;
45 }
46  
47 # The state of the connection: 'connecting', 'connected',
48 # 'disconnected', 'idle' or 'closed'. Replays the current value.
49 9 ▶ method status_o {
Subroutine status_o: called CC=1 SCAR=0.0
Pod documented
50 9 ▶ return rx_observable->new(sub ($subscriber) {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 9 $status_subject->subscribe($subscriber);
52 9 return;
53 9 });
54 2 }
55  
56 # Joins a channel. Every call returns a channel object of its own, and
57 # the ones for the same name share one membership on the server, which
58 # lasts until each of them has left.
59 32 ▶ method join ($name) {
Subroutine join: called CC=6 SCAR=17.9
Pod documented
60 32 ▶ $idle_timer->unsubscribe if $idle_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $idle_timer 20 12 32
61 32 undef $idle_timer;
62 32 ▶ return $channels{$name}->open if $channels{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $channels{$name} 4 28 32
63 my $membership = $channels{$name} = BoardStreams::Client::Membership->new(
64 name => $name,
65 link => {
66 60 ▶ send => sub ($message) { $self->_send($message) },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 27 ▶ forget => sub { delete $channels{$name}; $self->_rest },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 1 ▶ reconnect => sub { $self->_lost($socket) },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 },
70 28 );
71 28 my $channel = $membership->open;
72 28 ▶ if ($fatal) { $membership->receive({ type => 'rejected', reason => $fatal->reason, data => $fatal->data }) }
Branch: 3 branches
BranchTrueFalseTotal
if ($fatal) { } 3 25 28
elsif ($idle) { } 1 24 25
elsif ($open) { } 6 18 24
73 1 elsif ($idle) { $self->_wake }
74 6 elsif ($open) { $membership->connected }
75 28 return $channel;
76 2 }
77  
78 # Closes the connection for good and leaves all channels.
79 16 ▶ method close {
Subroutine close: called CC=3 SCAR=11.1
Pod documented
80 16 ▶ return if $status_subject->get_value eq 'closed';
Branch: 1 branch
BranchTrueFalseTotal
if $status_subject->get_value eq "closed" 0 16 16
81 16 $_->leave for values %channels;
82 16 $self->_shutdown;
83 16 return;
84 2 }
85  
86 33 ▶ method _connect {
Subroutine _connect: called CC=1 SCAR=0.1
87 33 my $mine = $socket = {};
88 33 ▶ $ua->websocket($url => sub ($ua, $tx) {
Subroutine __ANON__: called CC=5 SCAR=16.4
89 33 ▶ my $current = $socket && refaddr $socket == refaddr $mine;
Condition33%1/3
$socket && builtin::refaddr($socket) == builtin::refaddr($mine)
!ll&&!rl&&r
0033
MC/DC0%0/2
$socket && builtin::refaddr($socket) == builtin::refaddr($mine)
$socket builtin::refaddr($socket) == builtin::refaddr($mine)
Truth table33%1/3
A && B
A= $socketB= builtin::refaddr($socket) == builtin::refaddr($mine)
ABresult
0X0
100
111
90 33 ▶ unless ($tx->is_websocket) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx->is_websocket) 3 30 33
91 3 ▶ $self->_lost($mine) if $current;
Branch: 1 branch
BranchTrueFalseTotal
if $current 3 0 3
92 3 return;
93 }
94 30 ▶ return $tx->finish unless $current; # given up on while it was connecting
Branch: 1 branch
BranchTrueFalseTotal
unless $current 0 30 30
95 30 $mine->{tx} = $tx;
96  
97 # Nothing is sent when the socket opens: the connection counts
98 # as established only once the server's first heartbeat has
99 # shown that it speaks the same protocol version as this client.
100 62 ▶ $tx->on(json => sub ($tx, $message) {
Subroutine __ANON__: called CC=15 SCAR=27.9
101 62 ▶ return unless $socket and refaddr $socket == refaddr $mine and ref $message eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $socket and builtin::refaddr($socket) == builtin::refaddr($mine) and ref $message eq "HASH" 0 62 62
Condition33%2/6
$socket and builtin::refaddr($socket) == builtin::refaddr($mine) and ref $message eq "HASH"
!ll&&!rl&&r
0062
$socket and builtin::refaddr($socket) == builtin::refaddr($mine)
!ll&&!rl&&r
0062
MC/DC0%0/3
$socket and builtin::refaddr($socket) == builtin::refaddr($mine) and ref $message eq "HASH"
$socket builtin::refaddr($socket) == builtin::refaddr($mine) ref $message eq "HASH"
Truth table25%1/4
A and B and C
A= $socketB= builtin::refaddr($socket) == builtin::refaddr($mine)C= ref $message eq "HASH"
ABCresult
0XX0
10X0
1100
1111
102 62 $attempt = 0;
103 62 $last_seen = time;
104 62 ▶ my $type = $message->{type} // '';
Condition50%1/2
$message->{"type"} // ""
l!l
620
MC/DC0%0/1
$message->{"type"} // ""
$message->{"type"}
Truth table50%1/2
A
A= $message->{"type"}
Aresult
11
00
105 62 ▶ unless ($open) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($open) 23 39 62
106 23 ▶ return unless $type eq 'heartbeat';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "heartbeat" 0 23 23
107 return $self->_protocol_mismatch($message->{protocol})
108 23 ▶ unless ($message->{protocol} // '') eq BoardStreams::PROTOCOL_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
unless ($message->{"protocol"} // "") eq 2 2 21 23
Condition100%2/2
$message->{"protocol"} // ""
l!l
221
MC/DC100%1/1
$message->{"protocol"} // ""
$message->{"protocol"}
Truth table100%2/2
A
A= $message->{"protocol"}
Aresult
11
00
109 21 $open = 1;
110 21 $status_subject->next('connected');
111 21 $_->connected for values %channels;
112 }
113 60 ▶ return $self->_watch($message->{interval}) if $type eq 'heartbeat';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "heartbeat" 22 38 60
114 # The server is closing this connection on purpose; coming
115 # back uninvited would defeat that.
116 return $self->_fail(BoardStreams::Client::Error->new(
117 38 ▶ reason => $message->{reason}, data => $message->{data})) if $type eq 'disconnect';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "disconnect" 1 37 38
118 my ($channel) = defined $message->{channel} && !ref $message->{channel}
119 31 ? grep { defined } $channels{ $message->{channel} }
120 37 ▶ : grep { $_->owns($message->{id}) } values %channels;
Branch: 1 branch
BranchTrueFalseTotal
defined $message->{'channel'} && !ref($message->{'channel'}) ? : 31 6 37
Condition66%2/3
defined $message->{"channel"} and not ref $message->{"channel"}
!ll&&!rl&&r
6031
MC/DC50%1/2
defined $message->{"channel"} and not ref $message->{"channel"}
defined $message->{"channel"} not ref $message->{"channel"}
Truth table66%2/3
A and not(B)
A= defined $message->{"channel"}B= not ref $message->{"channel"}
ABresult
0X0
100
111
121 37 ▶ $channel->receive($message) if $channel;
Branch: 1 branch
BranchTrueFalseTotal
if $channel 35 2 37
122 30 });
123 30 ▶ $tx->on(finish => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 30 $mine->{finished} = 1;
125 30 $self->_lost($mine);
126 30 });
127 33 });
128 2 }
129  
130 # The server speaks another version of the protocol; reconnecting would
131 # not help.
132 2 ▶ method _protocol_mismatch ($server_protocol) {
Subroutine _protocol_mismatch: called CC=2 SCAR=6.9
133 2 ▶ warn sprintf "BoardStreams: this client speaks protocol version %s but the server speaks %s; "
Condition100%2/2
$server_protocol // "an unknown (older) one"
l!l
11
MC/DC100%1/1
$server_protocol // "an unknown (older) one"
$server_protocol
Truth table100%2/2
A
A= $server_protocol
Aresult
11
00
134 . "the two must be the same, so no connection is made.\n",
135 BoardStreams::PROTOCOL_VERSION, $server_protocol // 'an unknown (older) one';
136 2 $self->_fail(BoardStreams::Client::Error->new(
137 reason => 'protocol_mismatch',
138 data => { client => BoardStreams::PROTOCOL_VERSION, server => $server_protocol }));
139 2 }
140  
141 # Stops for good, without reconnecting. Every channel, current or
142 # future, is refused with the given error.
143 3 ▶ method _fail ($error) {
Subroutine _fail: called CC=2 SCAR=6.9
144 3 $fatal = $error;
145 3 $_->receive({ type => 'rejected', reason => $error->reason, data => $error->data }) for values %channels;
146 3 $self->_shutdown;
147 2 }
148  
149 19 ▶ method _shutdown {
Subroutine _shutdown: called CC=3 SCAR=11.0
150 19 $status_subject->next('closed');
151 19 ▶ $timer->unsubscribe if $timer;
Branch: 1 branch
BranchTrueFalseTotal
if $timer 6 13 19
152 19 undef $timer;
153 19 ▶ $idle_timer->unsubscribe if $idle_timer;
Branch: 1 branch
BranchTrueFalseTotal
if $idle_timer 16 3 19
154 19 undef $idle_timer;
155 19 $idle = 0;
156 19 $self->_lost($socket);
157 19 $status_subject->complete;
158 2 }
159  
160 # The server says how often it sends heartbeats; from then on, silence
161 # for two and a half intervals means the connection is dead even if
162 # the socket has not noticed.
163 22 ▶ method _watch ($interval) {
Subroutine _watch: called CC=4 SCAR=14.0
164 22 my $limit = $interval * 2.5;
165 22 ▶ return if $watchdog and $limit == $silence_limit;
Branch: 1 branch
BranchTrueFalseTotal
if $watchdog and $limit == $silence_limit 1 21 22
Condition66%2/3
$watchdog and $limit == $silence_limit
!ll&&!rl&&r
2101
MC/DC50%1/2
$watchdog and $limit == $silence_limit
$watchdog $limit == $silence_limit
Truth table66%2/3
A and B
A= $watchdogB= $limit == $silence_limit
ABresult
0X0
100
111
166 21 $silence_limit = $limit;
167 21 ▶ $watchdog->unsubscribe if $watchdog;
Branch: 1 branch
BranchTrueFalseTotal
if $watchdog 0 21 21
168 14 ▶ $watchdog = rx_interval($interval / 2)->subscribe(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
169 14 ▶ $self->_lost($socket) if $watchdog and time - $last_seen > $silence_limit;
Branch: 1 branch
BranchTrueFalseTotal
if $watchdog and &BoardStreams::Client::time() - $last_seen > $silence_limit 1 13 14
Condition66%2/3
$watchdog and &BoardStreams::Client::time() - $last_seen > $silence_limit
!ll&&!rl&&r
0131
MC/DC50%1/2
$watchdog and &BoardStreams::Client::time() - $last_seen > $silence_limit
$watchdog &BoardStreams::Client::time() - $last_seen > $silence_limit
Truth table66%2/3
A and B
A= $watchdogB= &BoardStreams::Client::time() - $last_seen > $silence_limit
ABresult
0X0
100
111
170 21 });
171 2 }
172  
173 # Gives up on a connection, whether it closed or just went silent, and
174 # schedules a new one. A silent connection is abandoned without
175 # waiting for it to close, which may take very long.
176 57 ▶ method _lost ($lost) {
Subroutine _lost: called CC=11 SCAR=24.0
177 57 ▶ return unless $lost and $socket and refaddr $socket == refaddr $lost;
Branch: 1 branch
BranchTrueFalseTotal
unless $lost and $socket and builtin::refaddr($socket) == builtin::refaddr($lost) 24 33 57
Condition83%5/6
$lost and $socket and builtin::refaddr($socket) == builtin::refaddr($lost)
!ll&&!rl&&r
24033
$lost and $socket
!ll&&!rl&&r
42033
MC/DC66%2/3
$lost and $socket and builtin::refaddr($socket) == builtin::refaddr($lost)
$lost $socket builtin::refaddr($socket) == builtin::refaddr($lost)
Truth table75%3/4
A and B and C
A= $lostB= $socketC= builtin::refaddr($socket) == builtin::refaddr($lost)
ABCresult
0XX0
10X0
1100
1111
178 33 $open = 0;
179 33 undef $socket;
180 33 ▶ $watchdog->unsubscribe if $watchdog;
Branch: 1 branch
BranchTrueFalseTotal
if $watchdog 21 12 33
181 33 undef $watchdog;
182 33 ▶ $lost->{tx}->finish if $lost->{tx} and !$lost->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if $lost->{"tx"} and not $lost->{"finished"} 20 13 33
Condition100%3/3
$lost->{"tx"} and not $lost->{"finished"}
!ll&&!rl&&r
31020
MC/DC100%2/2
$lost->{"tx"} and not $lost->{"finished"}
$lost->{"tx"} not $lost->{"finished"}
Truth table100%3/3
A and not(B)
A= $lost->{"tx"}B= not $lost->{"finished"}
ABresult
0X0
100
111
183 33 $_->disconnected for values %channels;
184 33 ▶ return if $idle or $status_subject->get_value eq 'closed';
Branch: 1 branch
BranchTrueFalseTotal
if $idle or $status_subject->get_value eq "closed" 18 15 33
Condition100%3/3
$idle or $status_subject->get_value eq "closed"
l!l&&r!l&&!r
21615
MC/DC100%2/2
$idle or $status_subject->get_value eq "closed"
$idle $status_subject->get_value eq "closed"
Truth table100%3/3
A or B
A= $idleB= $status_subject->get_value eq "closed"
ABresult
1X1
011
000
185 15 $status_subject->next('disconnected');
186 15 my $nominal = $min_delay * 2**$attempt++;
187 15 ▶ $nominal = $max_delay if $nominal > $max_delay;
Branch: 1 branch
BranchTrueFalseTotal
if $nominal > $max_delay 2 13 15
188 13 ▶ $timer = rx_timer($nominal * $self->_jitter)->subscribe(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
189 13 $status_subject->next('connecting');
190 13 $self->_connect;
191 15 });
192 2 }
193  
194 # Between half and all of the nominal delay, at random, so that the
195 # clients a server lets go of at once do not all come back at once.
196 15 ▶ method _jitter { 0.5 + rand() / 2 }
Subroutine _jitter: called CC=1 SCAR=0.0
197  
198 # With no channel joined there is nothing to receive, so after a while
199 # the connection is closed, until the next join opens one again.
200 46 ▶ method _rest {
Subroutine _rest: called CC=6 SCAR=18.0
201 46 ▶ return if !defined $idle_delay or %channels or $idle or $idle_timer;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $idle_delay or %channels or $idle or $idle_timer 4 42 46
Condition77%7/9
not defined $idle_delay or %channels or $idle or $idle_timer
l!l&&r!l&&!r
4042
not defined $idle_delay or %channels or $idle
l!l&&r!l&&!r
4042
not defined $idle_delay or %channels
l!l&&r!l&&!r
1342
MC/DC50%2/4
not defined $idle_delay or %channels or $idle or $idle_timer
not defined $idle_delay %channels $idle $idle_timer
Truth table60%3/5
not(A) or B or C or D
A= not defined $idle_delayB= %channelsC= $idleD= $idle_timer
ABCDresult
1XXX1
01XX1
001X1
00011
00000
202 42 ▶ return if $status_subject->get_value eq 'closed';
Branch: 1 branch
BranchTrueFalseTotal
if $status_subject->get_value eq "closed" 3 39 42
203 3 ▶ $idle_timer = rx_timer($idle_delay)->subscribe(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
204 3 undef $idle_timer;
205 3 $idle = 1;
206 3 ▶ $timer->unsubscribe if $timer;
Branch: 1 branch
BranchTrueFalseTotal
if $timer 1 2 3
207 3 undef $timer;
208 3 $self->_lost($socket);
209 3 $status_subject->next('idle');
210 39 });
211 2 }
212  
213 1 ▶ method _wake {
Subroutine _wake: called CC=1 SCAR=0.0
214 1 $idle = 0;
215 1 $attempt = 0;
216 1 $status_subject->next('connecting');
217 1 $self->_connect;
218 2 }
219  
220 60 ▶ method _send ($message) {
Subroutine _send: called CC=4 SCAR=14.3
221 60 ▶ $socket->{tx}->send({ json => $message }) if $open and $socket and $socket->{tx};
Branch: 1 branch
BranchTrueFalseTotal
if $open and $socket and $socket->{"tx"} 59 1 60
Condition66%4/6
$open and $socket and $socket->{"tx"}
!ll&&!rl&&r
1059
$open and $socket
!ll&&!rl&&r
1059
MC/DC33%1/3
$open and $socket and $socket->{"tx"}
$open $socket $socket->{"tx"}
Truth table50%2/4
A and B and C
A= $openB= $socketC= $socket->{"tx"}
ABCresult
0XX0
10X0
1100
1111
222 2 }
223 }
224  
225 2 1;
226 __END__
« BoardStreams/Context.pm ↑ Summary BoardStreams/Update.pm »