lib/PAGI/App/WebSocket/Broadcast.pm

filter: Statementstmt 36.7% 25 / 68 Branchbran 4.1% 1 / 24 Conditioncond 21.4% 3 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 75.0% 6 / 8 Podpod 50.0% 2 / 4 totaltotal 29.3% 37 / 126 CC 22 SCAR 42.7
CC
22
Cov
27%
__ANON__
156.0
broadcast
30.0
client_count
8.1
CRAP 71.6
« blib/lib/PAGI/App/Proxy.pm ↑ Summary blib/lib/PAGI/App/WrapCGI.pm »
1 package PAGI::App::WebSocket::Broadcast;
2 1 $PAGI::App::WebSocket::Broadcast::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 PAGI::App::WebSocket::Broadcast - Pub/sub WebSocket broadcast
10  
11 =head1 SYNOPSIS
12  
13 use PAGI::App::WebSocket::Broadcast;
14  
15 my $app = PAGI::App::WebSocket::Broadcast->new->to_app;
16  
17 =cut
18  
19 # Shared state for all connections
20 1 my %channels; # channel => { clients => { id => send_cb } }
21 1 my $next_id = 1;
22  
23 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod undocumented
24 1 my ($class, %args) = @_;
25  
26 return bless {
27 default_channel => $args{channel} // 'default',
28 1 ▶ echo_self => $args{echo_self} // 0,
Condition50%2/4
$args{"channel"} // "default"
l!l
01
$args{"echo_self"} // 0
l!l
01
MC/DC0%0/1
$args{"channel"} // "default"
$args{"channel"}
MC/DC0%0/1
$args{"echo_self"} // 0
$args{"echo_self"}
Truth table50%1/2
A
A= $args{"channel"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"echo_self"}
Aresult
11
00
29 }, $class;
30 }
31  
32 1 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=5.8
Pod undocumented
33 1 my ($self) = @_;
34  
35 1 my $default_channel = $self->{default_channel};
36 1 my $echo_self = $self->{echo_self};
37  
38 0 ▶ return async sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
39 0 my ($scope, $receive, $send) = @_;
40 0 ▶ die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'websocket';
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"type"} ne "websocket" 0 0 0
41  
42 # Accept the connection
43 0 await $send->({ type => 'websocket.accept' });
44  
45 0 my $client_id = $next_id++;
46 0 my $channel = $default_channel;
47  
48 # Register client
49 0 ▶ $channels{$channel} //= { clients => {} };
Condition0%0/2
$channels{$channel} //= {"clients", {}}
l!l
00
MC/DC0%0/1
$channels{$channel} //= {"clients", {}}
$channels{$channel}
Truth table0%0/2
A
A= $channels{$channel}
Aresult
11
00
50 0 $channels{$channel}{clients}{$client_id} = $send;
51  
52 0 eval {
53 0 while (1) {
54 0 my $event = await $receive->();
55  
56 0 ▶ if ($event->{type} eq 'websocket.receive') {
Branch: 2 branches
BranchTrueFalseTotal
if ($event->{'type'} eq 'websocket.receive') { } 0 0 0
elsif ($event->{'type'} eq 'websocket.disconnect') { } 0 0 0
57 0 ▶ my $message = $event->{text} // $event->{bytes};
Condition0%0/3
$event->{"text"} // $event->{"bytes"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$event->{"text"} // $event->{"bytes"}
$event->{"text"} $event->{"bytes"}
Truth table0%0/3
A // B
A= $event->{"text"}B= $event->{"bytes"}
ABresult
1X1
011
000
58 0 my $is_text = exists $event->{text};
59  
60 # Broadcast to all clients in the channel
61 0 my $clients = $channels{$channel}{clients};
62 0 for my $id (keys %$clients) {
63 0 ▶ next if $id eq $client_id && !$echo_self;
Branch: 1 branch
BranchTrueFalseTotal
if $id eq $client_id and not $echo_self 0 0 0
Condition0%0/3
$id eq $client_id and not $echo_self
!ll&&!rl&&r
000
MC/DC0%0/2
$id eq $client_id and not $echo_self
$id eq $client_id not $echo_self
Truth table0%0/3
A and not(B)
A= $id eq $client_idB= not $echo_self
ABresult
0X0
100
111
64 0 my $client_send = $clients->{$id};
65 0 eval {
66 0 ▶ if ($is_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_text) { } 0 0 0
67 0 await $client_send->({
68 type => 'websocket.send',
69 text => $message,
70 });
71 } else {
72 0 await $client_send->({
73 type => 'websocket.send',
74 bytes => $message,
75 });
76 }
77 };
78 # Remove dead clients
79 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
80 0 delete $clients->{$id};
81 }
82 }
83 } elsif ($event->{type} eq 'websocket.disconnect') {
84 0 last;
85 }
86 }
87 };
88  
89 # Cleanup
90 0 delete $channels{$channel}{clients}{$client_id};
91 0 ▶ delete $channels{$channel} if !keys %{$channels{$channel}{clients}};
Branch: 1 branch
BranchTrueFalseTotal
if not keys %{$channels{$channel}{"clients"};} 0 0 0
92 1 };
93 }
94  
95 # Class method to broadcast to a channel
96 0 ▶sub broadcast {
Subroutine broadcast: not called CC=5 SCAR=34.0
Pod documented
97 0 my ($class, $channel, $message, %opts) = @_;
98  
99 0 ▶ return unless $channels{$channel};
Branch: 1 branch
BranchTrueFalseTotal
unless $channels{$channel} 0 0 0
100  
101 0 my $is_text = !$opts{binary};
102 0 my $clients = $channels{$channel}{clients};
103  
104 0 for my $id (keys %$clients) {
105 0 my $send = $clients->{$id};
106 0 eval {
107 0 ▶ if ($is_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_text) { } 0 0 0
108 0 $send->({ type => 'websocket.send', text => $message });
109 } else {
110 0 $send->({ type => 'websocket.send', bytes => $message });
111 }
112 };
113 0 ▶ delete $clients->{$id} if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
114 }
115 }
116  
117 # Get connected client count
118 1 ▶sub client_count {
Subroutine client_count: called CC=5 SCAR=20.9
Pod documented
119 1 my ($class, $channel) = @_;
120 1 ▶ $channel //= undef;
Condition50%1/2
$channel //= undef
l!l
01
MC/DC0%0/1
$channel //= undef
$channel
Truth table50%1/2
A
A= $channel
Aresult
11
00
121  
122 1 ▶ if ($channel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($channel) 0 1 1
123 0 ▶ return 0 unless $channels{$channel};
Branch: 1 branch
BranchTrueFalseTotal
unless $channels{$channel} 0 0 0
124 0 return scalar keys %{$channels{$channel}{clients}};
125 }
126 1 my $total = 0;
127 0 $total += scalar keys %{$_->{clients}} for values %channels;
128 1 return $total;
129 }
130  
131 1 1;
132  
133 __END__
« blib/lib/PAGI/App/Proxy.pm ↑ Summary blib/lib/PAGI/App/WrapCGI.pm »