Clacks/Client.pm

filter: Statementstmt 8.1% 59 / 726 Branchbran 0.0% 0 / 386 Conditioncond 0.0% 0 / 123 MC/DCmcdc 0.0% 0 / 80 Subroutinesub 35.8% 19 / 53 Podpod 100% 30 / 30 totaltotal 7.7% 108 / 1398 CC 245 SCAR 54.8
CC
245
Cov
5%
doNetwork
992.0
getNext
420.0
clientlist
342.0
CRAP 240.1
« Clacks/Server.pm ↑ Summary Clacks/PostgreSQL2Clacks.pm »
1 package Net::Clacks::Client;
2 #---AUTOPRAGMASTART---
3 4 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Carp qw[carp croak confess cluck longmess shortmess];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 our $VERSION = 37;
10 4 ▶use autodie qw( close );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Array::Contains;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Encode qw(is_utf8 encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use builtin qw[true false is_bool];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶no warnings qw(experimental::builtin); ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #---AUTOPRAGMAEND---
18  
19 4 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 #use IO::Socket::UNIX;
21 4 ▶use Time::HiRes qw[sleep usleep time];
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 0 ▶sub new($class, $server, $port, $username, $password, $clientname, $iscaching = 0) {
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
28 0 my $self = bless {}, $class;
29  
30 0 ▶ if(!defined($server) || !length($server)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $server and length $server) 0 0 0
Condition0%0/3
defined $server and length $server
!ll&&!rl&&r
000
MC/DC0%0/2
defined $server and length $server
defined $server length $server
Truth table0%0/3
A and B
A= defined $serverB= length $server
ABresult
0X0
100
111
31 0 croak("server not defined!");
32 }
33 0 ▶ if(!defined($port) || !length($port)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port and length $port) 0 0 0
Condition0%0/3
defined $port and length $port
!ll&&!rl&&r
000
MC/DC0%0/2
defined $port and length $port
defined $port length $port
Truth table0%0/3
A and B
A= defined $portB= length $port
ABresult
0X0
100
111
34 0 croak("port not defined!");
35 }
36 0 ▶ if(!defined($username) || !length($username)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username and length $username) 0 0 0
Condition0%0/3
defined $username and length $username
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and length $username
defined $username length $username
Truth table0%0/3
A and B
A= defined $usernameB= length $username
ABresult
0X0
100
111
37 0 croak("username not defined!");
38 }
39 0 ▶ if(!defined($password) || !length($password)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password and length $password) 0 0 0
Condition0%0/3
defined $password and length $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $password and length $password
defined $password length $password
Truth table0%0/3
A and B
A= defined $passwordB= length $password
ABresult
0X0
100
111
40 0 croak("password not defined!");
41 }
42 0 ▶ if(!defined($clientname) || !length($clientname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clientname and length $clientname) 0 0 0
Condition0%0/3
defined $clientname and length $clientname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $clientname and length $clientname
defined $clientname length $clientname
Truth table0%0/3
A and B
A= defined $clientnameB= length $clientname
ABresult
0X0
100
111
43 0 croak("clientname not defined!");
44 }
45  
46 0 $self->{server} = $server;
47 0 $self->{port} = $port;
48  
49 0 $self->init($username, $password, $clientname, $iscaching);
50  
51 0 return $self;
52 }
53  
54 0 ▶sub newSocket($class, $socketpath, $username, $password, $clientname, $iscaching = 0) {
Subroutine newSocket: not called CC=11 SCAR=48.8
Pod documented
55 0 my $self = bless {}, $class;
56  
57 0 ▶ if(!defined($socketpath) || !length($socketpath)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socketpath and length $socketpath) 0 0 0
Condition0%0/3
defined $socketpath and length $socketpath
!ll&&!rl&&r
000
MC/DC0%0/2
defined $socketpath and length $socketpath
defined $socketpath length $socketpath
Truth table0%0/3
A and B
A= defined $socketpathB= length $socketpath
ABresult
0X0
100
111
58 0 croak("socketpath not defined!");
59 }
60 0 ▶ if(!defined($username) || !length($username)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username and length $username) 0 0 0
Condition0%0/3
defined $username and length $username
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and length $username
defined $username length $username
Truth table0%0/3
A and B
A= defined $usernameB= length $username
ABresult
0X0
100
111
61 0 croak("username not defined!");
62 }
63 0 ▶ if(!defined($password) || !length($password)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password and length $password) 0 0 0
Condition0%0/3
defined $password and length $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $password and length $password
defined $password length $password
Truth table0%0/3
A and B
A= defined $passwordB= length $password
ABresult
0X0
100
111
64 0 croak("password not defined!");
65 }
66 0 ▶ if(!defined($clientname) || !length($clientname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clientname and length $clientname) 0 0 0
Condition0%0/3
defined $clientname and length $clientname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $clientname and length $clientname
defined $clientname length $clientname
Truth table0%0/3
A and B
A= defined $clientnameB= length $clientname
ABresult
0X0
100
111
67 0 croak("clientname not defined!");
68 }
69  
70 0 my $udsloaded = 0;
71 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
72 0 require IO::Socket::UNIX;
73 0 $udsloaded = 1;
74 };
75 0 ▶ if(!$udsloaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($udsloaded) 0 0 0
76 0 croak("Specified a unix domain socket, but i couldn't load IO::Socket::UNIX!");
77 }
78  
79 0 $self->{socketpath} = $socketpath;
80  
81 0 $self->init($username, $password, $clientname, $iscaching);
82  
83 0 return $self;
84 }
85  
86 ▶sub init($self, $username, $password, $clientname, $iscaching) {
Subroutine init: not called CC=9 SCAR=45.0
Pod documented
87 0 ▶ if(!defined($username) || $username eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $username or $username eq "") 0 0 0
Condition0%0/3
not defined $username or $username eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $username or $username eq ""
not defined $username $username eq ""
Truth table0%0/3
not(A) or B
A= not defined $usernameB= $username eq ""
ABresult
1X1
011
000
88 0 croak("Username not defined!");
89 }
90 0 ▶ if(!defined($password) || $password eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $password or $password eq "") 0 0 0
Condition0%0/3
not defined $password or $password eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $password or $password eq ""
not defined $password $password eq ""
Truth table0%0/3
not(A) or B
A= not defined $passwordB= $password eq ""
ABresult
1X1
011
000
91 0 croak("Password not defined!");
92 }
93  
94 0 ▶ if(!defined($clientname || $clientname eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($clientname || $clientname eq "")) 0 0 0
Condition0%0/3
$clientname || $clientname eq ''
l!l&&r!l&&!r
000
MC/DC0%0/2
$clientname || $clientname eq ''
$clientname $clientname eq ''
Truth table0%0/3
A || B
A= $clientnameB= $clientname eq ''
ABresult
1X1
011
000
95 0 croak("Clientname not defined!");
96 }
97 0 $self->{clientname} = $clientname;
98  
99 0 $self->{authtoken} = encode_base64($username, '') . ':' . encode_base64($password, '');
100  
101 0 ▶ if(!defined($iscaching)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $iscaching) 0 0 0
102 0 $iscaching = 0;
103 }
104 0 $self->{iscaching} = $iscaching;
105  
106 0 ▶ if($self->{iscaching}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"iscaching"}) 0 0 0
107 0 $self->{cache} = {};
108 }
109  
110 0 $self->{needreconnect} = 1;
111 0 $self->{inlines} = [];
112 0 $self->{firstconnect} = 1;
113  
114 # Maximum number of seconds any synchronous request (retrieve, flush, keylist,
115 # clientlist) will wait for a server response before giving up. Without this
116 # cap a hung or silently-dropped server connection turned into an indefinite
117 # client-side hang. Callers can override this after construction.
118 0 $self->{requesttimeout} = 30;
119  
120 # Maximum time to spend trying to drain the outbuffer when the server isn't
121 # reading (kernel send buffer full -> syswrite returns EAGAIN repeatedly).
122 # Tracked in doNetwork() via writefailtime; on expiry the connection is
123 # marked for reconnect. This is the symmetric counterpart of the server's
124 # stalledwritetimeout and is what stops the many "send loop" sites that
125 # otherwise spin forever when the server is alive but not consuming.
126 0 $self->{stalledwritetimeout} = 30;
127  
128 # TCP connect timeout. Without this, a TCP connect to an unreachable host
129 # waits for the kernel SYN-retransmit timeout (~75 s on Linux) before
130 # giving up. Defaulted generously so a transiently-busy server (full
131 # listen backlog, scheduler delays under load) or a slow link doesn't
132 # produce false-positive failures. Unix-domain sockets are not affected —
133 # connect there fails or succeeds immediately.
134 0 $self->{connecttimeout} = 60;
135  
136 # TLS handshake timeout. Implemented via deferred handshake + IO::Select
137 # polling so we don't depend on SIGALRM, which is unsafe in applications
138 # that already use signals or have their own event loop. Defaulted
139 # generously to tolerate slow servers (RSA signing on a small CPU is not
140 # instant) and high-RTT links.
141 0 $self->{ssltimeout} = 60;
142  
143 0 $self->{memcached_compatibility} = 0;
144  
145 $self->{remembrancenames} = [
146 0 'Ivy Bdubs',
147 'Terry Pratchett',
148 'Sven Guckes',
149 'Sheila', # faithful four-legged family member of @NightStorm_KPC
150 'Dolly Parton',
151 ];
152 0 $self->{remembranceinterval} = 3600; # One hour
153 0 $self->{nextremembrance} = time + $self->{remembranceinterval};
154  
155 0 $self->reconnect();
156  
157 0 return;
158 }
159  
160 ▶sub _safeCloseSocket($self, $socket) {
Subroutine _safeCloseSocket: not called CC=3 SCAR=24.8
161 0 ▶ return if(!defined($socket));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $socket 0 0 0
162 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
163 0 ▶ if(ref($socket) =~ /^IO::Socket::SSL/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($socket) =~ /^IO::Socket::SSL/u) { } 0 0 0
164 0 $socket->close(SSL_no_shutdown => 1, SSL_fast_shutdown => 1);
165 } else {
166 0 $socket->close;
167 }
168 };
169 0 return;
170 }
171  
172 # Perform a TLS handshake with our own deadline, without using SIGALRM.
173 # Strategy: put the socket into non-blocking mode up front, defer the handshake
174 # at start_SSL (SSL_startHandshake => 0), then drive connect_SSL() ourselves and
175 # poll with IO::Select::can_read / can_write between attempts. This is safe in
176 # applications that already use signals or have their own event loop.
177 #
178 # Returns the wrapped socket on success, or undef on timeout / handshake error.
179 # On failure, the caller is responsible for closing the original $socket.
180 ▶sub _sslHandshakeWithTimeout($self, $socket, $timeout) {
Subroutine _sslHandshakeWithTimeout: not called CC=6 SCAR=37.4
181 0 $socket->blocking(0);
182 0 my $wrapped = IO::Socket::SSL->start_SSL($socket,
183 SSL_verify_mode => SSL_VERIFY_NONE,
184 SSL_startHandshake => 0,
185 );
186 0 ▶ if(!$wrapped) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wrapped) 0 0 0
187 0 return;
188 }
189  
190 0 my $deadline = time + $timeout;
191 0 my $select = IO::Select->new($wrapped);
192 0 while(1) {
193 0 ▶ if($wrapped->connect_SSL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wrapped->connect_SSL) 0 0 0
194 0 return $wrapped;
195 }
196 # connect_SSL returned false; figure out why and (maybe) wait for the
197 # right kind of socket readiness.
198 0 my $remaining = $deadline - time;
199 0 ▶ if($remaining <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining <= 0) 0 0 0
200 0 return;
201 }
202 0 ▶ if($SSL_ERROR == SSL_WANT_READ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Net::Clacks::Client::SSL_ERROR == 96848840063488) { } 0 0 0
elsif ($Net::Clacks::Client::SSL_ERROR == 96848840063584) { } 0 0 0
203 0 $select->can_read($remaining);
204 } elsif($SSL_ERROR == SSL_WANT_WRITE) {
205 0 $select->can_write($remaining);
206 } else {
207 # Real handshake error (cert mismatch, protocol error, etc.).
208 0 return;
209 }
210 }
211 }
212  
213 ▶sub reconnect($self) {
Subroutine reconnect: not called CC=13 SCAR=52.0
Pod documented
214 # Tear the old connection down: remove from the selector first (otherwise the
215 # selector keeps the handle alive and the OS FD stays open), then explicitly
216 # close. Without the explicit close, server-side FDs accumulate until the
217 # ping timeout — under reconnect churn the server can run out of FDs.
218 0 ▶ if(defined($self->{selector}) && defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"selector"} and defined $self->{"socket"}) 0 0 0
Condition0%0/3
defined $self->{"selector"} and defined $self->{"socket"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"selector"} and defined $self->{"socket"}
defined $self->{"selector"} defined $self->{"socket"}
Truth table0%0/3
A and B
A= defined $self->{"selector"}B= defined $self->{"socket"}
ABresult
0X0
100
111
219 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
220 0 $self->{selector}->remove($self->{socket});
221 };
222 }
223 0 undef $self->{selector};
224 0 ▶ if(defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"socket"}) 0 0 0
225 0 $self->_safeCloseSocket($self->{socket});
226 0 delete $self->{socket};
227 }
228  
229 0 ▶ if(!$self->{firstconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"firstconnect"}) 0 0 0
230 # Not our first connection (=real reconnect).
231 # wait a short random time before reconnecting. In case all
232 # clients got disconnected, we want to avoid having all clients reconnect
233 # at the exact same time
234 0 my $waittime = rand(4000)/1000;
235 0 sleep($waittime);
236 }
237  
238 0 my $socket;
239 0 ▶ if(defined($self->{server}) && defined($self->{port})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'server'} and defined $self->{'port'}) { } 0 0 0
elsif (defined $self->{'socketpath'}) { } 0 0 0
Condition0%0/3
defined $self->{"server"} and defined $self->{"port"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"server"} and defined $self->{"port"}
defined $self->{"server"} defined $self->{"port"}
Truth table0%0/3
A and B
A= defined $self->{"server"}B= defined $self->{"port"}
ABresult
0X0
100
111
240 # Cap TCP connect at $self->{connecttimeout} so an unreachable host
241 # doesn't make us wait for the kernel SYN-retransmit timeout (~75s).
242 $socket = IO::Socket::IP->new(
243 PeerHost => $self->{server},
244 PeerPort => $self->{port},
245 Type => SOCK_STREAM,
246 Timeout => $self->{connecttimeout},
247 0 ▶ ) or croak("Failed to connect to Clacks TCP message service: $ERRNO");
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::IP"->new("PeerHost", $self->{"server"}, "PeerPort", $self->{"port"}, "Type", 1, "Timeout", $self->{"connecttimeout"}) or croak("Failed to connect to Clacks TCP message service: $Net::Clacks::Client::ERRNO") 0 0 0
248 } elsif(defined($self->{socketpath})) {
249 $socket = IO::Socket::UNIX->new(
250 Peer => $self->{socketpath},
251 0 ▶ Type => SOCK_STREAM,
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::UNIX"->new("Peer", $self->{"socketpath"}, "Type", 1) or croak("Failed to connect to Clacks Unix Domain Socket message service: $Net::Clacks::Client::ERRNO") 0 0 0
252 ) or croak("Failed to connect to Clacks Unix Domain Socket message service: $ERRNO");
253 } else {
254 0 croak("Neither TCP nor Unix domain socket specified. Don't know where to connect to.");
255 }
256  
257 0 ▶ if(ref $socket ne 'IO::Socket::UNIX') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $socket ne "IO::Socket::UNIX") 0 0 0
258 # ONLY USE SSL WHEN RUNNING OVER THE NETWORK
259 # There is simply no point in running it over a local socket.
260 # Use the non-SIGALRM-based handshake helper so we get a hard deadline
261 # without interfering with the embedding application's signal handling.
262 0 my $sslsocket = $self->_sslHandshakeWithTimeout($socket, $self->{ssltimeout});
263 0 ▶ if(!defined($sslsocket)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sslsocket) 0 0 0
264 # Release the underlying TCP FD — otherwise a flapping server /
265 # cert mismatch / handshake hang leaks one FD per reconnect attempt.
266 0 $self->_safeCloseSocket($socket);
267 0 croak("Can't use SSL (handshake failed or timed out after $self->{ssltimeout}s): " . $SSL_ERROR);
268 }
269 0 $socket = $sslsocket;
270 }
271  
272 # _sslHandshakeWithTimeout already left the TCP+SSL socket non-blocking.
273 # For Unix-domain sockets we still need to set non-blocking explicitly.
274 0 $socket->blocking(0);
275  
276 0 $self->{socket} = $socket;
277 0 $self->{selector} = IO::Select->new($self->{socket});
278 0 $self->{failcount} = 0;
279 0 $self->{writefailtime} = 0; # Stalled-write streak start; 0 = no streak in progress
280 0 $self->{lastping} = time;
281 0 $self->{inbuffer} = '';
282 0 $self->{incharbuffer} = [];
283 0 $self->{outbuffer} = '';
284 0 $self->{serverinfo} = 'UNKNOWN';
285 0 $self->{needreconnect} = 0;
286 0 $self->{firstline} = 1;
287 0 $self->{headertimeout} = time + 15;
288  
289 # Do *not* nuke "inlines" array, since it may hold "QUIT" messages that the client wants to handle, for example, to re-issue
290 # "LISTEN" commands.
291 # $self->{inlines} = ();
292  
293 0 ▶ if($self->{firstconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'firstconnect'}) { } 0 0 0
294 0 $self->{firstconnect} = 0;
295 } else {
296 0 push @{$self->{inlines}}, "RECONNECTED";
297 }
298  
299 # Startup "handshake". As everything else, this is asyncronous, both server and
300 # client send their respective version strings and then wait to recieve their counterparts
301 # Also, this part is REQUIRED, just to make sure we actually speek to CLACKS protocol
302 0 $self->{outbuffer} .= 'CLACKS ' . $self->{clientname} . "\r\n";
303 0 $self->{outbuffer} .= 'OVERHEAD A ' . $self->{authtoken} . "\r\n";
304 0 $self->doNetwork();
305  
306 0 return;
307 }
308  
309 ▶sub activate_memcached_compat($self) {
Subroutine activate_memcached_compat: not called CC=1 SCAR=6.9
Pod documented
310 0 $self->{memcached_compatibility} = 1;
311 0 return;
312 }
313  
314 ▶sub getRawSocket($self) {
Subroutine getRawSocket: not called CC=2 SCAR=17.9
Pod documented
315 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
316 0 $self->reconnect();
317 }
318  
319 0 return $self->{socket};
320 }
321  
322 0 ▶sub doNetwork($self, $readtimeout = 0) {
Subroutine doNetwork: not called CC=31 SCAR=69.0
Pod documented
323 0 ▶ if(!defined($readtimeout)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $readtimeout) 0 0 0
324 # Don't wait
325 0 $readtimeout = 0;
326 }
327  
328 # Negative read timeout means "send only"
329  
330 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
331 0 $self->reconnect();
332 }
333  
334 0 ▶ if($self->{nextremembrance} && time > $self->{nextremembrance}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"nextremembrance"} and &Net::Clacks::Client::time() > $self->{"nextremembrance"}) 0 0 0
Condition0%0/3
$self->{"nextremembrance"} and &Net::Clacks::Client::time() > $self->{"nextremembrance"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"nextremembrance"} and &Net::Clacks::Client::time() > $self->{"nextremembrance"}
$self->{"nextremembrance"} &Net::Clacks::Client::time() > $self->{"nextremembrance"}
Truth table0%0/3
A and B
A= $self->{"nextremembrance"}B= &Net::Clacks::Client::time() > $self->{"nextremembrance"}
ABresult
0X0
100
111
335 # A person is not dead while their name is still spoken.
336 0 $self->{nextremembrance} = time + $self->{remembranceinterval} + int(rand($self->{remembranceinterval} / 10));
337 0 my $neverforget = $self->{remembrancenames}->[rand @{$self->{remembrancenames}}];
338 0 $self->{outbuffer} .= 'OVERHEAD GNU ' . $neverforget . "\r\n";
339 }
340  
341 # doNetwork interleaves handling incoming and outgoing traffic.
342 # This is only relevant on slow links.
343 #
344 # It returns even if the outgoing or incoming buffers are not empty
345 # (meaning that partially buffered data can exists). This way we use the
346 # available bandwidth without blocking unduly the application (we assume it's a realtime
347 # application with multiple things going on at the same time)-
348 #
349 # The downside of this is that doNetwork() needs to be called on a regular basis and sending
350 # and recieving might be delayed until the next cycle. This delay can be minimized by simply
351 # not transfering huge values over clacks, but instead using it the way it was intended to be used:
352 # Small variables can be SET directly by clacks, huge datasets should be stored in the
353 # database and the recievers only NOTIFY'd that a change has taken place.
354 #
355 # The big exception here is the here is the ClacksCache part of the story. These functions
356 # call doNetwork() in a loop until the outbuffer is empty. And depending on requirement, they
357 # KEEP on calling doNetwork() until the answer is recieved. This makes ClacksCache functions
358 # syncronous and causes some delay in the calling function. But doing these asyncronous will
359 # cause more headaches, maybe even leading up to insanity. Believe me, i tried, but those pink
360 # elephants stomping about my rubber-padded room are such a distraction...
361  
362 0 my $workCount = 0;
363 0 my $now = time;
364  
365 0 ▶ if(length($self->{outbuffer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"outbuffer"}) 0 0 0
366 0 my $brokenpipe = 0;
367 0 my $writeok = 0;
368 0 my $written;
369 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
370 0 ▶ local $SIG{PIPE} = sub { $brokenpipe = 1; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
371 0 $written = syswrite($self->{socket}, $self->{outbuffer});
372 0 $writeok = 1;
373 };
374  
375 0 ▶ if($brokenpipe || !$writeok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($brokenpipe or not $writeok) 0 0 0
Condition0%0/3
$brokenpipe or not $writeok
l!l&&r!l&&!r
000
MC/DC0%0/2
$brokenpipe or not $writeok
$brokenpipe not $writeok
Truth table0%0/3
A or not(B)
A= $brokenpipeB= not $writeok
ABresult
1X1
011
000
376 0 $self->{needreconnect} = 1;
377 0 push @{$self->{inlines}}, "TIMEOUT";
378 0 return;
379 }
380  
381 0 ▶ if(defined($written) && $written > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $written and $written > 0) { } 0 0 0
Condition0%0/3
defined $written and $written > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $written and $written > 0
defined $written $written > 0
Truth table0%0/3
A and B
A= defined $writtenB= $written > 0
ABresult
0X0
100
111
382 # Forward progress: clear the stalled-write streak.
383 0 $self->{writefailtime} = 0;
384 0 $workCount += $written;
385 0 ▶ if(length($self->{outbuffer}) == $written) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'outbuffer'} == $written) { } 0 0 0
386 0 $self->{outbuffer} = '';
387 } else {
388 0 $self->{outbuffer} = substr($self->{outbuffer}, $written);
389 }
390 } else {
391 # No bytes written. Either undef ($! = EAGAIN/EWOULDBLOCK — kernel
392 # send buffer full because the server isn't reading) or a defined-
393 # but-zero return. Start (or continue) the stalled-write streak;
394 # if we go stalledwritetimeout seconds without a single successful
395 # byte while we have data queued, mark the connection for reconnect.
396 # This is the global counterpart to the server-side stall check
397 # and is what stops the various "send loop" call sites
398 # (notify/set/store/...) from spinning forever when the server is
399 # alive but not consuming.
400 0 ▶ if($self->{writefailtime} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'writefailtime'} == 0) { } 0 0 0
elsif ($now - $self->{'writefailtime'} > $self->{'stalledwritetimeout'}) { } 0 0 0
401 0 $self->{writefailtime} = $now;
402 } elsif(($now - $self->{writefailtime}) > $self->{stalledwritetimeout}) {
403 0 $self->{needreconnect} = 1;
404 0 push @{$self->{inlines}}, "TIMEOUT";
405 0 return;
406 }
407 }
408 }
409  
410 0 ▶ if($readtimeout < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($readtimeout < 0) 0 0 0
411 0 return 1;
412 }
413
414  
415 {
416 0 my @temp;
417 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
418 0 @temp = $self->{selector}->can_read($readtimeout);
419 };
420 0 ▶ if(scalar @temp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @temp == 0) 0 0 0
421 # Timeout
422 0 return $workCount;
423 }
424 }
425  
426 0 my $totalread = 0;
427 0 while(1) {
428 0 my $buf;
429 my $bytes;
430 0 my $readok = 0;
431 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
432 0 $bytes = sysread($self->{socket}, $buf, 10_000); # Read in at most 10kB at once
433 0 $readok = 1;
434 };
435 0 ▶ if(!$readok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($readok) 0 0 0
436 0 $self->{needreconnect} = 1;
437 0 push @{$self->{inlines}}, "TIMEOUT";
438 0 return;
439 }
440 0 ▶ if(defined($bytes) && $bytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes and $bytes == 0) 0 0 0
Condition0%0/3
defined $bytes and $bytes == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bytes and $bytes == 0
defined $bytes $bytes == 0
Truth table0%0/3
A and B
A= defined $bytesB= $bytes == 0
ABresult
0X0
100
111
441 # sysread returns 0 on EOF — the peer cleanly closed the socket
442 # (FIN received). The previous code ignored the return value and
443 # only relied on the failcount-after-many-empty-reads heuristic
444 # below, which took ~250ms to trigger and was unnecessarily fuzzy.
445 0 $self->{needreconnect} = 1;
446 0 push @{$self->{inlines}}, "TIMEOUT";
447 0 return;
448 }
449 0 ▶ if(defined($bytes) && $bytes > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes and $bytes > 0) 0 0 0
Condition0%0/3
defined $bytes and $bytes > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bytes and $bytes > 0
defined $bytes $bytes > 0
Truth table0%0/3
A and B
A= defined $bytesB= $bytes > 0
ABresult
0X0
100
111
450 0 $totalread += $bytes;
451 0 push @{$self->{incharbuffer}}, split//, $buf;
452 0 next;
453 }
454 # $bytes is undef -> $! = EAGAIN/EWOULDBLOCK or a real error.
455 # Falls through to the totalread / failcount check below.
456 0 last;
457 }
458
459 # Check if we actually got data after checking with can_read() first
460 0 ▶ if($totalread) {
Branch: 1 branch
BranchTrueFalseTotal
if ($totalread) { } 0 0 0
461 0 $self->{failcount} = 0;
462 } else {
463 # This should normally not happen, but thanks to SSL, it sometimes does
464 # We ignore single instances of those but disconnect if many happen in a row
465 0 $self->{failcount}++;
466 0 sleep(0.05);
467
468 0 ▶ if($self->{failcount} > 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"failcount"} > 5) 0 0 0
469 0 $self->{needreconnect} = 1;
470 0 return;
471 }
472 }
473 0 ▶ while(@{$self->{incharbuffer}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"incharbuffer"};}) 0 0 0
474 0 my $char = shift @{$self->{incharbuffer}};
475 0 $workCount++;
476 0 ▶ if($char eq "\r") {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq "\r") { } 0 0 0
elsif ($char eq "\n") { } 0 0 0
477 0 next;
478 } elsif($char eq "\n") {
479 0 ▶ if($self->{inbuffer} eq 'NOP') { # Just drop "No OPerations" packets, only used by server to
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inbuffer"} eq "NOP") 0 0 0
480 # verify that the connection is still active
481 #$self->{firstline}
482 0 $self->{inbuffer} = '';
483 0 next;
484 }
485  
486 0 ▶ if($self->{firstline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"firstline"}) 0 0 0
487 0 ▶ if($self->{inbuffer} !~ /^CLACKS\ /) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'inbuffer'} =~ /^CLACKS\ /u) { } 0 0 0
488 # Whoops, not a clacks server or something gone wrong with the protocol
489 0 $self->{needreconnect} = 1;
490 0 return 0;
491 } else {
492 0 $self->{firstline} = 0;
493 }
494 }
495  
496 0 push @{$self->{inlines}}, $self->{inbuffer};
497 0 $self->{inbuffer} = '';
498 } else {
499 0 $self->{inbuffer} .= $char;
500 }
501 }
502  
503 0 ▶ if($self->{firstline} && $self->{headertimeout} < time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"firstline"} and $self->{"headertimeout"} < &Net::Clacks::Client::time()) 0 0 0
Condition0%0/3
$self->{"firstline"} and $self->{"headertimeout"} < &Net::Clacks::Client::time()
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"firstline"} and $self->{"headertimeout"} < &Net::Clacks::Client::time()
$self->{"firstline"} $self->{"headertimeout"} < &Net::Clacks::Client::time()
Truth table0%0/3
A and B
A= $self->{"firstline"}B= $self->{"headertimeout"} < &Net::Clacks::Client::time()
ABresult
0X0
100
111
504 0 $self->{needreconnect} = 1;
505 0 return 0;
506 }
507  
508 0 return $workCount;
509 }
510  
511 4 my %overheadflags = (
512 A => "auth_token", # Authentication token
513 O => "auth_ok", # Authentication OK
514 F => "auth_failed", # Authentication FAILED
515  
516 E => 'error_message', # Server to client error message
517  
518 C => "close_all_connections",
519 D => "discard_message",
520 G => "forward_message",
521 I => "set_interclacks_mode", # value: true/false, disables 'G' and 'U'
522 M => "informal_message", # informal message
523 N => "no_logging",
524 S => "shutdown_service", # value: positive number (number in seconds before shutdown). If interclacks clients are present, should be high
525 # enough to flush all buffers to them
526 U => "return_to_sender",
527 Z => "no_flags", # Only sent when no other flags are set
528 );
529  
530 ▶sub getNext($self) {
Subroutine getNext: not called CC=20 SCAR=60.4
Pod documented
531 # Recieve next incoming message (if any)
532  
533 restartgetnext:
534 0 my $line = shift @{$self->{inlines}};
535  
536 0 ▶ if(!defined($line)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
537 0 return;
538 }
539  
540 0 my %data;
541 #print STDERR "> $line\n";
542 0 ▶ if($line =~ /^NOTIFY\ (.+)/) {
Branch: 8 branches
BranchTrueFalseTotal
if ($line =~ /^NOTIFY\ (.+)/u) { } 0 0 0
elsif ($line =~ /^SET\ (.+?)\=(.*)/u) { } 0 0 0
elsif ($line =~ /^CLACKS\ (.+)/u) { } 0 0 0
elsif ($line =~ /^DEBUG\ (.+?)\=(.*)/u) { } 0 0 0
elsif ($line =~ /^QUIT/u) { } 0 0 0
elsif ($line =~ /^TIMEOUT/u) { } 0 0 0
elsif ($line =~ /^RECONNECTED/u) { } 0 0 0
elsif ($line =~ /^OVERHEAD\ (.+?)\ (.+)/u) { } 0 0 0
543 0 %data = (
544 type => 'notify',
545 name => $1,
546 );
547 } elsif($line =~ /^SET\ (.+?)\=(.*)/) {
548 0 %data = (
549 type => 'set',
550 name => $1,
551 data => $2,
552 );
553 } elsif($line =~ /^CLACKS\ (.+)/) {
554 0 %data = (
555 type => 'serverinfo',
556 data => $1,
557 );
558 } elsif($line =~ /^DEBUG\ (.+?)\=(.*)/) {
559 0 %data = (
560 type => 'debug',
561 host => $1,
562 command => $2,
563 );
564 } elsif($line =~ /^QUIT/) {
565 0 %data = (
566 type => 'disconnect',
567 data => 'quit',
568 );
569 0 $self->{needreconnect} = 1;
570 } elsif($line =~ /^TIMEOUT/) {
571 0 %data = (
572 type => 'disconnect',
573 data => 'timeout',
574 );
575 0 $self->{needreconnect} = 1;
576 } elsif($line =~ /^RECONNECTED/) {
577 0 %data = (
578 type => 'reconnected',
579 data => 'send your LISTEN requests again',
580 );
581 } elsif($line =~ /^OVERHEAD\ (.+?)\ (.+)/) {
582 # Minimal handling of OVERHEAD flags
583 0 my ($flags, $value) = ($1, $2);
584 0 my @flagparts = split//, $flags;
585 0 my %parsedflags;
586 0 foreach my $key (sort keys %overheadflags) {
587 0 ▶ if(contains($key, \@flagparts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (contains($key, \@flagparts)) { } 0 0 0
588 0 $parsedflags{$overheadflags{$key}} = 1;
589 } else {
590 0 $parsedflags{$overheadflags{$key}} = 0;
591 }
592 }
593  
594 0 ▶ if($parsedflags{auth_ok}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($parsedflags{'auth_ok'}) { } 0 0 0
elsif ($parsedflags{'error_message'}) { } 0 0 0
elsif ($parsedflags{'auth_failed'}) { } 0 0 0
elsif ($parsedflags{'informal_message'}) { } 0 0 0
595 #print STDERR "Clacks AUTH OK\n";
596 0 goto restartgetnext; # try the next message
597 } elsif($parsedflags{error_message}) {
598 0 %data = (
599 type => 'error_message',
600 data => $value,
601 );
602 } elsif($parsedflags{auth_failed}) {
603 0 croak("Clacks Authentication failed!");
604 } elsif($parsedflags{informal_message}) {
605 0 ▶ if($parsedflags{forward_message}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsedflags{"forward_message"}) 0 0 0
606 0 %data = (
607 type => 'informal',
608 data => $value,
609 );
610 }
611 0 ▶ if($parsedflags{return_to_sender}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsedflags{"return_to_sender"}) 0 0 0
612 0 my $uturn = 'OVERHEAD M';
613 0 ▶ if($parsedflags{no_logging}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsedflags{"no_logging"}) 0 0 0
614 0 $uturn .= 'N';
615 }
616 0 $uturn .= ' ' . $value;
617 0 $self->{outbuffer} .= $uturn;
618 }
619 }
620  
621 } else {
622 # UNKNOWN, ignore
623 0 goto restartgetnext; # try the next message
624 }
625  
626 0 ▶ if(!defined($data{type})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data{"type"}) 0 0 0
627 0 return;
628 }
629  
630 0 $data{rawline} = $line;
631  
632 0 return \%data;
633 }
634  
635  
636 ▶sub ping($self) {
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
637 0 ▶ if($self->{lastping} < (time - 120)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lastping"} < &Net::Clacks::Client::time() - 120) 0 0 0
638 # Only send a ping every 120 seconds or less
639 0 $self->{outbuffer} .= "PING\r\n";
640 0 $self->{lastping} = time;
641 }
642  
643 0 return;
644 }
645  
646 ▶sub disablePing($self) {
Subroutine disablePing: not called CC=1 SCAR=6.9
Pod documented
647 0 $self->{outbuffer} .= "NOPING\r\n";
648  
649 0 return;
650 }
651  
652  
653 ▶sub notify($self, $varname) {
Subroutine notify: not called CC=7 SCAR=40.3
Pod documented
654 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
655 0 carp("varname not defined!");
656 0 return;
657 }
658  
659 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
660 0 $self->reconnect;
661 }
662  
663 0 $self->{outbuffer} .= "NOTIFY $varname\r\n";
664  
665 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
666 0 while(1) {
667 0 $self->doNetwork();
668 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
669 # Nothing we can do, really...
670 0 return;
671 }
672 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
673 0 usleep(1000);
674 }
675 0 $self->autohandle_messages();
676 }
677  
678 0 return;
679 }
680  
681 0 ▶sub set($self, $varname, $value, $forcesend = 0) { ## no critic (NamingConventions::ProhibitAmbiguousNames)
Subroutine set: not called CC=15 SCAR=54.8
Pod documented
682 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
683 0 carp("varname not defined!");
684 0 return;
685 }
686 0 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
687 0 carp("value not defined!");
688 0 return;
689 }
690  
691 0 ▶ if(!defined($forcesend)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $forcesend) 0 0 0
692 0 $forcesend = 0;
693 }
694  
695 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
696 0 $self->reconnect;
697 }
698  
699 # Handle caching to lower output volumne
700 0 ▶ if($self->{iscaching} && !$forcesend && defined($self->{cache}->{$varname}) && $self->{cache}->{$varname} eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"iscaching"} and not $forcesend and defined $self->{"cache"}{$varname} and $self->{"cache"}{$varname} eq $value) 0 0 0
Condition0%0/9
$self->{"iscaching"} and not $forcesend and defined $self->{"cache"}{$varname} and $self->{"cache"}{$varname} eq $value
!ll&&!rl&&r
000
$self->{"iscaching"} and not $forcesend and defined $self->{"cache"}{$varname}
!ll&&!rl&&r
000
$self->{"iscaching"} and not $forcesend
!ll&&!rl&&r
000
MC/DC0%0/4
$self->{"iscaching"} and not $forcesend and defined $self->{"cache"}{$varname} and $self->{"cache"}{$varname} eq $value
$self->{"iscaching"} not $forcesend defined $self->{"cache"}{$varname} $self->{"cache"}{$varname} eq $value
Truth table0%0/5
A and not(B) and C and D
A= $self->{"iscaching"}B= not $forcesendC= defined $self->{"cache"}{$varname}D= $self->{"cache"}{$varname} eq $value
ABCDresult
0XXX0
10XX0
110X0
11100
11111
701 # Already the same value send
702 0 return;
703 }
704  
705 0 ▶ if($self->{iscaching}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"iscaching"}) 0 0 0
706 0 $self->{cache}->{$varname} = $value;
707 }
708  
709 0 $self->{outbuffer} .= "SET $varname=$value\r\n";
710  
711 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
712 0 while(1) {
713 0 $self->doNetwork();
714 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
715 # Nothing we can do, really...
716 0 return;
717 }
718 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
719 0 usleep(1000);
720 }
721  
722 0 $self->autohandle_messages();
723 }
724  
725 0 return;
726 }
727  
728 ▶sub listen($self, $varname) { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
Subroutine listen: not called CC=4 SCAR=30.0
Pod documented
729 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
730 0 carp("varname not defined!");
731 0 return;
732 }
733  
734 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
735 0 $self->reconnect;
736 }
737  
738 0 $self->{outbuffer} .= "LISTEN $varname\r\n";
739  
740 0 return;
741 }
742  
743 ▶sub unlisten($self, $varname) {
Subroutine unlisten: not called CC=4 SCAR=30.0
Pod documented
744 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
745 0 carp("varname not defined!");
746 0 return;
747 }
748  
749 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
750 0 $self->reconnect;
751 }
752  
753 0 $self->{outbuffer} .= "UNLISTEN $varname\r\n";
754  
755 0 return;
756 }
757  
758 ▶sub setMonitormode($self, $active) {
Subroutine setMonitormode: not called CC=4 SCAR=30.0
Pod documented
759 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
760 0 $self->reconnect;
761 }
762  
763 0 ▶ if(!defined($active) || !$active) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $active && $active) { } 0 0 0
Condition0%0/3
defined $active && $active
!ll&&!rl&&r
000
MC/DC0%0/2
defined $active && $active
defined $active $active
Truth table0%0/3
A && B
A= defined $activeB= $active
ABresult
0X0
100
111
764 0 $self->{outbuffer} .= "UNMONITOR\r\n";
765 } else {
766 0 $self->{outbuffer} .= "MONITOR\r\n";
767 }
768  
769 0 return;
770 }
771  
772 ▶sub getServerinfo($self) {
Subroutine getServerinfo: not called CC=1 SCAR=6.9
Pod documented
773 0 return $self->{serverinfo};
774 }
775  
776 # ---------------- ClackCache handling --------------------
777 # ClacksCache handling always implies doNetwork()
778 # Also, we do NOT use the caching system used for SET
779 ▶sub store($self, $varname, $value) {
Subroutine store: not called CC=8 SCAR=42.8
Pod documented
780 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
781 0 carp("varname not defined!");
782 0 return;
783 }
784 0 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
785 0 carp("value not defined!");
786 0 return;
787 }
788  
789 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
790 0 $self->reconnect;
791 }
792  
793 0 $self->{outbuffer} .= "STORE $varname=$value\r\n";
794 0 while(1) {
795 0 $self->doNetwork();
796 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
797 # Nothing we can do, really...
798 0 return;
799 }
800 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
801 0 usleep(1000);
802 }
803  
804 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
805 0 $self->autohandle_messages();
806 }
807  
808 0 return;
809 }
810  
811 ▶sub retrieve($self, $varname) {
Subroutine retrieve: not called CC=14 SCAR=53.5
Pod documented
812 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
813 0 carp("varname not defined!");
814 0 return;
815 }
816  
817 0 my $value;
818  
819 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
820 0 $self->reconnect;
821 }
822  
823 0 $self->{outbuffer} .= "RETRIEVE $varname\r\n";
824  
825 # Make sure we send everything
826 0 while(1) {
827 0 $self->doNetwork();
828 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
829 # Nothing we can do, really...
830 0 return;
831 }
832 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
833 }
834  
835 # Now, wait for the answer
836 0 my $answerline;
837 0 my $deadline = time + $self->{requesttimeout};
838 0 while(1) {
839 0 ▶ if(time > $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Net::Clacks::Client::time() > $deadline) 0 0 0
840 # Server didn't answer in time — assume the connection is wedged so
841 # the next call reconnects rather than hanging here forever.
842 0 carp("retrieve($varname) timed out after $self->{requesttimeout}s; marking connection for reconnect");
843 0 $self->{needreconnect} = 1;
844 0 return;
845 }
846 0 $self->doNetwork(0.5);
847 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
848 # Nothing we can do, really...
849 0 return;
850 }
851 0 ▶ for(my $i = 0; $i < scalar @{$self->{inlines}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"inlines"};}) 0 0 0
852 0 ▶ if($self->{inlines}->[$i] =~ /^RETRIEVED\ $varname/ || $self->{inlines}->[$i] =~ /^NOTRETRIEVED\ $varname/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^RETRIEVED\ $varname/u or $self->{"inlines"}[$i] =~ /^NOTRETRIEVED\ $varname/u) 0 0 0
Condition0%0/3
$self->{"inlines"}[$i] =~ /^RETRIEVED\ $varname/u or $self->{"inlines"}[$i] =~ /^NOTRETRIEVED\ $varname/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"inlines"}[$i] =~ /^RETRIEVED\ $varname/u or $self->{"inlines"}[$i] =~ /^NOTRETRIEVED\ $varname/u
$self->{"inlines"}[$i] =~ /^RETRIEVED\ $varname/u $self->{"inlines"}[$i] =~ /^NOTRETRIEVED\ $varname/u
Truth table0%0/3
A or B
A= $self->{"inlines"}[$i] =~ /^RETRIEVED\ $varname/uB= $self->{"inlines"}[$i] =~ /^NOTRETRIEVED\ $varname/u
ABresult
1X1
011
000
853 # Remove the answer from in in-queue directly (out of sequence), because we don't need in in the getNext function
854 0 $answerline = splice @{$self->{inlines}}, $i, 1;
855 0 last;
856 }
857 }
858 0 ▶ last if(defined($answerline));
Branch: 1 branch
BranchTrueFalseTotal
if defined $answerline 0 0 0
859 }
860  
861 0 ▶ if($answerline =~ /^RETRIEVED\ (.+?)\=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($answerline =~ /^RETRIEVED\ (.+?)\=(.*)/u) 0 0 0
862 0 my ($key, $val) = ($1, $2);
863 0 ▶ if($key ne $varname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne $varname) 0 0 0
864 #print STDERR "Retrieved clacks key $key does not match requested varname $varname!\n";
865 0 return;
866 }
867 0 return $val;
868 }
869  
870 # No matching key
871 0 return;
872 }
873  
874 ▶sub remove($self, $varname) {
Subroutine remove: not called CC=7 SCAR=40.3
Pod documented
875 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
876 0 carp("varname not defined!");
877 0 return;
878 }
879  
880 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
881 0 $self->reconnect;
882 }
883  
884 0 $self->{outbuffer} .= "REMOVE $varname\r\n";
885 0 while(1) {
886 0 $self->doNetwork();
887 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
888 # Nothing we can do, really...
889 0 return;
890 }
891 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
892 0 usleep(1000);
893 }
894  
895 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
896 0 $self->autohandle_messages();
897 }
898  
899 0 return;
900 }
901  
902 0 ▶sub increment($self, $varname, $stepsize = '') {
Subroutine increment: not called CC=10 SCAR=47.0
Pod documented
903 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
904 0 carp("varname not defined!");
905 0 return;
906 }
907  
908 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
909 0 $self->reconnect;
910 }
911  
912 0 ▶ if(!defined($stepsize) || $stepsize eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stepsize or $stepsize eq '') { } 0 0 0
Condition0%0/3
not defined $stepsize or $stepsize eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $stepsize or $stepsize eq ""
not defined $stepsize $stepsize eq ""
Truth table0%0/3
not(A) or B
A= not defined $stepsizeB= $stepsize eq ""
ABresult
1X1
011
000
913 0 $self->{outbuffer} .= "INCREMENT $varname\r\n";
914 } else {
915 0 $stepsize = 0 + $stepsize;
916 0 $self->{outbuffer} .= "INCREMENT $varname=$stepsize\r\n";
917 }
918  
919 0 while(1) {
920 0 $self->doNetwork();
921 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
922 # Nothing we can do, really...
923 0 return;
924 }
925 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
926 0 usleep(1000);
927 }
928  
929 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
930 0 $self->autohandle_messages();
931 }
932  
933 0 return;
934 }
935  
936 0 ▶sub decrement($self, $varname, $stepsize = '') {
Subroutine decrement: not called CC=10 SCAR=47.0
Pod documented
937 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
938 0 carp("varname not defined!");
939 0 return;
940 }
941  
942 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
943 0 $self->reconnect;
944 }
945  
946 0 ▶ if(!defined($stepsize) || $stepsize eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stepsize or $stepsize eq '') { } 0 0 0
Condition0%0/3
not defined $stepsize or $stepsize eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $stepsize or $stepsize eq ""
not defined $stepsize $stepsize eq ""
Truth table0%0/3
not(A) or B
A= not defined $stepsizeB= $stepsize eq ""
ABresult
1X1
011
000
947 0 $self->{outbuffer} .= "DECREMENT $varname\r\n";
948 } else {
949 0 $stepsize = 0 + $stepsize;
950 0 $self->{outbuffer} .= "DECREMENT $varname=$stepsize\r\n";
951 }
952 0 while(1) {
953 0 $self->doNetwork();
954 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
955 # Nothing we can do, really...
956 0 return;
957 }
958 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
959 0 usleep(1000);
960 }
961  
962 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
963 0 $self->autohandle_messages();
964 }
965  
966 0 return;
967 }
968  
969 ▶sub clearcache($self) {
Subroutine clearcache: not called CC=5 SCAR=34.0
Pod documented
970 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
971 0 $self->reconnect;
972 }
973  
974 0 $self->{outbuffer} .= "CLEARCACHE\r\n";
975 0 while(1) {
976 0 $self->doNetwork();
977 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
978 # Nothing we can do, really...
979 0 return;
980 }
981 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
982 0 usleep(1000);
983 }
984  
985 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
986 0 $self->autohandle_messages();
987 }
988  
989 0 return;
990 }
991  
992 ▶sub keylist($self) {
Subroutine keylist: not called CC=17 SCAR=57.2
Pod documented
993 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
994 0 $self->reconnect;
995 }
996  
997 0 my $value;
998  
999 0 $self->{outbuffer} .= "KEYLIST\r\n";
1000  
1001 # Make sure we send everything
1002 0 while(1) {
1003 0 $self->doNetwork();
1004 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1005 # Nothing we can do, really...
1006 0 return;
1007 }
1008 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1009 0 usleep(1000);
1010 }
1011  
1012 # Now, wait for the answer
1013 0 my $liststartfound = 0;
1014 0 my $listendfound = 0;
1015 0 my $deadline = time + $self->{requesttimeout};
1016 0 while(1) {
1017 0 ▶ if(time > $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Net::Clacks::Client::time() > $deadline) 0 0 0
1018 0 carp("keylist() timed out after $self->{requesttimeout}s; marking connection for reconnect");
1019 0 $self->{needreconnect} = 1;
1020 0 return;
1021 }
1022 0 $self->doNetwork(0.5);
1023 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1024 # Nothing we can do, really...
1025 0 return;
1026 }
1027 0 $liststartfound = 0;
1028 0 $listendfound = 0;
1029 0 ▶ for(my $i = 0; $i < scalar @{$self->{inlines}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"inlines"};}) 0 0 0
1030 0 ▶ if($self->{inlines}->[$i] =~ /^KEYLISTSTART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^KEYLISTSTART/u) 0 0 0
1031 0 $liststartfound = 1;
1032 0 next;
1033 }
1034 0 ▶ next unless($liststartfound);
Branch: 1 branch
BranchTrueFalseTotal
unless $liststartfound 0 0 0
1035 0 ▶ if($self->{inlines}->[$i] =~ /^KEYLISTEND/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^KEYLISTEND/u) 0 0 0
1036 0 $listendfound = 1;
1037 0 last;
1038 }
1039 }
1040 0 ▶ last if($listendfound);
Branch: 1 branch
BranchTrueFalseTotal
if $listendfound 0 0 0
1041 }
1042  
1043 # Now, grab the keys from inlines buffer
1044 0 my @keys;
1045 0 my $idx = 0;
1046 0 my $listfound = 0;
1047 0 ▶ while($idx < scalar @{$self->{inlines}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < scalar @{$self->{"inlines"};}) 0 0 0
1048 0 ▶ if($self->{inlines}->[$idx] =~ /^KEYLISTSTART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$idx] =~ /^KEYLISTSTART/u) 0 0 0
1049 # Just remove this line
1050 0 splice @{$self->{inlines}}, $idx, 1;
1051 0 $listfound = 1;
1052 0 next;
1053 }
1054  
1055 0 ▶ if(!$listfound) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listfound) 0 0 0
1056 0 $idx++;
1057 0 next;
1058 }
1059  
1060 0 ▶ if($self->{inlines}->[$idx] =~ /^KEYLISTEND/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$idx] =~ /^KEYLISTEND/u) 0 0 0
1061 # End of list
1062 0 last;
1063 }
1064  
1065 0 ▶ if($self->{inlines}->[$idx] =~ /^KEY\ (.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'inlines'}[$idx] =~ /^KEY\ (.+)/u) { } 0 0 0
1066 0 push @keys, $1;
1067 # Don't increment $idx, but the rest of the array one element down
1068 0 splice @{$self->{inlines}}, $idx, 1;
1069 } else {
1070 0 $idx++;
1071 }
1072 }
1073  
1074 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
1075 0 $self->autohandle_messages();
1076 }
1077  
1078 0 return @keys;
1079 }
1080  
1081 ▶sub clientlist($self) {
Subroutine clientlist: not called CC=18 SCAR=58.3
Pod documented
1082 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1083 0 $self->reconnect;
1084 }
1085  
1086 0 my $value;
1087  
1088 0 $self->{outbuffer} .= "CLIENTLIST\r\n";
1089  
1090 # Make sure we send everything
1091 0 while(1) {
1092 0 $self->doNetwork();
1093 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1094 # Nothing we can do, really...
1095 0 return;
1096 }
1097 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1098 0 usleep(1000);
1099 }
1100  
1101 # Now, wait for the answer
1102 0 my $liststartfound = 0;
1103 0 my $listendfound = 0;
1104 0 my $deadline = time + $self->{requesttimeout};
1105 0 while(1) {
1106 0 ▶ if(time > $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Net::Clacks::Client::time() > $deadline) 0 0 0
1107 0 carp("clientlist() timed out after $self->{requesttimeout}s; marking connection for reconnect");
1108 0 $self->{needreconnect} = 1;
1109 0 return;
1110 }
1111 0 $self->doNetwork(0.5);
1112 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1113 # Nothing we can do, really...
1114 0 return;
1115 }
1116 0 $liststartfound = 0;
1117 0 $listendfound = 0;
1118 0 ▶ for(my $i = 0; $i < scalar @{$self->{inlines}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"inlines"};}) 0 0 0
1119 0 ▶ if($self->{inlines}->[$i] =~ /^CLIENTLISTSTART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^CLIENTLISTSTART/u) 0 0 0
1120 0 $liststartfound = 1;
1121 0 next;
1122 }
1123 0 ▶ next unless($liststartfound);
Branch: 1 branch
BranchTrueFalseTotal
unless $liststartfound 0 0 0
1124 0 ▶ if($self->{inlines}->[$i] =~ /^CLIENTLISTEND/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^CLIENTLISTEND/u) 0 0 0
1125 0 $listendfound = 1;
1126 0 last;
1127 }
1128 }
1129 0 ▶ last if($listendfound);
Branch: 1 branch
BranchTrueFalseTotal
if $listendfound 0 0 0
1130 }
1131  
1132 # Now, grab the keys from inlines buffer
1133 0 my @keys;
1134 0 my $idx = 0;
1135 0 my $listfound = 0;
1136 0 ▶ while($idx < scalar @{$self->{inlines}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < scalar @{$self->{"inlines"};}) 0 0 0
1137 0 ▶ if($self->{inlines}->[$idx] =~ /^CLIENTLISTSTART/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$idx] =~ /^CLIENTLISTSTART/u) 0 0 0
1138 # Just remove this line
1139 0 splice @{$self->{inlines}}, $idx, 1;
1140 0 $listfound = 1;
1141 0 next;
1142 }
1143  
1144 0 ▶ if(!$listfound) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listfound) 0 0 0
1145 0 $idx++;
1146 0 next;
1147 }
1148  
1149 0 ▶ if($self->{inlines}->[$idx] =~ /^CLIENTLISTEND/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$idx] =~ /^CLIENTLISTEND/u) 0 0 0
1150 # End of list
1151 0 last;
1152 }
1153  
1154 0 ▶ if($self->{inlines}->[$idx] =~ /^CLIENT\ (.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'inlines'}[$idx] =~ /^CLIENT\ (.+)/u) { } 0 0 0
1155 0 my @parts = split/\;/, $1;
1156 0 my %data;
1157 0 foreach my $part (@parts) {
1158 0 my ($datakey, $datavalue) = split/\=/, $part, 2;
1159 #$datakey = lc $datakey;
1160 0 $data{lc $datakey} = $datavalue;
1161 }
1162 0 push @keys, \%data;
1163 # Don't increment $idx, but the rest of the array one element down
1164 0 splice @{$self->{inlines}}, $idx, 1;
1165 } else {
1166 0 $idx++;
1167 }
1168 }
1169  
1170 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
1171 0 $self->autohandle_messages();
1172 }
1173  
1174 0 return @keys;
1175 }
1176  
1177 0 ▶sub flush($self, $flushid = '') {
Subroutine flush: not called CC=12 SCAR=50.5
Pod documented
1178 0 ▶ if(!defined($flushid) || $flushid eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $flushid or $flushid eq "") 0 0 0
Condition0%0/3
not defined $flushid or $flushid eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $flushid or $flushid eq ""
not defined $flushid $flushid eq ""
Truth table0%0/3
not(A) or B
A= not defined $flushidB= $flushid eq ""
ABresult
1X1
011
000
1179 0 $flushid = 'AUTO' . int(rand(1_000_000)) . int(rand(1_000_000));
1180 }
1181  
1182 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1183 0 $self->reconnect;
1184 }
1185  
1186 0 $self->{outbuffer} .= "FLUSH $flushid\r\n";
1187  
1188 # Make sure we send everything
1189 0 while(1) {
1190 0 $self->doNetwork();
1191 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1192 # Nothing we can do, really...
1193 0 return;
1194 }
1195 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1196 }
1197  
1198 # Now, wait for the answer
1199 0 my $answerline;
1200 0 my $deadline = time + $self->{requesttimeout};
1201 0 while(1) {
1202 0 ▶ if(time > $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Net::Clacks::Client::time() > $deadline) 0 0 0
1203 0 carp("flush($flushid) timed out after $self->{requesttimeout}s; marking connection for reconnect");
1204 0 $self->{needreconnect} = 1;
1205 0 return;
1206 }
1207 0 $self->doNetwork(0.5);
1208 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1209 # Nothing we can do, really...
1210 0 return;
1211 }
1212 0 ▶ for(my $i = 0; $i < scalar @{$self->{inlines}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"inlines"};}) 0 0 0
1213 0 ▶ if($self->{inlines}->[$i] =~ /^FLUSHED\ $flushid/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inlines"}[$i] =~ /^FLUSHED\ $flushid/u) 0 0 0
1214 # Remove the answer from in in-queue directly (out of sequence), because we don't need in in the getNext function
1215 0 $answerline = splice @{$self->{inlines}}, $i, 1;
1216 0 last;
1217 }
1218 }
1219 0 ▶ last if(defined($answerline));
Branch: 1 branch
BranchTrueFalseTotal
if defined $answerline 0 0 0
1220 }
1221  
1222 0 return;
1223 }
1224  
1225  
1226 ▶sub autohandle_messages($self) {
Subroutine autohandle_messages: not called CC=3 SCAR=24.8
Pod documented
1227 0 $self->doNetwork();
1228  
1229 0 ▶ while((my $line = $self->getNext())) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->getNext) 0 0 0
1230 0 ▶ if($line->{type} eq 'disconnect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"type"} eq "disconnect") 0 0 0
1231 0 $self->{needreconnect} = 1;
1232 }
1233 }
1234  
1235 0 return;
1236 }
1237  
1238 # ---------------- ClackCache handling --------------------
1239  
1240 ▶sub sendRawCommand($self, $command) {
Subroutine sendRawCommand: not called CC=4 SCAR=30.0
Pod documented
1241 0 ▶ if(!defined($command) || !length($command)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command and length $command) 0 0 0
Condition0%0/3
defined $command and length $command
!ll&&!rl&&r
000
MC/DC0%0/2
defined $command and length $command
defined $command length $command
Truth table0%0/3
A and B
A= defined $commandB= length $command
ABresult
0X0
100
111
1242 0 carp("command not defined!");
1243 0 return;
1244 }
1245  
1246 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1247 0 $self->reconnect;
1248 }
1249  
1250 0 $self->{outbuffer} .= $command . "\r\n";
1251  
1252 0 return;
1253 }
1254  
1255 # setAndStore combines the SET and STORE command into the SETANDSTORE server command. This is mostly done
1256 # for optimizing interclacks connections
1257 # Other clients will only get a SET notification, but the server also runs a STORE operation
1258 0 ▶sub setAndStore($self, $varname, $value, $forcesend = 0) {
Subroutine setAndStore: not called CC=9 SCAR=45.0
Pod documented
1259 0 ▶ if(!defined($varname) || !length($varname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $varname and length $varname) 0 0 0
Condition0%0/3
defined $varname and length $varname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $varname and length $varname
defined $varname length $varname
Truth table0%0/3
A and B
A= defined $varnameB= length $varname
ABresult
0X0
100
111
1260 0 carp("varname not defined!");
1261 0 return;
1262 }
1263  
1264 0 ▶ if(!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
1265 0 carp("value not defined!");
1266 0 return;
1267 }
1268  
1269 0 ▶ if(!defined($forcesend)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $forcesend) 0 0 0
1270 0 $forcesend = 0;
1271 }
1272  
1273 0 $self->{outbuffer} .= "SETANDSTORE $varname=$value\r\n";
1274  
1275 0 ▶ if($self->{memcached_compatibility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memcached_compatibility"}) 0 0 0
1276 0 while(1) {
1277 0 $self->doNetwork();
1278 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1279 # Nothing we can do, really...
1280 0 return;
1281 }
1282 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1283 0 usleep(1000);
1284 }
1285  
1286 0 $self->autohandle_messages();
1287 }
1288  
1289 0 return;
1290 }
1291  
1292 ▶sub fastdisconnect($self) {
Subroutine fastdisconnect: not called CC=4 SCAR=30.0
Pod documented
1293 # Mirror reconnect()'s teardown sequence: remove from selector, close, then
1294 # delete. The previous "delete only" approach left the FD open whenever the
1295 # selector or any other reference still pinned the socket.
1296 0 ▶ if(defined($self->{selector}) && defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"selector"} and defined $self->{"socket"}) 0 0 0
Condition0%0/3
defined $self->{"selector"} and defined $self->{"socket"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"selector"} and defined $self->{"socket"}
defined $self->{"selector"} defined $self->{"socket"}
Truth table0%0/3
A and B
A= defined $self->{"selector"}B= defined $self->{"socket"}
ABresult
0X0
100
111
1297 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
1298 0 $self->{selector}->remove($self->{socket});
1299 };
1300 }
1301 0 undef $self->{selector};
1302 0 ▶ if(defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"socket"}) 0 0 0
1303 0 $self->_safeCloseSocket($self->{socket});
1304 0 delete $self->{socket};
1305 }
1306 0 $self->{needreconnect} = 1;
1307 0 return;
1308 }
1309  
1310 ▶sub disconnect($self) {
Subroutine disconnect: not called CC=8 SCAR=42.8
Pod documented
1311 0 ▶ if($self->{needreconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"needreconnect"}) 0 0 0
1312 # We are not connected, just do nothing
1313 0 return;
1314 }
1315  
1316 0 $self->flush();
1317 0 $self->{outbuffer} .= "QUIT\r\n";
1318 0 my $endtime = time + 0.5; # Wait a maximum of half a second to send
1319 0 while(1) {
1320 0 ▶ last if(time > $endtime);
Branch: 1 branch
BranchTrueFalseTotal
if &Net::Clacks::Client::time() > $endtime 0 0 0
1321 0 my $xstart = time;
1322 0 $self->doNetwork(-1);
1323 0 my $xend = time;
1324 0 my $timetaken = $xend - $xstart;
1325 0 ▶ if($timetaken > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timetaken > 1) 0 0 0
1326 #print STDERR "\n §§§§§§§§§§§§§§§§§§§§§§§§§§§§§ doNetwork() took ", $timetaken, " seconds\n";
1327 }
1328 0 ▶ last if(!length($self->{outbuffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"outbuffer"} 0 0 0
1329 0 sleep(0.02);
1330 }
1331 0 sleep(0.2); # Wait for the OS to flush the socket
1332  
1333 # Tear down properly: deselect, close, delete. Just `delete` left the FD open
1334 # whenever IO::Select still held a reference, which is true here because we
1335 # never removed it from the selector.
1336 0 ▶ if(defined($self->{selector}) && defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"selector"} and defined $self->{"socket"}) 0 0 0
Condition0%0/3
defined $self->{"selector"} and defined $self->{"socket"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"selector"} and defined $self->{"socket"}
defined $self->{"selector"} defined $self->{"socket"}
Truth table0%0/3
A and B
A= defined $self->{"selector"}B= defined $self->{"socket"}
ABresult
0X0
100
111
1337 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
1338 0 $self->{selector}->remove($self->{socket});
1339 };
1340 }
1341 0 undef $self->{selector};
1342 0 ▶ if(defined($self->{socket})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"socket"}) 0 0 0
1343 0 $self->_safeCloseSocket($self->{socket});
1344 0 delete $self->{socket};
1345 }
1346 0 $self->{needreconnect} = 1;
1347  
1348 0 return;
1349 }
1350  
1351 ▶sub DESTROY($self) {
Subroutine DESTROY: not called CC=2 SCAR=17.9
1352 # During Perl's global destruction phase, package symbol tables are torn
1353 # down in arbitrary order. By the time DESTROY runs here, methods on
1354 # IO::Socket::SSL, IO::Select, or even IO::Socket may already be
1355 # unavailable. Skip cleanup entirely in that phase — the kernel closes
1356 # any leftover FD when the process exits, and the server detects that
1357 # as EOF and removes us cleanly. Trying to be "polite" here can stall
1358 # exit (flush() waits up to requesttimeout seconds) or call methods on
1359 # half-destroyed package state.
1360 0 ▶ return if(${^GLOBAL_PHASE} eq 'DESTRUCT');
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 0 0
1361  
1362 # Outside global destruction we do a *fast* close (no flush(), no QUIT,
1363 # no sleeps). DESTROY can run at moments where the graceful path is
1364 # inappropriate — e.g. a worker child unwinding after fork, an exception
1365 # being propagated, a local-block exit. Callers who want a graceful
1366 # protocol-level close should call $client->disconnect() explicitly
1367 # before dropping their reference; this DESTROY is just the safety net.
1368 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
1369 0 $self->fastdisconnect();
1370 };
1371  
1372 0 return;
1373 }
1374  
1375 4 1;
1376 __END__
« Clacks/Server.pm ↑ Summary Clacks/PostgreSQL2Clacks.pm »