| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # | ||
| 3 | # Calling a BioMoby services (with or without SOAP). | ||
| 4 | # | ||
| 5 | # $Id: moses-testing-service.pl,v 1.17 2010/01/20 14:35:58 kawas Exp $ | ||
| 6 | # Contact: Martin Senger <martin.senger@gmail.com> | ||
| 7 | # ----------------------------------------------------------- | ||
| 8 | BEGIN { | ||
| 9 | |||
| 10 | # some command-line options | ||
| 11 | use Getopt::Std; | ||
| 12 | use vars qw/ $opt_h $opt_d $opt_v $opt_l $opt_e $opt_c $opt_a $opt_C /; | ||
| 13 | getopts('hdvl:e:c:C:a:'); | ||
| 14 | |||
| 15 | # usage | ||
| 16 | if ( $opt_h or ( @ARGV == 0 and ( not $opt_c or not $opt_C ) ) ) { | ||
| 17 | print STDOUT <<'END_OF_USAGE'; | ||
| 18 | Calling a BioMoby services (without using SOAP, just locally). | ||
| 19 | Usage: # calling a local module representing a service, without using SOAP | ||
| 20 | [-vd] [-l <lib-location>] <package-name> [<input-file>] | ||
| 21 | or | ||
| 22 | [-vd] -c <service-url> [<input-file>] | ||
| 23 | or | ||
| 24 | [-vd] -C <service-url> [<input-file>] | ||
| 25 | or | ||
| 26 | [-vd] -e <service-url> [<input-file>] | ||
| 27 | or | ||
| 28 | [-vd] -a <service-url> [<input-file>] | ||
| 29 | |||
| 30 | It also needs a location of a local cache (and potentially a | ||
| 31 | BioMoby registry endpoint). It takes it from the | ||
| 32 | 'moby-service.cfg' configuration file. | ||
| 33 | |||
| 34 | # calling a real service, using SOAP | ||
| 35 | -e <service-url> <service-name> [<input-file>] | ||
| 36 | |||
| 37 | # calling a real service, using cgi | ||
| 38 | -c <service-url> [<input-file>] | ||
| 39 | |||
| 40 | # calling a real service, using asynchronous cgi | ||
| 41 | -C <service-url> [<input-file>] | ||
| 42 | |||
| 43 | # calling a real service, using async SOAP | ||
| 44 | -a <service-url> <service-name> [<input-file>] | ||
| 45 | |||
| 46 | <package-name> is a full name of a called module (service) | ||
| 47 | e.g. Service::Mabuhay | ||
| 48 | |||
| 49 | -l <lib-location> | ||
| 50 | A directory where is called service stored. | ||
| 51 | Default: src/Perl/services | ||
| 52 | |||
| 53 | -e <service-url> | ||
| 54 | A service endpoint | ||
| 55 | (e.g. http://localhost/cgi-bin/MobyServer.cgi) | ||
| 56 | |||
| 57 | -c <cgi-service-url> | ||
| 58 | A cgi biomoby service url | ||
| 59 | (e.g. http://localhost/cgi-bin/HelloBiomobyWorld.cgi) | ||
| 60 | |||
| 61 | -C <async-cgi-service-url> | ||
| 62 | An asynchronous cgi biomoby service url | ||
| 63 | (e.g. http://localhost/cgi-bin/HelloBiomobyWorldAsync.cgi) | ||
| 64 | |||
| 65 | -a <asynchronous service-url> | ||
| 66 | An asynchronous service url | ||
| 67 | (e.g. http://localhost/cgi-bin/AsyncMobyServer.cgi) | ||
| 68 | |||
| 69 | <input-file> | ||
| 70 | A BioMoby XML file with input data. | ||
| 71 | Default: an empty BioMoby request | ||
| 72 | |||
| 73 | -v ... verbose | ||
| 74 | -d ... debug | ||
| 75 | -h ... help | ||
| 76 | END_OF_USAGE | ||
| 77 | exit(0); | ||
| 78 | } | ||
| 79 | use HTTP::Request; | ||
| 80 | use LWP::UserAgent; | ||
| 81 | use XML::LibXML; | ||
| 82 | use MOBY::Async::WSRF; | ||
| 83 | use MOBY::Async::LSAE; | ||
| 84 | |||
| 85 | # use MOSES::MOBY::Base; | ||
| 86 | # load modules, depending on the mode of calling | ||
| 87 | if ($opt_e) { | ||
| 88 | |||
| 89 | # calling a real service, using SOAP | ||
| 90 | eval "use SOAP::Lite; 1;" | ||
| 91 | or die "$@\n"; | ||
| 92 | } elsif ($opt_c) { | ||
| 93 | |||
| 94 | # calling a real service, using cgi | ||
| 95 | eval "use HTTP::Request; 1;" | ||
| 96 | or die "$@\n"; | ||
| 97 | eval "use LWP::UserAgent; 1;" | ||
| 98 | or die "$@\n"; | ||
| 99 | } elsif ($opt_C) { | ||
| 100 | |||
| 101 | # calling a real service, using async cgi | ||
| 102 | eval "use HTTP::Request; 1;" | ||
| 103 | or die "$@\n"; | ||
| 104 | eval "use LWP::UserAgent; 1;" | ||
| 105 | or die "$@\n"; | ||
| 106 | eval "use XML::LibXML; 1;" | ||
| 107 | or die "$@\n"; | ||
| 108 | eval "use MOBY::Async::WSRF; 1;" | ||
| 109 | or die "$@\n"; | ||
| 110 | eval "use MOBY::Async::LSAE; 1;" | ||
| 111 | or die "$@\n"; | ||
| 112 | } else { | ||
| 113 | |||
| 114 | # calling a local service module, without SOAP | ||
| 115 | eval "use MOSES::MOBY::Base; 1;"; | ||
| 116 | |||
| 117 | # take the lib location from the config file | ||
| 118 | require lib; | ||
| 119 | lib->import( MOSES::MOBY::Config->param("generators.impl.outdir") ); | ||
| 120 | require lib; | ||
| 121 | lib->import( MOSES::MOBY::Config->param("generators.outdir") ); | ||
| 122 | unshift( @INC, $opt_l ) if $opt_l; | ||
| 123 | $LOG->level('INFO') if $opt_v; | ||
| 124 | $LOG->level('DEBUG') if $opt_d; | ||
| 125 | } | ||
| 126 | |||
| 127 | # load these modules always to get constants and to avoid warnings | ||
| 128 | eval "use MOBY::Async::LSAE; 1;" | ||
| 129 | or die "$@\n"; | ||
| 130 | eval "use MOBY::Async::WSRF; 1;" | ||
| 131 | or die "$@\n"; | ||
| 132 | } | ||
| 133 | use strict; | ||
| 134 | |||
| 135 | sub _empty_input { | ||
| 136 | return <<'END_OF_XML'; | ||
| 137 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 138 | <moby:MOBY xmlns:moby="http://www.biomoby.org/moby"> | ||
| 139 | <moby:mobyContent> | ||
| 140 | <moby:mobyData moby:queryID="job_0"/> | ||
| 141 | </moby:mobyContent> | ||
| 142 | </moby:MOBY> | ||
| 143 | END_OF_XML | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _check_status { | ||
| 147 | my ( $status, $completed, $queryID, $opt_v ) = @_; | ||
| 148 | if ( $status->type == LSAE_PERCENT_PROGRESS_EVENT ) { | ||
| 149 | print "Current percentage for job $queryID: ", $status->percentage, "\n" | ||
| 150 | if $opt_v; | ||
| 151 | if ( $status->percentage >= 100 ) { | ||
| 152 | $completed->{$queryID} = 1; | ||
| 153 | } elsif ( $status->percentage < 100 ) { | ||
| 154 | print "\tmsg: ", | ||
| 155 | ( $status->message ? $status->message : "no message found ..." ), | ||
| 156 | "\n" | ||
| 157 | if $opt_v; | ||
| 158 | |||
| 159 | #sleep(20); | ||
| 160 | } else { | ||
| 161 | die "ERROR: analysis event block not well formed.\n"; | ||
| 162 | } | ||
| 163 | } elsif ( $status->type == LSAE_STATE_CHANGED_EVENT ) { | ||
| 164 | print "Current state for job $queryID: ", $status->new_state, "\n" | ||
| 165 | if $opt_v; | ||
| 166 | if ( ( $status->new_state =~ m"completed"i ) | ||
| 167 | || ( $status->new_state =~ m"terminated_by_request"i ) | ||
| 168 | || ( $status->new_state =~ m"terminated_by_error"i ) ) | ||
| 169 | { | ||
| 170 | $completed->{$queryID} = 1; | ||
| 171 | } elsif ( ( $status->new_state =~ m"created"i ) | ||
| 172 | || ( $status->new_state =~ m"running"i ) ) | ||
| 173 | { | ||
| 174 | print "\tmsg: ", | ||
| 175 | ( $status->message ? $status->message : "no message found ..." ), | ||
| 176 | "\n" | ||
| 177 | if $opt_v; | ||
| 178 | |||
| 179 | #sleep(20); | ||
| 180 | } else { | ||
| 181 | die "ERROR: analysis event block not well formed.\n"; | ||
| 182 | } | ||
| 183 | } elsif ( $status->type == LSAE_STEP_PROGRESS_EVENT ) { | ||
| 184 | print "Steps completed for job $queryID: ", $status->steps_completed, | ||
| 185 | "\n" | ||
| 186 | if $opt_v; | ||
| 187 | if ( $status->steps_completed >= $status->total_steps ) { | ||
| 188 | $completed->{$queryID} = 1; | ||
| 189 | } elsif ( $status->steps_completed < $status->total_steps ) { | ||
| 190 | print "\tmsg: ", | ||
| 191 | ( $status->message ? $status->message : "no message found ..." ), | ||
| 192 | "\n" | ||
| 193 | if $opt_v; | ||
| 194 | |||
| 195 | #sleep(20); | ||
| 196 | } else { | ||
| 197 | die "ERROR: analysis event block not well formed.\n"; | ||
| 198 | } | ||
| 199 | } elsif ( $status->type == LSAE_TIME_PROGRESS_EVENT ) { | ||
| 200 | print "Time remaining for job $queryID: ", $status->remaining, "\n" | ||
| 201 | if $opt_v; | ||
| 202 | if ( $status->remaining == 0 ) { | ||
| 203 | $completed->{$queryID} = 1; | ||
| 204 | } elsif ( $status->remaining > 0 ) { | ||
| 205 | print "\tmsg: ", | ||
| 206 | ( $status->message ? $status->message : "no message found ..." ), | ||
| 207 | "\n" | ||
| 208 | if $opt_v; | ||
| 209 | |||
| 210 | #sleep(20); | ||
| 211 | } else { | ||
| 212 | die "ERROR: analysis event block not well formed.\n"; | ||
| 213 | } | ||
| 214 | } else { | ||
| 215 | warn | ||
| 216 | "Whilst checking the status of our resource, we entered into a possible infinite loop ...\n"; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | sub _get_query_ids { | ||
| 221 | my $input = shift; | ||
| 222 | my @query_ids = (); | ||
| 223 | my $parser = XML::LibXML->new(); | ||
| 224 | my $doc = $parser->parse_string($input); | ||
| 225 | my $iterator = $doc->getElementsByLocalName("mobyData"); | ||
| 226 | for ( 1 .. $iterator->size() ) { | ||
| 227 | my $node = $iterator->get_node($_); | ||
| 228 | my $id = $node->getAttribute("queryID") | ||
| 229 | || $node->getAttribute( | ||
| 230 | $node->lookupNamespacePrefix($WSRF::Constants::MOBY_MESSAGE_NS) | ||
| 231 | . ":queryID" ); | ||
| 232 | push @query_ids, $id; | ||
| 233 | } | ||
| 234 | return @query_ids; | ||
| 235 | } | ||
| 236 | |||
| 237 | # --- what service to call | ||
| 238 | my $module = shift | ||
| 239 | unless $opt_c | ||
| 240 | or $opt_C; # eg. Service::Mabuhay, or just Mabuhay | ||
| 241 | my $service; | ||
| 242 | ( $service = $module ) =~ s/.*::// unless $opt_c or $opt_C; | ||
| 243 | |||
| 244 | # --- call the service | ||
| 245 | if ($opt_e) { | ||
| 246 | |||
| 247 | # calling a real service, using SOAP | ||
| 248 | my $soap = SOAP::Lite->uri("http://biomoby.org/")->proxy($opt_e)->on_fault( | ||
| 249 | sub { | ||
| 250 | my $soap = shift; | ||
| 251 | my $res = shift; | ||
| 252 | my $msg = | ||
| 253 | ref $res | ||
| 254 | ? "--- SOAP FAULT ---\n" | ||
| 255 | . $res->faultcode . " " | ||
| 256 | . $res->faultstring | ||
| 257 | : "--- TRANSPORT ERROR ---\n" | ||
| 258 | . $soap->transport->status | ||
| 259 | . "\n$res\n"; | ||
| 260 | die $msg; | ||
| 261 | } | ||
| 262 | ); | ||
| 263 | my $input = ''; | ||
| 264 | if ( @ARGV > 0 ) { | ||
| 265 | my $data = shift; # a file name | ||
| 266 | open INPUT, "<$data" | ||
| 267 | or die "Cannot read '$data': $!\n"; | ||
| 268 | while (<INPUT>) { $input .= $_; } | ||
| 269 | close INPUT; | ||
| 270 | } else { | ||
| 271 | $input = _empty_input; | ||
| 272 | } | ||
| 273 | print $soap ->$service( SOAP::Data->type( 'string' => "$input" ) )->result; | ||
| 274 | } elsif ($opt_c) { | ||
| 275 | |||
| 276 | # calling a real service, using cgi | ||
| 277 | my $ua = LWP::UserAgent->new; | ||
| 278 | my $input = ''; | ||
| 279 | if ( @ARGV > 0 ) { | ||
| 280 | my $data = shift; # a file name | ||
| 281 | open INPUT, "<$data" | ||
| 282 | or die "Cannot read '$data': $!\n"; | ||
| 283 | while (<INPUT>) { $input .= $_; } | ||
| 284 | close INPUT; | ||
| 285 | } else { | ||
| 286 | $input = _empty_input; | ||
| 287 | } | ||
| 288 | my $req = HTTP::Request->new( POST => $opt_c ); | ||
| 289 | $req->content_type('application/x-www-form-urlencoded'); | ||
| 290 | $req->content("data=$input"); | ||
| 291 | print "\n" . $ua->request($req)->as_string . "\n"; | ||
| 292 | } elsif ($opt_C) { | ||
| 293 | my $input = ''; | ||
| 294 | if ( @ARGV > 0 ) { | ||
| 295 | my $data = shift; # a file name | ||
| 296 | open INPUT, "<$data" | ||
| 297 | or die "Cannot read '$data': $!\n"; | ||
| 298 | while (<INPUT>) { $input .= $_; } | ||
| 299 | close INPUT; | ||
| 300 | } else { | ||
| 301 | $input = _empty_input; | ||
| 302 | } | ||
| 303 | |||
| 304 | # extract all of the query ids from $input | ||
| 305 | my @query_ids = _get_query_ids($input); | ||
| 306 | my %completed = (); | ||
| 307 | print "Sending the following data to $opt_C\n$input\n"; | ||
| 308 | |||
| 309 | # post to the web service | ||
| 310 | my $ua = LWP::UserAgent->new; | ||
| 311 | my $req = HTTP::Request->new( POST => $opt_C ); | ||
| 312 | $req->content_type('text/xml'); | ||
| 313 | $req->content("$input"); | ||
| 314 | |||
| 315 | # get the response | ||
| 316 | my $response = $ua->request($req); | ||
| 317 | |||
| 318 | # do we have an error? | ||
| 319 | die "Error calling service: " . $response->status_line | ||
| 320 | if ( $response->code != 200 ); | ||
| 321 | my $epr = $response->header("moby-wsrf"); | ||
| 322 | my $parser = XML::LibXML->new(); | ||
| 323 | my $doc = $parser->parse_string($epr); | ||
| 324 | my $ID = | ||
| 325 | $doc->getElementsByLocalName("ServiceInvocationId")->get_node(1) | ||
| 326 | ->textContent; | ||
| 327 | $ID =~ s/ //gi; | ||
| 328 | my $searchTerm = ""; | ||
| 329 | $searchTerm .= | ||
| 330 | "<wsrf-rp:GetMultipleResourceProperties xmlns:wsrf-rp='$WSRF::Constants::WSRP' xmlns:mobyws='$WSRF::Constants::MOBY'>"; | ||
| 331 | |||
| 332 | foreach my $queryID (@query_ids) { | ||
| 333 | $searchTerm .= | ||
| 334 | "<wsrf-rp:ResourceProperty>mobyws:status_" | ||
| 335 | . $queryID | ||
| 336 | . "</wsrf-rp:ResourceProperty>"; | ||
| 337 | } | ||
| 338 | $searchTerm .= "</wsrf-rp:GetMultipleResourceProperties>"; | ||
| 339 | my $header = _moby_wsrf_header( $opt_C, $ID ); | ||
| 340 | $header =~ s/\n//gi; | ||
| 341 | |||
| 342 | # poll | ||
| 343 | while (1) { | ||
| 344 | |||
| 345 | # | ||
| 346 | # Poll the service for the status of all query IDs associated with this service invocation. | ||
| 347 | # | ||
| 348 | $req = HTTP::Request->new( POST => $opt_C . "/status" ); | ||
| 349 | $req->header( "moby-wsrf" => $header ); | ||
| 350 | $req->content_type('application/x-www-form-urlencoded'); | ||
| 351 | $req->content("data=$searchTerm"); | ||
| 352 | $response = $ua->request($req); | ||
| 353 | my $xml = $response->content(); | ||
| 354 | my $parser = XML::LibXML->new(); | ||
| 355 | my $doc = $parser->parse_string($xml); | ||
| 356 | |||
| 357 | foreach my $queryID (@query_ids) { | ||
| 358 | |||
| 359 | # skip poll if current job completed | ||
| 360 | next if $completed{$queryID}; | ||
| 361 | |||
| 362 | # | ||
| 363 | # Find status of this query ID. | ||
| 364 | # | ||
| 365 | #my $xpath = "//*[local-name() = 'analysis_event'][\@id='$queryID']"; | ||
| 366 | #my $xpath = "//analysis_event[\@id='$queryID']"; | ||
| 367 | my $xpath = "//*[local-name() = 'analysis_event'][\@*='$queryID']"; | ||
| 368 | my $xpc = XML::LibXML::XPathContext->new(); | ||
| 369 | my $nodes = $xpc->findnodes( $xpath, $doc->documentElement ); | ||
| 370 | |||
| 371 | # should only be one ... | ||
| 372 | die | ||
| 373 | "Service returned unexpected/malformed resource property XML, which should contain service status info." | ||
| 374 | unless $nodes->size() == 1; | ||
| 375 | my $status = | ||
| 376 | LSAE::AnalysisEventBlock->new( $nodes->get_node(1)->toString() ); | ||
| 377 | &_check_status( $status, \%completed, $queryID, $opt_v ); | ||
| 378 | } #end foreach | ||
| 379 | last if scalar keys(%completed) == $#query_ids + 1; | ||
| 380 | my $interval = 20; | ||
| 381 | print "Checking job state again in $interval seconds.\n\n" if $opt_v; | ||
| 382 | sleep($interval); | ||
| 383 | } #end while(1) | ||
| 384 | #last if scalar keys(%completed) == $#query_ids + 1; | ||
| 385 | |||
| 386 | # task is finished, obtain the result | ||
| 387 | $searchTerm = ""; | ||
| 388 | $searchTerm .= | ||
| 389 | "<wsrf-rp:GetMultipleResourceProperties xmlns:wsrf-rp='$WSRF::Constants::WSRP' xmlns:mobyws='$WSRF::Constants::MOBY'>"; | ||
| 390 | foreach my $queryID (@query_ids) { | ||
| 391 | $searchTerm .= | ||
| 392 | "<wsrf-rp:ResourceProperty>mobyws:result_" | ||
| 393 | . $queryID | ||
| 394 | . "</wsrf-rp:ResourceProperty>"; | ||
| 395 | } | ||
| 396 | $searchTerm .= "</wsrf-rp:GetMultipleResourceProperties>"; | ||
| 397 | $header = _moby_wsrf_header( $opt_C, $ID ); | ||
| 398 | $header =~ s/\n//gi; | ||
| 399 | $req = HTTP::Request->new( POST => $opt_C . "/results" ); | ||
| 400 | $req->header( "moby-wsrf" => $header ); | ||
| 401 | $req->content_type('application/x-www-form-urlencoded'); | ||
| 402 | $req->content("data=$searchTerm"); | ||
| 403 | $response = $ua->request($req); | ||
| 404 | |||
| 405 | # create nicely formatted XML | ||
| 406 | $parser = XML::LibXML->new(); | ||
| 407 | $doc = $parser->parse_string( $response->content ); | ||
| 408 | print "\n" . $doc->toString(1); | ||
| 409 | |||
| 410 | # destroy the job | ||
| 411 | $searchTerm = '<Destroy xmlns="http://docs.oasis-open.org/wsrf/rl-2"/> '; | ||
| 412 | $req = HTTP::Request->new( POST => $opt_C . "/destroy" ); | ||
| 413 | $req->header( "moby-wsrf" => $header ); | ||
| 414 | $req->content_type('application/x-www-form-urlencoded'); | ||
| 415 | $req->content("data=$searchTerm"); | ||
| 416 | $response = $ua->request($req); | ||
| 417 | print "Destroying the resource returned:\n\t" . $response->content . "\n" | ||
| 418 | if $opt_v; | ||
| 419 | } elsif ($opt_a) { | ||
| 420 | |||
| 421 | # calling a real service, using async soap | ||
| 422 | # call using async mode for async service ... _submit | ||
| 423 | $service .= "_submit"; | ||
| 424 | |||
| 425 | # set up the wsrf call | ||
| 426 | my $soap = WSRF::Lite->proxy($opt_a)->uri($WSRF::Constants::MOBY)->on_fault( | ||
| 427 | sub { | ||
| 428 | my $soap = shift; | ||
| 429 | my $res = shift; | ||
| 430 | my $msg = | ||
| 431 | ref $res | ||
| 432 | ? "--- SOAP FAULT ---\n" | ||
| 433 | . $res->faultcode . " " | ||
| 434 | . $res->faultstring | ||
| 435 | : "--- TRANSPORT ERROR ---\n" | ||
| 436 | . $soap->transport->status | ||
| 437 | . "\n$res\n"; | ||
| 438 | die $msg; | ||
| 439 | } | ||
| 440 | ); | ||
| 441 | |||
| 442 | # get the input | ||
| 443 | my $input = ''; | ||
| 444 | if ( @ARGV > 0 ) { | ||
| 445 | my $data = shift; # a file name | ||
| 446 | open INPUT, "<$data" | ||
| 447 | or die "Cannot read '$data': $!\n"; | ||
| 448 | while (<INPUT>) { $input .= $_; } | ||
| 449 | close INPUT; | ||
| 450 | } else { | ||
| 451 | $input = _empty_input; | ||
| 452 | } | ||
| 453 | |||
| 454 | # extract all of the query ids from $input | ||
| 455 | my @query_ids = _get_query_ids($input); | ||
| 456 | print "\nSending the following data to $service asynchronously:\n", $input, | ||
| 457 | "\n" | ||
| 458 | if $opt_v; | ||
| 459 | |||
| 460 | # submit the job | ||
| 461 | my $epr = | ||
| 462 | ( $soap->$service( SOAP::Data->type( 'string' => "$input" ) )->result ); | ||
| 463 | |||
| 464 | # Get address from the returned Endpoint Reference | ||
| 465 | my $address = $epr->{'EndpointReference'}->{Address}; | ||
| 466 | |||
| 467 | # Get resource identifier from the returned Endpoint Reference | ||
| 468 | my $identifier = | ||
| 469 | $epr->{'EndpointReference'}->{ReferenceParameters}->{ServiceInvocationId}; | ||
| 470 | |||
| 471 | # Compose the Endpoint Reference | ||
| 472 | my $EPR = WSRF::WS_Address->new(); | ||
| 473 | $EPR->Address($address); | ||
| 474 | $EPR->ReferenceParameters( '<mobyws:ServiceInvocationId xmlns:mobyws="' | ||
| 475 | . $WSRF::Constants::MOBY . '">' | ||
| 476 | . $identifier | ||
| 477 | . '</mobyws:ServiceInvocationId>' ); | ||
| 478 | my %completed = (); | ||
| 479 | while (1) { | ||
| 480 | foreach my $queryID (@query_ids) { | ||
| 481 | |||
| 482 | # skip poll if current job completed | ||
| 483 | next if $completed{$queryID}; | ||
| 484 | |||
| 485 | # poll the service for given query ID | ||
| 486 | my $searchTerm = ""; | ||
| 487 | $searchTerm .= | ||
| 488 | "<wsrp:ResourceProperty xmlns:wsrp='$WSRF::Constants::WSRP' xmlns:mobyws='$WSRF::Constants::MOBY'>"; | ||
| 489 | $searchTerm .= "mobyws:status_" . $queryID; | ||
| 490 | $searchTerm .= "</wsrp:ResourceProperty>"; | ||
| 491 | $soap = WSRF::Lite->uri($WSRF::Constants::WSRP)->on_action( | ||
| 492 | sub { | ||
| 493 | sprintf '%s/%s/%sRequest', $WSRF::Constants::WSRPW, $_[1], | ||
| 494 | $_[1]; | ||
| 495 | } | ||
| 496 | )->wsaddress($EPR) | ||
| 497 | ->GetMultipleResourceProperties( | ||
| 498 | SOAP::Data->value($searchTerm)->type('xml') ); | ||
| 499 | my $parser = XML::LibXML->new(); | ||
| 500 | my $xml = $soap->raw_xml; | ||
| 501 | my $doc = $parser->parse_string($xml); | ||
| 502 | $soap = $doc->getDocumentElement(); | ||
| 503 | my $prop_name = "status_" . $queryID; | ||
| 504 | my ($prop) = | ||
| 505 | $soap->getElementsByTagNameNS( $WSRF::Constants::MOBY, | ||
| 506 | $prop_name ) | ||
| 507 | || $soap->getElementsByTagName($prop_name); | ||
| 508 | my $event = $prop->getFirstChild->toString | ||
| 509 | unless ref $prop eq "XML::LibXML::NodeList"; | ||
| 510 | $event = $prop->pop()->getFirstChild->toString | ||
| 511 | if ref $prop eq "XML::LibXML::NodeList"; | ||
| 512 | my $status = LSAE::AnalysisEventBlock->new($event); | ||
| 513 | |||
| 514 | if ( $status->type == LSAE_PERCENT_PROGRESS_EVENT ) { | ||
| 515 | if ( $status->percentage >= 100 ) { | ||
| 516 | $completed{$queryID} = 1; | ||
| 517 | } elsif ( $status->percentage < 100 ) { | ||
| 518 | print "Current percentage: ", $status->percentage, "\n" | ||
| 519 | if $opt_v; | ||
| 520 | sleep(20); | ||
| 521 | } else { | ||
| 522 | die "ERROR: analysis event block not well formed.\n"; | ||
| 523 | } | ||
| 524 | } elsif ( $status->type == LSAE_STATE_CHANGED_EVENT ) { | ||
| 525 | if ( ( $status->new_state =~ m"completed"i ) | ||
| 526 | || ( $status->new_state =~ m"terminated_by_request"i ) | ||
| 527 | || ( $status->new_state =~ m"terminated_by_error"i ) ) | ||
| 528 | { | ||
| 529 | $completed{$queryID} = 1; | ||
| 530 | } elsif ( ( $status->new_state =~ m"created"i ) | ||
| 531 | || ( $status->new_state =~ m"running"i ) ) | ||
| 532 | { | ||
| 533 | print "Current State: ", $status->new_state, "\n" if $opt_v; | ||
| 534 | sleep(20); | ||
| 535 | } else { | ||
| 536 | die "ERROR: analysis event block not well formed.\n"; | ||
| 537 | } | ||
| 538 | } elsif ( $status->type == LSAE_STEP_PROGRESS_EVENT ) { | ||
| 539 | if ( $status->steps_completed >= $status->total_steps ) { | ||
| 540 | $completed{$queryID} = 1; | ||
| 541 | } elsif ( $status->steps_completed < $status->total_steps ) { | ||
| 542 | print "Steps completed: ", $status->steps_completed, "\n" | ||
| 543 | if $opt_v; | ||
| 544 | sleep(20); | ||
| 545 | } else { | ||
| 546 | die "ERROR: analysis event block not well formed.\n"; | ||
| 547 | } | ||
| 548 | } elsif ( $status->type == LSAE_TIME_PROGRESS_EVENT ) { | ||
| 549 | if ( $status->remaining == 0 ) { | ||
| 550 | $completed{$queryID} = 1; | ||
| 551 | } elsif ( $status->remaining > 0 ) { | ||
| 552 | print "Time remaining: ", $status->remaining, "\n" | ||
| 553 | if $opt_v; | ||
| 554 | sleep(20); | ||
| 555 | } else { | ||
| 556 | die "ERROR: analysis event block not well formed.\n"; | ||
| 557 | } | ||
| 558 | } | ||
| 559 | } | ||
| 560 | last if scalar keys(%completed) == $#query_ids + 1; | ||
| 561 | } | ||
| 562 | foreach my $queryID (@query_ids) { | ||
| 563 | |||
| 564 | # get the result | ||
| 565 | my $searchTerm .= | ||
| 566 | "<wsrp:ResourceProperty xmlns:wsrp='$WSRF::Constants::WSRP' xmlns:mobyws='$WSRF::Constants::MOBY'>"; | ||
| 567 | $searchTerm .= "mobyws:result_" . $queryID; | ||
| 568 | $searchTerm .= "</wsrp:ResourceProperty>"; | ||
| 569 | my $ans = WSRF::Lite->uri($WSRF::Constants::WSRP)->on_action( | ||
| 570 | sub { | ||
| 571 | sprintf '%s/%s/%sRequest', $WSRF::Constants::WSRPW, $_[1], | ||
| 572 | $_[1]; | ||
| 573 | } | ||
| 574 | )->wsaddress($EPR) | ||
| 575 | ->GetMultipleResourceProperties( | ||
| 576 | SOAP::Data->value($searchTerm)->type('xml') ); | ||
| 577 | die "ERROR: " . $ans->faultstring if ( $ans->fault ); | ||
| 578 | my $parser = XML::LibXML->new(); | ||
| 579 | my $xml = $ans->raw_xml; | ||
| 580 | my $doc = $parser->parse_string($xml); | ||
| 581 | $soap = $doc->getDocumentElement(); | ||
| 582 | my $prop_name = "result_" . $queryID; | ||
| 583 | my ($prop) = | ||
| 584 | $soap->getElementsByTagNameNS( $WSRF::Constants::MOBY, $prop_name ) | ||
| 585 | || $soap->getElementsByTagName($prop_name); | ||
| 586 | my $result = $prop->getFirstChild->toString | ||
| 587 | unless ref $prop eq "XML::LibXML::NodeList"; | ||
| 588 | $result = $prop->pop()->getFirstChild->toString | ||
| 589 | if ref $prop eq "XML::LibXML::NodeList"; | ||
| 590 | print $result; | ||
| 591 | } | ||
| 592 | |||
| 593 | # destroy the result | ||
| 594 | my $ans = WSRF::Lite->uri($WSRF::Constants::WSRL)->on_action( | ||
| 595 | sub { | ||
| 596 | sprintf '%s/ImmediateResourceTermination/%sRequest', | ||
| 597 | $WSRF::Constants::WSRLW, $_[1]; | ||
| 598 | } | ||
| 599 | )->wsaddress($EPR)->Destroy(); | ||
| 600 | } else { | ||
| 601 | |||
| 602 | # calling a local service module, without SOAP | ||
| 603 | my $data; | ||
| 604 | if ( @ARGV > 0 ) { | ||
| 605 | $data = shift; # a file name | ||
| 606 | } else { | ||
| 607 | use File::Temp qw( tempfile ); | ||
| 608 | my $fh; | ||
| 609 | ( $fh, $data ) = tempfile( UNLINK => 1 ); | ||
| 610 | print $fh _empty_input(); | ||
| 611 | close $fh; | ||
| 612 | } | ||
| 613 | eval "require $module" or croak $@; | ||
| 614 | eval { | ||
| 615 | my $target = new $module; | ||
| 616 | print $target->$service($data), "\n"; | ||
| 617 | } or croak $@; | ||
| 618 | } | ||
| 619 | |||
| 620 | sub _moby_wsrf_header { | ||
| 621 | my ( $url, $id ) = @_; | ||
| 622 | return <<"END OF XML"; | ||
| 623 | <moby-wsrf> | ||
| 624 | <wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">http://docs.oasis-open.org/wsrf/rpw-2/GetMultipleResourceProperties/GetMultipleResourcePropertiesRequest</wsa:Action> | ||
| 625 | <wsa:To xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa="http://www.w3.org/2005/08/addressing" wsu:Id="To">$url</wsa:To> | ||
| 626 | <mobyws:ServiceInvocationId xmlns:mobyws="http://biomoby.org/" xmlns:wsa="http://www.w3.org/2005/08/addressing" wsa:IsReferenceParameter="true">$id</mobyws:ServiceInvocationId> | ||
| 627 | </moby-wsrf> | ||
| 628 | END OF XML | ||
| 629 | } | ||
| 630 | __END__ |