lib/Data/Send/Local.pm

filter: Statementstmt 87.5% 56 / 64 Branchbran 44.7% 17 / 38 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 50.0% 2 / 4 totaltotal 70.7% 87 / 123 CC 22 SCAR 23.0
CC
22
Cov
71%
test
30.0
sendLocal
10.8
recvLocal
6.6
CRAP 10.0
↑ Summary
1 #!/usr/bin/perl
2 #-------------------------------------------------------------------------------
3 # Send a block of data from one process to another on the local machine
4 # Philip R Brenan at gmail dot com, Appa Apps Ltd Inc, 2016-2017
5 #-------------------------------------------------------------------------------
6  
7 package Data::Send::Local;
8 2 our $VERSION = 20180405;
9 2 ▶use v5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings FATAL => qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Data::Table::Text qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #1 Send and receive
18  
19 sub sendLocal($$;$) #S Send a block of data locally. Returns B<undef> on success otherwise an error message
20 1 ▶ {my ($socketName, $data, $timeOut) = @_; # Socket name (a socket file name that already exists), data, optional timeout for socket to be created - defaults to 10 seconds
Subroutine sendLocal: called CC=9 SCAR=23.8
Pod documented
21  
22 1 ▶ if (!-S $socketName) # Wait for a bit if necessary for the socket to be created
Branch: 1 branch
BranchTrueFalseTotal
unless (-S $socketName) 1 0 1
23 1 ▶ {for(1..($timeOut//10)) {sleep 1; last if -S $socketName}
Branch: 1 branch
BranchTrueFalseTotal
if -S $socketName 1 0 1
Condition50%1/2
$timeOut // 10
l!l
01
MC/DC0%0/1
$timeOut // 10
$timeOut
Truth table50%1/2
A
A= $timeOut
Aresult
11
00
24 }
25 1 ▶ -S $socketName or return "No such socket: $socketName"; # Socket not available
Branch: 1 branch
BranchTrueFalseTotal
-S $socketName or return "No such socket: $socketName" 1 0 1
26  
27 1 ▶ socket(my $socket, AF_UNIX, SOCK_DGRAM, 0) or return $!;
Branch: 1 branch
BranchTrueFalseTotal
socket my $socket, 1, 2, 0 or return $! 1 0 1
28 1 ▶ connect($socket, sockaddr_un($socketName)) or return $!;
Branch: 1 branch
BranchTrueFalseTotal
connect $socket, sockaddr_un($socketName) or return $! 1 0 1
29 1 ▶ send($socket, dump($data), 0) or return $!;
Branch: 1 branch
BranchTrueFalseTotal
send $socket, Data::Send::Local::dump($data), 0 or return $! 1 0 1
30 1 close($socket);
31  
32 undef # Return without errors
33 1 }
34  
35 sub recvLocal($;$$) #S Receive a block of data sent locally. Returns the data received.
36 1 ▶ {my ($socketName, $user, $length) = @_; # Socket name (a socket file name that is created), optional username of the owner of the socket, maximum length to receive - defaults to one megabyte.
Subroutine recvLocal: called CC=6 SCAR=18.9
Pod documented
37  
38 1 unlink $socketName; # Remove existing socket to avoid 'already in use';
39 1 makePath($socketName); # Create socket directory
40 1 ▶ socket(my $socket, AF_UNIX, SOCK_DGRAM, 0) or confess $!;
Branch: 1 branch
BranchTrueFalseTotal
socket my $socket, 1, 2, 0 or confess($!) 1 0 1
41 1 ▶ bind($socket, sockaddr_un($socketName)) or confess $!;
Branch: 1 branch
BranchTrueFalseTotal
bind $socket, sockaddr_un($socketName) or confess($!) 1 0 1
42  
43 1 ▶ if ($user) # Do this to make the socket writable by some one else
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 0 1 1
44 0 {qx(chown $user:$user $socketName);
45 }
46  
47 1 ▶ recv($socket, my $read, $length // 1e6, 0);
Condition50%1/2
$length // 1000000
l!l
01
MC/DC0%0/1
$length // 1000000
$length
Truth table50%1/2
A
A= $length
Aresult
11
00
48 1 close($socket);
49 1 unlink $socketName; # Remove existing socket to force send to wait while the socket is created
50  
51 1 my $r = eval $read; # Reconstitute data
52 1 ▶ $@ and confess $@; # Bad data block
Branch: 1 branch
BranchTrueFalseTotal
$@ and confess($@) 0 1 1
53  
54 1 $r # Return data
55 }
56  
57 #-------------------------------------------------------------------------------
58 # Tests
59 #-------------------------------------------------------------------------------
60  
61 sub test2()
62 2 ▶ {my $socket = 'socket'; # Socket name
Subroutine test2: called CC=5 SCAR=16.4
Pod undocumented
63 2 my $data = 'hello'; # Data
64 2 autoflush STDOUT 1;
65  
66 2 ▶ if ($^O !~ m/\AMSWin32\Z/) # Ignore windows
Branch: 1 branch
BranchTrueFalseTotal
if (not $^O =~ /\AMSWin32\Z/) 2 0 2
67 2 {say STDOUT "1..2";
68 2 ▶ if (fork())
Branch: 1 branch
BranchTrueFalseTotal
if (fork) { } 1 1 2
69 1 ▶ {say STDOUT "ok" if Data::Send::Local::recvLocal($socket) eq $data; # Receive data
Branch: 1 branch
BranchTrueFalseTotal
if &recvLocal($socket) eq $data 1 0 1
70 }
71 else
72 1 {autoflush STDOUT 1;
73 1 ▶ say STDOUT "ok" unless Data::Send::Local::sendLocal($socket, $data); # Send data without error
Branch: 1 branch
BranchTrueFalseTotal
unless &sendLocal($socket, $data) 1 0 1
74 }
75 }
76 }
77  
78 2 ▶test2 unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 2 2
79  
80 # podDocumentation
81  
82 =pod
83  
84 =encoding utf-8
85  
86 =head1 Name
87  
88 Data::Send::Local - Send and receive a block of data between processes on the
89 local machine.
90  
91 =head1 Synopsis
92  
93 Send B<hello> between two processes running on the same machine over the
94 socket named B<socket>.
95  
96 use Test2::Bundle::More;
97  
98 my $socket = 'socket'; # Socket name
99 my $data = 'hello'; # Data
100  
101 if (fork())
102 {ok Data::Send::Local::recvLocal($socket) eq $data; # Receive data
103 }
104 else
105 {ok !Data::Send::Local::sendLocal($socket, $data); # Send data without error
106 }
107  
108 done_testing;
109  
110 =head1 Description
111  
112 The following sections describe the methods in each functional area of this
113 module. For an alphabetic listing of all methods by name see L<Index|/Index>.
114  
115  
116  
117 =head1 Send and receive
118  
119 =head2 sendLocal($$$)
120  
121 Send a block of data locally. Returns B<undef> on success otherwise an error message
122  
123 1 $socketName Socket name (a socket file name that already exists)
124 2 $data Data
125 3 $timeOut Optional timeout for socket to be created - defaults to 10 seconds
126  
127 This is a static method and so should be invoked as:
128  
129 Data::Send::Local::sendLocal
130  
131  
132 =head2 recvLocal($$)
133  
134 Receive a block of data sent locally. Returns the data received.
135  
136 1 $socketName Socket name (a socket file name that is created)
137 2 $length Optional maximum length to receive - defaults to one megabyte.
138  
139 This is a static method and so should be invoked as:
140  
141 Data::Send::Local::recvLocal
142  
143  
144  
145 =head1 Index
146  
147  
148 1 L<recvLocal|/recvLocal>
149  
150 2 L<sendLocal|/sendLocal>
151  
152 =head1 Installation
153  
154 This module is written in 100% Pure Perl and, thus, it is easy to read, use,
155 modify and install.
156  
157 Standard L<Module::Build> process for building and installing modules:
158  
159 perl Build.PL
160 ./Build
161 ./Build test
162 ./Build install
163  
164 =head1 Author
165  
166 L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com>
167  
168 L<http://www.appaapps.com|http://www.appaapps.com>
169  
170 =head1 Copyright
171  
172 Copyright (c) 2016-2017 Philip R Brenan.
173  
174 This module is free software. It may be used, redistributed and/or modified
175 under the same terms as Perl itself.
176  
177 =cut
178  
179  
180  
181 # Tests and documentation
182  
183 sub test
184 0 ▶ {my $p = __PACKAGE__;
Subroutine test: not called CC=5 SCAR=34.0
Pod undocumented
185 0 binmode($_, ":utf8") for *STDOUT, *STDERR;
186 0 ▶ return if eval "eof(${p}::DATA)";
Branch: 1 branch
BranchTrueFalseTotal
if eval "eof(${p}::DATA)" 0 0 0
187 0 my $s = eval "join('', <${p}::DATA>)";
188 0 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 0 0
189 0 eval $s;
190 0 ▶ $@ and die $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and die $@ 0 0 0
191 }
192  
193 2 ▶test unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 2 2
194  
195 2 1;
196 __DATA__
↑ Summary