From ad2af1c47c6b1fcf686bc78135a4dcfb77888e48 Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 16 Feb 2005 23:17:14 +0000 Subject: [PATCH] fixed up the magic auto-introspection and made sure we were closing connections when necessary git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@81 9efc2488-bf62-4759-914b-345cdb29e865 --- src/perlmods/OpenSRF/AppSession.pm | 1 - src/perlmods/OpenSRF/Application.pm | 53 ++++++++++++++++------ src/perlmods/OpenSRF/Application/Demo/Math.pm | 56 +++--------------------- src/perlmods/OpenSRF/Application/Demo/MathDB.pm | 8 ++-- src/perlmods/OpenSRF/Application/Settings.pm | 1 + src/perlmods/OpenSRF/DOM/Element/domainObject.pm | 2 +- src/perlmods/OpenSRF/System.pm | 6 ++- src/perlmods/OpenSRF/UnixServer.pm | 4 +- src/perlmods/OpenSRF/Utils/SettingsClient.pm | 6 ++- 9 files changed, 64 insertions(+), 73 deletions(-) diff --git a/src/perlmods/OpenSRF/AppSession.pm b/src/perlmods/OpenSRF/AppSession.pm index 35cdb67..230e3a1 100644 --- a/src/perlmods/OpenSRF/AppSession.pm +++ b/src/perlmods/OpenSRF/AppSession.pm @@ -179,7 +179,6 @@ sub get_app_targets { if( !ref($targets) ) { $targets = [ $targets ]; } } - warn "Returning Targets @$targets\n"; return @$targets; } diff --git a/src/perlmods/OpenSRF/Application.pm b/src/perlmods/OpenSRF/Application.pm index 0aa3c60..1f5325a 100644 --- a/src/perlmods/OpenSRF/Application.pm +++ b/src/perlmods/OpenSRF/Application.pm @@ -78,7 +78,7 @@ sub handler { my $method_proto = $session->last_message_api_level; $log->debug( " * Method API Level [$method_proto]", DEBUG); - my $coderef = $app->method_lookup( $method_name, $method_proto ); + my $coderef = $app->method_lookup( $method_name, $method_proto, 1, 1 ); unless ($coderef) { $session->status( OpenSRF::DomainObject::oilsMethodException->new() ); @@ -118,7 +118,7 @@ sub handler { $resp = $coderef->run( $appreq, @args); my $time = sprintf '%.3f', time() - $start; $log->debug( "Method duration for {$method_name -> ".join(', ', @args)."}: ". $time, DEBUG ); - if( ref( $resp ) ) { + if( defined( $resp ) ) { #$log->debug( "Calling respond_complete: ". $resp->toString(), INTERNAL ); $appreq->respond_complete( $resp ); } else { @@ -226,9 +226,9 @@ sub register_method { } sub retrieve_remote_apis { - my $session = AppSession->create('settings'); + my $session = OpenSRF::AppSession->create('settings'); try { - $session->connect; + $session->connect or OpenSRF::EX::WARN->throw("Connection to settings timed out"); } catch Error with { my $e = shift; $log->debug( "Remote subrequest returned an error:\n". $e ); @@ -238,15 +238,22 @@ sub retrieve_remote_apis { }; my $req = $session->request( 'opensrf.settings.xpath.get', '//activeapps/appname' ); - my $list = $req->recv->content; + my $list = $req->recv; + + if( UNIVERSAL::isa($list,"Error") ) { + throw $list; + } + + my $content = $list->content; $req->finish; $session->finish; $session->disconnect; - my %u_list = map { ($_ => 1) } @$list; + my %u_list = map { ($_ => 1) } @$content; for my $class ( keys %u_list ) { + next if($class eq $server_class); populate_remote_method_cache($class); } } @@ -254,7 +261,7 @@ sub retrieve_remote_apis { sub populate_remote_method_cache { my $class = shift; - my $session = AppSession->create($class); + my $session = OpenSRF::AppSession->create($class); try { $session->connect or OpenSRF::EX::WARN->throw("Connection to $class timed out"); @@ -267,6 +274,7 @@ sub populate_remote_method_cache { next if ( exists($_METHODS[$$method{api_level}]) && exists($_METHODS[$$method{api_level}]{$$method{api_name}}) ); $method->{remote} = 1; + bless($method, __PACKAGE__ ); $_METHODS[$$method{api_level}]{$$method{api_name}} = $method; } @@ -286,6 +294,7 @@ sub method_lookup { my $method = shift; my $proto = shift; my $no_recurse = shift || 0; + my $no_remote = shift || 0; # this instead of " || 1;" above to allow api_level 0 $proto = 1 unless (defined $proto); @@ -311,6 +320,11 @@ sub method_lookup { if (defined $meth) { $log->debug("Looks like we found [$method]!", DEBUG); $log->debug("Method object is ".Dumper($meth), INTERNAL); + if($no_remote and $meth->{remote}) { + $log->debug("OH CRAP We're not supposed to return remote methods", WARN); + return undef; + } + } elsif (!$no_recurse) { retrieve_remote_apis(); $meth = $self->method_lookup($method,$proto,1); @@ -324,10 +338,11 @@ sub run { my $req = shift; my $resp; + my @params = @_; if ( !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) { $log->debug("Creating a SubRequest object", DEBUG); - unshift @_, $req; + unshift @params, $req; $req = OpenSRF::AppSubrequest->new; } else { $log->debug("This is a top level request", DEBUG); @@ -335,30 +350,40 @@ sub run { if (!$self->{remote}) { my $code ||= \&{$self->{package} . '::' . $self->{method}}; - $resp = $code->($self, $req, @_); + $resp = $code->($self, $req, @params); if ( ref($req) and UNIVERSAL::isa($req, 'OpenSRF::AppSubrequest') ) { $req->respond($resp) if (defined $resp); + $log->debug("A SubRequest object is responding " . join(" ",$req->responses), DEBUG); return $req->responses; } else { $log->debug("A top level Request object is responding $resp", DEBUG); return $resp; } } else { - my $session = AppSession->create($self->{server_class}); + my $session = OpenSRF::AppSession->create($self->{server_class}); try { $session->connect or OpenSRF::EX::WARN->throw("Connection to [$$self{server_class}] timed out"); - my $remote_req = $session->request( $self->{api_name}, @_ ); - while (my $remote_resp = $remote_req->recv->content) { - $req->respond( $remote_resp ); + my $remote_req = $session->request( $self->{api_name}, @params ); + while (my $remote_resp = $remote_req->recv) { + OpenSRF::Utils::Logger->debug("Remote Subrequest Received " . $remote_resp, INTERNAL ); + if( UNIVERSAL::isa($remote_resp,"Error") ) { + throw $remote_resp; + } + $req->respond( $remote_resp->content ); } - return $req->responses; + $remote_req->finish(); + $session->finish(); } catch Error with { my $e = shift; $log->debug( "Remote subrequest returned an error:\n". $e ); return undef; }; + + $log->debug( "Remote Subrequest Responses " . join(" ", $req->responses), INTERNAL ); + + return $req->responses; } # huh? how'd we get here... return undef; diff --git a/src/perlmods/OpenSRF/Application/Demo/Math.pm b/src/perlmods/OpenSRF/Application/Demo/Math.pm index a895bb1..e373dc5 100644 --- a/src/perlmods/OpenSRF/Application/Demo/Math.pm +++ b/src/perlmods/OpenSRF/Application/Demo/Math.pm @@ -18,60 +18,18 @@ sub send_request { my $method_name = shift; my @params = @_; - - $log->debug( "Creating a client environment", DEBUG ); - my $session = OpenSRF::AppSession->create( - "dbmath", sysname => 'math', secret => '12345' ); - - $log->debug( "Sending request to math server", INTERNAL ); - - my $method = OpenSRF::DomainObject::oilsMethod->new( method => $method_name ); - - $method->params( @params ); - - - my $req; - my $resp; - try { - - for my $nn (0..1) { - my $vv = $session->connect(); - if($vv) { last; } - if( $nn and !$vv ) { - throw OpenSRF::EX::CRITICAL ("DBMath connect attempt timed out"); - } - } + my $method = $self->method_lookup( "dbmath.$method_name" ); + my ($resp) = $method->run( @params ); - $req = $session->request( $method ); - $resp = $req->recv(10); - - } catch OpenSRF::DomainObject::oilsAuthException with { - my $e = shift; - $e->throw(); - }; - - if ( defined($resp) and $resp and $resp->class->isa('OpenSRF::DomainObject::oilsResult') ){ - - $log->debug( "Math server returned " . $resp->toString(1), INTERNAL ); - $req->finish; - $session->finish; - return $resp; - - } else { + if(!defined($resp)) { + throw OpenSRF::EX::ERROR ("Did not receive expected data from MathDB\n" . $resp); + } - if( $resp ) { $log->debug( "Math received \n".$resp->toString(), ERROR ); } - else{ $log->debug( "Math received empty value", ERROR ); } - $req->finish; - $session->finish; - if( $resp ) { - throw OpenSRF::EX::ERROR ("Did not receive expected data from MathDB\n" . $resp); - } else { - throw OpenSRF::EX::ERROR ("Received no data from MathDB"); - } + $log->debug( "MathDB server returned " . $resp, INTERNAL ); + return $resp; - } } __PACKAGE__->register_method( method => 'send_request', api_name => '_send_request' ); diff --git a/src/perlmods/OpenSRF/Application/Demo/MathDB.pm b/src/perlmods/OpenSRF/Application/Demo/MathDB.pm index 24c13b6..6d0749e 100644 --- a/src/perlmods/OpenSRF/Application/Demo/MathDB.pm +++ b/src/perlmods/OpenSRF/Application/Demo/MathDB.pm @@ -12,7 +12,7 @@ sub DESTROY{} our $log = 'OpenSRF::Utils::Logger'; sub initialize {} -__PACKAGE__->register_method( method => 'add_1', api_name => 'add' ); +__PACKAGE__->register_method( method => 'add_1', api_name => 'dbmath.add' ); sub add_1 { my $self = shift; my $client = shift; @@ -26,7 +26,7 @@ sub add_1 { } -__PACKAGE__->register_method( method => 'sub_1', api_name => 'sub' ); +__PACKAGE__->register_method( method => 'sub_1', api_name => 'dbmath.sub' ); sub sub_1 { my $self = shift; my $client = shift; @@ -39,7 +39,7 @@ sub sub_1 { return JSON::number::new($a); } -__PACKAGE__->register_method( method => 'mult_1', api_name => 'mult' ); +__PACKAGE__->register_method( method => 'mult_1', api_name => 'dbmath.mult' ); sub mult_1 { my $self = shift; my $client = shift; @@ -52,7 +52,7 @@ sub mult_1 { return JSON::number::new($a); } -__PACKAGE__->register_method( method => 'div_1', api_name => 'div' ); +__PACKAGE__->register_method( method => 'div_1', api_name => 'dbmath.div' ); sub div_1 { my $self = shift; my $client = shift; diff --git a/src/perlmods/OpenSRF/Application/Settings.pm b/src/perlmods/OpenSRF/Application/Settings.pm index af7c3f3..989e09b 100644 --- a/src/perlmods/OpenSRF/Application/Settings.pm +++ b/src/perlmods/OpenSRF/Application/Settings.pm @@ -18,6 +18,7 @@ sub get_host_config { __PACKAGE__->register_method( method => 'xpath_get', api_name => 'opensrf.settings.xpath.get' ); sub xpath_get { my($self, $client, $xpath) = @_; + warn "*************** Received XPATH $xpath\n"; return OpenSRF::Utils::SettingsParser->new()->_get_all( $xpath ); } diff --git a/src/perlmods/OpenSRF/DOM/Element/domainObject.pm b/src/perlmods/OpenSRF/DOM/Element/domainObject.pm index f08e041..4b98512 100644 --- a/src/perlmods/OpenSRF/DOM/Element/domainObject.pm +++ b/src/perlmods/OpenSRF/DOM/Element/domainObject.pm @@ -4,7 +4,7 @@ use base 'OpenSRF::DOM::Element'; use OpenSRF::DOM; use OpenSRF::DOM::Element::domainObjectAttr; use OpenSRF::Utils::Logger qw(:level); -use OpenSRF::EX; +use OpenSRF::EX qw(:try); use Carp; #use OpenSRF::DomainObject::oilsPrimitive; #use OpenSRF::DomainObject::oilsResponse; diff --git a/src/perlmods/OpenSRF/System.pm b/src/perlmods/OpenSRF/System.pm index 7015428..ec2276c 100644 --- a/src/perlmods/OpenSRF/System.pm +++ b/src/perlmods/OpenSRF/System.pm @@ -13,6 +13,7 @@ use POSIX ":sys_wait_h"; use OpenSRF::Utils::Config; use OpenSRF::Utils::SettingsParser; use OpenSRF::Utils::SettingsClient; +use Net::Server::PreFork; use strict; my $bootstrap_config_file; @@ -109,6 +110,8 @@ sub load_bootstrap_config { OpenSRF::Utils::Config->load( config_file => $bootstrap_config_file ); + JSON->register_class_hint( name => "OpenSRF::Application", hint => "", type => "hash" ); + OpenSRF::Transport->message_envelope( "OpenSRF::Transport::SlimJabber::MessageWrapper" ); OpenSRF::Transport::PeerHandle->set_peer_client( "OpenSRF::Transport::SlimJabber::PeerConnection" ); OpenSRF::Transport::Listener->set_listener( "OpenSRF::Transport::SlimJabber::Inbound" ); @@ -262,8 +265,7 @@ sub launch_settings { # XXX the $self like this and pid automation will not work with this setup.... my($self) = @_; - use Net::Server::Single; - @OpenSRF::UnixServer::ISA = qw(OpenSRF Net::Server::Single); + @OpenSRF::UnixServer::ISA = qw(OpenSRF Net::Server::PreFork); my $pid = OpenSRF::Utils::safe_fork(); if( $pid ) { diff --git a/src/perlmods/OpenSRF/UnixServer.pm b/src/perlmods/OpenSRF/UnixServer.pm index cf8b19c..5e379f0 100644 --- a/src/perlmods/OpenSRF/UnixServer.pm +++ b/src/perlmods/OpenSRF/UnixServer.pm @@ -9,6 +9,7 @@ use OpenSRF::AppSession; use OpenSRF::DomainObject::oilsResponse qw/:status/; use OpenSRF::System; use OpenSRF::Utils::SettingsClient; +use JSON; use vars qw/@ISA $app/; use Carp; @@ -188,10 +189,11 @@ sub configure_hook { my $imp = $client->config_value("apps", $app, "implementation"); OpenSRF::Application::server_class($app); OpenSRF::Application->application_implementation( $imp ); + JSON->register_class_hint( name => $imp, hint => $app, type => "hash" ); OpenSRF::Application->application_implementation->initialize() if (OpenSRF::Application->application_implementation->can('initialize')); - if( $client->config_value("server_type") !~ /fork/i || $app eq "settings" ) { + if( $client->config_value("server_type") !~ /fork/i ) { $self->child_init_hook(); } diff --git a/src/perlmods/OpenSRF/Utils/SettingsClient.pm b/src/perlmods/OpenSRF/Utils/SettingsClient.pm index 0f1eeaa..86b25f0 100755 --- a/src/perlmods/OpenSRF/Utils/SettingsClient.pm +++ b/src/perlmods/OpenSRF/Utils/SettingsClient.pm @@ -56,10 +56,11 @@ sub grab_host_config { my $bsconfig = OpenSRF::Utils::Config->current; my $resp; + my $req; try { if( ! ($session->connect()) ) {die "Settings Connect timed out\n";} - my $req = $session->request( "opensrf.settings.host_config.get", $host ); + $req = $session->request( "opensrf.settings.host_config.get", $host ); $resp = $req->recv( timeout => 10 ); } catch OpenSRF::EX with { @@ -79,6 +80,9 @@ sub grab_host_config { } $host_config = $resp->content(); + $req->finish(); + $session->finish; + $session->disconnect(); } -- 2.11.0