Client/HeartBeat.pm

filter: Statementstmt 41.8% 36 / 86 Branchbran 3.1% 1 / 32 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 52.9% 9 / 17 Podpod 100% 5 / 5 totaltotal 29.3% 51 / 174 SCAR 56.1
CC
25
Cov
26%
_connect_hook
56.0
client_heart_beat
30.0
server_heart_beat
30.0
CRAP 273.9
« Client/Connection.pm ↑ Summary Client/Auth.pm »
1 #+##############################################################################
2 # #
3 # File: Net/STOMP/Client/HeartBeat.pm #
4 # #
5 # Description: Heart-beat support for Net::STOMP::Client #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package Net::STOMP::Client::HeartBeat;
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 our $VERSION = "2.4";
17 2 our $REVISION = sprintf("%d.%02d", q$Revision: 2.4 $ =~ /(\d+)\.(\d+)/);
18  
19 #
20 # used modules
21 #
22  
23 2 ▶use No::Worries::Die qw(dief);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use No::Worries::Export qw(export_control);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Params::Validate qw(validate_pos :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Time::HiRes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 #
29 # get/set the client heart-beat
30 #
31  
32 sub client_heart_beat : method {
33 0 ▶ my($self, $value);
Subroutine client_heart_beat: not called CC=5 SCAR=34.0
Pod documented
34  
35 0 $self = shift(@_);
36 0 ▶ return($self->{"client_heart_beat"}) if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 0 0
37 0 $value = $_[0];
38 0 ▶ if (@_ == 1 and defined($value) and ref($value) eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and defined $value and ref $value eq "") 0 0 0
Condition0%0/6
@_ == 1 and defined $value and ref $value eq ""
!ll&&!rl&&r
000
@_ == 1 and defined $value
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 and defined $value and ref $value eq ""
@_ == 1 defined $value ref $value eq ""
Truth table0%0/4
A and B and C
A= @_ == 1B= defined $valueC= ref $value eq ""
ABCresult
0XX0
10X0
1100
1111
39 0 $self->{"client_heart_beat"} = $value;
40 0 return($self);
41 }
42 # otherwise complain...
43 0 validate_pos(@_, { optional => 1, type => SCALAR });
44 }
45  
46 #
47 # get/set the server heart-beat
48 #
49  
50 sub server_heart_beat : method {
51 0 ▶ my($self, $value);
Subroutine server_heart_beat: not called CC=5 SCAR=34.0
Pod documented
52  
53 0 $self = shift(@_);
54 0 ▶ return($self->{"server_heart_beat"}) if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 0 0
55 0 $value = $_[0];
56 0 ▶ if (@_ == 1 and defined($value) and ref($value) eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and defined $value and ref $value eq "") 0 0 0
Condition0%0/6
@_ == 1 and defined $value and ref $value eq ""
!ll&&!rl&&r
000
@_ == 1 and defined $value
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 1 and defined $value and ref $value eq ""
@_ == 1 defined $value ref $value eq ""
Truth table0%0/4
A and B and C
A= @_ == 1B= defined $valueC= ref $value eq ""
ABCresult
0XX0
10X0
1100
1111
57 0 $self->{"server_heart_beat"} = $value;
58 0 return($self);
59 }
60 # otherwise complain...
61 0 validate_pos(@_, { optional => 1, type => SCALAR });
62 }
63  
64 #
65 # get the last time we received/read data
66 #
67  
68 sub last_received : method {
69 0 ▶ my($self) = @_;
Subroutine last_received: not called CC=2 SCAR=17.9
Pod documented
70  
71 0 ▶ return(undef) unless $self->{"io"};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"io"} 0 0 0
72 0 return($self->{"io"}{"incoming_time"});
73 }
74  
75 #
76 # get the last time we sent/wrote data
77 #
78  
79 sub last_sent : method {
80 0 ▶ my($self) = @_;
Subroutine last_sent: not called CC=2 SCAR=17.9
Pod documented
81  
82 0 ▶ return(undef) unless $self->{"io"};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"io"} 0 0 0
83 0 return($self->{"io"}{"outgoing_time"});
84 }
85  
86 #
87 # send a NOOP frame only if needed wrt heart-beat settings
88 #
89  
90 sub beat : method {
91 0 ▶ my($self, %option) = @_;
Subroutine beat: not called CC=3 SCAR=24.8
Pod documented
92 0 my($delta, $sent);
93  
94 # check if client heart-beats are expected
95 0 $delta = $self->client_heart_beat();
96 0 ▶ return($self) unless $delta;
Branch: 1 branch
BranchTrueFalseTotal
unless $delta 0 0 0
97 # check the last time we sent data
98 0 $sent = $self->last_sent();
99 0 ▶ return($self) if Time::HiRes::time() - $sent < $delta / 2;
Branch: 1 branch
BranchTrueFalseTotal
if &Time::HiRes::time() - $sent < $delta / 2 0 0 0
100 # send a NOOP frame
101 0 return($self->noop(%option));
102 }
103  
104 #
105 # setup
106 #
107  
108 sub _setup ($) {
109 2 ▶ my($self) = @_;
Subroutine _setup: called CC=2 SCAR=7.2
110  
111 # additional options for new()
112 return(
113 2 ▶ "client_heart_beat" => { optional => 1, type => SCALAR },
Branch: 1 branch
BranchTrueFalseTotal
unless $self 2 0 2
114 "server_heart_beat" => { optional => 1, type => SCALAR },
115 ) unless $self;
116 }
117  
118 #
119 # hook for the CONNECT frame
120 #
121  
122 sub _connect_hook ($$) {
123 0 ▶ my($self, $frame) = @_;
Subroutine _connect_hook: not called CC=7 SCAR=40.3
124 0 my($chb, $shb);
125  
126 # do not override what the user did put in the frame
127 0 ▶ return if defined($frame->header("heart-beat"));
Branch: 1 branch
BranchTrueFalseTotal
if defined $frame->header("heart-beat") 0 0 0
128 # do nothing when only STOMP 1.0 is asked
129 0 ▶ return unless grep($_ ne "1.0", $self->accept_version());
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ ne "1.0"), $self->accept_version) 0 0 0
130 # add the appropriate header (in milliseconds!)
131 0 ▶ $chb = int(($self->client_heart_beat() || 0) * 1000.0 + 0.5);
Condition0%0/2
$self->client_heart_beat || 0
l!l
00
MC/DC0%0/1
$self->client_heart_beat || 0
$self->client_heart_beat
Truth table0%0/2
A
A= $self->client_heart_beat
Aresult
11
00
132 0 ▶ $shb = int(($self->server_heart_beat() || 0) * 1000.0 + 0.5);
Condition0%0/2
$self->server_heart_beat || 0
l!l
00
MC/DC0%0/1
$self->server_heart_beat || 0
$self->server_heart_beat
Truth table0%0/2
A
A= $self->server_heart_beat
Aresult
11
00
133 0 ▶ $frame->header("heart-beat", "$chb,$shb") if $chb or $shb;
Branch: 1 branch
BranchTrueFalseTotal
if $chb or $shb 0 0 0
Condition0%0/3
$chb or $shb
l!l&&r!l&&!r
000
MC/DC0%0/2
$chb or $shb
$chb $shb
Truth table0%0/3
A or B
A= $chbB= $shb
ABresult
1X1
011
000
134 }
135  
136 #
137 # negotiation helper
138 #
139  
140 sub _maxif ($$) {
141 0 ▶ my($x, $y) = @_;
Subroutine _maxif: not called CC=4 SCAR=30.0
142  
143 0 ▶ return(0) unless $x and $y;
Branch: 1 branch
BranchTrueFalseTotal
unless $x and $y 0 0 0
Condition0%0/3
$x and $y
!ll&&!rl&&r
000
MC/DC0%0/2
$x and $y
$x $y
Truth table0%0/3
A and B
A= $xB= $y
ABresult
0X0
100
111
144 0 ▶ return($x > $y ? $x : $y);
Branch: 1 branch
BranchTrueFalseTotal
$x > $y ? : 0 0 0
145 }
146  
147 #
148 # hook for the CONNECTED frame
149 #
150  
151 sub _connected_hook ($$) {
152 0 ▶ my($self, $frame) = @_;
Subroutine _connected_hook: not called CC=3 SCAR=24.8
153 0 my($value, $shb, $chb);
154  
155 0 $value = $frame->header("heart-beat");
156 0 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
157 # given specification: check
158 0 ▶ if ($value =~ /^(\d+),(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(\d+),(\d+)$/) { } 0 0 0
159 0 ($shb, $chb) = ($1 / 1000.0, $2 / 1000.0);
160 0 $self->server_heart_beat(_maxif($self->server_heart_beat(), $shb));
161 0 $self->client_heart_beat(_maxif($self->client_heart_beat(), $chb));
162 } else {
163 0 dief("unexpected heart-beat specification: %s", $value);
164 }
165 } else {
166 # missing specification: disable
167 0 $self->client_heart_beat(0);
168 0 $self->server_heart_beat(0);
169 }
170 }
171  
172 #
173 # register the setup and hooks
174 #
175  
176 {
177 2 ▶ no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
178 2 $Net::STOMP::Client::Setup{"heart-beat"} = \&_setup;
179 2 $Net::STOMP::Client::Hook{"CONNECT"}{"heart-beat"} = \&_connect_hook;
180 2 $Net::STOMP::Client::Hook{"CONNECTED"}{"heart-beat"} = \&_connected_hook;
181 }
182  
183 #
184 # export control
185 #
186  
187 sub import : method {
188 2 ▶ my($pkg, %exported);
Subroutine import: called CC=1 SCAR=0.0
189  
190 2 $pkg = shift(@_);
191 2 grep($exported{$_}++, qw(beat));
192 2 grep($exported{$_}++, map("${_}_heart_beat", qw(client server)));
193 2 grep($exported{$_}++, map("last_${_}", qw(received sent)));
194 2 export_control(scalar(caller()), $pkg, \%exported, @_);
195 }
196  
197 2 1;
198  
199 __END__
« Client/Connection.pm ↑ Summary Client/Auth.pm »