blib/lib/WebSphere/MQTT/Client.pm

filter: Statementstmt 52.5% 73 / 139 Branchbran 34.7% 25 / 72 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 60.0% 12 / 20 Podpod 0.0% 0 / 13 totaltotal 42.0% 113 / 269 CC 44 SCAR 25.2
CC
44
Cov
45%
subscribe
30.0
receivePub
20.0
unsubscribe
20.0
CRAP 12.4
↑ Summary blib/script/publish.pl »
1 package WebSphere::MQTT::Client;
2  
3 ################
4 #
5 # MQTT: WebSphere MQ Telemetry Transport
6 #
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 $VERSION="0.03";
17  
18 3 XSLoader::load('WebSphere::MQTT::Client', $VERSION);
19  
20  
21  
22 7 ▶sub new {
Subroutine new: called CC=6 SCAR=18.1
Pod undocumented
23 7 my $class = shift;
24 7 my (%args) = @_;
25
26 # Store parameters
27 7 my $self = {
28 'host' => '127.0.0.1', # broker's hostname (localhost)
29 'port' => 1883, # broker's port
30 'clientid' => undef, # our client ID
31 'debug' => 0, # debugging disabled
32
33
34 # Advanced options (with sensible defaults)
35 'clean_start' => 1, # set CleanStart flag ?
36 'keep_alive' => 10, # timeout (in seconds) for receiving data
37 'retry_count' => 10,
38 'retry_interval' => 10,
39 'async' => 0,
40 'persist' => undef,
41  
42 # Used internally only
43 'handle' => undef, # Connection Handle
44 'txqueue' => [], # TX messages in transit
45 'send_task_info' => undef, # Send Thread Parameters
46 'recv_task_info' => undef, # Receive Thread Parameters
47 'api_task_info' => undef, # API Thread Parameters
48  
49 # TODO: LWT stuff
50 #'lwt_enabled' => 0,
51 #'lwt_message' => undef,
52 #'lwt_qos' => 0,
53 #'lwt_topic' => undef,
54 #'lwt_retain' => 0,
55  
56 };
57
58 # Bless the hash into an object
59 7 bless $self, $class;
60  
61 # Arguments specified ?
62 7 foreach (keys %args) {
63 38 my $key = $_;
64 38 $key =~ tr/A-Z/a-z/;
65 38 $key =~ s/\W/_/g;
66 38 ▶ $key = 'host' if ($key eq 'hostname');
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "hostname" 7 31 38
67 38 $self->{$key} = $args{$_};
68 }
69
70 # Generate a Client ID if we don't have one
71 7 ▶ if (defined $self->{'clientid'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'clientid'}) { } 0 7 7
72 0 $self->{'clientid'} = substr($self->{'clientid'}, 0, 23);
73 } else {
74 7 my $hostname = hostname();
75 7 my ($host, $domain) = ($hostname =~ /^([^\.]+)\.?(.*)$/);
76 7 $self->{'clientid'} = substr($host, 0, 22-length($$)).'-'.$$;
77 }
78  
79 # Start threads (if enabled)
80 7 ▶ $self->xs_start_tasks() or die("xs_start_tasks() failed");
Branch: 1 branch
BranchTrueFalseTotal
$self->xs_start_tasks or die "xs_start_tasks() failed" 7 0 7
81  
82 # Dump configuration if Debug is enabled
83 7 ▶ $self->dump_config() if ($self->{'debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 7 7
84  
85 7 return $self;
86 }
87  
88  
89 0 ▶sub dump_config {
Subroutine dump_config: not called CC=2 SCAR=17.9
Pod undocumented
90 0 my $self = shift;
91
92 0 print "\n";
93 0 print "WebSphere::MQTT::Client config\n";
94 0 print "==============================\n";
95 0 foreach( sort keys %$self ) {
96 0 printf(" %15s: %s\n", $_, $self->{$_});
97 }
98 0 print "\n";
99  
100 }
101  
102  
103 0 ▶sub debug {
Subroutine debug: not called CC=3 SCAR=24.8
Pod undocumented
104 0 my $self = shift;
105 0 my ($debug) = @_;
106
107 0 ▶ if (defined $debug) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $debug) 0 0 0
108 0 ▶ if ($debug) { $self->{'debug'} = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) { } 0 0 0
109 0 else { $self->{'debug'} = 0; }
110 }
111
112 0 return $self->{'debug'};
113 }
114  
115  
116  
117 7 ▶sub connect {
Subroutine connect: called CC=7 SCAR=19.9
Pod undocumented
118 7 my $self = shift;
119
120 # Connect
121 7 my $result = $self->xs_connect( $self->{'api_task_info'} );
122  
123 # Print the result if debugging enabled
124 7 ▶ print "xs_connect: $result\n" if ($self->{'debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 7 7
125  
126 7 ▶ return $result unless($result eq 'OK');
Branch: 1 branch
BranchTrueFalseTotal
unless $result eq "OK" 2 5 7
127  
128 # New feature in 0.02: an asynchronous connect returns immediately.
129 # The state will sit in CONNECTING for as long as retries take place.
130 # This allows outbound messages to be published (and queued locally
131 # if QOS>0), even if the remote server is currently down.
132 #
133 # Note: when all the retries are used up, the state changes to
134 # CONNECTION_BROKEN and no publishing can take place until you call
135 # connect() again
136 #
137 5 ▶ return 0 if ($self->{'async'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"async"} 1 4 5
138
139 # Wait until we are connected
140 # FIXME: *with timeout*
141 4 while (1) {
142 43 $result = $self->status();
143 43 ▶ last unless $result eq 'CONNECTING';
Branch: 1 branch
BranchTrueFalseTotal
unless $result eq "CONNECTING" 4 39 43
144 39 select(undef, undef, undef, 0.5); # short sleep
145 }
146
147 # Failed to connect ?
148 4 ▶ if ($result ne 'CONNECTED') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result ne "CONNECTED") 4 0 4
149 4 $self->disconnect();
150 # backwards compatibility
151 4 ▶ return 'FAILED' if ($result eq 'CONNECTION_BROKEN');
Branch: 1 branch
BranchTrueFalseTotal
if $result eq "CONNECTION_BROKEN" 4 0 4
152 0 return $result;
153 }
154
155 # Success
156 0 return 0;
157 }
158  
159 4 ▶sub disconnect {
Subroutine disconnect: called CC=5 SCAR=19.1
Pod undocumented
160 4 my $self = shift;
161  
162 # Allow 10 seconds for any messages in transit to be delivered
163 4 ▶ for (my $tries=0; $self->txQueueSize > 0 && $tries < 10; $tries++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->txQueueSize > 0 and $tries < 10) 0 4 4
Condition33%1/3
$self->txQueueSize > 0 and $tries < 10
!ll&&!rl&&r
400
MC/DC0%0/2
$self->txQueueSize > 0 and $tries < 10
$self->txQueueSize > 0 $tries < 10
Truth table33%1/3
A and B
A= $self->txQueueSize > 0B= $tries < 10
ABresult
0X0
100
111
164 0 sleep 1;
165 }
166 4 $self->{'txqueue'} = [];
167  
168 # Disconnect
169 4 my $result = $self->xs_disconnect();
170
171 # Print the result if debugging enabled
172 4 ▶ print "xs_disconnect: $result\n" if ($self->{'debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 4 4
173
174 # Return 0 if result is OK
175 4 ▶ return 0 if ($result eq 'OK');
Branch: 1 branch
BranchTrueFalseTotal
if $result eq "OK" 4 0 4
176 0 return $result;
177 }
178  
179 4 ▶sub publish {
Subroutine publish: called CC=9 SCAR=25.4
Pod undocumented
180 4 my $self = shift;
181 4 my ($data, $topic, $qos, $retain, $cbfunc, $cbarg) = @_;
182  
183 4 ▶ croak("Usage: publish(data, topic, [qos, [retain]]") unless ((defined $data) && (defined $topic));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and defined $topic 0 4 4
Condition33%1/3
defined $data and defined $topic
!ll&&!rl&&r
004
MC/DC0%0/2
defined $data and defined $topic
defined $data defined $topic
Truth table33%1/3
A and B
A= defined $dataB= defined $topic
ABresult
0X0
100
111
184 4 ▶ $qos = 0 unless (defined $qos);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qos 0 4 4
185 4 ▶ $retain = 0 unless (defined $retain);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $retain 4 0 4
186  
187 # Keep the queue of TX message IDs tidy, because publishing a message
188 # may allocate a new message ID (possibly re-using an old one).
189 # Also gives an opportunity to invoke callbacks.
190 4 $self->txQueueSize;
191  
192 # Publish
193 4 my ($result,$hmsg) = $self->xs_publish( $data, $topic, $qos, $retain );
194  
195 # Print the result if debugging enabled
196 4 ▶ print "xs_publish[$data][$topic]: $result, $hmsg\n" if ($self->{'debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 4 4
197  
198 4 ▶ return $result if $result ne 'OK';
Branch: 1 branch
BranchTrueFalseTotal
if $result ne "OK" 2 2 4
199  
200 # New feature in 0.03: caller can provide callback function
201 # and argument, which will be invoked when message has had
202 # its delivery ACK'd.
203 # This allows QOS 1 publishers to use their existing queue
204 # without copying into the MQISDP persistence layer.
205 2 ▶ if ($cbfunc && $qos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cbfunc and $qos) 0 2 2
Condition33%1/3
$cbfunc and $qos
!ll&&!rl&&r
200
MC/DC0%0/2
$cbfunc and $qos
$cbfunc $qos
Truth table33%1/3
A and B
A= $cbfuncB= $qos
ABresult
0X0
100
111
206 0 push @{$self->{'txqueue'}}, [$hmsg, $cbfunc, $cbarg];
207 }
208 2 return 0;
209 }
210  
211 0 ▶sub subscribe {
Subroutine subscribe: not called CC=5 SCAR=34.0
Pod undocumented
212 0 my $self = shift;
213 0 my ($topic, $qos) = @_;
214
215 0 ▶ croak("Usage: subscribe(topic, [qos])") unless (defined $topic);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topic 0 0 0
216 0 ▶ $qos = 0 unless (defined $qos);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qos 0 0 0
217  
218 # Subscribe
219 0 my $result = $self->xs_subscribe( $topic, $qos );
220
221 # Print the result if debugging enabled
222 0 ▶ print "xs_subscribe[$topic]: $result\n" if ($self->{'debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
223
224 # Return 0 if result is OK
225 0 ▶ return 0 if ($result eq 'OK');
Branch: 1 branch
BranchTrueFalseTotal
if $result eq "OK" 0 0 0
226 0 return $result;
227 }
228  
229  
230 0 ▶sub receivePub {
Subroutine receivePub: not called CC=4 SCAR=30.0
Pod undocumented
231 0 my $self = shift;
232 # my(%args) = @_;
233 # FIXME: only receive messages which look like match=>'patt'
234  
235 0 $self->txQueueSize;
236 0 my $result = $self->xs_receivePub();
237  
238 # Print the result if debugging enabled
239 0 ▶ if ($self->{'debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
240 0 print "xs_receivePub[".$result->{'topic'}."]: ";
241 0 print $result->{'data'}."\n";
242 }
243  
244 # Note: if an error occurs (e.g. connection lost), we will get
245 # $result->{'status'} but nothing else. For API compatibility, we
246 # will treat this as a fatal error. If the application cares, it can
247 # use eval to catch this.
248 croak("receivePub status: $result->{'status'}") if
249 0 ▶ ($result->{'status'} ne 'OK' && $result->{'status'} ne 'PUBS_AVAILABLE');
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"status"} ne "OK" and $result->{"status"} ne "PUBS_AVAILABLE" 0 0 0
Condition0%0/3
$result->{"status"} ne "OK" and $result->{"status"} ne "PUBS_AVAILABLE"
!ll&&!rl&&r
000
MC/DC0%0/2
$result->{"status"} ne "OK" and $result->{"status"} ne "PUBS_AVAILABLE"
$result->{"status"} ne "OK" $result->{"status"} ne "PUBS_AVAILABLE"
Truth table0%0/3
A and B
A= $result->{"status"} ne "OK"B= $result->{"status"} ne "PUBS_AVAILABLE"
ABresult
0X0
100
111
250
251 0 return ( $result->{'topic'}, $result->{'data'}, $result->{'options'} );
252 }
253  
254 0 ▶sub unsubscribe {
Subroutine unsubscribe: not called CC=4 SCAR=30.0
Pod undocumented
255 0 my $self = shift;
256 0 my ($topic) = @_;
257
258 0 ▶ croak("Usage: unsubscribe(topic)") unless (defined $topic);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $topic 0 0 0
259  
260 # Subscribe
261 0 my $result = $self->xs_unsubscribe( $topic );
262
263 # Print the result if debugging enabled
264 0 ▶ print "xs_unsubscribe[$topic]: $result\n" if ($self->{'debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
265
266 # Return 0 if result is OK
267 0 ▶ return 0 if ($result eq 'OK');
Branch: 1 branch
BranchTrueFalseTotal
if $result eq "OK" 0 0 0
268 0 return $result;
269 }
270  
271  
272 43 ▶sub status {
Subroutine status: called CC=1 SCAR=0.0
Pod undocumented
273 43 my $self = shift;
274 43 $self->txQueueSize;
275 43 return $self->xs_status();
276 }
277  
278 #
279 # Check the status of any messages 'in transit', perform callbacks for
280 # those which have been delivered or dropped, and return the number of
281 # messages still left
282 #
283 51 ▶sub txQueueSize {
Subroutine txQueueSize: called CC=5 SCAR=29.5
Pod undocumented
284 51 my $self = shift;
285 51 my $q = $self->{'txqueue'};
286 51 ▶ return 0 unless @$q;
Branch: 1 branch
BranchTrueFalseTotal
unless @$q 51 0 51
287 0 my $i = 0;
288 #print "--- txQueue ---\n";
289 0 ▶ while ($i < @$q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$q) 0 0 0
290 0 my ($hmsg, $cbfunc, $cbarg) = @{$q->[$i]};
291 0 my $s = $self->xs_getMsgStatus($hmsg);
292 #print "Message $hmsg status $s\n";
293 0 ▶ if ($s eq 'DELIVERED') {
Branch: 2 branches
BranchTrueFalseTotal
if ($s eq 'DELIVERED') { } 0 0 0
elsif ($s =~ /ERROR/) { } 0 0 0
294 0 $cbfunc->(0, $cbarg); # success
295 0 splice @$q, $i, 1;
296 }
297 elsif ($s =~ /ERROR/) {
298 0 $cbfunc->($s, $cbarg); # fail
299 0 splice @$q, $i, 1;
300 }
301 else {
302 0 $i++; # still in transit
303 }
304 }
305 0 return scalar(@$q);
306 }
307  
308 0 ▶sub terminate {
Subroutine terminate: not called CC=4 SCAR=30.0
Pod undocumented
309 0 my $self = shift;
310  
311 # Disconnect first (if connected)
312 0 ▶ if (exists $self->{'handle'} and defined $self->{'handle'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"handle"} and defined $self->{"handle"}) 0 0 0
Condition0%0/3
exists $self->{"handle"} and defined $self->{"handle"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"handle"} and defined $self->{"handle"}
exists $self->{"handle"} defined $self->{"handle"}
Truth table0%0/3
A and B
A= exists $self->{"handle"}B= defined $self->{"handle"}
ABresult
0X0
100
111
313 0 $self->disconnect();
314 }
315  
316 # Terminate threads and free memory
317 0 my $result = $self->xs_terminate();
318
319 # Return 0 if result is OK
320 0 ▶ return 0 if ($result eq 'OK');
Branch: 1 branch
BranchTrueFalseTotal
if $result eq "OK" 0 0 0
321 0 return $result;
322 }
323  
324 0 ▶sub libversion {
Subroutine libversion: not called CC=1 SCAR=6.9
Pod undocumented
325 0 return eval { xs_version(); };
326 }
327  
328  
329 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
330 0 my $self=shift;
331
332 0 $self->terminate();
333 }
334  
335  
336 3 1;
337  
338 __END__
↑ Summary blib/script/publish.pl »