]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/perlmods/OpenSRF/AppSession.pm
now we only get the router target from the bootstrap file if
[Evergreen.git] / OpenSRF / src / perlmods / OpenSRF / AppSession.pm
1 package OpenSRF::AppSession;
2 use OpenSRF::DOM;
3 #use OpenSRF::DOM::Element::userAuth;
4 use OpenSRF::DomainObject::oilsMessage;
5 use OpenSRF::DomainObject::oilsMethod;
6 use OpenSRF::DomainObject::oilsResponse qw/:status/;
7 use OpenSRF::Transport::PeerHandle;
8 use OpenSRF::Utils::Logger qw(:level);
9 use OpenSRF::Utils::SettingsClient;
10 use OpenSRF::Utils::Config;
11 use OpenSRF::EX;
12 use OpenSRF;
13 use Exporter;
14 use base qw/Exporter OpenSRF/;
15 use Time::HiRes qw( time usleep );
16 use warnings;
17 use strict;
18
19 our @EXPORT_OK = qw/CONNECTING INIT_CONNECTED CONNECTED DISCONNECTED CLIENT SERVER/;
20 our %EXPORT_TAGS = ( state => [ qw/CONNECTING INIT_CONNECTED CONNECTED DISCONNECTED/ ],
21                  endpoint => [ qw/CLIENT SERVER/ ],
22 );
23
24 my $logger = "OpenSRF::Utils::Logger";
25
26 our %_CACHE;
27 our @_CLIENT_CACHE;
28 our @_RESEND_QUEUE;
29
30 sub kill_client_session_cache {
31         for my $session ( @_CLIENT_CACHE ) {
32                 $session->kill_me;
33         }
34 }
35
36 sub CONNECTING { return 3 };
37 sub INIT_CONNECTED { return 4 };
38 sub CONNECTED { return 1 };
39 sub DISCONNECTED { return 2 };
40
41 sub CLIENT { return 2 };
42 sub SERVER { return 1 };
43
44 sub find {
45         return undef unless (defined $_[1]);
46         return $_CACHE{$_[1]} if (exists($_CACHE{$_[1]}));
47 }
48
49 sub find_client {
50         my( $self, $app ) = @_;
51         $logger->debug( "Client Cache contains: " .scalar(@_CLIENT_CACHE), INTERNAL );
52         my ($client) = grep { $_->[0] eq $app and $_->[1] == 1 } @_CLIENT_CACHE;
53         $client->[1] = 0;
54         return $client->[2];
55 }
56
57 sub transport_connected {
58         my $self = shift;
59         if( ! exists $self->{peer_handle} || ! $self->{peer_handle} ) {
60                 return 0;
61         }
62         return $self->{peer_handle}->tcp_connected();
63 }
64
65 # ----------------------------------------------------------------------------
66 # Clears the transport buffers
67 # call this if you are not through with the sesssion, but you want 
68 # to have a clean slate.  You shouldn't have to call this if
69 # you are correctly 'recv'ing all of the data from a request.
70 # however, if you don't want all of the data, this will
71 # slough off any excess
72 #  * * Note: This will delete data for all sessions using this transport
73 # handle.  For example, all client sessions use the same handle.
74 # ----------------------------------------------------------------------------
75 sub buffer_reset {
76
77         my $self = shift;
78         if( ! exists $self->{peer_handle} || ! $self->{peer_handle} ) { 
79                 return 0;
80         }
81         $self->{peer_handle}->buffer_reset();
82 }
83
84
85 sub client_cache {
86         my $self = shift;
87         push @_CLIENT_CACHE, [ $self->app, 1, $self ];
88 }
89
90 # when any incoming data is received, this method is called.
91 sub server_build {
92         my $class = shift;
93         $class = ref($class) || $class;
94
95         my $sess_id = shift;
96         my $remote_id = shift;
97         my $service = shift;
98
99         return undef unless ($sess_id and $remote_id and $service);
100
101         my $config_client = OpenSRF::Utils::SettingsClient->new();
102         
103         if ( my $thingy = $class->find($sess_id) ) {
104                 $thingy->remote_id( $remote_id );
105                 $logger->debug( "AppSession returning existing session $sess_id", DEBUG );
106                 return $thingy;
107         } else {
108                 $logger->debug( "AppSession building new server session $sess_id", DEBUG );
109         }
110
111         if( $service eq "client" ) {
112                 #throw OpenSRF::EX::PANIC ("Attempting to build a client session as a server" .
113                 #       " Session ID [$sess_id], remote_id [$remote_id]");
114                 $logger->debug("Attempting to build a client session as ".
115                                 "a server Session ID [$sess_id], remote_id [$remote_id]", ERROR );
116         }
117
118
119         #my $max_requests = $conf->$service->max_requests;
120         my $max_requests        = $config_client->config_value("apps",$service,"max_requests");
121         $logger->debug( "Max Requests for $service is $max_requests", INTERNAL );# if $max_requests;
122
123         $logger->transport( "AppSession creating new session: $sess_id", INTERNAL );
124
125         my $self = bless { recv_queue  => [],
126                            request_queue  => [],
127                            requests  => 0,
128                            endpoint    => SERVER,
129                            state       => CONNECTING, 
130                            session_id  => $sess_id,
131                            remote_id    => $remote_id,
132                                 peer_handle => OpenSRF::Transport::PeerHandle->retrieve($service),
133                                 max_requests => $max_requests,
134                                 session_threadTrace => 0,
135                                 service => $service,
136                          } => $class;
137
138         return $_CACHE{$sess_id} = $self;
139 }
140
141 sub service { return shift()->{service}; }
142
143 sub continue_request {
144         my $self = shift;
145         $self->{'requests'}++;
146         return $self->{'requests'} <= $self->{'max_requests'} ? 1 : 0;
147 }
148
149 sub last_sent_payload {
150         my( $self, $payload ) = @_;
151         if( $payload ) {
152                 return $self->{'last_sent_payload'} = $payload;
153         }
154         return $self->{'last_sent_payload'};
155 }
156
157 sub last_sent_type {
158         my( $self, $type ) = @_;
159         if( $type ) {
160                 return $self->{'last_sent_type'} = $type;
161         }
162         return $self->{'last_sent_type'};
163 }
164
165 # When we're a client and we want to connect to a remote service
166 # create( $app, username => $user, secret => $passwd );
167 #    OR
168 # create( $app, sysname => $user, secret => $shared_secret );
169 sub create {
170         my $class = shift;
171         $class = ref($class) || $class;
172
173         my $app = shift;
174         #my %auth_args = @_;
175
176
177 #       unless ( $app &&
178 #                exists($auth_args{secret}) &&
179 #                ( exists($auth_args{username}) ||
180 #                  exists($auth_args{sysname}) ) ) {
181 #               throw OpenSRF::EX::User ( 'Insufficient authentication information for session creation');
182 #       }
183
184         if( my $thingy = OpenSRF::AppSession->find_client( $app ) ) {
185                         $logger->debug( "AppSession returning existing client session for $app", DEBUG );
186                         return $thingy;
187         } else {
188                 $logger->debug( "AppSession creating new client session for $app", DEBUG );
189         }
190
191
192         
193         #my $auth = OpenSRF::DOM::Element::userAuth->new( %auth_args );
194
195         my $config_client = OpenSRF::Utils::SettingsClient->new();
196
197         my $sess_id = time . rand( $$ );
198         while ( $class->find($sess_id) ) {
199                 $sess_id = time . rand( $$ );
200         }
201
202         my $r_id;
203
204         if( $app eq "settings" ) {
205                 my $conf = OpenSRF::Utils::Config->current;
206                 if(!$conf) { die("No transport target for $app!"); }
207                 $r_id = $conf->targets->$app->[0] || #just the first for now...
208                                 die("No transport target for $app!");
209         } else {
210                 my $targets =  $config_client->config_value("apps",$app,"transport_targets", "router_target");
211                 if( !ref($targets) ) { $targets = [ $targets ]; }
212
213                 # XXX for now, just use the first
214                         $r_id = $targets->[0] ||
215                                 die("No transport target for $app!");
216         }
217         
218         my $peer_handle = OpenSRF::Transport::PeerHandle->retrieve("client"); 
219         if( ! $peer_handle ) {
220                 $peer_handle = OpenSRF::Transport::PeerHandle->retrieve("system_client");
221         }
222
223         my $self = bless { app_name    => $app,
224                                 #client_auth => $auth,
225                            #recv_queue  => [],
226                            request_queue  => [],
227                            endpoint    => CLIENT,
228                            state       => DISCONNECTED,#since we're init'ing
229                            session_id  => $sess_id,
230                            remote_id   => $r_id,
231                            api_level   => 1,
232                            orig_remote_id   => $r_id,
233                                 peer_handle => $peer_handle,
234                                 session_threadTrace => 0,
235                          } => $class;
236
237         $self->client_cache();
238         $_CACHE{$sess_id} = $self;
239         return $self->find_client( $app );
240 }
241
242 sub api_level {
243         return shift()->{api_level};
244 }
245
246 sub app {
247         return shift()->{app_name};
248 }
249
250 sub reset {
251         my $self = shift;
252         $self->remote_id($$self{orig_remote_id});
253 }
254
255 # 'connect' can be used as a constructor if called as a class method,
256 # or used to connect a session that has disconnectd if called against
257 # an existing session that seems to be disconnected, or was just built
258 # using 'create' above.
259
260 # connect( $app, username => $user, secret => $passwd );
261 #    OR
262 # connect( $app, sysname => $user, secret => $shared_secret );
263
264 # --- Returns undef if the connect attempt times out.
265 # --- Returns the OpenSRF::EX object if one is returned by the server
266 # --- Returns self if connected
267 sub connect {
268         my $self = shift;
269         my $class = ref($self) || $self;
270
271         if ( ref( $self ) and  $self->state && $self->state == CONNECTED  ) {
272                 $logger->transport("ABC AppSession already connected", DEBUG );
273         } else {
274                 $logger->transport("ABC AppSession not connected, connecting..", DEBUG );
275         }
276         return $self if ( ref( $self ) and  $self->state && $self->state == CONNECTED  );
277
278         my $app = shift;
279         my $api_level = shift;
280         $api_level = 1 unless (defined $api_level);
281
282         $self = $class->create($app, @_) if (!ref($self));
283         return undef unless ($self);
284
285         $self->{api_level} = $api_level;
286
287         $self->reset;
288         $self->state(CONNECTING);
289         $self->send('CONNECT', "");
290
291         # if we want to connect to settings, we may not have 
292         # any data for the settings client to work with...
293         # just using a default for now XXX
294
295         my $time_remaining = 5;
296         
297 =head blah
298         my $client = OpenSRF::Utils::SettingsClient->new();
299         my $trans = $client->config_value("client_connection","transport_host");
300
301         if(!ref($trans)) {
302                 $time_remaining = $trans->{connect_timeout};
303         } else {
304                 # XXX for now, just use the first
305                 $time_remaining = $trans->[0]->{connect_timeout};
306         }
307 =cut
308
309         while ( $self->state != CONNECTED  and $time_remaining > 0 ) {
310                 my $starttime = time;
311                 $self->queue_wait($time_remaining);
312                 my $endtime = time;
313                 $time_remaining -= ($endtime - $starttime);
314         }
315
316         return undef unless($self->state == CONNECTED);
317
318         return $self;
319 }
320
321 sub finish {
322         my $self = shift;
323         if( ! $self->session_id ) {
324                 return 0;
325         }
326         #$self->disconnect if ($self->endpoint == CLIENT);
327         for my $ses ( @_CLIENT_CACHE ) {
328                 if ($ses->[2]->session_id eq $self->session_id) {
329                         $ses->[1] = 1;
330                 }
331         }
332 }
333
334 sub kill_me {
335         my $self = shift;
336         if( ! $self->session_id ) { return 0; }
337         $self->disconnect;
338         $logger->transport( "AppSession killing self: " . $self->session_id(), DEBUG );
339         my @a;
340         for my $ses ( @_CLIENT_CACHE ) {
341                 push @a, $ses 
342                         if ($ses->[2]->session_id ne $self->session_id);
343         }
344         @_CLIENT_CACHE = @a;
345         delete $_CACHE{$self->session_id};
346         delete($$self{$_}) for (keys %$self);
347 }
348
349 sub disconnect {
350         my $self = shift;
351         unless( $self->state == DISCONNECTED ) {
352                 $self->send('DISCONNECT', "") if ($self->endpoint == CLIENT);;
353                 $self->state( DISCONNECTED ); 
354         }
355         $self->reset;
356 }
357
358 sub request {
359         my $self = shift;
360         my $meth = shift;
361
362         my $method;
363         if (!ref $meth) {
364                 $method = new OpenSRF::DomainObject::oilsMethod ( method => $meth );
365         } else {
366                 $method = $meth;
367         }
368         
369         $method->params( @_ );
370
371         $self->send('REQUEST',$method);
372 }
373
374
375 sub send {
376         my $self = shift;
377         my @payload_list = @_; # this is a Domain Object
378
379
380         $logger->debug( "In send", INTERNAL );
381         
382         my $tT;
383
384         if( @payload_list % 2 ) { $tT = pop @payload_list; }
385
386         if( ! @payload_list ) {
387                 $logger->debug( "payload_list param is incomplete in AppSession::send()", ERROR );
388                 return undef; 
389         }
390
391         my $doc = OpenSRF::DOM->createDocument();
392
393         $logger->debug( "In send2", INTERNAL );
394
395         my $disconnect = 0;
396         my $connecting = 0;
397
398         while( @payload_list ) {
399
400                 my ($msg_type, $payload) = ( shift(@payload_list), shift(@payload_list) ); 
401
402                 if ($msg_type eq 'DISCONNECT' ) {
403                         $disconnect++;
404                         if( $self->state == DISCONNECTED) {
405                                 next;
406                         }
407                 }
408
409                 if( $msg_type eq "CONNECT" ) { $connecting++; }
410
411
412                 if( $payload ) {
413                         $logger->debug( "Payload is ".$payload->toString, INTERNAL );
414                 }
415         
416         
417                 my $msg = OpenSRF::DomainObject::oilsMessage->new();
418                 $logger->debug( "AppSession after creating oilsMessage $msg", INTERNAL );
419                 
420                 $msg->type($msg_type);
421                 $logger->debug( "AppSession after adding type" . $msg->toString(), INTERNAL );
422         
423                 #$msg->userAuth($self->client_auth) if ($self->endpoint == CLIENT && $msg_type eq 'CONNECT');
424         
425                 no warnings;
426                 $msg->threadTrace( $tT || int($self->session_threadTrace) || int($self->last_threadTrace) );
427                 use warnings;
428         
429                 if ($msg->type eq 'REQUEST') {
430                         if ( !defined($tT) || $self->last_threadTrace != $tT ) {
431                                 $msg->update_threadTrace;
432                                 $self->session_threadTrace( $msg->threadTrace );
433                                 $tT = $self->session_threadTrace;
434                                 OpenSRF::AppRequest->new($self, $payload);
435                         }
436                 }
437         
438                 $msg->api_level($self->api_level);
439                 $msg->payload($payload) if $payload;
440         
441                 $doc->documentElement->appendChild( $msg );
442
443         
444                 $logger->debug( "AppSession sending ".$msg->type." to ".$self->remote_id.
445                         " with threadTrace [".$msg->threadTrace."]", INFO );
446
447         }
448         
449         if ($self->endpoint == CLIENT and ! $disconnect) {
450                 $self->queue_wait(0);
451                 unless ($self->state == CONNECTED || ($self->state == CONNECTING && $connecting )) {
452                         my $v = $self->connect();
453                         if( ! $v ) {
454                                 $logger->debug( "Unable to connect to remote service in AppSession::send()", ERROR );
455                                 return undef;
456                         }
457                         if( ref($v) and $v->can("class") and $v->class->isa( "OpenSRF::EX" ) ) {
458                                 return $v;
459                         }
460                 }
461         } 
462
463         $logger->debug( "AppSession sending doc: " . $doc->toString(), INTERNAL );
464
465
466         $self->{peer_handle}->send( 
467                                         to     => $self->remote_id,
468                                    thread => $self->session_id,
469                                    body   => $doc->toString );
470
471         return $self->app_request( $tT );
472 }
473
474 sub app_request {
475         my $self = shift;
476         my $tT = shift;
477         
478         return undef unless (defined $tT);
479         my ($req) = grep { $_->threadTrace == $tT } @{ $self->{request_queue} };
480
481         return $req;
482 }
483
484 sub remove_app_request {
485         my $self = shift;
486         my $req = shift;
487         
488         my @list = grep { $_->threadTrace != $req->threadTrace } @{ $self->{request_queue} };
489
490         $self->{request_queue} = \@list;
491 }
492
493 sub endpoint {
494         return $_[0]->{endpoint};
495 }
496
497
498 sub session_id {
499         my $self = shift;
500         return $self->{session_id};
501 }
502
503 sub push_queue {
504         my $self = shift;
505         my $resp = shift;
506         my $req = $self->app_request($resp->[1]);
507         return $req->push_queue( $resp->[0] ) if ($req);
508         push @{ $self->{recv_queue} }, $resp->[0];
509 }
510
511 sub last_threadTrace {
512         my $self = shift;
513         my $new_last_threadTrace = shift;
514
515         my $old_last_threadTrace = $self->{last_threadTrace};
516         if (defined $new_last_threadTrace) {
517                 $self->{last_threadTrace} = $new_last_threadTrace;
518                 return $new_last_threadTrace unless ($old_last_threadTrace);
519         }
520
521         return $old_last_threadTrace;
522 }
523
524 sub session_threadTrace {
525         my $self = shift;
526         my $new_last_threadTrace = shift;
527
528         my $old_last_threadTrace = $self->{session_threadTrace};
529         if (defined $new_last_threadTrace) {
530                 $self->{session_threadTrace} = $new_last_threadTrace;
531                 return $new_last_threadTrace unless ($old_last_threadTrace);
532         }
533
534         return $old_last_threadTrace;
535 }
536
537 sub last_message_type {
538         my $self = shift;
539         my $new_last_message_type = shift;
540
541         my $old_last_message_type = $self->{last_message_type};
542         if (defined $new_last_message_type) {
543                 $self->{last_message_type} = $new_last_message_type;
544                 return $new_last_message_type unless ($old_last_message_type);
545         }
546
547         return $old_last_message_type;
548 }
549
550 sub last_message_api_level {
551         my $self = shift;
552         my $new_last_message_api_level = shift;
553
554         my $old_last_message_api_level = $self->{last_message_api_level};
555         if (defined $new_last_message_api_level) {
556                 $self->{last_message_api_level} = $new_last_message_api_level;
557                 return $new_last_message_api_level unless ($old_last_message_api_level);
558         }
559
560         return $old_last_message_api_level;
561 }
562
563 sub remote_id {
564         my $self = shift;
565         my $new_remote_id = shift;
566
567         my $old_remote_id = $self->{remote_id};
568         if (defined $new_remote_id) {
569                 $self->{remote_id} = $new_remote_id;
570                 return $new_remote_id unless ($old_remote_id);
571         }
572
573         return $old_remote_id;
574 }
575
576 sub client_auth {
577         return undef;
578         my $self = shift;
579         my $new_ua = shift;
580
581         my $old_ua = $self->{client_auth};
582         if (defined $new_ua) {
583                 $self->{client_auth} = $new_ua;
584                 return $new_ua unless ($old_ua);
585         }
586
587         return $old_ua->cloneNode(1);
588 }
589
590 sub state {
591         my $self = shift;
592         my $new_state = shift;
593
594         my $old_state = $self->{state};
595         if (defined $new_state) {
596                 $self->{state} = $new_state;
597                 return $new_state unless ($old_state);
598         }
599
600         return $old_state;
601 }
602
603 sub DESTROY {
604         my $self = shift;
605         delete $$self{$_} for keys %$self;
606         return undef;
607 }
608
609 sub recv {
610         my $self = shift;
611         my @proto_args = @_;
612         my %args;
613
614         if ( @proto_args ) {
615                 if ( !(@proto_args % 2) ) {
616                         %args = @proto_args;
617                 } elsif (@proto_args == 1) {
618                         %args = ( timeout => @proto_args );
619                 }
620         }
621
622         #$logger->debug( ref($self). " recv_queue before wait: " . $self->_print_queue(), INTERNAL );
623
624         if( exists( $args{timeout} ) ) {
625                 $args{timeout} = int($args{timeout});
626         } else {
627                 $args{timeout} = 10;
628         }
629
630         #$args{timeout} = 0 if ($self->complete);
631
632         $logger->debug( ref($self) ."->recv with timeout " . $args{timeout}, INTERNAL );
633
634         $args{count} ||= 1;
635
636         my $avail = @{ $self->{recv_queue} };
637         my $time_remaining = $args{timeout};
638
639         while ( $avail < $args{count} and $time_remaining > 0 ) {
640                 last if $self->complete;
641                 my $starttime = time;
642                 $self->queue_wait($time_remaining);
643                 my $endtime = time;
644                 $time_remaining -= ($endtime - $starttime);
645                 $avail = @{ $self->{recv_queue} };
646         }
647
648         #$logger->debug( ref($self)." queue after wait: " . $self->_print_queue(), INTERNAL );
649                 
650         my @list;
651         while ( my $msg = shift @{ $self->{recv_queue} } ) {
652                 push @list, $msg;
653                 last if (scalar(@list) >= $args{count});
654         }
655
656 #       $self->{recv_queue} = [@unlist, @{ $self->{recv_queue} }];
657         $logger->debug( "Number of matched responses: " . @list, DEBUG );
658
659         $self->queue_wait(0); # check for statuses
660         
661         return $list[0] unless (wantarray);
662         return @list;
663 }
664
665 sub push_resend {
666         my $self = shift;
667         push @OpenSRF::AppSession::_RESEND_QUEUE, @_;
668 }
669
670 sub flush_resend {
671         my $self = shift;
672         $logger->debug( "Resending..." . @_RESEND_QUEUE, DEBUG );
673         while ( my $req = shift @OpenSRF::AppSession::_RESEND_QUEUE ) {
674                 $req->resend;
675         }
676 }
677
678
679 sub queue_wait {
680         my $self = shift;
681         if( ! $self->{peer_handle} ) { return 0; }
682         my $timeout = shift || 0;
683         $logger->debug( "Calling queue_wait($timeout)" , DEBUG );
684         $logger->debug( "Timestamp before process($timeout) : " . $logger->format_time(), INTERNAL );
685         my $o = $self->{peer_handle}->process($timeout);
686         $logger->debug( "Timestamp after  process($timeout) : " . $logger->format_time(), INTERNAL );
687         $self->flush_resend;
688         return $o;
689 }
690
691 sub _print_queue {
692         my( $self ) = @_;
693         my $string = "";
694         foreach my $msg ( @{$self->{recv_queue}} ) {
695                 $string = $string . $msg->toString(1) . "\n";
696         }
697         return $string;
698 }
699
700 sub status {
701         my $self = shift;
702         $self->send( 'STATUS', @_ );
703 }
704
705 #-------------------------------------------------------------------------------
706
707 package OpenSRF::AppRequest;
708 use base qw/OpenSRF::AppSession/;
709 use OpenSRF::Utils::Logger qw/:level/;
710 use OpenSRF::DomainObject::oilsResponse qw/:status/;
711
712 sub new {
713         my $class = shift;
714         $class = ref($class) || $class;
715
716         my $session = shift;
717         my $threadTrace = $session->session_threadTrace || $session->last_threadTrace;
718         my $payload = shift;
719         
720         my $self = {    session     => $session,
721                         threadTrace => $threadTrace,
722                         payload     => $payload,
723                         complete    => 0,
724                         recv_queue  => [],
725         };
726
727         bless $self => $class;
728
729         push @{ $self->session->{request_queue} }, $self;
730
731         return $self;
732 }
733
734 sub queue_size {
735         my $size = @{$_[0]->{recv_queue}};
736         return $size;
737 }
738         
739 sub send {
740         shift()->session->send(@_);
741 }
742
743 sub finish {
744         my $self = shift;
745         $self->session->remove_app_request($self);
746         delete($$self{$_}) for (keys %$self);
747 }
748
749 sub session {
750         return shift()->{session};
751 }
752
753 sub complete {
754         my $self = shift;
755         my $complete = shift;
756         return $self->{complete} if ($self->{complete});
757         if (defined $complete) {
758                 $self->{complete} = $complete;
759         } else {
760                 $self->session->queue_wait(0);
761         }
762         return $self->{complete};
763 }
764
765 sub wait_complete {
766         my $self = shift;
767         my $timeout = shift || 1;
768         my $time_remaining = $timeout;
769
770         while ( ! $self->complete  and $time_remaining > 0 ) {
771                 my $starttime = time;
772                 $self->queue_wait($time_remaining);
773                 my $endtime = time;
774                 $time_remaining -= ($endtime - $starttime);
775         }
776
777         return $self->complete;
778 }
779
780 sub threadTrace {
781         return shift()->{threadTrace};
782 }
783
784 sub push_queue {
785         my $self = shift;
786         my $resp = shift;
787         if( !$resp ) { return 0; }
788         push @{ $self->{recv_queue} }, $resp;
789         OpenSRF::Utils::Logger->debug( "AppRequest pushed ".$resp->toString(), INTERNAL );
790 }
791
792 sub queue_wait {
793         my $self = shift;
794         OpenSRF::Utils::Logger->debug( "Calling queue_wait(@_)", DEBUG );
795         return $self->session->queue_wait(@_)
796 }
797
798 sub payload { return shift()->{payload}; }
799
800 sub resend {
801         my $self = shift;
802         OpenSRF::Utils::Logger->debug(
803                 "I'm resending the request for threadTrace ". $self->threadTrace, DEBUG);
804         if($self->payload) {
805                 OpenSRF::Utils::Logger->debug($self->payload->toString,INTERNAL);
806         }
807         return $self->session->send('REQUEST', $self->payload, $self->threadTrace );
808 }
809
810 sub status {
811         my $self = shift;
812         my $msg = shift;
813         $self->session->send( 'STATUS',$msg, $self->threadTrace );
814 }
815
816 sub respond {
817         my $self = shift;
818         my $msg = shift;
819
820         my $response;
821         if (ref($msg) && UNIVERSAL::can($msg, 'getAttribute') && $msg->getAttribute('name') =~ /oilsResult/) {
822                 $response = $msg;
823         } else {
824                 $response = new OpenSRF::DomainObject::oilsResult;
825                 $response->content($msg);
826         }
827
828         $self->session->send('RESULT', $response, $self->threadTrace);
829 }
830
831 sub respond_complete {
832         my $self = shift;
833         my $msg = shift;
834
835         my $response;
836         if (ref($msg) && UNIVERSAL::can($msg, 'getAttribute') && $msg->getAttribute('name') =~ /oilsResult/) {
837                 $response = $msg;
838         } else {
839                 $response = new OpenSRF::DomainObject::oilsResult;
840                 $response->content($msg);
841         }
842
843         my $stat = OpenSRF::DomainObject::oilsConnectStatus->new(
844                 statusCode => STATUS_COMPLETE(),
845                 status => 'Request Complete' );
846
847         $self->session->send( 'RESULT' => $response, 'STATUS' => $stat, $self->threadTrace);
848
849 }
850
851
852 package OpenSRF::AppSubrequest;
853
854 sub respond {
855         my $self = shift;
856         my $resp = shift;
857         push @{$$self{resp}}, $resp;
858 }
859
860 sub new {
861         return bless({resp => []}, 'OpenSRF::AppSubrequest');
862 }
863
864 sub responses { @{$_[0]->{resp}} }
865
866 sub status {}
867
868
869 1;
870