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