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

filter: Statementstmt 84.3% 108 / 128 Branchbran 47.7% 21 / 44 Conditioncond 37.5% 6 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 90.6% 29 / 32 Podpod 84.6% 11 / 13 totaltotal 72.4% 176 / 243 CC 31 SCAR 17.8
CC
31
Cov
72%
_drain_blocking
30.0
_outbox_can_send
12.0
_outbox_try_write
8.1
CRAP 5.9
« blib/lib/IPC/Manager/Test.pm ↑ Summary blib/lib/IPC/Manager/Client/JSONFile.pm »
1 package IPC::Manager::Client::UnixSocket;
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 File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 88 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 88 ▶use Errno qw/EAGAIN EWOULDBLOCK/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 88 ▶use POSIX qw/mkfifo/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 88 ▶use IO::Socket::UNIX 1.55 qw/SOCK_DGRAM/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 88 ▶use parent 'IPC::Manager::Base::FS::Handle';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 88 use Object::HashBase qw{
15 +buffer
16 +socket
17 +socket_cache
18 88 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 88 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 88 with 'IPC::Manager::Role::Outbox';
22  
23 2 ▶sub _viable { require IO::Socket::UNIX; IO::Socket::UNIX->VERSION('1.55'); 1 }
Subroutine _viable: called CC=1 SCAR=0.0
24  
25 547 ▶sub check_path { -S $_[1] }
Subroutine check_path: called CC=1 SCAR=0.0
Pod documented
26 130 ▶sub path_type { 'UNIX Socket' }
Subroutine path_type: called CC=1 SCAR=0.0
Pod documented
27  
28 # sun_path is 108 bytes on Linux, 104 on some BSDs. The socket file
29 # path "<route>/<on_disk_name>" (NUL-terminated) must fit. Leave room
30 # for the route, a separator, and the trailing NUL.
31 88 ▶use constant _SUN_PATH_LIMIT => 104;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 sub max_on_disk_name_length {
34 1412 ▶ my $self = shift;
Subroutine max_on_disk_name_length: called CC=1 SCAR=0.0
Pod documented
35 1412 return _SUN_PATH_LIMIT - length($self->{+ROUTE}) - 2; # '/' + NUL
36 }
37  
38 59 ▶sub have_handles_for_select { 1 }
Subroutine have_handles_for_select: called CC=1 SCAR=0.0
Pod documented
39 162 ▶sub handles_for_select { $_[0]->{+SOCKET} }
Subroutine handles_for_select: called CC=1 SCAR=0.0
Pod documented
40  
41 49 ▶sub suspend_supported { 0 }
Subroutine suspend_supported: called CC=1 SCAR=0.0
Pod undocumented
42 1 ▶sub suspend { croak "suspend is not supported by the UnixSocket driver" }
Subroutine suspend: called CC=1 SCAR=0.0
Pod documented
43  
44 sub make_path {
45 129 ▶ my $self = shift;
Subroutine make_path: called CC=2 SCAR=7.0
Pod documented
46 129 my $path = $self->path;
47  
48 129 ▶ my $s = IO::Socket::UNIX->new(
Branch: 1 branch
BranchTrueFalseTotal
my $s = "IO::Socket::UNIX"->new("Type", 2, "Local", $path, "Blocking", 0) or die "Cannot create reader socket: $!" 0 129 129
49 Type => SOCK_DGRAM,
50 Local => $path,
51 Blocking => 0,
52 ) or die "Cannot create reader socket: $!";
53  
54 129 $self->{+SOCKET} = $s;
55 }
56  
57 sub pre_disconnect_hook {
58 103 ▶ my $self = shift;
Subroutine pre_disconnect_hook: called CC=3 SCAR=11.1
Pod documented
59 103 ▶ return unless defined $self->{+PATH};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"path"} 1 102 103
60 102 ▶ unlink($self->{+PATH}) or warn "Could not unlink socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->{"path"} or warn "Could not unlink socket: $!" 0 102 102
61 }
62  
63 sub init {
64 130 ▶ my $self = shift;
Subroutine init: called CC=2 SCAR=7.1
Pod undocumented
65  
66 130 ▶ $self->{+BUFFER} //= [];
Condition50%1/2
$self->{"buffer"} //= []
l!l
0130
MC/DC0%0/1
$self->{"buffer"} //= []
$self->{"buffer"}
Truth table50%1/2
A
A= $self->{"buffer"}
Aresult
11
00
67  
68 130 $self->SUPER::init();
69 }
70  
71 sub _recv_all {
72 122 ▶ my $self = shift;
Subroutine _recv_all: called CC=3 SCAR=11.0
73  
74 122 my $s = $self->{+SOCKET};
75 122 while (1) {
76 246 my $msg;
77 246 my $ret = $s->recv($msg, 65536, 0);
78 246 ▶ last unless defined $ret && length $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret and length $msg 122 124 246
Condition66%2/3
defined $ret and length $msg
!ll&&!rl&&r
1220124
MC/DC50%1/2
defined $ret and length $msg
defined $ret length $msg
Truth table66%2/3
A and B
A= defined $retB= length $msg
ABresult
0X0
100
111
79 124 push @{$self->{+BUFFER}} => $msg;
80 }
81 }
82  
83 sub fill_buffer {
84 4 ▶ my $self = shift;
Subroutine fill_buffer: called CC=2 SCAR=7.0
Pod documented
85  
86 4 $self->_recv_all;
87  
88 4 ▶ return @{$self->{+BUFFER}} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'buffer'};} ? : 4 0 4
89 }
90  
91 sub get_messages {
92 118 ▶ my $self = shift;
Subroutine get_messages: called CC=2 SCAR=6.9
Pod documented
93  
94 118 my @out;
95  
96 118 push @out => $self->read_resume_file;
97  
98 118 $self->_recv_all;
99 118 for my $msg (@{$self->{+BUFFER}}) {
100 124 $msg = IPC::Manager::Message->new($self->{+SERIALIZER}->deserialize($msg));
101 124 push @out => $msg;
102 124 $self->{+STATS}->{read}->{$msg->{from}}++;
103 }
104  
105 118 @{$self->{+BUFFER}} = ();
106  
107 118 return $self->sort_messages(@out);
108 }
109  
110 sub _peer_socket {
111 126 ▶ my ($self, $peer) = @_;
Subroutine _peer_socket: called CC=5 SCAR=16.2
112  
113 126 ▶ my $sock = $self->peer_exists($peer) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $sock = $self->peer_exists($peer) or return undef 1 125 126
114  
115 125 ▶ return $self->{+SOCKET_CACHE}->{$sock} //= do {
Condition66%2/3
$self->{"socket_cache"}{$sock} //= do { my $w = "IO::Socket::UNIX"->new("Type", 2, "Peer", $sock, "Blocking", $self->send_blocking ? 1 : 0) or die "Cannot connect to socket: $!"; $w }
l!l&&r!l&&!r
33920
MC/DC0%0/2
$self->{"socket_cache"}{$sock} //= do { my $w = "IO::Socket::UNIX"->new("Type", 2, "Peer", $sock, "Blocking", $self->send_blocking ? 1 : 0) or die "Cannot connect to socket: $!"; $w }
$self->{"socket_cache"}{$sock} do { my $w = "IO::Socket::UNIX"->new("Type", 2, "Peer", $sock, "Blocking", $self->send_blocking ? 1 : 0) or die "Cannot connect to socket: $!"; $w }
Truth table66%2/3
A //= B
A= $self->{"socket_cache"}{$sock}B= do { my $w = "IO::Socket::UNIX"->new("Type", 2, "Peer", $sock, "Blocking", $self->send_blocking ? 1 : 0) or die "Cannot connect to socket: $!"; $w }
ABresult
1X1
011
000
116 92 ▶ my $w = IO::Socket::UNIX->new(
Branch: 2 branches
BranchTrueFalseTotal
my $w = "IO::Socket::UNIX"->new("Type", 2, "Peer", $sock, "Blocking", $self->send_blocking ? 1 : 0) or die "Cannot connect to socket: $!" 0 92 92
$self->send_blocking ? : 69 23 92
117 Type => SOCK_DGRAM,
118 Peer => $sock,
119 Blocking => $self->send_blocking ? 1 : 0,
120 ) or die "Cannot connect to socket: $!";
121 92 $w;
122 };
123 }
124  
125 sub _outbox_set_blocking {
126 27 ▶ my ($self, $bool) = @_;
Subroutine _outbox_set_blocking: called CC=4 SCAR=17.9
127 27 ▶ for my $s (values %{$self->{+SOCKET_CACHE} // {}}) {
Condition50%1/2
$self->{"socket_cache"} // {}
l!l
027
MC/DC0%0/1
$self->{"socket_cache"} // {}
$self->{"socket_cache"}
Truth table50%1/2
A
A= $self->{"socket_cache"}
Aresult
11
00
128 0 ▶ $s->blocking($bool ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 0 0 0
129 }
130 }
131  
132 sub _outbox_can_send {
133 0 ▶ my ($self, $peer) = @_;
Subroutine _outbox_can_send: not called CC=3 SCAR=24.8
134  
135 0 ▶ my $s = $self->_peer_socket($peer) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->_peer_socket($peer) or return 0 0 0 0
136  
137 0 require IO::Select;
138 0 my $sel = IO::Select->new($s);
139 0 ▶ return $sel->can_write(0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sel->can_write(0) ? : 0 0 0
140 }
141  
142 sub _outbox_try_write {
143 34 ▶ my ($self, $peer, $payload) = @_;
Subroutine _outbox_try_write: called CC=5 SCAR=20.9
144  
145 34 $self->pid_check;
146 34 ▶ my $s = $self->_peer_socket($peer)
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->_peer_socket($peer) or die "'${peer}' is not a valid message recipient" 0 34 34
147 or die "'$peer' is not a valid message recipient";
148  
149 34 my $rc = $s->send($payload);
150 34 ▶ if (defined $rc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rc) 34 0 34
151 34 $self->{+STATS}->{sent}->{$peer}++;
152 34 return 1;
153 }
154 0 ▶ return 0 if $! == EAGAIN || $! == EWOULDBLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 or $! == 11 0 0 0
Condition0%0/3
$! == 11 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11 $! == 11
Truth table0%0/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
155 0 die "Cannot send message: $!";
156 }
157  
158 sub _outbox_writable_handle {
159 0 ▶ my ($self, $peer) = @_;
Subroutine _outbox_writable_handle: not called CC=1 SCAR=6.9
160 0 return $self->_peer_socket($peer);
161 }
162  
163 sub send_message {
164 126 ▶ my $self = shift;
Subroutine send_message: called CC=6 SCAR=18.1
Pod documented
165 126 my $msg = $self->build_message(@_);
166  
167 126 ▶ my $peer_id = $msg->to or croak "No peer specified";
Branch: 1 branch
BranchTrueFalseTotal
my $peer_id = $msg->to or croak("No peer specified") 0 126 126
168 126 my $payload = $self->{+SERIALIZER}->serialize($msg);
169  
170 126 $self->pid_check;
171  
172 126 ▶ if ($self->send_blocking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->send_blocking) 92 34 126
173 92 ▶ $self->_drain_blocking($peer_id) if $self->pending_sends_to($peer_id);
Branch: 1 branch
BranchTrueFalseTotal
if $self->pending_sends_to($peer_id) 0 92 92
174  
175 92 ▶ my $s = $self->_peer_socket($peer_id)
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->_peer_socket($peer_id) or die "'${peer_id}' is not a valid message recipient" 1 91 92
176 or die "'$peer_id' is not a valid message recipient";
177  
178 91 ▶ $s->send($payload) or die "Cannot send message: $!";
Branch: 1 branch
BranchTrueFalseTotal
$s->send($payload) or die "Cannot send message: $!" 0 91 91
179 91 $self->{+STATS}->{sent}->{$peer_id}++;
180 91 return 1;
181 }
182  
183 34 return $self->try_send_message($peer_id, $payload);
184 }
185  
186 sub _drain_blocking {
187 0 ▶ my ($self, $peer) = @_;
Subroutine _drain_blocking: not called CC=5 SCAR=34.0
188  
189 0 ▶ my $s = $self->_peer_socket($peer) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->_peer_socket($peer) or return 0 0 0
190  
191 0 ▶ while ($self->{_OUTBOX}{$peer} && @{$self->{_OUTBOX}{$peer}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_OUTBOX"}{$peer} and @{$self->{"_OUTBOX"}{$peer};}) 0 0 0
Condition0%0/3
$self->{"_OUTBOX"}{$peer} and @{$self->{"_OUTBOX"}{$peer};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_OUTBOX"}{$peer} and @{$self->{"_OUTBOX"}{$peer};}
$self->{"_OUTBOX"}{$peer} @{$self->{"_OUTBOX"}{$peer};}
Truth table0%0/3
A and B
A= $self->{"_OUTBOX"}{$peer}B= @{$self->{"_OUTBOX"}{$peer};}
ABresult
0X0
100
111
192 0 my $entry = shift @{$self->{_OUTBOX}{$peer}};
193 0 my ($payload) = @$entry;
194 0 ▶ $s->send($payload) or die "Cannot send message: $!";
Branch: 1 branch
BranchTrueFalseTotal
$s->send($payload) or die "Cannot send message: $!" 0 0 0
195 0 $self->{+STATS}->{sent}->{$peer}++;
196 }
197 0 delete $self->{_OUTBOX}{$peer};
198 }
199  
200 88 1;
201  
202 __END__
« blib/lib/IPC/Manager/Test.pm ↑ Summary blib/lib/IPC/Manager/Client/JSONFile.pm »