]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Application.pm
LP#1729610: Allow queuing (for a while) during child backlog
[OpenSRF.git] / src / perl / lib / OpenSRF / Application.pm
1 package OpenSRF::Application;
2 # vim:noet:ts=4
3 use vars qw/$_app $log @_METHODS $thunk $server_class/;
4
5 use base qw/OpenSRF/;
6 use OpenSRF::AppSession;
7 use OpenSRF::DomainObject::oilsMethod;
8 use OpenSRF::DomainObject::oilsResponse qw/:status/;
9 use OpenSRF::Utils::Logger qw/:level $logger/;
10 use Data::Dumper;
11 use Time::HiRes qw/time/;
12 use OpenSRF::EX qw/:try/;
13 use Carp;
14 use OpenSRF::Utils::JSON;
15
16 sub DESTROY{};
17
18 use strict;
19 use warnings;
20
21 $log = 'OpenSRF::Utils::Logger';
22
23 our $in_request = 0;
24 our @pending_requests;
25 our $shared_conf;
26
27 sub package {
28         my $self = shift;
29         return 1 unless ref($self);
30         return $self->{package};
31 }
32
33 sub signature {
34         my $self = shift;
35         return 0 unless ref($self);
36         return $self->{signature};
37 }
38
39 sub strict {
40     my $self = shift; 
41     return 0 unless ref($self);
42     return $self->{strict};
43 }
44
45 sub argc {
46         my $self = shift;
47         return 0 unless ref($self);
48         return $self->{argc};
49 }
50
51 sub max_bundle_size {
52         my $self = shift;
53         return 0 unless ref($self);
54         return $self->{max_bundle_size} if (defined($self->{max_bundle_size}));
55         return 25600; # 25K
56 }
57
58 sub max_bundle_count {
59         my $self = shift;
60         return 0 unless ref($self);
61         return $self->{max_bundle_count} || 0;
62 }
63
64 sub max_chunk_size {
65         my $self = shift;
66         return 0 unless ref($self);
67         return $self->{max_chunk_size} if (defined($self->{max_chunk_size}));
68         return $self->max_bundle_size * 2;
69 }
70
71 sub api_name {
72         my $self = shift;
73         return 1 unless ref($self);
74         return $self->{api_name};
75 }
76
77 sub api_level {
78         my $self = shift;
79         return 1 unless ref($self);
80         return $self->{api_level};
81 }
82
83 sub session {
84         my $self = shift;
85         my $session = shift;
86
87         if($session) {
88                 $self->{session} = $session;
89         }
90         return $self->{session};
91 }
92
93 sub server_class {
94         my $class = shift;
95         if($class) {
96                 $server_class = $class;
97         }
98         return $server_class;
99 }
100
101 sub thunk {
102         my $self = shift;
103         my $flag = shift;
104         $thunk = $flag if (defined $flag);
105         return $thunk;
106 }
107
108 sub application_implementation {
109         my $self = shift;
110         my $app = shift;
111
112         if (defined $app) {
113                 $_app = $app;
114                 $_app->use;
115                 if( $@ ) {
116                         $log->error( "Error loading application_implementation: $app -> $@", ERROR);
117                 }
118
119         }
120
121         return $_app;
122 }
123
124 sub handler {
125         my ($self, $session, $app_msg) = @_;
126
127         if( ! $app_msg ) {
128                 return 1;  # error?
129         }
130
131         my $app = $self->application_implementation;
132
133         if ($session->last_message_type eq 'REQUEST') {
134
135         my @p = $app_msg->params;
136                 my $method_name = $app_msg->method;
137                 my $method_proto = $session->last_message_api_level;
138
139                 # By default, we log all method params at the info level
140                 # Here we are consult our shared portion of the config file
141                 # to look for any exceptions to this behavior
142                 my $logdata = "CALL: ".$session->service." $method_name ";
143                 my $redact_params = 0;
144                 if (@p) {
145                         if (ref($shared_conf->shared->log_protect) eq 'ARRAY') {
146                                 foreach my $match_string (@{$shared_conf->shared->log_protect}) {
147                                         if ($method_name =~ /^$match_string/) {
148                                                 $redact_params = 1;
149                                                 last;
150                                         }
151                                 }
152                         }
153                         if ($redact_params) {
154                                 $logdata .= "**PARAMS REDACTED**";
155                         } else {
156                                 $logdata .= join(', ', map { (defined $_) ? $_ : '' } @p);
157                         }
158                 }
159                 $log->info($logdata);
160
161                 my $coderef = $app->method_lookup( $method_name, $method_proto, 1, 1 );
162
163                 unless ($coderef) {
164                         $session->status( OpenSRF::DomainObject::oilsMethodException->new( 
165                                                 statusCode => STATUS_NOTFOUND(),
166                                                 status => "Method [$method_name] not found for $app"));
167                         return 1;
168                 }
169
170                 unless ($session->continue_request) {
171                         $session->status(
172                                 OpenSRF::DomainObject::oilsConnectStatus->new(
173                                                 statusCode => STATUS_REDIRECTED(),
174                                                 status => 'Disconnect on max requests' ) );
175                         $session->kill_me;
176                         return 1;
177                 }
178
179                 if (ref $coderef) {
180                         my @args = $app_msg->params;
181                         $coderef->session( $session );
182                         my $appreq = OpenSRF::AppRequest->new( $session );
183                         $appreq->max_bundle_size( $coderef->max_bundle_size );
184                         $appreq->max_bundle_count( $coderef->max_bundle_count );
185                         $appreq->max_chunk_size( $coderef->max_chunk_size );
186
187                         $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", INTERNAL );
188                         if( $in_request ) {
189                                 $log->debug( "Pushing onto pending requests: " . $appreq->threadTrace, DEBUG );
190                                 push @pending_requests, [ $appreq, \@args, $coderef ]; 
191                                 return 1;
192                         }
193
194
195                         $in_request++;
196
197                         $log->debug( "Executing coderef for {$method_name}", INTERNAL );
198
199                         my $resp;
200                         try {
201                                 # un-if(0) this block to enable param checking based on signature and argc
202                                 if ($coderef->strict) {
203                                         if (@args < $coderef->argc) {
204                                                 die     "Not enough params passed to ".
205                                                         $coderef->api_name." : requires ". $coderef->argc
206                                         }
207                                         if (@args) {
208                                                 my $sig = $coderef->signature;
209                                                 if ($sig && exists $sig->{params}) {
210                                                         for my $p (0 .. scalar(@{ $sig->{params} }) - 1 ) {
211                                                                 my $s = $sig->{params}->[$p];
212                                                                 my $a = $args[$p];
213                                                                 if ($s->{class} && OpenSRF::Utils::JSON->lookup_hint(ref $a) ne $s->{class}) {
214                                                                         die "Incorrect param class at position $p : should be a '$$s{class}'";
215                                                                 } elsif ($s->{type}) {
216                                                                         if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
217                                                                                 die "Incorrect param type at position $p : should be an 'object'";
218                                                                         } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
219                                                                                 die "Incorrect param type at position $p : should be an 'array'";
220                                                                         } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
221                                                                                 die "Incorrect param type at position $p : should be a 'number'";
222                                                                         } elsif (lc($s->{type}) eq 'string' && ref($a)) {
223                                                                                 die "Incorrect param type at position $p : should be a 'string'";
224                                                                         }
225                                                                 }
226                                                         }
227                                                 }
228                                         }
229                                 }
230
231                                 my $start = time();
232                                 $resp = $coderef->run( $appreq, @args); 
233                                 my $time = sprintf '%.3f', time() - $start;
234
235                                 $log->debug( "Method duration for [$method_name]:  ". $time );
236                                 $appreq->respond_complete( $resp );
237
238                         } catch Error with {
239                                 my $e = shift;
240                                 warn "Caught error from 'run' method: $e\n";
241
242                                 if(UNIVERSAL::isa($e,"Error")) {
243                                         $e = $e->stringify();
244                                 } 
245                                 my $sess_id = $session->session_id;
246                                 $session->status(
247                                         OpenSRF::DomainObject::oilsMethodException->new(
248                                                         statusCode      => STATUS_INTERNALSERVERERROR(),
249                                                         status          => " *** Call to [$method_name] failed for session ".
250                                                                            "[$sess_id], thread trace ".
251                                                                            "[".$appreq->threadTrace."]:\n$e\n"
252                                         )
253                                 );
254                         };
255
256
257
258                         # ----------------------------------------------
259
260
261                         # XXX may need this later
262                         # $_->[1] = 1 for (@OpenSRF::AppSession::_CLIENT_CACHE);
263
264                         $in_request--;
265
266                         $log->debug( "Pending Requests: " . scalar(@pending_requests), INTERNAL );
267
268                         # cycle through queued requests
269                         while( my $aref = shift @pending_requests ) {
270                                 $in_request++;
271                                 my $resp;
272                                 try {
273                                         # un-if(0) this block to enable param checking based on signature and argc
274                                         if (0) {
275                                                 if (@args < $aref->[2]->argc) {
276                                                         die     "Not enough params passed to ".
277                                                                 $aref->[2]->api_name." : requires ". $aref->[2]->argc
278                                                 }
279                                                 if (@args) {
280                                                         my $sig = $aref->[2]->signature;
281                                                         if ($sig && exists $sig->{params}) {
282                                                                 for my $p (0 .. scalar(@{ $sig->{params} }) - 1 ) {
283                                                                         my $s = $sig->{params}->[$p];
284                                                                         my $a = $args[$p];
285                                                                         if ($s->{class} && OpenSRF::Utils::JSON->lookup_hint(ref $a) ne $s->{class}) {
286                                                                                 die "Incorrect param class at position $p : should be a '$$s{class}'";
287                                                                         } elsif ($s->{type}) {
288                                                                                 if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
289                                                                                         die "Incorrect param type at position $p : should be an 'object'";
290                                                                                 } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
291                                                                                         die "Incorrect param type at position $p : should be an 'array'";
292                                                                                 } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
293                                                                                         die "Incorrect param type at position $p : should be a 'number'";
294                                                                                 } elsif (lc($s->{type}) eq 'string' && ref($a)) {
295                                                                                         die "Incorrect param type at position $p : should be a 'string'";
296                                                                                 }
297                                                                         }
298                                                                 }
299                                                         }
300                                                 }
301                                         }
302
303                                         my $start = time;
304                                         my $response = $aref->[2]->run( $aref->[0], @{$aref->[1]} );
305                                         my $time = sprintf '%.3f', time - $start;
306                                         $log->debug( "Method duration for [".$aref->[2]->api_name." -> ".join(', ',@{$aref->[1]}).']:  '.$time, DEBUG );
307
308                                         $appreq = $aref->[0];   
309                                         $appreq->respond_complete( $response );
310                                         $log->debug( "Executed: " . $appreq->threadTrace, INTERNAL );
311
312                                 } catch Error with {
313                                         my $e = shift;
314                                         if(UNIVERSAL::isa($e,"Error")) {
315                                                 $e = $e->stringify();
316                                         }
317                                         $session->status(
318                                                 OpenSRF::DomainObject::oilsMethodException->new(
319                                                                 statusCode => STATUS_INTERNALSERVERERROR(),
320                                                                 status => "Call to [".$aref->[2]->api_name."] faild:  $e"
321                                                 )
322                                         );
323                                 };
324                                 $in_request--;
325                         }
326
327                         return 1;
328                 } 
329
330                 $log->info("Received non-REQUEST message in Application handler");
331
332                 my $res = OpenSRF::DomainObject::oilsMethodException->new( 
333                                 status => "Received non-REQUEST message in Application handler");
334                 $session->send('ERROR', $res);
335                 $session->kill_me;
336                 return 1;
337
338         } else {
339                 $session->push_queue([ $app_msg, $session->last_threadTrace ]);
340         }
341
342         $session->last_message_type('');
343         $session->last_message_api_level('');
344
345         return 1;
346 }
347
348 sub is_registered {
349         my $self = shift;
350         my $api_name = shift;
351         my $api_level = shift || 1;
352         return exists($_METHODS[$api_level]{$api_name});
353 }
354
355
356 sub normalize_whitespace {
357         my $txt = shift;
358
359         $txt =~ s/^\s+//gso;
360         $txt =~ s/\s+$//gso;
361         $txt =~ s/\s+/ /gso;
362         $txt =~ s/\n//gso;
363         $txt =~ s/\. /\.  /gso;
364
365         return $txt;
366 }
367
368 sub parse_string_signature {
369         my $string = shift;
370         return [] unless $string;
371         my @chunks = split(/\@/smo, $string);
372
373         my @params;
374         my $ret;
375         my $desc = '';
376         for (@chunks) {
377                 if (/^return (.+)$/so) {
378                         $ret = [normalize_whitespace($1)];
379                 } elsif (/^param (\w+) \b(.+)$/so) {
380                         push @params, [ $1, normalize_whitespace($2) ];
381                 } else {
382                         $desc .= '@' if $desc;
383                         $desc .= $_;
384                 }
385         }
386
387         return [normalize_whitespace($desc),\@params, $ret];
388 }
389
390 sub parse_array_signature {
391         my $array = shift;
392         my ($d,$p,$r) = @$array;
393         return {} unless ($d or $p or $r);
394
395         return {
396                 desc    => $d,
397                 params  => [
398                         map { 
399                                 { name  => $$_[0],
400                                   desc  => $$_[1],
401                                   type  => $$_[2],
402                                   class => $$_[3],
403                                 }
404                         } @$p
405                 ],
406                 'return'=>
407                         { desc  => $$r[0],
408                           type  => $$r[1],
409                           class => $$r[2],
410                         }
411         };
412 }
413
414 sub register_method {
415         my $self = shift;
416         my $app = ref($self) || $self;
417         my %args = @_;
418
419
420         throw OpenSRF::DomainObject::oilsMethodException unless ($args{method});
421
422         $args{api_level} = 1 unless(defined($args{api_level}));
423         $args{stream} ||= 0;
424         $args{remote} ||= 0;
425         $args{argc} ||= 0;
426         $args{package} ||= $app;                
427         $args{server_class} = server_class();
428         $args{api_name} ||= $args{server_class} . '.' . $args{method};
429
430         # un-if(0) this block to enable signature parsing
431         if (!$args{signature}) {
432                 if ($args{notes} && !ref($args{notes})) {
433                         $args{signature} =
434                                 parse_array_signature( parse_string_signature( $args{notes} ) );
435                 }
436         } elsif( !ref($args{signature}) ) {
437                 $args{signature} =
438                         parse_array_signature( parse_string_signature( $args{signature} ) );
439         } elsif( ref($args{signature}) eq 'ARRAY') {
440                 $args{signature} =
441                         parse_array_signature( $args{signature} );
442         }
443         
444         unless ($args{object_hint}) {
445                 ($args{object_hint} = $args{package}) =~ s/::/_/go;
446         }
447
448         OpenSRF::Utils::JSON->register_class_hint(
449                 strip => ['session'],
450                 name => $app,
451                 hint => $args{object_hint},
452                 type => "hash"
453         );
454
455         $_METHODS[$args{api_level}]{$args{api_name}} = bless \%args => $app;
456
457         __PACKAGE__->register_method(
458                 stream => 0,
459                 argc => $args{argc},
460                 api_name => $args{api_name}.'.atomic',
461                 method => 'make_stream_atomic',
462                 notes => "This is a system generated method.  Please see the definition for $args{api_name}",
463         ) if ($args{stream});
464 }
465
466 sub retrieve_remote_apis {
467         my $method = shift;
468         my $session = OpenSRF::AppSession->create('router');
469         try {
470                 $session->connect or OpenSRF::EX::WARN->throw("Connection to router timed out");
471         } catch Error with {
472                 my $e = shift;
473                 $log->debug( "Remote subrequest returned an error:\n". $e );
474                 return undef;
475         } finally {
476                 return undef unless ($session->state == $session->CONNECTED);
477         };
478
479         my $req = $session->request( 'opensrf.router.info.class.list' );
480         my $list = $req->recv;
481
482         if( UNIVERSAL::isa($list,"Error") ) {
483                 throw $list;
484         }
485
486         my $content = $list->content;
487
488         $req->finish;
489         $session->finish;
490         $session->disconnect;
491
492         my %u_list = map { ($_ => 1) } @$content;
493
494         for my $class ( keys %u_list ) {
495                 next if($class eq $server_class);
496                 populate_remote_method_cache($class, $method);
497         }
498 }
499
500 sub populate_remote_method_cache {
501         my $class = shift;
502         my $meth = shift;
503
504         my $session = OpenSRF::AppSession->create($class);
505         try {
506                 $session->connect or OpenSRF::EX::WARN->throw("Connection to $class timed out");
507
508                 my $call = 'opensrf.system.method.all' unless (defined $meth);
509                 $call = 'opensrf.system.method' if (defined $meth);
510
511                 my $req = $session->request( $call, $meth );
512
513                 while (my $method = $req->recv) {
514                         next if (UNIVERSAL::isa($method, 'Error'));
515
516                         $method = $method->content;
517                         next if ( exists($_METHODS[$$method{api_level}]) &&
518                                 exists($_METHODS[$$method{api_level}]{$$method{api_name}}) );
519                         $method->{remote} = 1;
520                         bless($method, __PACKAGE__ );
521                         $_METHODS[$$method{api_level}]{$$method{api_name}} = $method;
522                 }
523
524                 $req->finish;
525                 $session->finish;
526                 $session->disconnect;
527
528         } catch Error with {
529                 my $e = shift;
530                 $log->debug( "Remote subrequest returned an error:\n". $e );
531                 return undef;
532         };
533 }
534
535 sub method_lookup {             
536         my $self = shift;
537         my $method = shift;
538         my $proto = shift;
539         my $no_recurse = shift || 0;
540         my $no_remote = shift || 0;
541
542         # this instead of " || 1;" above to allow api_level 0
543         $proto = $self->api_level unless (defined $proto);
544
545         my $class = ref($self) || $self;
546
547         $log->debug("Lookup of [$method] by [$class] in api_level [$proto]", DEBUG);
548         $log->debug("Available methods\n\t".join("\n\t", keys %{ $_METHODS[$proto] }), INTERNAL);
549
550         my $meth;
551         if (__PACKAGE__->thunk) {
552                 for my $p ( reverse(1 .. $proto) ) {
553                         if (exists $_METHODS[$p]{$method}) {
554                                 $meth = $_METHODS[$p]{$method};
555                         }
556                 }
557         } else {
558                 if (exists $_METHODS[$proto]{$method}) {
559                         $meth = $_METHODS[$proto]{$method};
560                 }
561         }
562
563         if (defined $meth) {
564                 if($no_remote and $meth->{remote}) {
565                         $log->debug("OH CRAP We're not supposed to return remote methods", WARN);
566                         return undef;
567                 }
568
569         } elsif (!$no_recurse) {
570                 $log->debug("We didn't find [$method], asking everyone else.", DEBUG);
571                 retrieve_remote_apis($method);
572                 $meth = $self->method_lookup($method,$proto,1);
573         }
574
575         if ($meth && ref($self)) {
576                 $meth->session($self->session); # Pass the caller's session
577                 $meth->max_chunk_size($self->max_chunk_size);
578                 $meth->max_bundle_size($self->max_bundle_size);
579         }
580
581         return $meth;
582 }
583
584 sub dispatch {
585         my $self = shift;
586         $log->debug("Creating a dispatching SubRequest object", DEBUG);
587     my $req = OpenSRF::AppSubrequest->new(
588         session => $self->session,
589         max_chunk_size  => $self->max_chunk_size,
590         max_bundle_size  => $self->max_bundle_size,
591         respond_directly => 1
592     );
593     return $self->run($req,@_);
594 }
595
596 sub run {
597         my $self = shift;
598         my $req = shift;
599
600         my $resp;
601         my @params = @_;
602
603         if ( !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) {
604                 $log->debug("Creating a SubRequest object", DEBUG);
605                 unshift @params, $req;
606                 $req = OpenSRF::AppSubrequest->new(
607                         session => $self->session,
608                         max_chunk_size  => $self->max_chunk_size,
609                         max_bundle_size  => $self->max_bundle_size
610                 );
611         } else {
612                 $log->debug("This is a top level request", DEBUG);
613         }
614
615         if (!$self->{remote}) {
616                 my $code = \&{$self->{package} . '::' . $self->{method}};
617                 my $err = undef;
618                 my $warnhandler;
619
620                 try {
621                         $warnhandler = $SIG{__WARN__};
622                         $SIG{__WARN__} = sub {
623                                 (my $msg = shift) =~ s/\n$//;
624                                 $log->warn($self->{api_name} . ": $msg");
625                                 return 1; # prevents warning going out to stderr
626                         };
627
628                         $resp = $code->($self, $req, @params);
629
630                 } catch Error with {
631                         $err = shift;
632
633                         if( ref($self) eq 'HASH') {
634                                 $log->error("Sub $$self{package}::$$self{method} DIED!!!\n\t$err\n", ERROR);
635                         }
636                 } finally {
637                         $SIG{__WARN__} = $warnhandler;
638                 };
639
640                 if($err) {
641                         if(UNIVERSAL::isa($err,"Error")) { 
642                                 throw $err;
643                         } else {
644                                 die $err->stringify; 
645                         }
646                 }
647
648
649                 $log->debug("Coderef for [$$self{package}::$$self{method}] has been run", DEBUG);
650
651                 if ( ref($req) and UNIVERSAL::isa($req, 'OpenSRF::AppSubrequest') and !$req->respond_directly ) {
652                         $req->respond($resp) if (defined $resp);
653                         $log->debug("SubRequest object is responding with : " . join(" ",$req->responses), DEBUG);
654                         return $req->responses;
655                 } else {
656                         $log->debug("A top level Request object is responding $resp", DEBUG) if (defined $resp);
657                         return $resp;
658                 }
659         } else {
660                 my $session = OpenSRF::AppSession->create($self->{server_class});
661                 try {
662                         #$session->connect or OpenSRF::EX::WARN->throw("Connection to [$$self{server_class}] timed out");
663                         my $remote_req = $session->request( $self->{api_name}, @params );
664                         while (my $remote_resp = $remote_req->recv) {
665                                 OpenSRF::Utils::Logger->debug("Remote Subrequest Received " . $remote_resp, INTERNAL );
666                                 if( UNIVERSAL::isa($remote_resp,"Error") ) {
667                                         throw $remote_resp;
668                                 }
669                                 $req->respond( $remote_resp->content );
670                         }
671                         $remote_req->finish();
672
673                 } catch Error with {
674                         my $e = shift;
675                         $log->debug( "Remote subrequest returned an error:\n". $e );
676                         return undef;
677                 };
678
679                 if ($session) {
680                         $session->disconnect();
681                         $session->finish();
682                 }
683
684                 $log->debug( "Remote Subrequest Responses " . join(" ", $req->responses), INTERNAL );
685
686                 return $req->responses;
687         }
688         # huh? how'd we get here...
689         return undef;
690 }
691
692 sub introspect {
693         my $self = shift;
694         my $client = shift;
695         my $method = shift;
696         my $limit = shift;
697         my $offset = shift;
698
699         if ($self->api_name =~ /all$/o) {
700                 $offset = $limit;
701                 $limit = $method;
702                 $method = undef; 
703         }
704
705         my ($seen,$returned) = (0,0);
706         for my $api_level ( reverse(1 .. $#_METHODS) ) {
707                 for my $api_name ( sort keys %{$_METHODS[$api_level]} ) {
708                         if (!$offset || $offset <= $seen) {
709                                 if (!$_METHODS[$api_level]{$api_name}{remote}) {
710                                         if (defined($method)) {
711                                                 if ($api_name =~ $method) {
712                                                         if (!$limit || $returned < $limit) {
713                                                                 $client->respond( $_METHODS[$api_level]{$api_name} );
714                                                                 $returned++;
715                                                         }
716                                                 }
717                                         } else {
718                                                 if (!$limit || $returned < $limit) {
719                                                         $client->respond( $_METHODS[$api_level]{$api_name} );
720                                                         $returned++;
721                                                 }
722                                         }
723                                 }
724                         }
725                         $seen++;
726                 }
727         }
728
729         return undef;
730 }
731 __PACKAGE__->register_method(
732         stream => 1,
733         method => 'introspect',
734         api_name => 'opensrf.system.method.all',
735         argc => 0,
736         signature => {
737                 desc => q/This method is used to introspect an entire OpenSRF Application/,
738                 return => {
739                         desc => q/A stream of objects describing the methods available via this OpenSRF Application/,
740                         type => 'object'
741                 }
742         },
743 );
744 __PACKAGE__->register_method(
745         stream => 1,
746         method => 'introspect',
747         argc => 1,
748         api_name => 'opensrf.system.method',
749         argc => 1,
750         signature => {
751                 desc => q/Use this method to get the definition of a single OpenSRF Method/,
752                 params => [
753                         { desc => q/The method to introspect/,
754                           type => 'string' },
755                 ],
756                 return => { desc => q/An object describing the method requested, or an error if it can't be found/,
757                             type => 'object' }
758         },
759 );
760
761 sub echo_method {
762         my $self = shift;
763         my $client = shift;
764         my @args = @_;
765
766         $client->respond( $_ ) for (@args);
767         return undef;
768 }
769 __PACKAGE__->register_method(
770         stream => 1,
771         method => 'echo_method',
772         argc => 1,
773         api_name => 'opensrf.system.echo',
774         signature => {
775                 desc => q/A test method that will echo back it's arguments in a streaming response/,
776                 params => [
777                         { desc => q/One or more arguments to echo back/ }
778                 ],
779                 return => { desc => q/A stream of the arguments passed/ }
780         },
781 );
782
783 sub time_method {
784         my( $self, $conn ) = @_;
785         return CORE::time;
786 }
787 __PACKAGE__->register_method(
788         method => 'time_method',
789         argc => 0,
790         api_name => 'opensrf.system.time',
791         signature => {
792                 desc => q/Returns the current system time as epoch seconds/,
793                 return => { desc => q/epoch seconds/ }
794         }
795 );
796
797 sub make_stream_atomic {
798         my $self = shift;
799         my $req = shift;
800         my @args = @_;
801
802         (my $m_name = $self->api_name) =~ s/\.atomic$//o;
803         my $m = $self->method_lookup($m_name);
804
805         $m->session( $req->session );
806         my @results = $m->run(@args);
807         $m->session('');
808
809         return \@results;
810 }
811
812 1;