[MOBY-guts] biomoby commit

Eddie Kawas kawas at dev.open-bio.org
Thu Apr 2 15:37:20 UTC 2009


kawas
Thu Apr  2 11:37:20 EDT 2009
Update of /home/repository/moby/moby-live/Perl/MOSES-MOBY/bin/scripts
In directory dev.open-bio.org:/tmp/cvs-serv729/Perl/MOSES-MOBY/bin/scripts

Modified Files:
	moses-testing-service.pl 
Log Message:
added a -C option for testing asynchronous POST services.
moby-live/Perl/MOSES-MOBY/bin/scripts moses-testing-service.pl,1.7,1.8
===================================================================
RCS file: /home/repository/moby/moby-live/Perl/MOSES-MOBY/bin/scripts/moses-testing-service.pl,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- /home/repository/moby/moby-live/Perl/MOSES-MOBY/bin/scripts/moses-testing-service.pl	2008/09/02 17:58:02	1.7
+++ /home/repository/moby/moby-live/Perl/MOSES-MOBY/bin/scripts/moses-testing-service.pl	2009/04/02 15:37:20	1.8
@@ -10,8 +10,8 @@
 
 	# some command-line options
 	use Getopt::Std;
-	use vars qw/ $opt_h $opt_d $opt_v $opt_l $opt_e $opt_c $opt_a /;
-	getopts('hdvl:e:c:a:');
+	use vars qw/ $opt_h $opt_d $opt_v $opt_l $opt_e $opt_c $opt_a $opt_C /;
+	getopts('hdvl:e:c:C:a:');
 
 	# usage
 	if ( $opt_h or ( @ARGV == 0 and ( not $opt_c ) ) ) {
@@ -32,7 +32,10 @@
        # calling a real service, using cgi
        -c <service-url> [<input-file>]
 
-       # calling a real service, using SOAP
+       # calling a real service, using asynchronous cgi
+       -C <service-url> [<input-file>]
+
+       # calling a real service, using async SOAP
        -a <service-url> <service-name> [<input-file>]
 
     <package-name> is a full name of a called module (service)
@@ -64,6 +67,11 @@
 END_OF_USAGE
 		exit(0);
 	}
+	use HTTP::Request;
+	use LWP::UserAgent;
+	use XML::LibXML;
+	use MOBY::Async::WSRF;
+	use MOBY::Async::LSAE;
 
 	# use MOSES::MOBY::Base;
 	# load modules, depending on the mode of calling
@@ -79,6 +87,19 @@
 		  or die "$@\n";
 		eval "use LWP::UserAgent; 1;"
 		  or die "$@\n";
+	} elsif ($opt_C) {
+
+		# calling a real service, using async cgi
+		eval "use HTTP::Request; 1;"
+		  or die "$@\n";
+		eval "use LWP::UserAgent; 1;"
+		  or die "$@\n";
+		eval "use XML::LibXML; 1;"
+		  or die "$@\n";
+		eval "use MOBY::Async::WSRF; 1;"
+		  or die "$@\n";
+		eval "use MOBY::Async::LSAE; 1;"
+		  or die "$@\n";
 	} else {
 
 		# calling a local service module, without SOAP
@@ -93,12 +114,13 @@
 		$LOG->level('INFO')  if $opt_v;
 		$LOG->level('DEBUG') if $opt_d;
 	}
+
 	# load these modules always to get constants and to avoid warnings
 	eval "use MOBY::Async::LSAE; 1;"
 	  or die "$@\n";
 	eval "use MOBY::Async::WSRF; 1;"
 	  or die "$@\n";
-	
+
 }
 
 use strict;
@@ -114,8 +136,59 @@
 END_OF_XML
 }
 
