line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
2
|
|
|
2
|
|
482
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
43
|
|
15
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
106
|
|
16
|
|
|
|
|
|
|
our $VERSION = "2.3"; |
17
|
|
|
|
|
|
|
our $REVISION = sprintf("%d.%02d", q$Revision: 2.6 $ =~ /(\d+)\.(\d+)/); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
# used modules |
21
|
|
|
|
|
|
|
# |
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
2
|
|
855
|
use IO::Socket::INET qw(); |
|
2
|
|
|
|
|
32007
|
|
|
2
|
|
|
|
|
45
|
|
24
|
2
|
|
|
2
|
|
11
|
use List::Util qw(shuffle); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
174
|
|
25
|
2
|
|
|
2
|
|
721
|
use Net::STOMP::Client::Peer qw(); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
44
|
|
26
|
2
|
|
|
2
|
|
393
|
use No::Worries::Die qw(dief); |
|
2
|
|
|
|
|
5447
|
|
|
2
|
|
|
|
|
10
|
|
27
|
2
|
|
|
2
|
|
930
|
use No::Worries::File qw(file_read); |
|
2
|
|
|
|
|
2219
|
|
|
2
|
|
|
|
|
8
|
|
28
|
2
|
|
|
2
|
|
1039
|
use No::Worries::Log qw(log_debug); |
|
2
|
|
|
|
|
26129
|
|
|
2
|
|
|
|
|
12
|
|
29
|
2
|
|
|
2
|
|
198
|
use Params::Validate qw(validate :types); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
274
|
|
30
|
2
|
|
|
2
|
|
911
|
use Time::HiRes qw(); |
|
2
|
|
|
|
|
1887
|
|
|
2
|
|
|
|
|
2308
|
|
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
|
|
|
16
|
|
15
|
my($uri) = @_; |
44
|
|
|
|
|
|
|
|
45
|
16
|
100
|
|
|
|
57
|
if ($uri =~ m{ ^ (tcp|ssl|stomp|stomp\+ssl) |
46
|
|
|
|
|
|
|
\:\/\/ ([_a-z0-9\.\-]+) \: (\d+) \/? $ }ix) { |
47
|
13
|
|
|
|
|
42
|
return(Net::STOMP::Client::Peer->new( |
48
|
|
|
|
|
|
|
proto => $1, |
49
|
|
|
|
|
|
|
host => $2, |
50
|
|
|
|
|
|
|
port => $3, |
51
|
|
|
|
|
|
|
)); |
52
|
|
|
|
|
|
|
} else { |
53
|
3
|
|
|
|
|
10
|
dief("unexpected server uri: %s", $uri); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# |
58
|
|
|
|
|
|
|
# set the default connection options |
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _default_options ($) { |
62
|
4
|
|
|
4
|
|
5
|
my($option) = @_; |
63
|
|
|
|
|
|
|
|
64
|
4
|
|
|
|
|
7
|
$option->{randomize} = 1; |
65
|
4
|
|
|
|
|
5
|
$option->{sleep} = 0.01; |
66
|
4
|
|
|
|
|
4
|
$option->{max_sleep} = 30; |
67
|
4
|
|
|
|
|
5
|
$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
|
|
|
1
|
|
2
|
my($option, $string) = @_; |
76
|
|
|
|
|
|
|
|
77
|
1
|
50
|
|
|
|
5
|
if ($string =~ /\b(backOffMultiplier=(\d+(\.\d+)?))\b/) { |
78
|
0
|
|
|
|
|
0
|
$option->{multiplier} = $2; |
79
|
|
|
|
|
|
|
} |
80
|
1
|
50
|
|
|
|
5
|
if ($string =~ /\b(useExponentialBackOff=false)\b/) { |
81
|
0
|
|
|
|
|
0
|
$option->{multiplier} = 0; |
82
|
|
|
|
|
|
|
} |
83
|
1
|
50
|
|
|
|
4
|
if ($string =~ /\b(randomize=false)\b/) { |
84
|
1
|
|
|
|
|
2
|
$option->{randomize} = 0; |
85
|
|
|
|
|
|
|
} |
86
|
1
|
50
|
|
|
|
5
|
if ($string =~ /\b(initialReconnectDelay=(\d+))\b/) { |
87
|
1
|
|
|
|
|
8
|
$option->{sleep} = $2 / 1000; |
88
|
|
|
|
|
|
|
} |
89
|
1
|
50
|
|
|
|
4
|
if ($string =~ /\b(maxReconnectDelay=(\d+))\b/) { |
90
|
0
|
|
|
|
|
0
|
$option->{max_sleep} = $2 / 1000; |
91
|
|
|
|
|
|
|
} |
92
|
1
|
50
|
|
|
|
3
|
if ($string =~ /\b(maxReconnectAttempts=(\d+))\b/) { |
93
|
1
|
|
|
|
|
4
|
$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
|
|
|
12
|
|
3500
|
my($uri) = @_; |
108
|
12
|
|
|
|
|
11
|
my(@peers, %option, @list); |
109
|
|
|
|
|
|
|
|
110
|
12
|
|
|
|
|
9
|
while (1) { |
111
|
12
|
50
|
|
|
|
57
|
if ($uri =~ /^file:(.+)$/) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# list of URIs stored in a file, one per line |
113
|
0
|
|
|
|
|
0
|
@list = (); |
114
|
0
|
|
|
|
|
0
|
foreach my $line (split(/\n/, file_read($1))) { |
115
|
0
|
|
|
|
|
0
|
$line =~ s/\#.*//; |
116
|
0
|
|
|
|
|
0
|
$line =~ s/\s+//g; |
117
|
0
|
0
|
|
|
|
0
|
push(@list, $line) if length($line); |
118
|
|
|
|
|
|
|
} |
119
|
0
|
0
|
|
|
|
0
|
if (@list == 1) { |
120
|
|
|
|
|
|
|
# if only one, allow failover syntax for it |
121
|
0
|
|
|
|
|
0
|
$uri = shift(@list); |
122
|
|
|
|
|
|
|
} else { |
123
|
|
|
|
|
|
|
# otherwise, they must be simple URIs |
124
|
0
|
|
|
|
|
0
|
@peers = map(_uri2peer($_), @list); |
125
|
0
|
|
|
|
|
0
|
last; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} elsif ($uri =~ m{ ^ failover \: (?:\/\/)? \( ([_a-z0-9\.\-\:\/\,]+) \) |
128
|
|
|
|
|
|
|
( \? [_a-z0-9\.\=\-\&]+ ) ? $ }ix) { |
129
|
|
|
|
|
|
|
# failover with options |
130
|
3
|
|
|
|
|
6
|
_default_options(\%option); |
131
|
3
|
100
|
|
|
|
12
|
_parse_options(\%option, $2) if $2; |
132
|
3
|
|
|
|
|
12
|
@peers = map(_uri2peer($_), split(/,/, $1)); |
133
|
3
|
|
|
|
|
5
|
last; |
134
|
|
|
|
|
|
|
} elsif ($uri =~ m{ ^ failover \: ([_a-z0-9\.\-\:\/\,]+) $ }ix) { |
135
|
|
|
|
|
|
|
# failover without options |
136
|
1
|
|
|
|
|
6
|
_default_options(\%option); |
137
|
1
|
|
|
|
|
5
|
@peers = map(_uri2peer($_), split(/,/, $1)); |
138
|
1
|
|
|
|
|
2
|
last; |
139
|
|
|
|
|
|
|
} else { |
140
|
|
|
|
|
|
|
# otherwise this must be a simple URI |
141
|
8
|
|
|
|
|
12
|
@peers = (_uri2peer($uri)); |
142
|
5
|
|
|
|
|
5
|
last; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
} |
145
|
9
|
50
|
|
|
|
14
|
dief("empty server uri: %s", $uri) unless @peers; |
146
|
9
|
|
|
|
|
20
|
return(\@peers, \%option); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# |
150
|
|
|
|
|
|
|
# attempt to connect to one peer (low level) |
151
|
|
|
|
|
|
|
# |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub _attempt ($%) { |
154
|
0
|
|
|
0
|
|
|
my($peer, %sockopt) = @_; |
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
|
0
|
|
|
|
|
if ($peer->proto() =~ /\b(ssl)\b/) { |
163
|
|
|
|
|
|
|
# with SSL |
164
|
0
|
0
|
|
|
|
|
unless ($IO::Socket::SSL::VERSION) { |
165
|
0
|
|
|
|
|
|
eval { require IO::Socket::SSL }; |
|
0
|
|
|
|
|
|
|
166
|
0
|
0
|
|
|
|
|
return(sprintf("cannot load IO::Socket::SSL: %s", $@)) if $@; |
167
|
|
|
|
|
|
|
} |
168
|
0
|
|
|
|
|
|
$socket = IO::Socket::SSL->new(%sockopt); |
169
|
0
|
0
|
|
|
|
|
return(sprintf("cannot SSL connect to %s:%d: %s", $peer->host(), |
170
|
|
|
|
|
|
|
$peer->port(), IO::Socket::SSL::errstr())) |
171
|
|
|
|
|
|
|
unless $socket; |
172
|
|
|
|
|
|
|
} else { |
173
|
|
|
|
|
|
|
# with plain TCP |
174
|
0
|
|
|
|
|
|
$socket = IO::Socket::INET->new(%sockopt); |
175
|
0
|
0
|
|
|
|
|
return(sprintf("cannot connect to %s:%d: %s", $peer->host(), |
176
|
|
|
|
|
|
|
$peer->port(), $!)) |
177
|
|
|
|
|
|
|
unless $socket; |
178
|
0
|
0
|
|
|
|
|
return(sprintf("cannot binmode(socket): %s", $!)) |
179
|
|
|
|
|
|
|
unless binmode($socket); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
# so far so good... |
182
|
0
|
|
|
|
|
|
@{ $peer }[3] = $socket->peerhost(); |
|
0
|
|
|
|
|
|
|
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
|
|
|
0
|
|
|
my($peers, $peeropt, $sockopt, $debug) = @_; |
192
|
0
|
|
|
|
|
|
my(@list, $count, $result); |
193
|
|
|
|
|
|
|
|
194
|
0
|
0
|
|
|
|
|
dief("no peers given!") unless @{ $peers }; |
|
0
|
|
|
|
|
|
|
195
|
0
|
|
|
|
|
|
$count = 0; |
196
|
0
|
|
|
|
|
|
while (1) { |
197
|
0
|
0
|
|
|
|
|
@list = $peeropt->{randomize} ? shuffle(@{ $peers }) : @{ $peers }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
|
foreach my $peer (@list) { |
199
|
0
|
|
|
|
|
|
$result = _attempt($peer, %{ $sockopt }); |
|
0
|
|
|
|
|
|
|
200
|
0
|
0
|
|
|
|
|
if (ref($result)) { |
201
|
0
|
0
|
|
|
|
|
log_debug("connect to %s ok: %s", $peer->uri(), $peer->addr()) |
202
|
|
|
|
|
|
|
if $debug =~ /\b(connection|all)\b/; |
203
|
0
|
|
|
|
|
|
return($result, $peer); |
204
|
|
|
|
|
|
|
} else { |
205
|
0
|
0
|
|
|
|
|
log_debug("connect to %s failed: %s", $peer->uri(), $result) |
206
|
|
|
|
|
|
|
if $debug =~ /\b(connection|all)\b/; |
207
|
|
|
|
|
|
|
} |
208
|
0
|
|
|
|
|
|
$count++; |
209
|
0
|
0
|
|
|
|
|
if (defined($peeropt->{max_attempt})) { |
210
|
0
|
0
|
|
|
|
|
last if $count >= $peeropt->{max_attempt}; |
211
|
|
|
|
|
|
|
} |
212
|
0
|
0
|
|
|
|
|
if ($peeropt->{sleep}) { |
213
|
0
|
|
|
|
|
|
Time::HiRes::sleep($peeropt->{sleep}); |
214
|
0
|
0
|
|
|
|
|
if ($peeropt->{multiplier}) { |
215
|
0
|
|
|
|
|
|
$peeropt->{sleep} *= $peeropt->{multiplier}; |
216
|
0
|
0
|
0
|
|
|
|
if ($peeropt->{max_sleep} and |
217
|
|
|
|
|
|
|
$peeropt->{sleep} > $peeropt->{max_sleep}) { |
218
|
0
|
|
|
|
|
|
$peeropt->{sleep} = $peeropt->{max_sleep}; |
219
|
0
|
|
|
|
|
|
delete($peeropt->{multiplier}); |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
} |
224
|
0
|
0
|
|
|
|
|
if (defined($peeropt->{max_attempt})) { |
225
|
0
|
0
|
|
|
|
|
last if $count >= $peeropt->{max_attempt}; |
226
|
|
|
|
|
|
|
} |
227
|
0
|
0
|
|
|
|
|
last unless keys(%{ $peeropt }); |
|
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
|
|
|
|
|
|
|
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
|
|
|
0
|
1
|
|
my(%option, $proto, $peers, $peeropt); |
266
|
|
|
|
|
|
|
|
267
|
0
|
|
|
|
|
|
%option = validate(@_, \%new_options); |
268
|
0
|
|
0
|
|
|
|
$option{sockopt} ||= {}; |
269
|
0
|
|
0
|
|
|
|
$option{debug} ||= ""; |
270
|
0
|
0
|
|
|
|
|
if (defined($option{uri})) { |
271
|
|
|
|
|
|
|
# by URI |
272
|
|
|
|
|
|
|
dief("unexpected server host: %s", $option{host}) |
273
|
0
|
0
|
|
|
|
|
if defined($option{host}); |
274
|
|
|
|
|
|
|
dief("unexpected server port: %s", $option{port}) |
275
|
0
|
0
|
|
|
|
|
if defined($option{port}); |
276
|
0
|
|
|
|
|
|
($peers, $peeropt) = _parse_uri($option{uri}); |
277
|
|
|
|
|
|
|
} else { |
278
|
|
|
|
|
|
|
# by host + port |
279
|
0
|
0
|
|
|
|
|
dief("missing server host") unless defined($option{host}); |
280
|
0
|
0
|
|
|
|
|
dief("missing server port") unless defined($option{port}); |
281
|
0
|
|
|
|
|
|
$proto = "tcp"; |
282
|
0
|
0
|
|
|
|
|
$proto = "ssl" if grep(/^SSL_/, keys(%{ $option{sockopt} })); |
|
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
|
|
|
|
|
|
|
1; |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
__END__ |