blib/lib/IPC/Manager/Client/MessageFiles.pm

filter: Statementstmt 90.7% 98 / 108 Branchbran 63.0% 29 / 46 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 91.3% 21 / 23 Podpod 78.5% 11 / 14 totaltotal 79.1% 163 / 206 CC 29 SCAR 15.3
CC
29
Cov
80%
_write_message_file
9.2
message_files
8.5
inotify
6.0
CRAP 4.6
« blib/lib/IPC/Manager/Base/FS/Handle.pm ↑ Summary blib/lib/IPC/Manager.pm »
1 package IPC::Manager::Client::MessageFiles;
2 88 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 88 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 88 our $VERSION = '0.000036';
6  
7 88 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 88 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 88 ▶use IPC::Manager::Util qw/USE_INOTIFY/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 88 ▶use parent 'IPC::Manager::Base::FS';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 88 use Object::HashBase qw{
14 +dir_handle
15 +inotify
16  
17 +pend_count
18 +ready_count
19 88 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶sub _viable { 1 }
Subroutine _viable: called CC=1 SCAR=0.0
22  
23 679 ▶sub check_path { -d $_[1] }
Subroutine check_path: called CC=1 SCAR=0.0
Pod documented
24 148 ▶sub make_path { mkdir($_[1]) or die "Could not make dir '$_[1]': $!" }
Subroutine make_path: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
mkdir $_[1] or die "Could not make dir '$_[1]': $!" 0 148 148
25 151 ▶sub path_type { 'subdir' }
Subroutine path_type: called CC=1 SCAR=0.0
Pod documented
26  
27 sub init {
28 150 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
29  
30 150 $self->SUPER::init();
31  
32 150 $self->{+PEND_COUNT} = 0;
33 150 $self->{+READY_COUNT} = 0;
34  
35 # Prime the message-directory inotify watch now that the path exists,
36 # so events are captured from the start. Skip on reconnect so the
37 # first message_files() call does a full directory scan and picks up
38 # any messages written while the client was suspended.
39 150 $self->inotify if USE_INOTIFY && !$self->{+RECONNECT};
40 }
41  
42 58 ▶sub have_handles_for_select { USE_INOTIFY() }
Subroutine have_handles_for_select: called CC=1 SCAR=0.0
Pod documented
43 0 ▶sub handles_for_select { USE_INOTIFY() ? $_[0]->inotify->fh : () }
Subroutine handles_for_select: not called CC=1 SCAR=6.9
Pod documented
44  
45 sub inotify {
46 0 ▶ my $self = shift;
Subroutine inotify: not called CC=2 SCAR=17.9
Pod documented
47 0 croak "Not Implemented (Or you are missing Linux::Inotify2)" unless USE_INOTIFY();
48  
49 0 ▶ return $self->{+INOTIFY} if $self->{+INOTIFY};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inotify"} 0 0 0
50  
51 0 my $i = Linux::Inotify2->new;
52 0 $i->blocking(0);
53 0 $i->watch($self->path, Linux::Inotify2::IN_CREATE() | Linux::Inotify2::IN_MOVED_TO());
54  
55 0 return $self->{+INOTIFY} = $i;
56 }
57  
58 sub pre_disconnect_hook {
59 121 ▶ my $self = shift;
Subroutine pre_disconnect_hook: called CC=2 SCAR=7.0
Pod documented
60  
61 121 my $new_path = File::Spec->catfile($self->{+ROUTE}, "_" . $self->on_disk_name($self->{+ID}));
62 121 ▶ rename($self->path, $new_path) or die "Cannot rename directory: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $self->path, $new_path or die "Cannot rename directory: $!" 0 121 121
63 121 $self->{+PATH} = $new_path;
64 }
65  
66 sub dir_handle {
67 461 ▶ my $self = shift;
Subroutine dir_handle: called CC=3 SCAR=11.1
Pod documented
68 461 $self->pid_check;
69 461 ▶ my $out = $self->{+DIR_HANDLE} //= do {
Condition66%2/3
$self->{"dir_handle"} //= do { opendir my $dh, $self->path or die "Could not open dir: $!"; $dh }
l!l&&r!l&&!r
3141470
MC/DC0%0/2
$self->{"dir_handle"} //= do { opendir my $dh, $self->path or die "Could not open dir: $!"; $dh }
$self->{"dir_handle"} do { opendir my $dh, $self->path or die "Could not open dir: $!"; $dh }
Truth table66%2/3
A //= B
A= $self->{"dir_handle"}B= do { opendir my $dh, $self->path or die "Could not open dir: $!"; $dh }
ABresult
1X1
011
000
70 147 ▶ opendir(my $dh, $self->path) or die "Could not open dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $self->path or die "Could not open dir: $!" 0 147 147
71 147 $dh;
72 };
73  
74 461 rewinddir($out);
75  
76 461 return $out;
77 }
78  
79 sub pending_messages {
80 123 ▶ my $self = shift;
Subroutine pending_messages: called CC=3 SCAR=11.7
Pod undocumented
81 123 ▶ return 1 if $self->{+PEND_COUNT};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pend_count"} 0 123 123
82 123 ▶ return $self->message_files('pend') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->message_files('pend') ? : 0 123 123
83 }
84  
85 sub ready_messages {
86 126 ▶ my $self = shift;
Subroutine ready_messages: called CC=4 SCAR=13.9
Pod undocumented
87 126 ▶ return 1 if $self->{+READY_COUNT};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ready_count"} 4 122 126
88 122 ▶ return 1 if $self->have_resume_file;
Branch: 1 branch
BranchTrueFalseTotal
if $self->have_resume_file 0 122 122
89 122 ▶ return $self->message_files('ready') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->message_files('ready') ? : 1 121 122
90 }
91  
92 sub message_files {
93 459 ▶ my $self = shift;
Subroutine message_files: called CC=8 SCAR=21.3
Pod documented
94 459 $self->pid_check;
95 459 my ($ext) = @_;
96  
97 459 if (USE_INOTIFY && !$self->{+READY_COUNT} && !$self->{+PEND_COUNT}) {
98 my $check_for_pre_inotify = $self->{+INOTIFY} ? 0 : 1;
99  
100 if ($self->select->can_read(0)) {
101 # Reset the status
102 $self->inotify->read;
103 }
104 else {
105 return undef unless $check_for_pre_inotify;
106 }
107 }
108  
109 459 my (@pend, @ready);
110 459 for my $file (readdir($self->dir_handle)) {
111 1057 ▶ if ($file =~ m/\.ready$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file =~ /\.ready$/) { } 139 918 1057
elsif ($file =~ /\.pend$/) { } 0 918 918
112 139 push @ready => $file;
113 }
114 elsif ($file =~ m/\.pend$/) {
115 0 push @pend => $file;
116 }
117 }
118  
119 459 $self->{+READY_COUNT} = @ready;
120 459 $self->{+PEND_COUNT} = @pend;
121  
122 459 ▶ return @ready ? \@ready : undef if $ext eq 'ready';
Branch: 2 branches
BranchTrueFalseTotal
if $ext eq "ready" 335 124 459
@ready ? : 113 222 335
123 124 ▶ return @pend ? \@pend : undef if $ext eq 'pend';
Branch: 2 branches
BranchTrueFalseTotal
if $ext eq "pend" 124 0 124
@pend ? : 0 124 124
124  
125 0 return undef;
126 }
127  
128 sub get_messages {
129 211 ▶ my $self = shift;
Subroutine get_messages: called CC=5 SCAR=16.1
Pod documented
130 211 my ($ext) = @_;
131  
132 211 my @out;
133  
134 211 ▶ my $ready = $self->message_files('ready') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ready = $self->message_files("ready") or return 100 111 211
135  
136 111 for my $msg (@$ready) {
137 130 my $full = File::Spec->catfile($self->path, $msg);
138 130 ▶ open(my $fh, '<', $full) or die "Could not open file '$full': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $full or die "Could not open file '${full}': $!" 0 130 130
139 130 my $content = do { local $/; <$fh> };
140 130 close($fh);
141 130 ▶ unlink($full) or die "Could not unlink file '$full': $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $full or die "Could not unlink file '${full}': $!" 0 130 130
142  
143 130 my $msg = IPC::Manager::Message->new($self->{+SERIALIZER}->deserialize($content));
144 130 push @out => $msg;
145  
146 130 $self->{+STATS}->{read}->{$msg->{from}}++;
147 }
148  
149 111 push @out => $self->read_resume_file;
150  
151 111 return $self->sort_messages(@out);
152 }
153  
154 sub _write_message_file {
155 132 ▶ my $self = shift;
Subroutine _write_message_file: called CC=8 SCAR=22.2
156 132 my ($msg, $peer) = @_;
157  
158 132 ▶ $peer //= $msg->to or croak "Message has no peer";
Branch: 1 branch
BranchTrueFalseTotal
$peer //= $msg->to or croak("Message has no peer") 0 132 132
Condition33%1/3
$peer //= $msg->to
l!l&&r!l&&!r
01320
MC/DC0%0/2
$peer //= $msg->to
$peer $msg->to
Truth table33%1/3
A //= B
A= $peerB= $msg->to
ABresult
1X1
011
000
159  
160 132 ▶ my $msg_dir = $self->peer_exists($peer) or croak "Client does not exist";
Branch: 1 branch
BranchTrueFalseTotal
my $msg_dir = $self->peer_exists($peer) or croak("Client does not exist") 1 131 132
161 131 my $msg_file = File::Spec->catfile($msg_dir, $msg->id);
162  
163 131 my $pend = "$msg_file.pend";
164 131 my $ready = "$msg_file.ready";
165  
166 131 ▶ confess "Message file '$msg_file' already exists" if -e $pend || -e $ready;
Branch: 1 branch
BranchTrueFalseTotal
if -e $pend or -e $ready 0 131 131
Condition33%1/3
-e $pend or -e $ready
l!l&&r!l&&!r
00131
MC/DC0%0/2
-e $pend or -e $ready
-e $pend -e $ready
Truth table33%1/3
A or B
A= -e $pendB= -e $ready
ABresult
1X1
011
000
167  
168 131 ▶ open(my $fh, '>', $pend) or die "Could not open '$pend': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $pend or die "Could not open '${pend}': $!" 0 131 131
169  
170 131 print $fh $self->{+SERIALIZER}->serialize($msg);
171  
172 131 close($fh);
173  
174 131 ▶ rename($pend, $ready) or die "Could not rename file: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $pend, $ready or die "Could not rename file: $!" 0 131 131
175  
176 131 $self->{+STATS}->{sent}->{$msg->{to}}++;
177 131 return $ready;
178 }
179  
180 sub send_message {
181 132 ▶ my $self = shift;
Subroutine send_message: called CC=1 SCAR=0.0
Pod documented
182 132 my $msg = $self->build_message(@_);
183 132 $self->pid_check;
184 132 $self->_write_message_file($msg);
185 }
186  
187 88 1;
188  
189 __END__
« blib/lib/IPC/Manager/Base/FS/Handle.pm ↑ Summary blib/lib/IPC/Manager.pm »