LP#1612771: Perl max_chunk_size additions
[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 10240;
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 104858; # 1/10 MB
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         $meth->session($self->session) if $meth && ref($self); # Pass the caller's session
576         return $meth;
577 }
578
579 sub run {
580         my $self = shift;
581         my $req = shift;
582
583         my $resp;
584         my @params = @_;
585
586         if ( !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) {
587                 $log->debug("Creating a SubRequest object", DEBUG);
588                 unshift @params, $req;
589                 $req = OpenSRF::AppSubrequest->new( session => $self->session );
590         } else {
591                 $log->debug("This is a top level request", DEBUG);
592         }
593
594         if (!$self->{remote}) {
595                 my $code = \&{$self->{package} . '::' . $self->{method}};
596                 my $err = undef;
597                 my $warnhandler;
598
599                 try {
600                         $warnhandler = $SIG{__WARN__};
601                         $SIG{__WARN__} = sub {
602                                 (my $msg = shift) =~ s/\n$//;
603                                 $log->warn($self->{api_name} . ": $msg");
604                                 return 1; # prevents warning going out to stderr
605                         };
606
607                         $resp = $code->($self, $req, @params);
608
609                 } catch Error with {
610                         $err = shift;
611
612                         if( ref($self) eq 'HASH') {
613                                 $log->error("Sub $$self{package}::$$self{method} DIED!!!\n\t$err\n", ERROR);
614                         }
615                 } finally {
616                         $SIG{__WARN__} = $warnhandler;
617                 };
618
619                 if($err) {
620                         if(UNIVERSAL::isa($err,"Error")) { 
621                                 throw $err;
622                         } else {
623                                 die $err->stringify; 
624                         }
625                 }
626
627
628                 $log->debug("Coderef for [$$self{package}::$$self{method}] has been run", DEBUG);
629
630                 if ( ref($req) and UNIVERSAL::isa($req, 'OpenSRF::AppSubrequest') ) {
631                         $req->respond($resp) if (defined $resp);
632                         $log->debug("SubRequest object is responding with : " . join(" ",$req->responses), DEBUG);
633                         return $req->responses;
634                 } else {
635                         $log->debug("A top level Request object is responding $resp", DEBUG) if (defined $resp);
636                         return $resp;
637                 }
638         } else {
639                 my $session = OpenSRF::AppSession->create($self->{server_class});
640                 try {
641                         #$session->connect or OpenSRF::EX::WARN->throw("Connection to [$$self{server_class}] timed out");
642                         my $remote_req = $session->request( $self->{api_name}, @params );
643                         while (my $remote_resp = $remote_req->recv) {
644                                 OpenSRF::Utils::Logger->debug("Remote Subrequest Received " . $remote_resp, INTERNAL );
645                                 if( UNIVERSAL::isa($remote_resp,"Error") ) {
646                                         throw $remote_resp;
647                                 }
648                                 $req->respond( $remote_resp->content );
649                         }
650                         $remote_req->finish();
651
652                 } catch Error with {
653                         my $e = shift;
654                         $log->debug( "Remote subrequest returned an error:\n". $e );
655                         return undef;
656                 };
657
658                 if ($session) {
659                         $session->disconnect();
660                         $session->finish();
661                 }
662
663                 $log->debug( "Remote Subrequest Responses " . join(" ", $req->responses), INTERNAL );
664
665                 return $req->responses;
666         }
667         # huh? how'd we get here...
668         return undef;
669 }
670
671 sub introspect {
672         my $self = shift;
673         my $client = shift;
674         my $method = shift;
675         my $limit = shift;
676         my $offset = shift;
677
678         if ($self->api_name =~ /all$/o) {
679                 $offset = $limit;
680                 $limit = $method;
681                 $method = undef; 
682         }
683
684         my ($seen,$returned) = (0,0);
685         for my $api_level ( reverse(1 .. $#_METHODS) ) {
686                 for my $api_name ( sort keys %{$_METHODS[$api_level]} ) {
687                         if (!$offset || $offset <= $seen) {
688                                 if (!$_METHODS[$api_level]{$api_name}{remote}) {
689                                         if (defined($method)) {
690                                                 if ($api_name =~ $method) {
691                                                         if (!$limit || $returned < $limit) {
692                                                                 $client->respond( $_METHODS[$api_level]{$api_name} );
693                                                                 $returned++;
694                                                         }
695                                                 }
696                                         } else {
697                                                 if (!$limit || $returned < $limit) {
698                                                         $client->respond( $_METHODS[$api_level]{$api_name} );
699                                                         $returned++;
700                                                 }
701                                         }
702                                 }
703                         }
704                         $seen++;
705                 }
706         }
707
708         return undef;
709 }
710 __PACKAGE__->register_method(
711         stream => 1,
712         method => 'introspect',
713         api_name => 'opensrf.system.method.all',
714         argc => 0,
715         signature => {
716                 desc => q/This method is used to introspect an entire OpenSRF Application/,
717                 return => {
718                         desc => q/A stream of objects describing the methods available via this OpenSRF Application/,
719                         type => 'object'
720                 }
721         },
722 );
723 __PACKAGE__->register_method(
724         stream => 1,
725         method => 'introspect',
726         argc => 1,
727         api_name => 'opensrf.system.method',
728         argc => 1,
729         signature => {
730                 desc => q/Use this method to get the definition of a single OpenSRF Method/,
731                 params => [
732                         { desc => q/The method to introspect/,
733                           type => 'string' },
734                 ],
735                 return => { desc => q/An object describing the method requested, or an error if it can't be found/,
736                             type => 'object' }
737         },
738 );
739
740 sub echo_method {
741         my $self = shift;
742         my $client = shift;
743         my @args = @_;
744
745         $client->respond( $_ ) for (@args);
746         return undef;
747 }
748 __PACKAGE__->register_method(
749         stream => 1,
750         method => 'echo_method',
751         argc => 1,
752         api_name => 'opensrf.system.echo',
753         signature => {
754                 desc => q/A test method that will echo back it's arguments in a streaming response/,
755                 params => [
756                         { desc => q/One or more arguments to echo back/ }
757                 ],
758                 return => { desc => q/A stream of the arguments passed/ }
759         },
760 );
761
762 sub time_method {
763         my( $self, $conn ) = @_;
764         return CORE::time;
765 }
766 __PACKAGE__->register_method(
767         method => 'time_method',
768         argc => 0,
769         api_name => 'opensrf.system.time',
770         signature => {
771                 desc => q/Returns the current system time as epoch seconds/,
772                 return => { desc => q/epoch seconds/ }
773         }
774 );
775
776 sub make_stream_atomic {
777         my $self = shift;
778         my $req = shift;
779         my @args = @_;
780
781         (my $m_name = $self->api_name) =~ s/\.atomic$//o;
782         my $m = $self->method_lookup($m_name);
783
784         $m->session( $req->session );
785         my @results = $m->run(@args);
786         $m->session('');
787
788         return \@results;
789 }
790
791 1;