]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Application.pm
LP#1612771: bundling and chunking
[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 2 * $self->max_bundle_size;
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
186                         $log->debug( "in_request = $in_request : [" . $appreq->threadTrace."]", INTERNAL );
187                         if( $in_request ) {
188                                 $log->debug( "Pushing onto pending requests: " . $appreq->threadTrace, DEBUG );
189                                 push @pending_requests, [ $appreq, \@args, $coderef ]; 
190                                 return 1;
191                         }
192
193
194                         $in_request++;
195
196                         $log->debug( "Executing coderef for {$method_name}", INTERNAL );
197
198                         my $resp;
199                         try {
200                                 # un-if(0) this block to enable param checking based on signature and argc
201                                 if ($coderef->strict) {
202                                         if (@args < $coderef->argc) {
203                                                 die     "Not enough params passed to ".
204                                                         $coderef->api_name." : requires ". $coderef->argc
205                                         }
206                                         if (@args) {
207                                                 my $sig = $coderef->signature;
208                                                 if ($sig && exists $sig->{params}) {
209                                                         for my $p (0 .. scalar(@{ $sig->{params} }) - 1 ) {
210                                                                 my $s = $sig->{params}->[$p];
211                                                                 my $a = $args[$p];
212                                                                 if ($s->{class} && OpenSRF::Utils::JSON->lookup_hint(ref $a) ne $s->{class}) {
213                                                                         die "Incorrect param class at position $p : should be a '$$s{class}'";
214                                                                 } elsif ($s->{type}) {
215                                                                         if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
216                                                                                 die "Incorrect param type at position $p : should be an 'object'";
217                                                                         } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
218                                                                                 die "Incorrect param type at position $p : should be an 'array'";
219                                                                         } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
220                                                                                 die "Incorrect param type at position $p : should be a 'number'";
221                                                                         } elsif (lc($s->{type}) eq 'string' && ref($a)) {
222                                                                                 die "Incorrect param type at position $p : should be a 'string'";
223                                                                         }
224                                                                 }
225                                                         }
226                                                 }
227                                         }
228                                 }
229
230                                 my $start = time();
231                                 $resp = $coderef->run( $appreq, @args); 
232                                 my $time = sprintf '%.3f', time() - $start;
233
234                                 $log->debug( "Method duration for [$method_name]:  ". $time );
235                                 $appreq->respond_complete( $resp );
236
237                         } catch Error with {
238                                 my $e = shift;
239                                 warn "Caught error from 'run' method: $e\n";
240
241                                 if(UNIVERSAL::isa($e,"Error")) {
242                                         $e = $e->stringify();
243                                 } 
244                                 my $sess_id = $session->session_id;
245                                 $session->status(
246                                         OpenSRF::DomainObject::oilsMethodException->new(
247                                                         statusCode      => STATUS_INTERNALSERVERERROR(),
248                                                         status          => " *** Call to [$method_name] failed for session ".
249                                                                            "[$sess_id], thread trace ".
250                                                                            "[".$appreq->threadTrace."]:\n$e\n"
251                                         )
252                                 );
253                         };
254
255
256
257                         # ----------------------------------------------
258
259
260                         # XXX may need this later
261                         # $_->[1] = 1 for (@OpenSRF::AppSession::_CLIENT_CACHE);
262
263                         $in_request--;
264
265                         $log->debug( "Pending Requests: " . scalar(@pending_requests), INTERNAL );
266
267                         # cycle through queued requests
268                         while( my $aref = shift @pending_requests ) {
269                                 $in_request++;
270                                 my $resp;
271                                 try {
272                                         # un-if(0) this block to enable param checking based on signature and argc
273                                         if (0) {
274                                                 if (@args < $aref->[2]->argc) {
275                                                         die     "Not enough params passed to ".
276                                                                 $aref->[2]->api_name." : requires ". $aref->[2]->argc
277                                                 }
278                                                 if (@args) {
279                                                         my $sig = $aref->[2]->signature;
280                                                         if ($sig && exists $sig->{params}) {
281                                                                 for my $p (0 .. scalar(@{ $sig->{params} }) - 1 ) {
282                                                                         my $s = $sig->{params}->[$p];
283                                                                         my $a = $args[$p];
284                                                                         if ($s->{class} && OpenSRF::Utils::JSON->lookup_hint(ref $a) ne $s->{class}) {
285                                                                                 die "Incorrect param class at position $p : should be a '$$s{class}'";
286                                                                         } elsif ($s->{type}) {
287                                                                                 if (lc($s->{type}) eq 'object' && $a !~ /HASH/o) {
288                                                                                         die "Incorrect param type at position $p : should be an 'object'";
289                                                                                 } elsif (lc($s->{type}) eq 'array' && $a !~ /ARRAY/o) {
290                                                                                         die "Incorrect param type at position $p : should be an 'array'";
291                                                                                 } elsif (lc($s->{type}) eq 'number' && (ref($a) || $a !~ /^-?\d+(?:\.\d+)?$/o)) {
292                                                                                         die "Incorrect param type at position $p : should be a 'number'";
293                                                                                 } elsif (lc($s->{type}) eq 'string' && ref($a)) {
294                                                                                         die "Incorrect param type at position $p : should be a 'string'";
295                                                                                 }
296                                                                         }
297                                                                 }
298                                                         }
299                                                 }
300                                         }
301
302                                         my $start = time;
303                                         my $response = $aref->[2]->run( $aref->[0], @{$aref->[1]} );
304                                         my $time = sprintf '%.3f', time - $start;
305                                         $log->debug( "Method duration for [".$aref->[2]->api_name." -> ".join(', ',@{$aref->[1]}).']:  '.$time, DEBUG );
306
307                                         $appreq = $aref->[0];   
308                                         $appreq->respond_complete( $response );
309                                         $log->debug( "Executed: " . $appreq->threadTrace, INTERNAL );
310
311                                 } catch Error with {
312                                         my $e = shift;
313                                         if(UNIVERSAL::isa($e,"Error")) {
314                                                 $e = $e->stringify();
315                                         }
316                                         $session->status(
317                                                 OpenSRF::DomainObject::oilsMethodException->new(
318                                                                 statusCode => STATUS_INTERNALSERVERERROR(),
319                                                                 status => "Call to [".$aref->[2]->api_name."] faild:  $e"
320                                                 )
321                                         );
322                                 };
323                                 $in_request--;
324                         }
325
326                         return 1;
327                 } 
328
329                 $log->info("Received non-REQUEST message in Application handler");
330
331                 my $res = OpenSRF::DomainObject::oilsMethodException->new( 
332                                 status => "Received non-REQUEST message in Application handler");
333                 $session->send('ERROR', $res);
334                 $session->kill_me;
335                 return 1;
336
337         } else {
338                 $session->push_queue([ $app_msg, $session->last_threadTrace ]);
339         }
340
341         $session->last_message_type('');
342         $session->last_message_api_level('');
343
344         return 1;
345 }
346
347 sub is_registered {
348         my $self = shift;
349         my $api_name = shift;
350         my $api_level = shift || 1;
351         return exists($_METHODS[$api_level]{$api_name});
352 }
353
354
355 sub normalize_whitespace {
356         my $txt = shift;
357
358         $txt =~ s/^\s+//gso;
359         $txt =~ s/\s+$//gso;
360         $txt =~ s/\s+/ /gso;
361         $txt =~ s/\n//gso;
362         $txt =~ s/\. /\.  /gso;
363
364         return $txt;
365 }
366
367 sub parse_string_signature {
368         my $string = shift;
369         return [] unless $string;
370         my @chunks = split(/\@/smo, $string);
371
372         my @params;
373         my $ret;
374         my $desc = '';
375         for (@chunks) {
376                 if (/^return (.+)$/so) {
377                         $ret = [normalize_whitespace($1)];
378                 } elsif (/^param (\w+) \b(.+)$/so) {
379                         push @params, [ $1, normalize_whitespace($2) ];
380                 } else {
381                         $desc .= '@' if $desc;
382                         $desc .= $_;
383                 }
384         }
385
386         return [normalize_whitespace($desc),\@params, $ret];
387 }
388
389 sub parse_array_signature {
390         my $array = shift;
391         my ($d,$p,$r) = @$array;
392         return {} unless ($d or $p or $r);
393
394         return {
395                 desc    => $d,
396                 params  => [
397                         map { 
398                                 { name  => $$_[0],
399                                   desc  => $$_[1],
400                                   type  => $$_[2],
401                                   class => $$_[3],
402                                 }
403                         } @$p
404                 ],
405                 'return'=>
406                         { desc  => $$r[0],
407                           type  => $$r[1],
408                           class => $$r[2],
409                         }
410         };
411 }
412
413 sub register_method {
414         my $self = shift;
415         my $app = ref($self) || $self;
416         my %args = @_;
417
418
419         throw OpenSRF::DomainObject::oilsMethodException unless ($args{method});
420
421         $args{api_level} = 1 unless(defined($args{api_level}));
422         $args{stream} ||= 0;
423         $args{remote} ||= 0;
424         $args{argc} ||= 0;
425         $args{package} ||= $app;                
426         $args{server_class} = server_class();
427         $args{api_name} ||= $args{server_class} . '.' . $args{method};
428
429         # un-if(0) this block to enable signature parsing
430         if (!$args{signature}) {
431                 if ($args{notes} && !ref($args{notes})) {
432                         $args{signature} =
433                                 parse_array_signature( parse_string_signature( $args{notes} ) );
434                 }
435         } elsif( !ref($args{signature}) ) {
436                 $args{signature} =
437                         parse_array_signature( parse_string_signature( $args{signature} ) );
438         } elsif( ref($args{signature}) eq 'ARRAY') {
439                 $args{signature} =
440                         parse_array_signature( $args{signature} );
441         }
442         
443         unless ($args{object_hint}) {
444                 ($args{object_hint} = $args{package}) =~ s/::/_/go;
445         }
446
447         OpenSRF::Utils::JSON->register_class_hint(
448                 strip => ['session'],
449                 name => $app,
450                 hint => $args{object_hint},
451                 type => "hash"
452         );
453
454         $_METHODS[$args{api_level}]{$args{api_name}} = bless \%args => $app;
455
456         __PACKAGE__->register_method(
457                 stream => 0,
458                 argc => $args{argc},
459                 api_name => $args{api_name}.'.atomic',
460                 method => 'make_stream_atomic',
461                 notes => "This is a system generated method.  Please see the definition for $args{api_name}",
462         ) if ($args{stream});
463 }
464
465 sub retrieve_remote_apis {
466         my $method = shift;
467         my $session = OpenSRF::AppSession->create('router');
468         try {
469                 $session->connect or OpenSRF::EX::WARN->throw("Connection to router timed out");
470         } catch Error with {
471                 my $e = shift;
472                 $log->debug( "Remote subrequest returned an error:\n". $e );
473                 return undef;
474         } finally {
475                 return undef unless ($session->state == $session->CONNECTED);
476         };
477
478         my $req = $session->request( 'opensrf.router.info.class.list' );
479         my $list = $req->recv;
480
481         if( UNIVERSAL::isa($list,"Error") ) {
482                 throw $list;
483         }
484
485         my $content = $list->content;
486
487         $req->finish;
488         $session->finish;
489         $session->disconnect;
490
491         my %u_list = map { ($_ => 1) } @$content;
492
493         for my $class ( keys %u_list ) {
494                 next if($class eq $server_class);
495                 populate_remote_method_cache($class, $method);
496         }
497 }
498
499 sub populate_remote_method_cache {
500         my $class = shift;
501         my $meth = shift;
502
503         my $session = OpenSRF::AppSession->create($class);
504         try {
505                 $session->connect or OpenSRF::EX::WARN->throw("Connection to $class timed out");
506
507                 my $call = 'opensrf.system.method.all' unless (defined $meth);
508                 $call = 'opensrf.system.method' if (defined $meth);
509
510                 my $req = $session->request( $call, $meth );
511
512                 while (my $method = $req->recv) {
513                         next if (UNIVERSAL::isa($method, 'Error'));
514
515                         $method = $method->content;
516                         next if ( exists($_METHODS[$$method{api_level}]) &&
517                                 exists($_METHODS[$$method{api_level}]{$$method{api_name}}) );
518                         $method->{remote} = 1;
519                         bless($method, __PACKAGE__ );
520                         $_METHODS[$$method{api_level}]{$$method{api_name}} = $method;
521                 }
522
523                 $req->finish;
524                 $session->finish;
525                 $session->disconnect;
526
527         } catch Error with {
528                 my $e = shift;
529                 $log->debug( "Remote subrequest returned an error:\n". $e );
530                 return undef;
531         };
532 }
533
534 sub method_lookup {             
535         my $self = shift;
536         my $method = shift;
537         my $proto = shift;
538         my $no_recurse = shift || 0;
539         my $no_remote = shift || 0;
540
541         # this instead of " || 1;" above to allow api_level 0
542         $proto = $self->api_level unless (defined $proto);
543
544         my $class = ref($self) || $self;
545
546         $log->debug("Lookup of [$method] by [$class] in api_level [$proto]", DEBUG);
547         $log->debug("Available methods\n\t".join("\n\t", keys %{ $_METHODS[$proto] }), INTERNAL);
548
549         my $meth;
550         if (__PACKAGE__->thunk) {
551                 for my $p ( reverse(1 .. $proto) ) {
552                         if (exists $_METHODS[$p]{$method}) {
553                                 $meth = $_METHODS[$p]{$method};
554                         }
555                 }
556         } else {
557                 if (exists $_METHODS[$proto]{$method}) {
558                         $meth = $_METHODS[$proto]{$method};
559                 }
560         }
561
562         if (defined $meth) {
563                 if($no_remote and $meth->{remote}) {
564                         $log->debug("OH CRAP We're not supposed to return remote methods", WARN);
565                         return undef;
566                 }
567
568         } elsif (!$no_recurse) {
569                 $log->debug("We didn't find [$method], asking everyone else.", DEBUG);
570                 retrieve_remote_apis($method);
571                 $meth = $self->method_lookup($method,$proto,1);
572         }
573
574         $meth->session($self->session) if $meth && ref($self); # Pass the caller's session
575         return $meth;
576 }
577
578 sub run {
579         my $self = shift;
580         my $req = shift;
581
582         my $resp;
583         my @params = @_;
584
585         if ( !UNIVERSAL::isa($req, 'OpenSRF::AppRequest') ) {
586                 $log->debug("Creating a SubRequest object", DEBUG);
587                 unshift @params, $req;
588                 $req = OpenSRF::AppSubrequest->new( session => $self->session );
589         } else {
590                 $log->debug("This is a top level request", DEBUG);
591         }
592
593         if (!$self->{remote}) {
594                 my $code = \&{$self->{package} . '::' . $self->{method}};
595                 my $err = undef;
596                 my $warnhandler;
597
598                 try {
599                         $warnhandler = $SIG{__WARN__};
600                         $SIG{__WARN__} = sub {
601                                 (my $msg = shift) =~ s/\n$//;
602                                 $log->warn($self->{api_name} . ": $msg");
603                                 return 1; # prevents warning going out to stderr
604                         };
605
606                         $resp = $code->($self, $req, @params);
607
608                 } catch Error with {
609                         $err = shift;
610
611                         if( ref($self) eq 'HASH') {
612                                 $log->error("Sub $$self{package}::$$self{method} DIED!!!\n\t$err\n", ERROR);
613                         }
614                 } finally {
615                         $SIG{__WARN__} = $warnhandler;
616                 };
617
618                 if($err) {
619                         if(UNIVERSAL::isa($err,"Error")) { 
620                                 throw $err;
621                         } else {
622                                 die $err->stringify; 
623                         }
624                 }
625
626
627                 $log->debug("Coderef for [$$self{package}::$$self{method}] has been run", DEBUG);
628
629                 if ( ref($req) and UNIVERSAL::isa($req, 'OpenSRF::AppSubrequest') ) {
630                         $req->respond($resp) if (defined $resp);
631                         $log->debug("SubRequest object is responding with : " . join(" ",$req->responses), DEBUG);
632                         return $req->responses;
633                 } else {
634                         $log->debug("A top level Request object is responding $resp", DEBUG) if (defined $resp);
635                         return $resp;
636                 }
637         } else {
638                 my $session = OpenSRF::AppSession->create($self->{server_class});
639                 try {
640                         #$session->connect or OpenSRF::EX::WARN->throw("Connection to [$$self{server_class}] timed out");
641                         my $remote_req = $session->request( $self->{api_name}, @params );
642                         while (my $remote_resp = $remote_req->recv) {
643                                 OpenSRF::Utils::Logger->debug("Remote Subrequest Received " . $remote_resp, INTERNAL );
644                                 if( UNIVERSAL::isa($remote_resp,"Error") ) {
645                                         throw $remote_resp;
646                                 }
647                                 $req->respond( $remote_resp->content );
648                         }
649                         $remote_req->finish();
650
651                 } catch Error with {
652                         my $e = shift;
653                         $log->debug( "Remote subrequest returned an error:\n". $e );
654                         return undef;
655                 };
656
657                 if ($session) {
658                         $session->disconnect();
659                         $session->finish();
660                 }
661
662                 $log->debug( "Remote Subrequest Responses " . join(" ", $req->responses), INTERNAL );
663
664                 return $req->responses;
665         }
666         # huh? how'd we get here...
667         return undef;
668 }
669
670 sub introspect {
671         my $self = shift;
672         my $client = shift;
673         my $method = shift;
674         my $limit = shift;
675         my $offset = shift;
676
677         if ($self->api_name =~ /all$/o) {
678                 $offset = $limit;
679                 $limit = $method;
680                 $method = undef; 
681         }
682
683         my ($seen,$returned) = (0,0);
684         for my $api_level ( reverse(1 .. $#_METHODS) ) {
685                 for my $api_name ( sort keys %{$_METHODS[$api_level]} ) {
686                         if (!$offset || $offset <= $seen) {
687                                 if (!$_METHODS[$api_level]{$api_name}{remote}) {
688                                         if (defined($method)) {
689                                                 if ($api_name =~ $method) {
690                                                         if (!$limit || $returned < $limit) {
691                                                                 $client->respond( $_METHODS[$api_level]{$api_name} );
692                                                                 $returned++;
693                                                         }
694                                                 }
695                                         } else {
696                                                 if (!$limit || $returned < $limit) {
697                                                         $client->respond( $_METHODS[$api_level]{$api_name} );
698                                                         $returned++;
699                                                 }
700                                         }
701                                 }
702                         }
703                         $seen++;
704                 }
705         }
706
707         return undef;
708 }
709 __PACKAGE__->register_method(
710         stream => 1,
711         method => 'introspect',
712         api_name => 'opensrf.system.method.all',
713         argc => 0,
714         signature => {
715                 desc => q/This method is used to introspect an entire OpenSRF Application/,
716                 return => {
717                         desc => q/A stream of objects describing the methods available via this OpenSRF Application/,
718                         type => 'object'
719                 }
720         },
721 );
722 __PACKAGE__->register_method(
723         stream => 1,
724         method => 'introspect',
725         argc => 1,
726         api_name => 'opensrf.system.method',
727         argc => 1,
728         signature => {
729                 desc => q/Use this method to get the definition of a single OpenSRF Method/,
730                 params => [
731                         { desc => q/The method to introspect/,
732                           type => 'string' },
733                 ],
734                 return => { desc => q/An object describing the method requested, or an error if it can't be found/,
735                             type => 'object' }
736         },
737 );
738
739 sub echo_method {
740         my $self = shift;
741         my $client = shift;
742         my @args = @_;
743
744         $client->respond( $_ ) for (@args);
745         return undef;
746 }
747 __PACKAGE__->register_method(
748         stream => 1,
749         method => 'echo_method',
750         argc => 1,
751         api_name => 'opensrf.system.echo',
752         signature => {
753                 desc => q/A test method that will echo back it's arguments in a streaming response/,
754                 params => [
755                         { desc => q/One or more arguments to echo back/ }
756                 ],
757                 return => { desc => q/A stream of the arguments passed/ }
758         },
759 );
760
761 sub time_method {
762         my( $self, $conn ) = @_;
763         return CORE::time;
764 }
765 __PACKAGE__->register_method(
766         method => 'time_method',
767         argc => 0,
768         api_name => 'opensrf.system.time',
769         signature => {
770                 desc => q/Returns the current system time as epoch seconds/,
771                 return => { desc => q/epoch seconds/ }
772         }
773 );
774
775 sub make_stream_atomic {
776         my $self = shift;
777         my $req = shift;
778         my @args = @_;
779
780         (my $m_name = $self->api_name) =~ s/\.atomic$//o;
781         my $m = $self->method_lookup($m_name);
782
783         $m->session( $req->session );
784         my @results = $m->run(@args);
785         $m->session('');
786
787         return \@results;
788 }
789
790 1;