+sub _check_status {
+	my ( $status, $completed, $queryID, $opt_v ) = @_;
+	if ( $status->type == LSAE_PERCENT_PROGRESS_EVENT ) {
+		if ( $status->percentage >= 100 ) {
+			$completed->{$queryID} = 1;
+		} elsif ( $status->percentage < 100 ) {
+			print "Current percentage: ", $status->percentage, "\n" if $opt_v;
+			sleep(20);
+		} else {
+			die "ERROR:  analysis event block not well formed.\n";
+		}
+
+	} elsif ( $status->type == LSAE_STATE_CHANGED_EVENT ) {
+		if (    ( $status->new_state =~ m"completed"i )
+			 || ( $status->new_state =~ m"terminated_by_request"i )
+			 || ( $status->new_state =~ m"terminated_by_error"i ) )
+		{
+			$completed->{$queryID} = 1;
+		} elsif (    ( $status->new_state =~ m"created"i )
+				  || ( $status->new_state =~ m"running"i ) )
+		{
+			print "Current State: ", $status->new_state, "\n" if $opt_v;
+			sleep(20);
+		} else {
+			die "ERROR:  analysis event block not well formed.\n";
+		}
+
+	} elsif ( $status->type == LSAE_STEP_PROGRESS_EVENT ) {
+		if ( $status->steps_completed >= $status->total_steps ) {
+			$completed->{$queryID} = 1;
+		} elsif ( $status->steps_completed < $status->total_steps ) {
+			print "Steps completed: ", $status->steps_completed, "\n" if $opt_v;
+			sleep(20);
+		} else {
+			die "ERROR:  analysis event block not well formed.\n";
+		}
+
+	} elsif ( $status->type == LSAE_TIME_PROGRESS_EVENT ) {
+		if ( $status->remaining == 0 ) {
+			$completed->{$queryID} = 1;
+		} elsif ( $status->remaining > 0 ) {
+			print "Time remaining: ", $status->remaining, "\n" if $opt_v;
+			sleep(20);
+		} else {
+			die "ERROR:  analysis event block not well formed.\n";
+		}
+	} else {
+		warn "Whilst checking the status of our resource, we entered into a possible infinite loop ...\n";
+	}
+}
+
 sub _get_query_ids {
-	my $input = shift;
+	my $input     = shift;
 	my @query_ids = ();
 	my $parser    = XML::LibXML->new();
 	my $doc       = $parser->parse_string($input);
@@ -124,17 +197,17 @@
 		my $node = $iterator->get_node($_);
 		my $id   = $node->getAttribute("queryID")
 		  || $node->getAttribute(
-				 $node->lookupNamespacePrefix($WSRF::Constants::MOBY_MESSAGE_NS)
-				   . ":queryID" );
+						 $node->lookupNamespacePrefix($WSRF::Constants::MOBY_MESSAGE_NS)
+						   . ":queryID" );
 		push @query_ids, $id;
 	}
 	return @query_ids;
 }
 
 # --- what service to call
-my $module = shift unless $opt_c;    # eg. Service::Mabuhay, or just Mabuhay
+my $module = shift unless $opt_c or $opt_C;    # eg. Service::Mabuhay, or just Mabuhay
 my $service;
-( $service = $module ) =~ s/.*::// unless $opt_c;
+( $service = $module ) =~ s/.*::// unless $opt_c or $opt_C;
 
 # --- call the service
 if ($opt_e) {
@@ -146,12 +219,8 @@
 			my $res  = shift;
 			my $msg =
 			  ref $res
-			  ? "--- SOAP FAULT ---\n"
-			  . $res->faultcode . " "
-			  . $res->faultstring
-			  : "--- TRANSPORT ERROR ---\n"
-			  . $soap->transport->status
-			  . "\n$res\n";
+			  ? "--- SOAP FAULT ---\n" . $res->faultcode . " " . $res->faultstring
+			  : "--- TRANSPORT ERROR ---\n" . $soap->transport->status . "\n$res\n";
 			die $msg;
 		}
 	);
@@ -190,6 +259,117 @@
 	$req->content("data=$input");
 	print "\n" . $ua->request($req)->as_string . "\n";
 
