lib/ControlFreak/Socket.pm

filter: Statementstmt 67.5% 73 / 108 Branchbran 40.4% 17 / 42 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 68.4% 13 / 19 Podpod 44.4% 4 / 9 totaltotal 55.3% 109 / 197 CC 27 SCAR 23.4
CC
27
Cov
57%
_bind
22.1
set_listen_qsize
20.0
unbind
6.0
CRAP 10.4
« lib/ControlFreak/Util.pm ↑ Summary bin/cfk-share-mem-proxy.pl »
1 package ControlFreak::Socket;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Object::Tiny qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
7 name
8 host
9 service
10 nonblocking
11 listen_qsize
12  
13 fh
14 8 };
15 8 ▶use Params::Util qw{ _STRING };
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Scalar::Util();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Socket qw(AF_INET AF_UNIX SOCK_STREAM SOL_SOCKET SO_REUSEADDR SOMAXCONN);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use AnyEvent::Util qw(fh_nonblocking AF_INET6);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use AnyEvent::Socket();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 ControlFreak::Socket - Defines a (shared) socket controlled by ControlFreak
24  
25 =cut
26  
27 =head1 SYNOPSIS
28  
29 $sock = ControlFreak::Socket->new(
30 ctrl => $ctrl,
31 name => "fcgisock",
32 host => "unix/",
33 service => "/tmp/cfk-x.sock",
34 options => "TBD",
35 );
36 $sock->bind;
37 $sock->unbind;
38 $sock->set_host;
39 $sock->set_service;
40 print $sock->service;
41  
42 =head1 DESCRIPTION
43  
44 Each socket object has a unique name inside B<ControlFreak> controller,
45 services interested in a socket just reference it using this name.
46 The controller pipes the socket to children's stdin after forking,
47 and before executing the service.
48  
49 =head1 METHODS
50  
51 =head2 new(%param)
52  
53 Creates a socket objects. Params are:
54  
55 =over 4
56  
57 =item * ctrl
58  
59 The controller to attach the socket to. If not specified, the
60 socket object won't be created, C<new()> will just return undef.
61  
62 =item * name
63  
64 The name of the socket, MUST be unique within C<ctrl>.
65  
66 =item * host
67  
68 eg. '127.0.0.0', '0.0.0.0', 'unix/', '[::1]'.
69  
70 =item * service
71  
72 eg. '80', '/tmp/cfk.sock'.
73  
74 =back
75  
76 If a socket with that name already exists, it will return undef
77 and log the error.
78  
79 =cut
80  
81 5 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
82 5 my $class = shift;
83 5 my %param = @_;
84  
85 5 delete $param{fh};
86 5 my $ctrl = $param{ctrl};
87 5 ▶ unless ($ctrl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctrl) 1 4 5
88 1 warn "Socket creation attempt without ctrl";
89 1 return;
90 }
91  
92 4 ▶ unless ($param{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($param{"name"}) 0 4 4
93 0 $ctrl->log->error("Socket creation attempt without a name");
94 0 return;
95 }
96  
97 4 my $socket = $class->SUPER::new(%param);
98 4 $socket->{ctrl} = $ctrl;
99 4 ▶ unless ($ctrl->add_socket($socket)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctrl->add_socket($socket)) 1 3 4
100 1 $ctrl->log->error("A socket by that name already exists");
101 1 return;
102 }
103 3 Scalar::Util::weaken($socket->{ctrl});
104 3 return $socket;
105 }
106  
107 =head2 bind
108  
109 Creates, binds the socket and puts it in listen mode, then returns
110 immediately.
111 Once bound, $socket->fh will return the filehandle.
112  
113 =cut
114  
115 2 ▶sub bind {
Subroutine bind: called CC=3 SCAR=11.9
Pod documented
116 2 my $socket = shift;
117  
118 2 my $ctrl = $socket->{ctrl};
119 2 my $name = $socket->name;
120 2 ▶ if ($socket->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket->{"fh"}) 0 2 2
121 0 $ctrl->log->error("'$name' socket is already bound");
122 0 return;
123 }
124  
125 2 my ($fh, $host, $service) = $socket->_bind;
126 2 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 2 2
127 0 $ctrl->log->error("cannot bind '$name': $!");
128 0 return;
129 }
130 2 $ctrl->log->info("'$name' socket is now bound: $fh");
131 ## reset with real values
132 2 $socket->{service} = $service;
133 2 $socket->{host} = $host;
134 2 $socket->{fh} = $fh;
135 2 return;
136 }
137  
138 2 ▶sub _bind {
Subroutine _bind: called CC=13 SCAR=31.0
139 2 my $socket = shift;
140  
141 2 my $host = $socket->host;
142 2 my $service = $socket->service;
143  
144 ## part reaped from AnyEvent::Socket
145  
146 2 ▶ my $ipn = AnyEvent::Socket::parse_address($host)
Branch: 1 branch
BranchTrueFalseTotal
my $ipn = &AnyEvent::Socket::parse_address($host) or Carp::croak('AnyEvent::Socket::tcp_server: ' . "cannot parse '${host}' as host address") 2 0 2
147 or Carp::croak "AnyEvent::Socket::tcp_server: "
148 . "cannot parse '$host' as host address";
149  
150 2 my $af = AnyEvent::Socket::address_family($ipn);
151  
152 2 my $fh;
153  
154 # win32 perl is too stupid to get this right :/
155 2 Carp::croak "tcp_server/socket: address family not supported"
156 if AnyEvent::WIN32 && $af == AF_UNIX;
157  
158 2 ▶ socket $fh, $af, SOCK_STREAM, 0
Branch: 1 branch
BranchTrueFalseTotal
socket $fh, $af, 1, 0 or Carp::croak("tcp_server/socket: $!") 2 0 2
159 or Carp::croak "tcp_server/socket: $!";
160  
161 2 ▶ if ($af == AF_INET || $af == AF_INET6) {
Branch: 2 branches
BranchTrueFalseTotal
if ($af == 2 or $af == 10) { } 2 0 2
elsif ($af == 1) { } 0 0 0
Condition33%1/3
$af == 2 or $af == 10
l!l&&r!l&&!r
200
MC/DC0%0/2
$af == 2 or $af == 10
$af == 2 $af == 10
Truth table33%1/3
A or B
A= $af == 2B= $af == 10
ABresult
1X1
011
000
162 2 ▶ setsockopt $fh, SOL_SOCKET, SO_REUSEADDR, 1
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $fh, 1, 2, 1 or Carp::croak("tcp_server/so_reuseaddr: $!") 2 0 2
163 or Carp::croak "tcp_server/so_reuseaddr: $!"
164 unless AnyEvent::WIN32; # work around windows bug
165  
166 2 ▶ unless ($service =~ /^\d*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service =~ /^\d*$/) 0 2 2
167 0 ▶ $service = (getservbyname $service, "tcp")[2]
Branch: 1 branch
BranchTrueFalseTotal
$service = (getservbyname $service, "tcp")[2] or Carp::croak("${service}: service unknown") 0 0 0
168 or Carp::croak "$service: service unknown"
169 }
170 } elsif ($af == AF_UNIX) {
171 0 unlink $service;
172 }
173  
174 2 ▶ CORE::bind $fh, AnyEvent::Socket::pack_sockaddr($service, $ipn)
Branch: 1 branch
BranchTrueFalseTotal
CORE::bind $fh, &AnyEvent::Socket::pack_sockaddr($service, $ipn) or Carp::croak("bind: $!") 2 0 2
175 or Carp::croak "bind: $!";
176  
177 2 ▶ fh_nonblocking $fh, ($socket->nonblocking ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$socket->nonblocking ? : 0 2 2
178  
179 2 ▶ my $len = $socket->listen_qsize || SOMAXCONN;
Condition50%1/2
$socket->listen_qsize || 4096
l!l
02
MC/DC0%0/1
$socket->listen_qsize || 4096
$socket->listen_qsize
Truth table50%1/2
A
A= $socket->listen_qsize
Aresult
11
00
180 2 ($service, $host) = AnyEvent::Socket::unpack_sockaddr( getsockname $fh );
181 2 ($host, $service) = (AnyEvent::Socket::format_address($host), $service);
182  
183 2 ▶ listen $fh, $len or Carp::croak "listen: $!";
Branch: 1 branch
BranchTrueFalseTotal
listen $fh, $len or Carp::croak("listen: $!") 2 0 2
184 2 return ($fh, $host, $service);
185 }
186  
187 =head2 is_bound
188  
189 Returns true if the socket is bound.
190  
191 =cut
192  
193 9 ▶sub is_bound {
Subroutine is_bound: called CC=2 SCAR=6.9
Pod documented
194 9 ▶ return shift->{fh} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{'fh'} ? : 8 1 9
195 }
196  
197 =head2 unbind()
198  
199 Unbind and destroys the socket.
200  
201 =cut
202  
203 0 ▶sub unbind {
Subroutine unbind: not called CC=2 SCAR=17.9
Pod documented
204 0 my $socket = shift;
205 0 ▶ return unless $socket->is_bound;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket->is_bound 0 0 0
206 0 $socket->{fh} = undef;
207 0 return 1;
208 }
209  
210 0 ▶sub set_host {
Subroutine set_host: not called CC=2 SCAR=17.9
Pod undocumented
211 0 my $sock = shift;
212 0 ▶ my $value = _STRING($_[0]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $value = &_STRING($_[0]) or return 0 0 0
213 0 $value =~ s/[\n\r\t\0]+//g; ## desc should be one line
214 0 $sock->{host} = $value;
215 0 return 1;
216 }
217  
218 0 ▶sub set_service {
Subroutine set_service: not called CC=2 SCAR=17.9
Pod undocumented
219 0 my $sock = shift;
220 0 ▶ my $value = _STRING($_[0]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $value = &_STRING($_[0]) or return 0 0 0
221 0 $value =~ s/[\n\r\t\0]+//g; ## desc should be one line
222 0 $sock->{service} = $value;
223 0 return 1;
224 }
225  
226 0 ▶sub set_nonblocking {
Subroutine set_nonblocking: not called CC=2 SCAR=17.9
Pod undocumented
227 0 my $sock = shift;
228 0 ▶ my $value = shift() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 0 0
229 0 $sock->{nonblocking} = $value;
230 0 return 1;
231 }
232  
233 0 ▶sub set_listen_qsize {
Subroutine set_listen_qsize: not called CC=4 SCAR=30.0
Pod undocumented
234 0 my $sock = shift;
235 0 my $size = shift;
236 0 ▶ $size = SOMAXCONN if $size && $size =~ /^\s*max\s*$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $size and $size =~ /^\s*max\s*$/i 0 0 0
Condition0%0/3
$size and $size =~ /^\s*max\s*$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$size and $size =~ /^\s*max\s*$/i
$size $size =~ /^\s*max\s*$/i
Truth table0%0/3
A and B
A= $sizeB= $size =~ /^\s*max\s*$/i
ABresult
0X0
100
111
237 0 ▶ my $value = _NUMBER($size) || 0;
Condition0%0/2
_NUMBER($size) || 0
l!l
00
MC/DC0%0/1
_NUMBER($size) || 0
_NUMBER($size)
Truth table0%0/2
A
A= _NUMBER($size)
Aresult
11
00
238 0 $sock->{listen_qsize} = $value;
239 }
240  
241 0 ▶sub unset {
Subroutine unset: not called CC=2 SCAR=17.9
Pod undocumented
242 0 my $sock = shift;
243 0 ▶ my $attr = shift || "";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
244 0 $sock->{$attr} = undef;
245 0 return 1;
246 }
247  
248 =head1 AUTHOR
249  
250 Yann Kerherve <yannk@cpan.org>
251  
252 =cut
253  
254 8 "chaussette";
« lib/ControlFreak/Util.pm ↑ Summary bin/cfk-share-mem-proxy.pl »