| 1 | =pod | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MOBY::Async::Service - an object for communicating with Asynchronous MOBY Services | ||
| 6 | |||
| 7 | =head1 AUTHORS | ||
| 8 | |||
| 9 | Former developer | ||
| 10 | Enrique de Andres Saiz (enrique.deandres@pcm.uam.es) - | ||
| 11 | INB GNHC-1 (Madrid Science Park, Spain) (2006-2007). | ||
| 12 | |||
| 13 | Maintainers | ||
| 14 | Jose Maria Fernandez (jmfernandez@cnio.es), | ||
| 15 | Jose Manuel Rodriguez (jmrodriguez@cnio.es) - | ||
| 16 | INB GN2 (CNIO, Spain). | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | It provides a class to invoke asynchronous services. Its use is very similar to | ||
| 21 | MOBY::Client::Service because it is its super-class. It also provides additional | ||
| 22 | methods in order to have more control over the asynchronous service execution. | ||
| 23 | |||
| 24 | =head1 METHODS | ||
| 25 | |||
| 26 | =head2 new | ||
| 27 | |||
| 28 | Name : new | ||
| 29 | Function : create a service connection. | ||
| 30 | Usage : $Service = MOBY::Async::Service->new(@args) | ||
| 31 | Args : service - string with a WSDL defining an asynchronous | ||
| 32 | MOBY service | ||
| 33 | Returns : MOBY::Async::Service object, undef if no wsdl. | ||
| 34 | |||
| 35 | =head2 silent | ||
| 36 | |||
| 37 | Name : silent | ||
| 38 | Function : get/set silent mode; if silent is not set, status messages | ||
| 39 | reported when execute method is invoked. | ||
| 40 | Usage : $Service->silent() | ||
| 41 | $Service->silent($boolean) | ||
| 42 | Args : $boolean - 0 or 1 (default). | ||
| 43 | Returns : 0 or 1. | ||
| 44 | |||
| 45 | =head2 execute | ||
| 46 | |||
| 47 | Name : execute | ||
| 48 | Function : execute the asynchronous MOBY service; this method invoke | ||
| 49 | internally to the submit, poll and result methods. It | ||
| 50 | calculates polling time according to the status messages | ||
| 51 | received from the provider. If from that messages is not | ||
| 52 | possible to infer the polling time, it calculates a | ||
| 53 | pseudo-random polling time, whoose value increases until | ||
| 54 | is up to around 1 hour. | ||
| 55 | Usage : $result = $Service->execute(%args) | ||
| 56 | Args : XMLinputlist => \@data | ||
| 57 | Returns : a MOBY message containing whatever the service provides | ||
| 58 | as output. | ||
| 59 | Comment : for more information about arguments look up execute | ||
| 60 | method at MOBY::Client::Service. | ||
| 61 | |||
| 62 | =head2 enumerated_execute | ||
| 63 | |||
| 64 | Name : enumerated_execute | ||
| 65 | Function : execute the asynchronous MOBY service using self-enumerated | ||
| 66 | inputs; this method invoke internally to the enumerated_submit, | ||
| 67 | poll and result methods. It calculates polling time according | ||
| 68 | to the status messages received from the provider. If from | ||
| 69 | that messages is not possible to infer the polling time, it | ||
| 70 | calculates a pseudo-random polling time, whoose value increases | ||
| 71 | until is up to around 1 hour. | ||
| 72 | Usage : $result = $Service->execute(%args) | ||
| 73 | Args : Input => \%data | ||
| 74 | Returns : a MOBY message containing whatever the service provides | ||
| 75 | as output. | ||
| 76 | Comment : for more information about arguments look up enumerated_execute | ||
| 77 | method at MOBY::Client::Service. | ||
| 78 | |||
| 79 | =head2 submit | ||
| 80 | |||
| 81 | Name : submit | ||
| 82 | Function : submit the asynchronous MOBY service. | ||
| 83 | Usage : ($EPR, @queryIDs) = $Service->submit(%args) | ||
| 84 | Args : XMLinputlist => \@data | ||
| 85 | Returns : WSRF::WS_Address object with an EPR and the input queryIDs. | ||
| 86 | Comment : for more information about arguments look up execute | ||
| 87 | method at MOBY::Client::Service. | ||
| 88 | |||
| 89 | =head2 enumerated_submit | ||
| 90 | |||
| 91 | Name : enumerated_submit | ||
| 92 | Function : submit the asynchronous MOBY service using self-enumerated | ||
| 93 | inputs. | ||
| 94 | Usage : ($EPR, @queryIDs) = $Service->submit(%args) | ||
| 95 | Args : XMLinputlist => \%data | ||
| 96 | Returns : WSRF::WS_Address object with an EPR and the input queryIDs. | ||
| 97 | Comment : for more information about arguments look up enumerated_execute | ||
| 98 | method at MOBY::Client::Service. | ||
| 99 | |||
| 100 | =head2 poll | ||
| 101 | |||
| 102 | Name : poll | ||
| 103 | Function : gets the status of a set of queryIDs. | ||
| 104 | Usage : @status = $Service->poll($EPR, @queryIDs) | ||
| 105 | Args : $EPR - WSRF::WS_Address object. | ||
| 106 | @queryIDs - an array containing queryIDs values. | ||
| 107 | Returns : an array of LSAE::AnalysisEventBlock objects. | ||
| 108 | |||
| 109 | =head2 result | ||
| 110 | |||
| 111 | Name : result | ||
| 112 | Function : get the result of a set of queryIDs. | ||
| 113 | Usage : @result = $Service->result($EPR, @queryIDs) | ||
| 114 | Args : $EPR - WSRF::WS_Address object. | ||
| 115 | @queryIDs - an array containing queryIDs values. | ||
| 116 | Returns : an array of MOBY messages. | ||
| 117 | |||
| 118 | =head2 destroy | ||
| 119 | |||
| 120 | Name : destroy | ||
| 121 | Function : destroy the resource associated to the execution of | ||
| 122 | an asynchronous MOBY service. | ||
| 123 | Usage : $Service->result($EPR); | ||
| 124 | Args : $EPR - WSRF::WS_Address object. | ||
| 125 | Returns : nothing. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | package MOBY::Async::Service; | ||
| 130 | use strict; | ||
| 131 | use XML::LibXML; | ||
| 132 | use MOBY::Async::WSRF; | ||
| 133 | use MOBY::Async::LSAE; | ||
| 134 | use MOBY::CommonSubs qw(:all); | ||
| 135 | use MOBY::Client::Service; | ||
| 136 | use base qw(MOBY::Client::Service); | ||
| 137 | |||
| 138 | use vars qw /$VERSION/; | ||
| 139 | $VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/; | ||
| 140 | |||
| 141 | sub _getPollingTime($$$@); | ||
| 142 | sub _getServiceEndpoint($); | ||
| 143 | sub _getPseudoRandomPollingTime($$); | ||
| 144 | sub _composeResponse(@); | ||
| 145 | |||
| 146 | sub new { | ||
| 147 | my ($this, %args) = @_; | ||
| 148 | my $class = ref($this) || $this; | ||
| 149 | |||
| 150 | my $self = $class->SUPER::new(%args); | ||
| 151 | $self->{silent} = 1; | ||
| 152 | |||
| 153 | bless $self, $class; | ||
| 154 | return $self; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub silent { | ||
| 158 | my $self = shift; | ||
| 159 | $self->{silent} = shift; | ||
| 160 | return $self->{silent}; | ||
| 161 | } | ||
| 162 | |||
| 163 | =head2 raw_execute | ||
| 164 | |||
| 165 | Calls the service asynchronously with the given scalar XML input. Behaves exactly as C<execute>. | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub raw_execute { | ||
| 170 | my ($self, $input) = @_; | ||
| 171 | |||
| 172 | my $start = time; | ||
| 173 | my ($EPR, @queryIDs) = $self->raw_submit($input); | ||
| 174 | |||
| 175 | my $pollingTime; | ||
| 176 | my ($i, $j) = (0, 1); | ||
| 177 | my @status; | ||
| 178 | while ( $pollingTime = _getPollingTime($i, $j, $start, @status) ) { | ||
| 179 | ($i, $j) = ($j, $i+$j); | ||
| 180 | |||
| 181 | print "(next polling in $pollingTime seconds)\n\n" unless ($self->{silent}); | ||
| 182 | |||
| 183 | sleep $pollingTime; | ||
| 184 | @status = $self->poll($EPR, @queryIDs); | ||
| 185 | |||
| 186 | unless ($self->{silent}) { | ||
| 187 | foreach my $st (@status) { | ||
| 188 | print $st->XML."\n"; | ||
| 189 | } | ||
| 190 | print "\n"; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | my @responses = $self->result($EPR, @queryIDs); | ||
| 195 | $self->destroy($EPR); | ||
| 196 | my $response = _composeResponse(@responses); | ||
| 197 | |||
| 198 | print "Finished.\n\n" unless ($self->{silent}); | ||
| 199 | |||
| 200 | return $response; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub execute { | ||
| 204 | my ($self, %args) = @_; | ||
| 205 | |||
| 206 | my $start = time; | ||
| 207 | my ($EPR, @queryIDs) = $self->submit(%args); | ||
| 208 | |||
| 209 | my $pollingTime; | ||
| 210 | my ($i, $j) = (0, 1); | ||
| 211 | my @status; | ||
| 212 | while ( $pollingTime = _getPollingTime($i, $j, $start, @status) ) { | ||
| 213 | ($i, $j) = ($j, $i+$j); | ||
| 214 | |||
| 215 | print "(next polling in $pollingTime seconds)\n\n" unless ($self->{silent}); | ||
| 216 | |||
| 217 | sleep $pollingTime; | ||
| 218 | @status = $self->poll($EPR, @queryIDs); | ||
| 219 | |||
| 220 | unless ($self->{silent}) { | ||
| 221 | foreach my $st (@status) { | ||
| 222 | print $st->XML."\n"; | ||
| 223 | } | ||
| 224 | print "\n"; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | my @responses = $self->result($EPR, @queryIDs); | ||
| 229 | $self->destroy($EPR); | ||
| 230 | my $response = _composeResponse(@responses); | ||
| 231 | |||
| 232 | print "Finished.\n\n" unless ($self->{silent}); | ||
| 233 | |||
| 234 | return $response; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub submit { | ||
| 238 | my ($self, %args) = @_; | ||
| 239 | |||
| 240 | print "Creating WS-Resource...\n\n" unless ($self->{silent}); | ||
| 241 | |||
| 242 | # Compose the moby message (part of this block is copied from MOBY::Client::Service) | ||
| 243 | die "ERROR: expected listref for XMLinputlist" unless ( ref( $args{XMLinputlist} ) eq 'ARRAY' ); | ||
| 244 | my @inputs = @{ $args{XMLinputlist} }; | ||
| 245 | my @queryIDs; | ||
| 246 | my $data; | ||
| 247 | foreach ( @inputs ) { | ||
| 248 | |||
| 249 | die "ERROR: expected listref [articleName, XML] for data element" unless ( ref( $_ ) eq 'ARRAY' ); | ||
| 250 | my $qID = $self->_nextQueryID; | ||
| 251 | push (@queryIDs, $qID); | ||
| 252 | $data .= "<moby:mobyData queryID='$qID'>"; | ||
| 253 | |||
| 254 | while ( my ( $articleName, $XML ) = splice( @{$_}, 0, 2 ) ) { | ||
| 255 | |||
| 256 | $articleName ||= ""; | ||
| 257 | if ( ref( $XML ) ne 'ARRAY' ) { | ||
| 258 | |||
| 259 | $XML ||= ""; | ||
| 260 | if ( $XML =~ /\<(moby\:|)Value\>/ ) { | ||
| 261 | $data .= "<moby:Parameter moby:articleName='$articleName'>$XML</moby:Parameter>"; | ||
| 262 | } else { | ||
| 263 | $data .= "<moby:Simple moby:articleName='$articleName'>\n$XML\n</moby:Simple>\n"; | ||
| 264 | } | ||
| 265 | |||
| 266 | } elsif ( ref( $XML ) eq 'ARRAY' ) { | ||
| 267 | |||
| 268 | my @objs = @{$XML}; | ||
| 269 | $data .= "<moby:Collection moby:articleName='$articleName'>\n"; | ||
| 270 | foreach ( @objs ) { | ||
| 271 | $data .= "<moby:Simple>$_</moby:Simple>\n"; | ||
| 272 | } | ||
| 273 | $data .= "</moby:Collection>\n"; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | $data .= "</moby:mobyData>\n"; | ||
| 277 | } | ||
| 278 | my $version = $self->{smessageVersion}; | ||
| 279 | $data = "<?xml version='1.0' encoding='UTF-8'?> | ||
| 280 | <moby:MOBY moby:smessageVersion='$version' xmlns:moby='$WSRF::Constants::MOBY_MESSAGE_NS' xmlns='$WSRF::Constants::MOBY_MESSAGE_NS'> | ||
| 281 | <moby:mobyContent> | ||
| 282 | $data | ||
| 283 | </moby:mobyContent> | ||
| 284 | </moby:MOBY>"; | ||
| 285 | |||
| 286 | # Create the resource and submit the batch-call | ||
| 287 | my $func = $self->{serviceName}.'_submit'; | ||
| 288 | my $ans = WSRF::Lite | ||
| 289 | -> proxy(_getServiceEndpoint($self->{service})) | ||
| 290 | -> uri($WSRF::Constants::MOBY) | ||
| 291 | -> $func(SOAP::Data->value($data)->type('string')); | ||
| 292 | die "ERROR: ".$ans->faultstring if ($ans->fault); | ||
| 293 | |||
| 294 | # Get address from the returned Endpoint Reference | ||
| 295 | my $address = $ans->match("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}Address") ? | ||
| 296 | $ans->valueof("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}Address") : | ||
| 297 | die "ERROR: no EndpointReference returned"; | ||
| 298 | die "ERROR: no address into returned EndpointReference" unless ($address); | ||
| 299 | |||
| 300 | # Get resource identifier from the returned Endpoint Reference | ||
| 301 | my $identifier; | ||
| 302 | if ($ans->dataof("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}ReferenceParameters/*")) { | ||
| 303 | foreach my $a ($ans->dataof("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}ReferenceParameters/*")) { | ||
| 304 | my $name = $a->name(); | ||
| 305 | my $uri = $a->uri(); | ||
| 306 | my $value = $a->value(); | ||
| 307 | if ($name eq "ServiceInvocationId" && $uri eq $WSRF::Constants::MOBY) { | ||
| 308 | $identifier = $value; | ||
| 309 | last; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | } | ||
| 313 | die "ERROR: no identifier into returned EndpointReference" unless ($identifier); | ||
| 314 | |||
| 315 | # Compose the Endpoint Reference | ||
| 316 | my $EPR = WSRF::WS_Address->new(); | ||
| 317 | $EPR->Address($address); | ||
| 318 | $EPR->ReferenceParameters('<mobyws:ServiceInvocationId xmlns:mobyws="'.$WSRF::Constants::MOBY.'">'.$identifier.'</mobyws:ServiceInvocationId>'); | ||
| 319 | |||
| 320 | print XML::LibXML->new->parse_string($EPR->XML)->getDocumentElement()->toString."\n\n" unless ($self->{silent}); | ||
| 321 | |||
| 322 | $SIG{TERM} = sub { | ||
| 323 | $self->destroy($EPR); | ||
| 324 | print "Finished.\n\n" unless ($self->{silent}); | ||
| 325 | exit; | ||
| 326 | }; | ||
| 327 | $SIG{INT} = sub { | ||
| 328 | $self->destroy($EPR); | ||
| 329 | print "Finished.\n\n" unless ($self->{silent}); | ||
| 330 | exit; | ||
| 331 | }; | ||
| 332 | |||
| 333 | # Return Endpoint Reference and the queryIDs | ||
| 334 | return ($EPR, @queryIDs); | ||
| 335 | } | ||
| 336 | |||
| 337 | sub raw_submit { | ||
| 338 | my ($self, $xml) = @_; | ||
| 339 | |||
| 340 | print "Creating WS-Resource...\n\n" unless ($self->{silent}); | ||
| 341 | |||
| 342 | my @queryIDs = $self->_get_query_ids($xml); | ||
| 343 | my $data = $xml; | ||
| 344 | |||
| 345 | # Create the resource and submit the batch-call | ||
| 346 | my $func = $self->{serviceName}.'_submit'; | ||
| 347 | my $ans = WSRF::Lite | ||
| 348 | -> proxy(_getServiceEndpoint($self->{service})) | ||
| 349 | -> uri($WSRF::Constants::MOBY) | ||
| 350 | -> $func(SOAP::Data->value($data)->type('string')); | ||
| 351 | die "ERROR: ".$ans->faultstring if ($ans->fault); | ||
| 352 | |||
| 353 | # Get address from the returned Endpoint Reference | ||
| 354 | my $address = $ans->match("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}Address") ? | ||
| 355 | $ans->valueof("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}Address") : | ||
| 356 | die "ERROR: no EndpointReference returned"; | ||
| 357 | die "ERROR: no address into returned EndpointReference" unless ($address); | ||
| 358 | |||
| 359 | # Get resource identifier from the returned Endpoint Reference | ||
| 360 | my $identifier; | ||
| 361 | if ($ans->dataof("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}ReferenceParameters/*")) { | ||
| 362 | foreach my $a ($ans->dataof("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}ReferenceParameters/*")) { | ||
| 363 | my $name = $a->name(); | ||
| 364 | my $uri = $a->uri(); | ||
| 365 | my $value = $a->value(); | ||
| 366 | if ($name eq "ServiceInvocationId" && $uri eq $WSRF::Constants::MOBY) { | ||
| 367 | $identifier = $value; | ||
| 368 | last; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | } | ||
| 372 | die "ERROR: no identifier into returned EndpointReference" unless ($identifier); | ||
| 373 | |||
| 374 | # Compose the Endpoint Reference | ||
| 375 | my $EPR = WSRF::WS_Address->new(); | ||
| 376 | $EPR->Address($address); | ||
| 377 | $EPR->ReferenceParameters('<mobyws:ServiceInvocationId xmlns:mobyws="'.$WSRF::Constants::MOBY.'">'.$identifier.'</mobyws:ServiceInvocationId>'); | ||
| 378 | |||
| 379 | print XML::LibXML->new->parse_string($EPR->XML)->getDocumentElement()->toString."\n\n" unless ($self->{silent}); | ||
| 380 | |||
| 381 | $SIG{TERM} = sub { | ||
| 382 | $self->destroy($EPR); | ||
| 383 | print "Finished.\n\n" unless ($self->{silent}); | ||
| 384 | exit; | ||
| 385 | }; | ||
| 386 | $SIG{INT} = sub { | ||
| 387 | $self->destroy($EPR); | ||
| 388 | print "Finished.\n\n" unless ($self->{silent}); | ||
| 389 | exit; | ||
| 390 | }; | ||
| 391 | |||
| 392 | # Return Endpoint Reference and the queryIDs | ||
| 393 | return ($EPR, @queryIDs); | ||
| 394 | } | ||
| 395 | |||
| 396 | sub _get_query_ids { | ||
| 397 | my ($self, $input) = @_; | ||
| 398 | my @query_ids = (); | ||
| 399 | my $parser = XML::LibXML->new(); | ||
| 400 | my $doc = $parser->parse_string($input); | ||
| 401 | my $iterator = $doc->getElementsByLocalName("mobyData"); | ||
| 402 | for ( 1 .. $iterator->size() ) { | ||
| 403 | my $node = $iterator->get_node($_); | ||
| 404 | my $id = $node->getAttribute("queryID") | ||
| 405 | || $node->getAttribute( | ||
| 406 | $node->lookupNamespacePrefix($WSRF::Constants::MOBY_MESSAGE_NS) | ||
| 407 | . ":queryID" ); | ||
| 408 | push @query_ids, $id; | ||
| 409 | } | ||
| 410 | return @query_ids; | ||
| 411 | } | ||
| 412 | |||
| 413 | sub enumerated_execute { | ||
| 414 | my ($self, %args) = @_; | ||
| 415 | |||
| 416 | my $start = time; | ||
| 417 | my ($EPR, @queryIDs) = $self->enumerated_submit(%args); | ||
| 418 | |||
| 419 | my $pollingTime; | ||
| 420 | my ($i, $j) = (0, 1); | ||
| 421 | my @status; | ||
| 422 | while ( $pollingTime = _getPollingTime($i, $j, $start, @status) ) { | ||
| 423 | ($i, $j) = ($j, $i+$j); | ||
| 424 | |||
| 425 | print "(next polling in $pollingTime seconds)\n\n" unless ($self->{silent}); | ||
| 426 | |||
| 427 | sleep $pollingTime; | ||
| 428 | @status = $self->poll($EPR, @queryIDs); | ||
| 429 | |||
| 430 | unless ($self->{silent}) { | ||
| 431 | foreach my $st (@status) { | ||
| 432 | print $st->XML."\n"; | ||
| 433 | } | ||
| 434 | print "\n"; | ||
| 435 | } | ||
| 436 | } | ||
| 437 | |||
| 438 | my @responses = $self->result($EPR, @queryIDs); | ||
| 439 | $self->destroy($EPR); | ||
| 440 | my $response = _composeResponse(@responses); | ||
| 441 | |||
| 442 | print "Finished.\n\n" unless ($self->{silent}); | ||
| 443 | |||
| 444 | return $response; | ||
| 445 | } | ||
| 446 | |||
| 447 | sub enumerated_submit { | ||
| 448 | my ($self, %args) = @_; | ||
| 449 | |||
| 450 | print "Creating WS-Resource...\n\n" unless ($self->{silent}); | ||
| 451 | |||
| 452 | # Compose the moby message (part of this block is copied from MOBY::Client::Service) | ||
| 453 | die "ERROR: expected Input to be a HASH ref" unless ( ref( $args{Input} ) eq 'HASH' ); | ||
| 454 | my %inputs = %{$args{Input}}; | ||
| 455 | my @queryIDs = keys %inputs; | ||
| 456 | my $data; | ||
| 457 | foreach my $qID ( @queryIDs ) { | ||
| 458 | |||
| 459 | die "ERROR: expected hashref {articleName => XML} for each queryID" unless ( ref($inputs{$qID}) eq 'HASH' ); | ||
| 460 | my %articles = %{$inputs{$qID}}; | ||
| 461 | $data .= "<moby:mobyData queryID='$qID'>"; | ||
| 462 | |||
| 463 | foreach my $articleName(keys %articles){ | ||
| 464 | |||
| 465 | my $XML = $articles{$articleName}; | ||
| 466 | if ( ref( $XML ) ne 'ARRAY' ) { | ||
| 467 | |||
| 468 | $XML ||= ""; | ||
| 469 | if ( $XML =~ /\<(moby\:|)Value\>/ ){ | ||
| 470 | $data .= "<moby:Parameter moby:articleName='$articleName'>$XML</moby:Parameter>"; | ||
| 471 | } else { | ||
| 472 | $data .= "<moby:Simple moby:articleName='$articleName'>\n$XML\n</moby:Simple>\n"; | ||
| 473 | } | ||
| 474 | |||
| 475 | } elsif ( ref( $XML ) eq 'ARRAY' ) { | ||
| 476 | |||
| 477 | my @objs = @{$XML}; | ||
| 478 | $data .= "<moby:Collection moby:articleName='$articleName'>\n"; | ||
| 479 | foreach ( @objs ) { | ||
| 480 | $data .= "<moby:Simple>$_</moby:Simple>\n"; | ||
| 481 | } | ||
| 482 | $data .= "</moby:Collection>\n"; | ||
| 483 | } | ||
| 484 | } | ||
| 485 | $data .= "</moby:mobyData>\n"; | ||
| 486 | } | ||
| 487 | my $version = $self->{smessageVersion}; | ||
| 488 | $data = "<?xml version='1.0' encoding='UTF-8'?> | ||
| 489 | <moby:MOBY moby:smessageVersion='$version' xmlns='$WSRF::Constants::MOBY_MESSAGE_NS' xmlns:moby='$WSRF::Constants::MOBY_MESSAGE_NS'> | ||
| 490 | <moby:mobyContent> | ||
| 491 | $data | ||
| 492 | </moby:mobyContent> | ||
| 493 | </moby:MOBY>"; | ||
| 494 | |||
| 495 | # Create the resource and submit the batch-call | ||
| 496 | my $func = $self->{serviceName}.'_submit'; | ||
| 497 | my $ans = WSRF::Lite | ||
| 498 | -> proxy(_getServiceEndpoint($self->{service})) | ||
| 499 | -> uri($WSRF::Constants::MOBY) | ||
| 500 | -> $func(SOAP::Data->value($data)->type('string')); | ||
| 501 | die "ERROR: ".$ans->faultstring if ($ans->fault); | ||
| 502 | |||
| 503 | # Get address from the returned Endpoint Reference | ||
| 504 | my $address = $ans->match("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}Address") ? | ||
| 505 | $ans->valueof("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}Address") : | ||
| 506 | die "ERROR: no EndpointReference returned"; | ||
| 507 | die "ERROR: no address into returned EndpointReference" unless ($address); | ||
| 508 | |||
| 509 | # Get resource identifier from the returned Endpoint Reference | ||
| 510 | my $identifier; | ||
| 511 | if ($ans->dataof("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}ReferenceParameters/*")) { | ||
| 512 | foreach my $a ($ans->dataof("//{$SOAP::Constants::NS_ENV}Body//{$WSRF::Constants::WSA}ReferenceParameters/*")) { | ||
| 513 | my $name = $a->name(); | ||
| 514 | my $uri = $a->uri(); | ||
| 515 | my $value = $a->value(); | ||
| 516 | if ($name eq "ServiceInvocationId" && $uri eq $WSRF::Constants::MOBY) { | ||
| 517 | $identifier = $value; | ||
| 518 | last; | ||
| 519 | } | ||
| 520 | } | ||
| 521 | } | ||
| 522 | die "ERROR: no identifier into returned EndpointReference" unless ($identifier); | ||
| 523 | |||
| 524 | # Compose the Endpoint Reference | ||
| 525 | my $EPR = WSRF::WS_Address->new(); | ||
| 526 | $EPR->Address($address); | ||
| 527 | $EPR->ReferenceParameters('<mobyws:ServiceInvocationId xmlns:mobyws="'.$WSRF::Constants::MOBY.'">'.$identifier.'</mobyws:ServiceInvocationId>'); | ||
| 528 | |||
| 529 | print XML::LibXML->new->parse_string($EPR->XML)->getDocumentElement()->toString."\n\n" unless ($self->{silent}); | ||
| 530 | |||
| 531 | $SIG{TERM} = sub { | ||
| 532 | $self->destroy($EPR); | ||
| 533 | print "Finished.\n\n" unless ($self->{silent}); | ||
| 534 | exit; | ||
| 535 | }; | ||
| 536 | $SIG{INT} = sub { | ||
| 537 | $self->destroy($EPR); | ||
| 538 | print "Finished.\n\n" unless ($self->{silent}); | ||
| 539 | exit; | ||
| 540 | }; | ||
| 541 | |||
| 542 | # Return Endpoint Reference and the queryIDs | ||
| 543 | return ($EPR, @queryIDs); | ||
| 544 | } | ||
| 545 | |||
| 546 | sub poll { | ||
| 547 | my ($self, $EPR, @queryIDs) = @_; | ||
| 548 | |||
| 549 | print "Polling...\n\n" unless ($self->{silent}); | ||
| 550 | |||
| 551 | my $searchTerm = ""; | ||
| 552 | foreach my $queryID (@queryIDs) { | ||
| 553 | #$searchTerm .= "<wsrp:ResourceProperty xmlns:wsrp='$WSRF::Constants::WSRP' xmlns:mobyws='$WSRF::Constants::MOBY'>"; | ||
| 554 | #$searchTerm .= "mobyws:status_".$queryID; | ||
| 555 | #$searchTerm .= "</wsrp:ResourceProperty>"; | ||
| 556 | $searchTerm .= "<wsrp:ResourceProperty xmlns:wsrp='$WSRF::Constants::WSRP' xmlns:mobyws='$WSRF::Constants::MOBY'>"; | ||
| 557 | $searchTerm .= "mobyws:status_".$queryID; | ||
| 558 | $searchTerm .= "</wsrp:ResourceProperty>"; | ||
| 559 | } | ||
| 560 | |||
| 561 | # my $ans = WSRF::Lite | ||
| 562 | # -> uri($WSRF::Constants::WSRPW) | ||
| 563 | # -> on_action( sub {sprintf '%s/%s', @_} ) | ||
| 564 | # -> wsaddress($EPR) | ||
| 565 | # -> GetMultipleResourceProperties(SOAP::Data->value($searchTerm)->type('xml')); | ||
| 566 | my $ans = WSRF::Lite | ||
| 567 | -> uri($WSRF::Constants::WSRP) | ||
| 568 | -> on_action( sub {sprintf '%s/%s/%sRequest', $WSRF::Constants::WSRPW,$_[1],$_[1]} ) | ||
| 569 | -> wsaddress($EPR) | ||
| 570 | -> GetMultipleResourceProperties(SOAP::Data->value($searchTerm)->type('xml')); | ||
| 571 | die "ERROR: ".$ans->faultstring if ($ans->fault); | ||
| 572 | |||
| 573 | my $parser = XML::LibXML->new(); | ||
| 574 | my $doc = $parser->parse_string($ans->raw_xml); | ||
| 575 | my $soap = $doc->getDocumentElement(); | ||
| 576 | |||
| 577 | my @ans; | ||
| 578 | foreach my $queryID (@queryIDs) { | ||
| 579 | my $prop_name = "status_".$queryID; | ||
| 580 | |||
| 581 | my ($prop) = $soap->getElementsByTagNameNS($WSRF::Constants::MOBY, $prop_name); | ||
| 582 | my $event = $prop->getFirstChild->toString; | ||
| 583 | my $status = LSAE::AnalysisEventBlock->new($event); | ||
| 584 | push(@ans, $status); | ||
| 585 | } | ||
| 586 | |||
| 587 | return @ans; | ||
| 588 | } | ||
| 589 | |||
| 590 | sub result { | ||
| 591 | my ($self, $EPR, @queryIDs) = @_; | ||
| 592 | |||
| 593 | print "Retrieving results...\n\n" unless ($self->{silent}); | ||
| 594 | |||
| 595 | my $searchTerm = ""; | ||
| 596 | foreach my $queryID (@queryIDs) { | ||
| 597 | $searchTerm .= "<wsrp:ResourceProperty xmlns:wsrp='$WSRF::Constants::WSRP' xmlns:mobyws='$WSRF::Constants::MOBY'>"; | ||
| 598 | $searchTerm .= "mobyws:result_".$queryID; | ||
| 599 | $searchTerm .= "</wsrp:ResourceProperty>"; | ||
| 600 | } | ||
| 601 | |||
| 602 | my $ans = WSRF::Lite | ||
| 603 | -> uri($WSRF::Constants::WSRP) | ||
| 604 | -> on_action( sub {sprintf '%s/%s/%sRequest', $WSRF::Constants::WSRPW,$_[1],$_[1]} ) | ||
| 605 | -> wsaddress($EPR) | ||
| 606 | -> GetMultipleResourceProperties(SOAP::Data->value($searchTerm)->type('xml')); | ||
| 607 | die "ERROR: ".$ans->faultstring if ($ans->fault); | ||
| 608 | |||
| 609 | my $parser = XML::LibXML->new(); | ||
| 610 | my $doc = $parser->parse_string($ans->raw_xml); | ||
| 611 | my $soap = $doc->getDocumentElement(); | ||
| 612 | |||
| 613 | my @ans; | ||
| 614 | foreach my $queryID (@queryIDs) { | ||
| 615 | my $prop_name = "result_".$queryID; | ||
| 616 | |||
| 617 | my ($prop) = $soap->getElementsByTagNameNS($WSRF::Constants::MOBY, $prop_name); | ||
| 618 | my $result = $prop->getFirstChild->toString; | ||
| 619 | push(@ans, $result); | ||
| 620 | } | ||
| 621 | |||
| 622 | return @ans; | ||
| 623 | } | ||
| 624 | |||
| 625 | sub destroy { | ||
| 626 | my ($self, $EPR) = @_; | ||
| 627 | |||
| 628 | print "Destroying WS-Resource...\n\n" unless ($self->{silent}); | ||
| 629 | |||
| 630 | my $ans = WSRF::Lite | ||
| 631 | -> uri($WSRF::Constants::WSRL) | ||
| 632 | -> on_action( sub {sprintf '%s/ImmediateResourceTermination/%sRequest', $WSRF::Constants::WSRLW,$_[1]} ) | ||
| 633 | -> wsaddress($EPR) | ||
| 634 | -> Destroy(); | ||
| 635 | die "ERROR: ".$ans->faultstring if ($ans->fault); | ||
| 636 | } | ||
| 637 | |||
| 638 | sub _getServiceEndpoint($) { | ||
| 639 | my ($wsdl) = @_; | ||
| 640 | |||
| 641 | $wsdl =~ /address location\s*=\s*["|'](.+)["|']/; | ||
| 642 | my $serviceEndpoint = $1; | ||
| 643 | |||
| 644 | return $serviceEndpoint; | ||
| 645 | } | ||
| 646 | |||
| 647 | sub _getPollingTime($$$@) { | ||
| 648 | my ($i, $j, $start, @status) = @_; | ||
| 649 | |||
| 650 | return _getPseudoRandomPollingTime($i, $j) unless (scalar(@status)); | ||
| 651 | |||
| 652 | my $pollingTime = 0; | ||
| 653 | foreach my $status (@status) { | ||
| 654 | my $pTime; | ||
| 655 | |||
| 656 | if ($status->type == LSAE_PERCENT_PROGRESS_EVENT) { | ||
| 657 | if ($status->percentage >= 100) { | ||
| 658 | |||
| 659 | $pTime = 0; | ||
| 660 | |||
| 661 | } elsif ($status->percentage < 100) { | ||
| 662 | |||
| 663 | $pTime = int( ( (100 - $status->percentage) * (time - $start) ) / $status->percentage ) + 1; | ||
| 664 | |||
| 665 | } else { | ||
| 666 | die "ERROR: analysis event block not well formed.\n"; | ||
| 667 | } | ||
| 668 | |||
| 669 | } elsif ($status->type == LSAE_STATE_CHANGED_EVENT) { | ||
| 670 | if ( ($status->new_state eq "completed") || | ||
| 671 | ($status->new_state eq "COMPLETED") || | ||
| 672 | ($status->new_state eq "terminated_by_request") || | ||
| 673 | ($status->new_state eq "TERMINATED_BY_REQUEST") || | ||
| 674 | ($status->new_state eq "terminated_by_error") || | ||
| 675 | ($status->new_state eq "TERMINATED_BY_ERROR") ) { | ||
| 676 | |||
| 677 | $pTime = 0; | ||
| 678 | |||
| 679 | } elsif ( ($status->new_state eq "created") || | ||
| 680 | ($status->new_state eq "CREATED") || | ||
| 681 | ($status->new_state eq "running") || | ||
| 682 | ($status->new_state eq "RUNNING") ) { | ||
| 683 | |||
| 684 | $pTime = _getPseudoRandomPollingTime($i, $j); | ||
| 685 | |||
| 686 | } else { | ||
| 687 | die "ERROR: analysis event block not well formed.\n"; | ||
| 688 | } | ||
| 689 | |||
| 690 | } elsif ($status->type == LSAE_STEP_PROGRESS_EVENT) { | ||
| 691 | if ($status->steps_completed >= $status->total_steps) { | ||
| 692 | |||
| 693 | $pTime = 0; | ||
| 694 | |||
| 695 | } elsif ($status->steps_completed < $status->total_steps) { | ||
| 696 | |||
| 697 | $pTime = int ( ( ($status->total_steps - $status->steps_completed) * (time - $start) ) / $status->steps_completed ) + 1; | ||
| 698 | |||
| 699 | } else { | ||
| 700 | die "ERROR: analysis event block not well formed.\n"; | ||
| 701 | } | ||
| 702 | |||
| 703 | } elsif ($status->type == LSAE_TIME_PROGRESS_EVENT) { | ||
| 704 | if ($status->remaining == 0) { | ||
| 705 | |||
| 706 | $pTime = 0; | ||
| 707 | |||
| 708 | } elsif ($status->remaining > 0) { | ||
| 709 | |||
| 710 | $pTime = $status->remaining; | ||
| 711 | |||
| 712 | } else { | ||
| 713 | die "ERROR: analysis event block not well formed.\n"; | ||
| 714 | } | ||
| 715 | } | ||
| 716 | |||
| 717 | $pollingTime = $pTime if ($pTime > $pollingTime); | ||
| 718 | } | ||
| 719 | |||
| 720 | return $pollingTime; | ||
| 721 | } | ||
| 722 | |||
| 723 | sub _getPseudoRandomPollingTime($$) { | ||
| 724 | my ($i, $j) = @_; | ||
| 725 | my $c = 15; | ||
| 726 | my $p = 0.1; | ||
| 727 | my $k = $i + $j; | ||
| 728 | $k = 240 if ($k > 240); | ||
| 729 | my $delay = ($c*$k) + int(rand(int(2*$p*$c*$k))) - int($p*$c*$k); | ||
| 730 | return $delay; | ||
| 731 | } | ||
| 732 | |||
| 733 | sub _composeResponse(@) { | ||
| 734 | my (@datas) = @_; | ||
| 735 | |||
| 736 | my @authorities; | ||
| 737 | my @exceptions; | ||
| 738 | my @queries; | ||
| 739 | |||
| 740 | foreach my $data (@datas) { | ||
| 741 | |||
| 742 | # Get moby document | ||
| 743 | my $parser = XML::LibXML->new(); | ||
| 744 | my $doc = $parser->parse_string($data); | ||
| 745 | my $moby = $doc->getDocumentElement(); | ||
| 746 | |||
| 747 | # Get authority | ||
| 748 | my @mobyContents = ($moby->getChildrenByTagNameNS($WSRF::Constants::MOBY_MESSAGE_NS, 'mobyContent')); | ||
| 749 | my $mobyContent = shift(@mobyContents); | ||
| 750 | my $authority = $mobyContent->getAttribute('authority') || $mobyContent->getAttributeNS($WSRF::Constants::MOBY_MESSAGE_NS, 'authority'); | ||
| 751 | push(@authorities, $authority); | ||
| 752 | |||
| 753 | # Get exceptions | ||
| 754 | my @mobyException = ($moby->getElementsByTagNameNS($WSRF::Constants::MOBY_MESSAGE_NS, 'mobyException')); | ||
| 755 | foreach my $mobyException (@mobyException) { | ||
| 756 | push(@exceptions, $mobyException->toString()); | ||
| 757 | } | ||
| 758 | |||
| 759 | # Get queries | ||
| 760 | my @mobyData = ($moby->getElementsByTagNameNS($WSRF::Constants::MOBY_MESSAGE_NS, 'mobyData')); | ||
| 761 | foreach my $mobyData (@mobyData) { | ||
| 762 | push(@queries, $mobyData->toString()); | ||
| 763 | } | ||
| 764 | } | ||
| 765 | |||
| 766 | my $moby; | ||
| 767 | $moby = responseHeader(shift(@authorities)); | ||
| 768 | $moby .= "<moby:serviceNotes xmlns:moby='$WSRF::Constants::MOBY_MESSAGE_NS'>".join("", @exceptions)."</moby:serviceNotes>" if (scalar(@exceptions)); | ||
| 769 | $moby .= join("", @queries) if (scalar(@queries)); | ||
| 770 | $moby .= responseFooter(); | ||
| 771 | |||
| 772 | return $moby; | ||
| 773 | } | ||
| 774 | |||
| 775 | 1; |