+} elsif ($opt_C) {
+
+	my $input = '';
+	if ( @ARGV > 0 ) {
+		my $data = shift;    # a file name
+		open INPUT, "<$data"
+		  or die "Cannot read '$data': $!\n";
+		while (<INPUT>) { $input .= $_; }
+		close INPUT;
+	} else {
+		$input = _empty_input;
+	}
+
+	# extract all of the query ids from $input
+	my @query_ids = _get_query_ids($input);
+	my %completed = ();
+
+	print "Sending the following data to $opt_C\n$input\n";
+
+	# post to the web service
+	my $ua = LWP::UserAgent->new;
+	my $req = HTTP::Request->new( POST => $opt_C );
+	$req->content_type('application/x-www-form-urlencoded');
+	$req->content("data=$input");
+
+	# get the response
+	my $response = $ua->request($req);
+
+	# do we have an error?
+	die "Error calling service: " . $response->status_line
+	  if ( $response->code != 200 );
+
+	my $epr    = $response->header("moby-wsrf");
+	my $parser = XML::LibXML->new();
+	my $doc    = $parser->parse_string($epr);
+	my $ID =
+	  $doc->getElementsByLocalName("ServiceInvocationId")->get_node(1)->textContent;
+	$ID =~ s/ //gi;
+	my $searchTerm = "";
+	$searchTerm .=
+"<wsrf-rp:GetMultipleResourceProperties xmlns:wsrf-rp='$WSRF::Constants::WSRP' xmlns:mobyws='$WSRF::Constants::MOBY'>";
+
+	foreach my $queryID (@query_ids) {
+		$searchTerm .=
+		    "<wsrf-rp:ResourceProperty>mobyws:status_" 
+		  . $queryID
+		  . "</wsrf-rp:ResourceProperty>";
+	}
+	$searchTerm .= "</wsrf-rp:GetMultipleResourceProperties>";
+	my $header = _moby_wsrf_header( $opt_C, $ID );
+	$header =~ s/\n//gi;
+
+	# poll
+	while (1) {
+		foreach my $queryID (@query_ids) {
+
+			# skip poll if current job completed
+			next if $completed{$queryID};
+
+			# poll the service for given query ID
+			$req = HTTP::Request->new( POST => $opt_C . "/status" );
+			$req->header( "moby-wsrf" => $header );
+			$req->content_type('application/x-www-form-urlencoded');
+			$req->content("data=$searchTerm");
+			$response = $ua->request($req);
+			my $xml    = $response->content();
+			my $parser = XML::LibXML->new();
+			my $doc    = $parser->parse_string($xml);
+			$doc = $doc->documentElement()->firstChild()->firstChild();
+
+			my $status = LSAE::AnalysisEventBlock->new( $doc->toString );
+			&_check_status( $status, \%completed, $queryID, $opt_v );
+		}
+		last if scalar keys(%completed) == $#query_ids + 1;
+	}
+
+	# task is finished, obtain the result
+	$searchTerm = "";
+	$searchTerm .=
+"<wsrf-rp:GetMultipleResourceProperties xmlns:wsrf-rp='$WSRF::Constants::WSRP' xmlns:mobyws='$WSRF::Constants::MOBY'>";
+	foreach my $queryID (@query_ids) {
+		$searchTerm .=
+		    "<wsrf-rp:ResourceProperty>mobyws:result_" 
+		  . $queryID
+		  . "</wsrf-rp:ResourceProperty>";
+	}
+	$searchTerm .= "</wsrf-rp:GetMultipleResourceProperties>";
+
+	$header = _moby_wsrf_header( $opt_C, $ID );
+	$header =~ s/\n//gi;
+
+	$req = HTTP::Request->new( POST => $opt_C . "/results" );
+	$req->header( "moby-wsrf" => $header );
+	$req->content_type('application/x-www-form-urlencoded');
+	$req->content("data=$searchTerm");
+	$response = $ua->request($req);
+
+	# create nicely formatted XML
+	$parser = XML::LibXML->new();
+	$doc    = $parser->parse_string( $response->content );
+	print "\n" . $doc->toString(1);
+
+	# destroy the job
+	$searchTerm = '<Destroy xmlns="http://docs.oasis-open.org/wsrf/rl-2"/> ';
+	$req = HTTP::Request->new( POST => $opt_C . "/destroy" );
+	$req->header( "moby-wsrf" => $header );
+	$req->content_type('application/x-www-form-urlencoded');
+	$req->content("data=$searchTerm");
+	$response = $ua->request($req);
+	print "Destroying the resource returned:\n\t" . $response->content . "\n" if $opt_v;
+
 } elsif ($opt_a) {
 
 	# calling a real service, using async soap
@@ -203,12 +383,8 @@
 			my $res  = shift;
 			my $msg =
 			  ref $res
-			  ? "--- SOAP FAULT ---\n"
-			  . $res->faultcode . " "
-			  . $res->faultstring
-			  : "--- TRANSPORT ERROR ---\n"
-			  . $soap->transport->status
-			  . "\n$res\n";
+			  ? "--- SOAP FAULT ---\n" . $res->faultcode . " " . $res->faultstring
+			  : "--- TRANSPORT ERROR ---\n" . $soap->transport->status . "\n$res\n";
 			die $msg;
 		}
 	);
@@ -227,13 +403,11 @@
 
 	# extract all of the query ids from $input
 	my @query_ids = _get_query_ids($input);
