| 1 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package Mojolicious::Plugin::Pubsub; | ||||||||||||||||||||||||||||
| 4 | #ABSTRACT: Pubsub plugin for Mojolicious | ||||||||||||||||||||||||||||
| 5 | 6 | $Mojolicious::Plugin::Pubsub::VERSION = '0.006'; | |||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Mojo::JSON qw( decode_json encode_json ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Mojo::Util qw( b64_decode b64_encode deprecated ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use IO::Socket::UNIX; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6 | my $client; | |||||||||||||||||||||||||||
| 14 | my $conf; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=11 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 6 | my ($self, $app, $cfg) = @_; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 6 | $cfg->{cbs} = []; | |||||||||||||||||||||||||||
| 20 | 6 | ▶ | push @{ $cfg->{subs} }, $cfg->{cb} if exists $cfg->{cb}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 6 | ▶ | $cfg->{socket} = $app->home->child($app->moniker . '.pubsub') unless exists $cfg->{socket}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 6 | $conf = $cfg; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 6 | my $loop = Mojo::IOLoop->singleton; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 6 | ▶ | pipe my $in, my $out or die "Could not open pipe pair: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 6 | my $pid = fork(); | |||||||||||||||||||||||||||
| 29 | 6 | ▶ | die "Could not fork: $!" if not defined $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 6 | ▶ | if ($pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 3 | close $out; | |||||||||||||||||||||||||||
| 33 | 3 | chomp(my $result = readline $in); | |||||||||||||||||||||||||||
| 34 | 3 | close $in; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | die "Could not establish pubsub socket: $result" if $result ne 'DONE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | } else { | ||||||||||||||||||||||||||||
| 38 | # now in fork | ||||||||||||||||||||||||||||
| 39 | 3 | close $in; | |||||||||||||||||||||||||||
| 40 | 3 | $loop->reset; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | my @streams; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | ▶ | unless (-e $conf->{socket} and IO::Socket::UNIX->new(Peer => $conf->{socket})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-e $conf->{"socket"} and "IO::Socket::UNIX"->new("Peer", $conf->{"socket"})
MC/DC0%0/2
-e $conf->{"socket"} and "IO::Socket::UNIX"->new("Peer", $conf->{"socket"})
-e $conf->{"socket"}
"IO::Socket::UNIX"->new("Peer", $conf->{"socket"})
Truth table33%1/3
A and B
A= -e $conf->{"socket"}B= "IO::Socket::UNIX"->new("Peer", $conf->{"socket"})
| |||||||||||||||||||||||||||||
| 45 | 3 | my $server = eval { $loop->server( | |||||||||||||||||||||||||||
| 46 | 6 | ▶ | {path => $conf->{socket}} => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 6 | my (undef, $stream) = @_; | |||||||||||||||||||||||||||
| 48 | 6 | push @streams, $stream; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 6 | my $msg; | |||||||||||||||||||||||||||
| 51 | $stream->on( | ||||||||||||||||||||||||||||
| 52 | 4 | ▶ | read => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 53 | 4 | my ($stream, $bytes) = @_; | |||||||||||||||||||||||||||
| 54 | 4 | $msg .= $bytes; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | while (length $msg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 4 | ▶ | if ($msg =~ s/^(.+\n)//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 4 | my $line = $1; | |||||||||||||||||||||||||||
| 59 | 4 | foreach my $str (@streams) { $str->write($line); } | |||||||||||||||||||||||||||
| 60 | } else { | ||||||||||||||||||||||||||||
| 61 | 0 | return; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 6 | ); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | $stream->on( | ||||||||||||||||||||||||||||
| 68 | 6 | ▶ | close => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 69 | 6 | @streams = grep $_ ne $_[0], @streams; | |||||||||||||||||||||||||||
| 70 | 6 | ▶ | $loop->stop unless @streams; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 6 | ); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 6 | $stream->timeout(0); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 3 | ); }; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 3 | ▶ | if (not defined $server) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | print $out $@; | |||||||||||||||||||||||||||
| 80 | 0 | close $out; | |||||||||||||||||||||||||||
| 81 | 0 | exit; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | print $out "DONE\n"; | |||||||||||||||||||||||||||
| 86 | 3 | close $out; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | $loop->start unless $loop->is_running; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 3 | exit; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | $loop->next_tick(sub { _connect() }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | $app->helper( | ||||||||||||||||||||||||||||
| 95 | 4 | ▶ | 'pubsub.publish' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 97 | 4 | my $msg = b64_encode(encode_json([@_]), ""); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | _send($msg . "\n"); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 4 | return $self; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 3 | ); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | $app->helper( | ||||||||||||||||||||||||||||
| 106 | 1 | ▶ | 'pubsub.subscribe' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | 1 | my $cb = shift; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | push @{ $conf->{subs} }, $cb; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | return $self; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 3 | ); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | $app->helper( | ||||||||||||||||||||||||||||
| 117 | 1 | ▶ | 'pubsub.unsubscribe' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 118 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | 1 | my $cb = shift; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | @{ $conf->{subs} } = grep { $_ != $cb } @{ $conf->{subs} }; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | return $self; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 3 | ); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | $app->helper( | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | publish => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 129 | 0 | deprecated '->publish is deprecated in favour of ->pubsub->publish'; | |||||||||||||||||||||||||||
| 130 | 0 | shift->pubsub->publish(@_); | |||||||||||||||||||||||||||
| 131 | 3 | }); | |||||||||||||||||||||||||||
| 132 | $app->helper( | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | subscribe => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 134 | 0 | deprecated '->subscribe is deprecated in favour of ->pubsub->subscribe'; | |||||||||||||||||||||||||||
| 135 | 0 | shift->pubsub->subscribe(@_); | |||||||||||||||||||||||||||
| 136 | 3 | }); | |||||||||||||||||||||||||||
| 137 | $app->helper( | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | unsubscribe => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 139 | 0 | deprecated '->unsubscribe is deprecated in favour of ->pubsub->unsubscribe'; | |||||||||||||||||||||||||||
| 140 | 0 | shift->pubsub->unsubscribe(@_); | |||||||||||||||||||||||||||
| 141 | 3 | }); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 4 | ▶ | sub _send { | ||||||||||||||||||||||||||
|
Subroutine
_send: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 146 | 4 | my ($msg) = @_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 4 | ▶ | if (not defined $client) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 3 | ▶ | return _connect(sub { $_[0]->write($msg); }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 1 | $client->write($msg); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 6 | ▶ | sub _connect { | ||||||||||||||||||||||||||
|
Subroutine
_connect: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 6 | my $cb = shift; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Mojo::IOLoop->singleton->client( | ||||||||||||||||||||||||||||
| 160 | 6 | ▶ | { path => $conf->{socket} } => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 161 | 6 | my ($loop, $err, $stream) = @_; | |||||||||||||||||||||||||||
| 162 | 6 | ▶ | die sprintf "Could not connect to %s: %s", $conf->{socket}, $err if defined $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 6 | ▶ | if (defined $client) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 3 | $stream->close(); | |||||||||||||||||||||||||||
| 166 | 3 | ▶ | $cb->($client) if defined $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 3 | return; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 3 | $client = $stream; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 3 | my $msg; | |||||||||||||||||||||||||||
| 174 | 4 | ▶ | $stream->on(read => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 175 | 4 | my ($stream, $bytes) = @_; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 4 | $msg .= $bytes; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 4 | ▶ | while (length $msg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 4 | ▶ | if ($msg =~ s/^(.+)\n//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 4 | my $b64 = $1; | |||||||||||||||||||||||||||
| 182 | 4 | my $args = decode_json(b64_decode($b64)); | |||||||||||||||||||||||||||
| 183 | 4 | foreach my $subscriber (@{ $conf->{subs} }) { | |||||||||||||||||||||||||||
| 184 | 4 | $subscriber->(@{ $args }); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | else { | ||||||||||||||||||||||||||||
| 188 | return | ||||||||||||||||||||||||||||
| 189 | 0 | } | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | 3 | }); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 3 | $stream->timeout(0); | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 3 | ▶ | $cb->($stream) if defined $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 6 | ); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 6 | 1; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | __END__ | ||||||||||||||||||||||||||||