Client/Connection.pm

filter: Statementstmt 46.9% 68 / 145 Branchbran 20.5% 16 / 78 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 82.3% 14 / 17 Podpod 100% 1 / 1 totaltotal 37.7% 99 / 262 SCAR 64.6
CC
47
Cov
36%
_try
272.0
_attempt
90.0
new
90.0
CRAP 637.2
« Client/IO.pm ↑ Summary Client/HeartBeat.pm »
1 #+##############################################################################
2 # #
3 # File: Net/STOMP/Client/Connection.pm #
4 # #
5 # Description: Connection support for Net::STOMP::Client #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package Net::STOMP::Client::Connection;
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 our $VERSION = "2.4";
17 3 our $REVISION = sprintf("%d.%02d", q$Revision: 2.8 $ =~ /(\d+)\.(\d+)/);
18  
19 #
20 # used modules
21 #
22  
23 3 ▶use IO::Socket::IP qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use Net::STOMP::Client::Peer qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use No::Worries::Die qw(dief);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use No::Worries::File qw(file_read);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use No::Worries::Log qw(log_debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use Params::Validate qw(validate :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use Time::HiRes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 #+++############################################################################
33 # #
34 # private helpers #
35 # #
36 #---############################################################################
37  
38 #
39 # convert a URI (with no options) to a peer object
40 #
41  
42 sub _uri2peer ($) {
43 16 ▶ my($uri) = @_;
Subroutine _uri2peer: called CC=2 SCAR=6.9
44  
45 16 ▶ if ($uri =~ m{ ^ (tcp|ssl|stomp|stomp\+ssl)
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m( ^ (tcp|ssl|stomp|stomp\+ssl) \:\/\/ \[? ([_a-z0-9\.\-\:]+?) \]? \: (\d+) \/? $ )xi) { } 13 3 16
46 \:\/\/ \[? ([_a-z0-9\.\-\:]+?) \]? \: (\d+) \/? $ }ix) {
47 13 return(Net::STOMP::Client::Peer->new(
48 proto => $1,
49 host => $2,
50 port => $3,
51 ));
52 } else {
53 3 dief("unexpected server uri: %s", $uri);
54 }
55 }
56  
57 #
58 # set the default connection options
59 #
60  
61 sub _default_options ($) {
62 4 ▶ my($option) = @_;
Subroutine _default_options: called CC=1 SCAR=0.0
63  
64 4 $option->{randomize} = 1;
65 4 $option->{sleep} = 0.01;
66 4 $option->{max_sleep} = 30;
67 4 $option->{multiplier} = 2;
68 }
69  
70 #
71 # parse an option string (we do not complain about unknown options)
72 #
73  
74 sub _parse_options ($$) {
75 1 ▶ my($option, $string) = @_;
Subroutine _parse_options: called CC=7 SCAR=22.3
76  
77 1 ▶ if ($string =~ /\b(backOffMultiplier=(\d+(\.\d+)?))\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\b(backOffMultiplier=(\d+(\.\d+)?))\b/) 0 1 1
78 0 $option->{multiplier} = $2;
79 }
80 1 ▶ if ($string =~ /\b(useExponentialBackOff=false)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\b(useExponentialBackOff=false)\b/) 0 1 1
81 0 $option->{multiplier} = 0;
82 }
83 1 ▶ if ($string =~ /\b(randomize=false)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\b(randomize=false)\b/) 1 0 1
84 1 $option->{randomize} = 0;
85 }
86 1 ▶ if ($string =~ /\b(initialReconnectDelay=(\d+))\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\b(initialReconnectDelay=(\d+))\b/) 1 0 1
87 1 $option->{sleep} = $2 / 1000;
88 }
89 1 ▶ if ($string =~ /\b(maxReconnectDelay=(\d+))\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\b(maxReconnectDelay=(\d+))\b/) 0 1 1
90 0 $option->{max_sleep} = $2 / 1000;
91 }
92 1 ▶ if ($string =~ /\b(maxReconnectAttempts=(\d+))\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\b(maxReconnectAttempts=(\d+))\b/) 1 0 1
93 1 $option->{max_attempt} = $2 + 1;
94 }
95 }
96  
97 #
98 # parse a connection URI
99 #
100 # supported URIs:
101 # - tcp://foo:12
102 # - file:/foo/bar
103 # - ActiveMQ failover URIs
104 #
105  
106 sub _parse_uri ($) {
107 12 ▶ my($uri) = @_;
Subroutine _parse_uri: called CC=9 SCAR=26.4
108 12 my(@peers, %option, @list);
109  
110 12 while (1) {
111 12 ▶ if ($uri =~ /^file:(.+)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($uri =~ /^file:(.+)$/) { } 0 12 12
elsif ($uri =~ m[ ^ failover \: (?:\/\/)? \( ([_a-z0-9\.\-\:\/\,]+) \) ( \? [_a-z0-9\.\=\-\&]+ ) ? $ ]xi) { } 3 9 12
elsif ($uri =~ m[ ^ failover \: ([_a-z0-9\.\-\:\/\,]+) $ ]xi) { } 1 8 9
112 # list of URIs stored in a file, one per line
113 0 @list = ();
114 0 foreach my $line (split(/\n/, file_read($1))) {
115 0 $line =~ s/\#.*//;
116 0 $line =~ s/\s+//g;
117 0 ▶ push(@list, $line) if length($line);
Branch: 1 branch
BranchTrueFalseTotal
if length $line 0 0 0
118 }
119 0 ▶ if (@list == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list == 1) { } 0 0 0
120 # if only one, allow failover syntax for it
121 0 $uri = shift(@list);
122 } else {
123 # otherwise, they must be simple URIs
124 0 @peers = map(_uri2peer($_), @list);
125 0 last;
126 }
127 } elsif ($uri =~ m{ ^ failover \: (?:\/\/)? \( ([_a-z0-9\.\-\:\/\,]+) \)
128 ( \? [_a-z0-9\.\=\-\&]+ ) ? $ }ix) {
129 # failover with options
130 3 _default_options(\%option);
131 3 ▶ _parse_options(\%option, $2) if $2;
Branch: 1 branch
BranchTrueFalseTotal
if $2 1 2 3
132 3 @peers = map(_uri2peer($_), split(/,/, $1));
133 3 last;
134 } elsif ($uri =~ m{ ^ failover \: ([_a-z0-9\.\-\:\/\,]+) $ }ix) {
135 # failover without options
136 1 _default_options(\%option);
137 1 @peers = map(_uri2peer($_), split(/,/, $1));
138 1 last;
139 } else {
140 # otherwise this must be a simple URI
141 8 @peers = (_uri2peer($uri));
142 5 last;
143 }
144 }
145 9 ▶ dief("empty server uri: %s", $uri) unless @peers;
Branch: 1 branch
BranchTrueFalseTotal
unless @peers 0 9 9
146 9 return(\@peers, \%option);
147 }
148  
149 #
150 # attempt to connect to one peer (low level)
151 #
152  
153 sub _attempt ($%) {
154 0 ▶ my($peer, %sockopt) = @_;
Subroutine _attempt: not called CC=9 SCAR=45.0
155 0 my($socket);
156  
157 # options sanity
158 0 $sockopt{Proto} = "tcp"; # yes, even SSL is TCP...
159 0 $sockopt{PeerAddr} = $peer->host();
160 0 $sockopt{PeerPort} = $peer->port();
161 # try to connect
162 0 ▶ if ($peer->proto() =~ /\b(ssl)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peer->proto =~ /\b(ssl)\b/) { } 0 0 0
163 # with SSL
164 0 ▶ unless ($IO::Socket::SSL::VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IO::Socket::SSL::VERSION) 0 0 0
165 0 eval { require IO::Socket::SSL };
166 0 ▶ return(sprintf("cannot load IO::Socket::SSL: %s", $@)) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
167 }
168 0 $socket = IO::Socket::SSL->new(%sockopt);
169 0 ▶ return(sprintf("cannot SSL connect to %s:%d: %s", $peer->host(),
Branch: 1 branch
BranchTrueFalseTotal
unless $socket and $socket->connected 0 0 0
Condition0%0/3
$socket and $socket->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$socket and $socket->connected
$socket $socket->connected
Truth table0%0/3
A and B
A= $socketB= $socket->connected
ABresult
0X0
100
111
170 $peer->port(), IO::Socket::SSL::errstr()))
171 unless $socket and $socket->connected();
172 } else {
173 # with plain TCP
174 0 $socket = IO::Socket::IP->new(%sockopt);
175 0 ▶ return(sprintf("cannot connect to %s:%d: %s", $peer->host(),
Branch: 1 branch
BranchTrueFalseTotal
unless $socket and $socket->connected 0 0 0
Condition0%0/3
$socket and $socket->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$socket and $socket->connected
$socket $socket->connected
Truth table0%0/3
A and B
A= $socketB= $socket->connected
ABresult
0X0
100
111
176 $peer->port(), $!))
177 unless $socket and $socket->connected();
178 0 ▶ return(sprintf("cannot binmode(socket): %s", $!))
Branch: 1 branch
BranchTrueFalseTotal
unless binmode $socket 0 0 0
179 unless binmode($socket);
180 }
181 # so far so good...
182 0 @{ $peer }[3] = $socket->peerhost();
183 0 return($socket);
184 }
185  
186 #
187 # try to connect to a list of peers (high level)
188 #
189  
190 sub _try ($$$$) {
191 0 ▶ my($peers, $peeropt, $sockopt, $debug) = @_;
Subroutine _try: not called CC=16 SCAR=56.1
192 0 my(@list, $count, $result);
193  
194 0 ▶ dief("no peers given!") unless @{ $peers };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$peers;} 0 0 0
195 0 $count = 0;
196 0 while (1) {
197 0 ▶ @list = $peeropt->{randomize} ? shuffle(@{ $peers }) : @{ $peers };
Branch: 1 branch
BranchTrueFalseTotal
$peeropt->{'randomize'} ? : 0 0 0
198 0 foreach my $peer (@list) {
199 0 $result = _attempt($peer, %{ $sockopt });
200 0 ▶ if (ref($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result) { } 0 0 0
201 0 ▶ log_debug("connect to %s ok: %s", $peer->uri(), $peer->addr())
Branch: 1 branch
BranchTrueFalseTotal
if $debug =~ /\b(connection|all)\b/ 0 0 0
202 if $debug =~ /\b(connection|all)\b/;
203 0 return($result, $peer);
204 } else {
205 0 ▶ log_debug("connect to %s failed: %s", $peer->uri(), $result)
Branch: 1 branch
BranchTrueFalseTotal
if $debug =~ /\b(connection|all)\b/ 0 0 0
206 if $debug =~ /\b(connection|all)\b/;
207 }
208 0 $count++;
209 0 ▶ if (defined($peeropt->{max_attempt})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $peeropt->{"max_attempt"}) 0 0 0
210 0 ▶ last if $count >= $peeropt->{max_attempt};
Branch: 1 branch
BranchTrueFalseTotal
if $count >= $peeropt->{"max_attempt"} 0 0 0
211 }
212 0 ▶ if ($peeropt->{sleep}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peeropt->{"sleep"}) 0 0 0
213 0 Time::HiRes::sleep($peeropt->{sleep});
214 0 ▶ if ($peeropt->{multiplier}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peeropt->{"multiplier"}) 0 0 0
215 0 $peeropt->{sleep} *= $peeropt->{multiplier};
216 0 ▶ if ($peeropt->{max_sleep} and
Branch: 1 branch
BranchTrueFalseTotal
if ($peeropt->{"max_sleep"} and $peeropt->{"sleep"} > $peeropt->{"max_sleep"}) 0 0 0
Condition0%0/3
$peeropt->{"max_sleep"} and $peeropt->{"sleep"} > $peeropt->{"max_sleep"}
!ll&&!rl&&r
000
MC/DC0%0/2
$peeropt->{"max_sleep"} and $peeropt->{"sleep"} > $peeropt->{"max_sleep"}
$peeropt->{"max_sleep"} $peeropt->{"sleep"} > $peeropt->{"max_sleep"}
Truth table0%0/3
A and B
A= $peeropt->{"max_sleep"}B= $peeropt->{"sleep"} > $peeropt->{"max_sleep"}
ABresult
0X0
100
111
217 $peeropt->{sleep} > $peeropt->{max_sleep}) {
218 0 $peeropt->{sleep} = $peeropt->{max_sleep};
219 0 delete($peeropt->{multiplier});
220 }
221 }
222 }
223 }
224 0 ▶ if (defined($peeropt->{max_attempt})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $peeropt->{"max_attempt"}) 0 0 0
225 0 ▶ last if $count >= $peeropt->{max_attempt};
Branch: 1 branch
BranchTrueFalseTotal
if $count >= $peeropt->{"max_attempt"} 0 0 0
226 }
227 0 ▶ last unless keys(%{ $peeropt });
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$peeropt;} 0 0 0
228 }
229 # in case of failure, we only report the last error message...
230 0 dief($result);
231 }
232  
233 #+++############################################################################
234 # #
235 # public function #
236 # #
237 #---############################################################################
238  
239 3 my %new_options = (
240 "host" => {
241 optional => 1,
242 type => SCALAR,
243 regex => qr/^[a-z0-9\.\-\:]+$/,
244 },
245 "port" => {
246 optional => 1,
247 type => SCALAR,
248 regex => qr/^\d+$/,
249 },
250 "uri" => {
251 optional => 1,
252 type => SCALAR,
253 },
254 "sockopt" => {
255 optional => 1,
256 type => HASHREF,
257 },
258 "debug" => {
259 optional => 1,
260 type => UNDEF|SCALAR,
261 },
262 );
263  
264 sub new (@) {
265 0 ▶ my(%option, $proto, $peers, $peeropt);
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
266  
267 0 %option = validate(@_, \%new_options);
268 0 ▶ $option{sockopt} ||= {};
Condition0%0/2
$option{"sockopt"} ||= {}
l!l
00
MC/DC0%0/1
$option{"sockopt"} ||= {}
$option{"sockopt"}
Truth table0%0/2
A
A= $option{"sockopt"}
Aresult
11
00
269 0 ▶ $option{debug} ||= "";
Condition0%0/2
$option{"debug"} ||= ""
l!l
00
MC/DC0%0/1
$option{"debug"} ||= ""
$option{"debug"}
Truth table0%0/2
A
A= $option{"debug"}
Aresult
11
00
270 0 ▶ if (defined($option{uri})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option{'uri'}) { } 0 0 0
271 # by URI
272 dief("unexpected server host: %s", $option{host})
273 0 ▶ if defined($option{host});
Branch: 1 branch
BranchTrueFalseTotal
if defined $option{"host"} 0 0 0
274 dief("unexpected server port: %s", $option{port})
275 0 ▶ if defined($option{port});
Branch: 1 branch
BranchTrueFalseTotal
if defined $option{"port"} 0 0 0
276 0 ($peers, $peeropt) = _parse_uri($option{uri});
277 } else {
278 # by host + port
279 0 ▶ dief("missing server host") unless defined($option{host});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option{"host"} 0 0 0
280 0 ▶ dief("missing server port") unless defined($option{port});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option{"port"} 0 0 0
281 0 $proto = "tcp";
282 0 ▶ $proto = "ssl" if grep(/^SSL_/, keys(%{ $option{sockopt} }));
Branch: 1 branch
BranchTrueFalseTotal
if grep /^SSL_/, keys %{$option{"sockopt"};} 0 0 0
283 $peers = [
284 Net::STOMP::Client::Peer->new(
285 proto => $proto,
286 host => $option{host},
287 port => $option{port},
288 0 ) ];
289 0 $peeropt = {};
290 }
291 0 return(_try($peers, $peeropt, $option{sockopt}, $option{debug}));
292 }
293  
294 3 1;
295  
296 __END__
« Client/IO.pm ↑ Summary Client/HeartBeat.pm »