-	print "\nSending the following data to $service asynchronously:\n",
-	  $input, "\n"
+	print "\nSending the following data to $service asynchronously:\n", $input, "\n"
 	  if $opt_v;
 
 	# submit the job
-	my $epr =
-	  ( $soap->$service( SOAP::Data->type( 'string' => "$input" ) )->result );
+	my $epr = ( $soap->$service( SOAP::Data->type( 'string' => "$input" ) )->result );
 
 	# Get address from the returned Endpoint Reference
 	my $address = $epr->{'EndpointReference'}->{Address};
@@ -265,12 +439,11 @@
 
 			$soap = WSRF::Lite->uri($WSRF::Constants::WSRP)->on_action(
 				sub {
-					sprintf '%s/%s/%sRequest', $WSRF::Constants::WSRPW, $_[1],
-					  $_[1];
+					sprintf '%s/%s/%sRequest', $WSRF::Constants::WSRPW, $_[1], $_[1];
 				}
 			  )->wsaddress($EPR)
 			  ->GetMultipleResourceProperties(
-								  SOAP::Data->value($searchTerm)->type('xml') );
+										  SOAP::Data->value($searchTerm)->type('xml') );
 
 			my $parser = XML::LibXML->new();
 			my $xml    = $soap->raw_xml;
@@ -279,8 +452,7 @@
 			my $prop_name = "status_" . $queryID;
 
 			my ($prop) =
-			  $soap->getElementsByTagNameNS( $WSRF::Constants::MOBY,
-											 $prop_name )
+			     $soap->getElementsByTagNameNS( $WSRF::Constants::MOBY, $prop_name )
 			  || $soap->getElementsByTagName($prop_name);
 			my $event = $prop->getFirstChild->toString
 			  unless ref $prop eq "XML::LibXML::NodeList";
@@ -338,6 +510,7 @@
 	}
 
 	foreach my $queryID (@query_ids) {
+
 		# get the result
 		my $searchTerm .=
 "<wsrp:ResourceProperty xmlns:wsrp='$WSRF::Constants::WSRP' xmlns:mobyws='$WSRF::Constants::MOBY'>";
@@ -345,21 +518,19 @@
 		$searchTerm .= "</wsrp:ResourceProperty>";
 		my $ans = WSRF::Lite->uri($WSRF::Constants::WSRP)->on_action(
 			sub {
-				sprintf '%s/%s/%sRequest', $WSRF::Constants::WSRPW, $_[1],
-				  $_[1];
+				sprintf '%s/%s/%sRequest', $WSRF::Constants::WSRPW, $_[1], $_[1];
 			}
 		  )->wsaddress($EPR)
 		  ->GetMultipleResourceProperties(
-								  SOAP::Data->value($searchTerm)->type('xml') );
+										  SOAP::Data->value($searchTerm)->type('xml') );
 		die "ERROR:  " . $ans->faultstring if ( $ans->fault );
 
 		my $parser = XML::LibXML->new();
 		my $xml    = $ans->raw_xml;
-		my $doc = $parser->parse_string($xml);
+		my $doc    = $parser->parse_string($xml);
 		$soap = $doc->getDocumentElement();
 		my $prop_name = "result_" . $queryID;
-		my ($prop) =
-		     $soap->getElementsByTagNameNS( $WSRF::Constants::MOBY, $prop_name )
+		my ($prop) = $soap->getElementsByTagNameNS( $WSRF::Constants::MOBY, $prop_name )
 		  || $soap->getElementsByTagName($prop_name);
 		my $result = $prop->getFirstChild->toString
 		  unless ref $prop eq "XML::LibXML::NodeList";
@@ -396,4 +567,14 @@
 	} or croak $@;
 }
 
+sub _moby_wsrf_header {
+	my ( $url, $id ) = @_;
+	return <<"END OF XML";
+<moby-wsrf>
+<wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">http://docs.oasis-open.org/wsrf/rpw-2/GetResourceProperty/GetResourcePropertiesRequest</wsa:Action>
+<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>
+<mobyws:ServiceInvocationId xmlns:mobyws="http://biomoby.org/" xmlns:wsa="http://www.w3.org/2005/08/addressing" wsa:IsReferenceParameter="true">$id</mobyws:ServiceInvocationId>
+</moby-wsrf>
+END OF XML
+}
 __END__




More information about the MOBY-guts mailing list