Client/Version.pm

filter: Statementstmt 43.5% 34 / 78 Branchbran 3.1% 1 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 61.5% 8 / 13 Podpod 100% 2 / 2 totaltotal 36.0% 45 / 125 SCAR 47.9
CC
18
Cov
32%
_check
72.0
_connected_hook
20.0
accept_version
12.0
CRAP 120.7
« Client/Auth.pm ↑ Summary Client/Receipt.pm »
1 #+##############################################################################
2 # #
3 # File: Net/STOMP/Client/Version.pm #
4 # #
5 # Description: Version support for Net::STOMP::Client #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package Net::STOMP::Client::Version;
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.3 $ =~ /(\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  
27 #
28 # global variables
29 #
30  
31 our(
32 2 %Supported, # hash of the supported STOMP protocol versions
33 );
34  
35 2 foreach my $version (qw(1.0 1.1 1.2)) {
36 6 $Supported{$version}++;
37 }
38  
39 #
40 # check a list of acceptable versions
41 #
42  
43 sub _check ($) {
44 0 ▶ my($value) = @_;
Subroutine _check: not called CC=8 SCAR=42.8
45  
46 0 ▶ unless (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
47 # undef: accept all supported
48 0 return(sort(keys(%Supported)));
49 }
50 0 ▶ if (ref($value) eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "") 0 0 0
51 # scalar
52 0 ▶ if ($value =~ /,/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /,/) { } 0 0 0
53 # assume a comma separated list
54 0 $value = [ split(/,/, $value) ];
55 # (will be checked further down)
56 } else {
57 # assume a single version
58 dief("unsupported STOMP version: %s", $value)
59 0 ▶ unless $Supported{$value};
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::STOMP::Client::Version::Supported{$value} 0 0 0
60 0 return($value);
61 }
62 }
63 0 ▶ if (ref($value) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 0 0 0
64 # array reference: accept all given
65 0 foreach my $version (@{ $value }) {
66 dief("unsupported STOMP version: %s", $version)
67 0 ▶ unless $Supported{$version};
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::STOMP::Client::Version::Supported{$version} 0 0 0
68 }
69 0 return(@{ $value });
70 }
71 0 dief("unexpected STOMP version: %s", $value);
72 }
73  
74 #
75 # get/set the acceptable versions
76 #
77  
78 sub accept_version : method {
79 0 ▶ my($self);
Subroutine accept_version: not called CC=3 SCAR=24.8
Pod documented
80  
81 0 $self = shift(@_);
82 0 ▶ return(@{ $self->{"accept_version"} }) if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 0 0
83 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
84 0 $self->{"accept_version"} = [ _check($_[0]) ];
85 0 return($self);
86 }
87 # otherwise complain...
88 0 validate_pos(@_, { optional => 1, type => UNDEF|SCALAR|ARRAYREF });
89 }
90  
91 #
92 # get the negotiated version
93 #
94  
95 sub version : method {
96 0 ▶ my($self) = @_;
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
97  
98 0 return($self->{"version"});
99 }
100  
101 #
102 # setup
103 #
104  
105 sub _setup ($) {
106 2 ▶ my($self) = @_;
Subroutine _setup: called CC=4 SCAR=23.7
107  
108 # additional options for new()
109 return(
110 2 ▶ "accept_version" => { optional => 1, type => UNDEF|SCALAR|ARRAYREF },
Branch: 1 branch
BranchTrueFalseTotal
unless $self 2 0 2
111 "version" => { optional => 1, type => UNDEF|SCALAR|ARRAYREF },
112 ) unless $self;
113 # FIXME: compatibility hack for Net::STOMP::Client 1.x (to be removed)
114 0 ▶ if (exists($self->{"version"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"version"}) 0 0 0
115 dief("options version and accept_version are mutually exclusive")
116 0 ▶ if exists($self->{"accept_version"});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"accept_version"} 0 0 0
117 0 $self->{"accept_version"} = delete($self->{"version"});
118 }
119 # check the accept_version option (and set defaults)
120 0 $self->accept_version($self->{"accept_version"});
121 }
122  
123 #
124 # hook for the CONNECT frame
125 #
126  
127 sub _connect_hook ($$) {
128 0 ▶ my($self, $frame) = @_;
Subroutine _connect_hook: not called CC=3 SCAR=24.8
129 0 my(@list);
130  
131 # do not override what the user did put in the frame
132 0 ▶ return if defined($frame->header("accept-version"));
Branch: 1 branch
BranchTrueFalseTotal
if defined $frame->header("accept-version") 0 0 0
133 # do nothing when only STOMP 1.0 is asked
134 0 @list = $self->accept_version();
135 0 ▶ return unless grep($_ ne "1.0", @list);
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ ne "1.0"), @list) 0 0 0
136 # add the appropriate header
137 0 $frame->header("accept-version", join(",", @list));
138 }
139  
140 #
141 # hook for the CONNECTED frame
142 #
143  
144 sub _connected_hook ($$) {
145 0 ▶ my($self, $frame) = @_;
Subroutine _connected_hook: not called CC=4 SCAR=30.0
146 0 my(@list, $version);
147  
148 0 @list = $self->accept_version();
149 0 $version = $frame->header("version");
150 0 ▶ if (defined($version)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version) { } 0 0 0
151 # the server must have chosen an acceptable version
152 0 ▶ dief("unexpected STOMP version: %s", $version)
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $version), @list) 0 0 0
153 unless grep($_ eq $version, @list);
154 } else {
155 # no version header present so assume 1.0
156 0 $version = "1.0";
157 0 ▶ dief("server only supports STOMP 1.0")
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $version), @list) 0 0 0
158 unless grep($_ eq $version, @list);
159 }
160 # so far so good
161 0 $self->{"version"} = $version;
162 }
163  
164 #
165 # register the setup and hooks
166 #
167  
168 {
169 2 ▶ no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
170 2 $Net::STOMP::Client::Setup{"version"} = \&_setup;
171 2 $Net::STOMP::Client::Hook{"CONNECT"}{"version"} = \&_connect_hook;
172 2 $Net::STOMP::Client::Hook{"CONNECTED"}{"version"} = \&_connected_hook;
173 }
174  
175 #
176 # export control
177 #
178  
179 sub import : method {
180 2 ▶ my($pkg, %exported);
Subroutine import: called CC=1 SCAR=0.0
181  
182 2 $pkg = shift(@_);
183 2 grep($exported{$_}++, qw(accept_version version));
184 2 export_control(scalar(caller()), $pkg, \%exported, @_);
185 }
186  
187 2 1;
188  
189 __END__
« Client/Auth.pm ↑ Summary Client/Receipt.pm »