App/Chart/Glib/Ex/DirBroadcast.pm

filter: Statementstmt 27.1% 48 / 177 Branchbran 8.6% 5 / 58 Conditioncond 4.7% 1 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 51.8% 14 / 27 Podpod 77.7% 7 / 9 totaltotal 24.6% 75 / 304 CC 41 SCAR 41.2
CC
41
Cov
21%
send
240.0
listen
30.0
directory
20.0
CRAP 61.8
« App/Chart/Gtk2/Ex/ToplevelBits.pm ↑ Summary App/Chart/DownloadCost.pm »
1 # connect_front, or priority level
2 # message length limit
3  
4  
5  
6 # Copyright 2007, 2008, 2009, 2010, 2015 Kevin Ryde
7  
8 # This file is part of Chart.
9 #
10 # Chart is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 3, or (at your option) any later version.
13 #
14 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 # details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Chart. If not, see <http://www.gnu.org/licenses/>.
21  
22 package App::Chart::Glib::Ex::DirBroadcast;
23 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Carp 'carp','croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use Class::Singleton 1.03; # 1.03 for _new_instance()
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use base 'Class::Singleton';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 *_new_instance = \&new;
32  
33 1 ▶use constant MAXLEN => 16384;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35  
36 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
37 1 my ($class, $directory) = @_;
38 1 return bless { directory => $directory }, $class;
39 }
40  
41 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.4
42 1 my ($self) = @_;
43 1 delete $self->{'listen_source_ids'};
44 # close socket before removing file
45 1 delete $self->{'listen_sock'};
46 1 ▶ if (my $filename = delete $self->{'listen_filename'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filename = delete $self->{"listen_filename"}) 0 1 1
47 ### DirBroadcast remove: $filename
48 0 unlink ($filename);
49 }
50 }
51  
52 0 ▶sub directory {
Subroutine directory: not called CC=4 SCAR=30.0
Pod documented
53 0 my ($self, $newval) = @_;
54 0 ▶ ref($self) or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 0 0 0
55  
56 0 ▶ if (@_ < 2) { return $self->{'directory'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) 0 0 0
57 0 ▶ if ($self->{'listen_source_ids'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"listen_source_ids"}) 0 0 0
58 0 croak 'DirBroadcast: cannot set directory after listen';
59 }
60 0 $self->{'directory'} = $newval;
61 }
62  
63 # connections is a hashref of key to arrayref of subrs, ie.
64 #
65 # $self->{'connections'} = { 'foo' => [ \&handler1, \&handler2 ],
66 # 'bar' => [ \&handler3, \&handler4 ] };
67 #
68 1 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.4
Pod documented
69 1 my ($self, $key, $subr) = @_;
70 1 ▶ ref($self) or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 0 1 1
71  
72 1 ▶ my $aref = ($self->{'connections'}->{$key} ||= []);
Condition50%1/2
$self->{"connections"}{$key} ||= []
l!l
01
MC/DC0%0/1
$self->{"connections"}{$key} ||= []
$self->{"connections"}{$key}
Truth table50%1/2
A
A= $self->{"connections"}{$key}
Aresult
11
00
73 1 push @$aref, $subr;
74 }
75 0 ▶sub connect_first {
Subroutine connect_first: not called CC=3 SCAR=24.8
Pod undocumented
76 0 my ($self, $key, $subr) = @_;
77 0 ▶ ref($self) or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 0 0 0
78 0 ▶ my $aref = ($self->{'connections'}->{$key} ||= []);
Condition0%0/2
$self->{"connections"}{$key} ||= []
l!l
00
MC/DC0%0/1
$self->{"connections"}{$key} ||= []
$self->{"connections"}{$key}
Truth table0%0/2
A
A= $self->{"connections"}{$key}
Aresult
11
00
79 0 unshift @$aref, $subr;
80 }
81  
82 0 ▶sub connect_for_object {
Subroutine connect_for_object: not called CC=2 SCAR=17.9
Pod documented
83 0 my ($self, $key, $subr, $obj) = @_;
84 0 ▶ ref($self) or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 0 0 0
85  
86 0 require Scalar::Util;
87 0 Scalar::Util::weaken ($obj);
88 0 my $csubr;
89 0 ▶ $csubr = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
90 0 ▶ if ($obj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj) { } 0 0 0
91 0 $subr->($obj, @_);
92 } else {
93 0 _disconnect ($self, $key, $csubr);
94 }
95 0 };
96 0 $self->connect ($key, $csubr);
97 }
98  
99 0 ▶sub _disconnect {
Subroutine _disconnect: not called CC=2 SCAR=17.9
100 0 my ($self, $key, $subr) = @_;
101 0 ▶ if (my $aref = $self->{'connections'}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $aref = $self->{"connections"}{$key}) 0 0 0
102 0 @$aref = grep {$_ != $subr} @$aref;
103 }
104 }
105  
106 1 ▶sub send_locally {
Subroutine send_locally: called CC=5 SCAR=18.9
Pod documented
107 1 my ($self, $key, @data) = @_;
108 1 ▶ ref($self) or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 0 1 1
109  
110 1 ▶ if ($self->{'hold'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'hold'}) { } 0 1 1
111 0 ▶ push @{$self->{'hold_list'}}, sub { send_locally ($self, $key, @data); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
112  
113 } else {
114 1 ▶ if (my $aref = $self->{'connections'}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $aref = $self->{"connections"}{$key}) 1 0 1
115 1 foreach my $subr (@$aref) {
116 1 $subr->(@data);
117 }
118 }
119 }
120 }
121  
122 0 ▶sub listen {
Subroutine listen: not called CC=5 SCAR=34.0
Pod documented
123 0 my ($self) = @_;
124 0 ▶ ref($self) or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 0 0 0
125  
126 0 ▶ if ($self->{'listen_source_ids'}) { return; } # already done
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"listen_source_ids"}) 0 0 0
127 0 my $directory = $self->{'directory'};
128 0 ▶ if (! defined $directory) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $directory) 0 0 0
129 0 croak 'DirBroadcast cannot listen until broadcast directory is set';
130 }
131  
132 0 require File::Path;
133 0 File::Path::mkpath ($directory);
134  
135 0 require Sys::Hostname;
136 0 my $hostname = Sys::Hostname::hostname();
137 0 my $listen_filename = $self->{'listen_filename'}
138 = File::Spec->catfile ($directory, "$hostname.$$");
139 0 unlink ($listen_filename); # possible previous leftover
140  
141 # as usual socket() and friends get FD_CLOEXEC set automatically, no need
142 # to do anything special to avoid propagating $listen_sock fd down to
143 # subprocess jobs
144 0 require Socket;
145 0 require IO::Socket;
146 my $listen_sock = $self->{'listen_sock'}
147 0 = do { local $^F = 0; # ensure close-on-exec for the socket
148 0 IO::Socket->new (Domain => Socket::AF_UNIX(),
149 Type => Socket::SOCK_DGRAM(),
150 Local => $listen_filename) };
151 0 ▶ binmode ($listen_sock, ':raw') or die;
Branch: 1 branch
BranchTrueFalseTotal
binmode $listen_sock, ":raw" or die 0 0 0
152 ### DirBroadcast listen: $listen_filename, $listen_sock->fileno
153  
154 0 require Glib;
155 0 require App::Chart::Glib::Ex::MoreUtils;
156 0 require Glib::Ex::SourceIds;
157  
158 0 $self->{'listen_source_ids'}
159 = Glib::Ex::SourceIds->new
160 (Glib::IO->add_watch ($listen_sock->fileno,
161 ['in', 'hup', 'err'],
162 \&_do_listen_read,
163 App::Chart::Glib::Ex::MoreUtils::ref_weak($self)));
164 }
165  
166 0 ▶sub _do_listen_read {
Subroutine _do_listen_read: not called CC=4 SCAR=30.0
167 0 my ($fd, $conditions, $ref_weak_self) = @_;
168 0 ▶ my $self = $$ref_weak_self || return 0; # Glib::SOURCE_REMOVE
Condition0%0/2
$$ref_weak_self || (return 0)
l!l
00
MC/DC0%0/1
$$ref_weak_self || (return 0)
$$ref_weak_self
Truth table0%0/2
A
A= $$ref_weak_self
Aresult
11
00
169  
170 ### DirBroadcast read: $fd, $conditions
171 0 my $buf;
172 0 my $ret = $self->{'listen_sock'}->recv ($buf, MAXLEN, 0);
173 0 ▶ if (! defined $ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret) 0 0 0
174 0 warn __PACKAGE__." listen read error: $!";
175 0 delete $self->{'listen_source_ids'};
176 0 delete $self->{'listen_sock'};
177 0 return 0; # Glib::SOURCE_REMOVE
178 }
179 ### receive: " bytes=".length($buf), "'$ret'"
180 0 require Storable;
181 0 my $args;
182 0 ▶ if (! eval { $args = Storable::thaw ($buf); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $args = Storable::thaw($buf); 1 } }) 0 0 0
183 0 carp "DirBroadcast: error thawing message, ignoring";
184 0 return 1; # Glib::SOURCE_CONTINUE
185 }
186  
187 ### $args
188 0 $self->send_locally (@$args);
189 0 return 1; # Glib::SOURCE_CONTINUE
190 }
191  
192 1 my $send_sock;
193  
194 0 ▶sub send {
Subroutine send: not called CC=15 SCAR=54.8
Pod documented
195 0 my ($self, $key, @data) = @_;
196 0 ▶ ref($self) or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 0 0 0
197  
198 0 ▶ if ($self->{'hold'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hold"}) 0 0 0
199 ### send hold back: $key
200 0 ▶ push @{$self->{'hold_list'}}, sub { $self->send ($key, @data); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
201 0 return;
202 }
203 #### send: $key, \@data
204  
205 0 $self->send_locally ($key, @data);
206  
207 0 my $directory = $self->{'directory'};
208 0 require IO::Dir;
209 0 ▶ my $dh = IO::Dir->new ($directory) || return;
Condition0%0/2
'IO::Dir'->new($directory) || (return)
l!l
00
MC/DC0%0/1
'IO::Dir'->new($directory) || (return)
'IO::Dir'->new($directory)
Truth table0%0/2
A
A= 'IO::Dir'->new($directory)
Aresult
11
00
210 0 my @filenames = $dh->read;
211 0 $dh->close;
212  
213 0 ▶ my $pattern = ($self->{'pattern'} ||= do {
Condition0%0/3
$self->{"pattern"} ||= do { require Sys::Hostname; my $hostname = Sys::Hostname::hostname(); qr/^\Q$hostname\E\.[0-9]+$/o }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"pattern"} ||= do { require Sys::Hostname; my $hostname = Sys::Hostname::hostname(); qr/^\Q$hostname\E\.[0-9]+$/o }
$self->{"pattern"} do { require Sys::Hostname; my $hostname = Sys::Hostname::hostname(); qr/^\Q$hostname\E\.[0-9]+$/o }
Truth table0%0/3
A ||= B
A= $self->{"pattern"}B= do { require Sys::Hostname; my $hostname = Sys::Hostname::hostname(); qr/^\Q$hostname\E\.[0-9]+$/o }
ABresult
1X1
011
000
214 0 require Sys::Hostname;
215 0 my $hostname = Sys::Hostname::hostname();
216 0 qr/^\Q$hostname\E\.[0-9]+$/o
217 });
218  
219 0 my $frozen;
220 0 foreach my $filename (@filenames) {
221 0 ▶ if ($filename !~ $pattern) { next; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename =~ /$pattern/) 0 0 0
222 0 $filename = File::Spec->catfile ($directory, $filename);
223  
224 0 ▶ if ($filename eq ($self->{'listen_filename'}||'')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq ($self->{"listen_filename"} || "")) 0 0 0
Condition0%0/2
$self->{'listen_filename'} || ''
l!l
00
MC/DC0%0/1
$self->{'listen_filename'} || ''
$self->{'listen_filename'}
Truth table0%0/2
A
A= $self->{'listen_filename'}
Aresult
11
00
225 0 next; # ourselves
226 }
227 ### DirBroadcast to: $filename
228  
229 # send_sock created on first of any DirBroadcast instance and then kept
230 # open
231 0 ▶ $send_sock ||= do {
Condition0%0/3
$send_sock ||= do { require IO::Socket; require Socket; my $sock = do { local $^F = 0; "IO::Socket"->new("Domain", Socket::AF_UNIX(), "Type", Socket::SOCK_DGRAM()) }; $sock->blocking(0); binmode $sock, ":raw" or die; $sock }
l!l&&r!l&&!r
000
MC/DC0%0/2
$send_sock ||= do { require IO::Socket; require Socket; my $sock = do { local $^F = 0; "IO::Socket"->new("Domain", Socket::AF_UNIX(), "Type", Socket::SOCK_DGRAM()) }; $sock->blocking(0); binmode $sock, ":raw" or die; $sock }
$send_sock do { require IO::Socket; require Socket; my $sock = do { local $^F = 0; "IO::Socket"->new("Domain", Socket::AF_UNIX(), "Type", Socket::SOCK_DGRAM()) }; $sock->blocking(0); binmode $sock, ":raw" or die; $sock }
Truth table0%0/3
A ||= B
A= $send_sockB= do { require IO::Socket; require Socket; my $sock = do { local $^F = 0; "IO::Socket"->new("Domain", Socket::AF_UNIX(), "Type", Socket::SOCK_DGRAM()) }; $sock->blocking(0); binmode $sock, ":raw" or die; $sock }
ABresult
1X1
011
000
232 0 require IO::Socket;
233 0 require Socket;
234 0 my $sock = do {
235 0 local $^F = 0; # ensure close-on-exec for the socket
236 0 IO::Socket->new (Domain => Socket::AF_UNIX(),
237 Type => Socket::SOCK_DGRAM());
238 };
239 0 $sock->blocking(0);
240 0 ▶ binmode ($sock, ':raw') or die;
Branch: 1 branch
BranchTrueFalseTotal
binmode $sock, ":raw" or die 0 0 0
241 0 $sock
242 };
243  
244 # put off freezing until we find someone to send to
245 0 ▶ if (! defined $frozen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $frozen) 0 0 0
246 0 require Storable;
247 0 $frozen = Storable::freeze ([$key, @data]);
248 0 ▶ if (length ($frozen) > MAXLEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $frozen > 16384) 0 0 0
249 0 croak 'DirBroadcast: message too long: ',length($frozen);
250 }
251 }
252  
253 0 my $sun = Socket::sockaddr_un ($filename);
254 0 my $sent = $send_sock->send ($frozen, 0, $sun);
255 0 ▶ if (! defined $sent || $sent != length($frozen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sent or $sent != length $frozen) 0 0 0
Condition0%0/3
not defined $sent or $sent != length $frozen
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $sent or $sent != length $frozen
not defined $sent $sent != length $frozen
Truth table0%0/3
not(A) or B
A= not defined $sentB= $sent != length $frozen
ABresult
1X1
011
000
256 ### send: (! defined $sent && "removing, error $!") || "removing, short send $sent bytes"
257 0 unlink ($filename);
258 }
259 }
260 }
261  
262 0 ▶sub hold {
Subroutine hold: not called CC=2 SCAR=17.9
Pod undocumented
263 0 my ($self) = @_;
264 0 ▶ ref($self) or $self = $self->instance;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = $self->instance 0 0 0
265 0 return App::Chart::Glib::Ex::DirBroadcast::Hold->new ($self);
266 }
267  
268 package App::Chart::Glib::Ex::DirBroadcast::Hold;
269 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
270 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
271  
272 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
273 0 my ($class, $dirb) = @_;
274 0 my $self = bless { }, $class;
275 0 $self->{'target'} = $dirb;
276 0 require Scalar::Util;
277 0 Scalar::Util::weaken ($self->{'target'});
278 0 $dirb->{'hold'} ++;
279 0 return $self;
280 }
281  
282 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=4 SCAR=30.0
283 0 my ($self) = @_;
284 0 ▶ my $dirb = delete $self->{'target'} || return;
Condition0%0/2
delete $self->{'target'} || (return)
l!l
00
MC/DC0%0/1
delete $self->{'target'} || (return)
delete $self->{'target'}
Truth table0%0/2
A
A= delete $self->{'target'}
Aresult
11
00
285 0 ▶ if (-- $dirb->{'hold'}) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (--$dirb->{"hold"}) 0 0 0
286  
287 0 my $hold_list = $dirb->{'hold_list'};
288 ### DirBroadcast::Hold now run: $hold_list
289 0 ▶ while (my $subr = shift @$hold_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subr = shift @$hold_list) 0 0 0
290 0 $subr->();
291 }
292 }
293  
294 1 1;
295 __END__
« App/Chart/Gtk2/Ex/ToplevelBits.pm ↑ Summary App/Chart/DownloadCost.pm »