]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/perlmods/OpenSRF/AppSession.pm
moved to new config settings
[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         my $conf = OpenSRF::Utils::Config->current;
204         if(!$conf) { die("No transport target for $app!"); }
205         $r_id = $conf->targets->$app->[0] || #just the first for now...
206                                 die("No transport target for $app!");
207
208 =head lasdf
209         } else {
210                 my $targets =  $config_client->config_value("apps",$app,"transport_targets", "transport_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 =cut
218         
219         my $peer_handle = OpenSRF::Transport::PeerHandle->retrieve("client"); 
220         if( ! $peer_handle ) {
221                 $peer_handle = OpenSRF::Transport::PeerHandle->retrieve("system_client");
222         }
223
224         my $self = bless { app_name    => $app,
225                                 #client_auth => $auth,
226                            #recv_queue  => [],
227                            request_queue  => [],
228                            endpoint    => CLIENT,
229                            state       => DISCONNECTED,#since we're init'ing
230                            session_id  => $sess_id,
231                            remote_id   => $r_id,
232                            api_level   => 1,
233                            orig_remote_id   => $r_id,
234                                 peer_handle => $peer_handle,
235                                 session_threadTrace => 0,
236                          } => $class;
237
238         $self->client_cache();
239         $_CACHE{$sess_id} = $self;
240         return $self->find_client( $app );
241 }
242
243 sub api_level {
244         return shift()->{api_level};
245 }
246
247 sub app {
248         return shift()->{app_name};
249 }
250
251 sub reset {
252         my $self = shift;
253         $self->remote_id($$self{orig_remote_id});
254 }
255
256 # 'connect' can be used as a constructor if called as a class method,
257 # or used to connect a session that has disconnectd if called against
258 # an existing session that seems to be disconnected, or was just built
259 # using 'create' above.
260
261 # connect( $app, username => $user, secret => $passwd );
262 #    OR
263 # connect( $app, sysname => $user, secret => $shared_secret );
264
265 # --- Returns undef if the connect attempt times out.
266 # --- Returns the OpenSRF::EX object if one is returned by the server
267 # --- Returns self if connected
268 sub connect {
269         my $self = shift;
270         my $class = ref($self) || $self;
271
272         if ( ref( $self ) and  $self->state && $self->state == CONNECTED  ) {
273                 $logger->transport("ABC AppSession already connected", DEBUG );
274         } else {
275                 $logger->transport("ABC AppSession not connected, connecting..", DEBUG );
276         }
277         return $self if ( ref( $self ) and  $self->state && $self->state == CONNECTED  );
278
279         my $app = shift;
280         my $api_level = shift;
281         $api_level = 1 unless (defined $api_level);
282
283         $self = $class->create($app, @_) if (!ref($self));
284         return undef unless ($self);
285
286         $self->{api_level} = $api_level;
287
288         $self->reset;
289         $self->state(CONNECTING);
290         $self->send('CONNECT', "");
291
292         # if we want to connect to settings, we may not have 
293         # any data for the settings client to work with...
294         # just using a default for now XXX
295
296         my $time_remaining = 5;
297         
298 =head blah
299         my $client = OpenSRF::Utils::SettingsClient->new();
300         my $trans = $client->config_value("client_connection","transport_host");
301
302         if(!ref($trans)) {
303                 $time_remaining = $trans->{connect_timeout};
304         } else {
305                 # XXX for now, just use the first
306                 $time_remaining = $trans->[0]->{connect_timeout};
307         }
308 =cut
309
310         while ( $self->state != CONNECTED  and $time_remaining > 0 ) {
311                 my $starttime = time;
312                 $self->queue_wait($time_remaining);
313                 my $endtime = time;
314                 $time_remaining -= ($endtime - $starttime);
315         }
316
317         return undef unless($self->state == CONNECTED);
318
319         return $self;
320 }
321
322 sub finish {
323         my $self = shift;
324         if( ! $self->session_id ) {
325                 return 0;
326         }
327         #$self->disconnect if ($self->endpoint == CLIENT);
328         for my $ses ( @_CLIENT_CACHE ) {
329                 if ($ses->[2]->session_id eq $self->session_id) {
330                         $ses->[1] = 1;
331                 }
332         }
333 }
334
335 sub kill_me {
336         my $self = shift;
337         if( ! $self->session_id ) { return 0; }
338         $self->disconnect;
339         $logger->transport( "AppSession killing self: " . $self->session_id(), DEBUG );
340         my @a;
341         for my $ses ( @_CLIENT_CACHE ) {
342                 push @a, $ses 
343                         if ($ses->[2]->session_id ne $self->session_id);
344         }
345         @_CLIENT_CACHE = @a;
346         delete $_CACHE{$self->session_id};
347         delete($$self{$_}) for (keys %$self);
348 }
349
350 sub disconnect {
351         my $self = shift;
352         unless( $self->state == DISCONNECTED ) {
353                 $self->send('DISCONNECT', "") if ($self->endpoint == CLIENT);;
354                 $self->state( DISCONNECTED ); 
355         }
356         $self->reset;
357 }
358
359 sub request {
360         my $self = shift;
361         my $meth = shift;
362
363         my $method;
364         if (!ref $meth) {
365                 $method = new OpenSRF::DomainObject::oilsMethod ( method => $meth );
366         } else {
367                 $method = $meth;
368         }
369         
370         $method->params( @_ );
371
372         $self->send('REQUEST',$method);
373 }
374
375
376 sub send {
377         my $self = shift;
378         my @payload_list = @_; # this is a Domain Object
379
380
381         $logger->debug( "In send", INTERNAL );
382         
383         my $tT;
384
385         if( @payload_list % 2 ) { $tT = pop @payload_list; }
386
387         if( ! @payload_list ) {
388                 $logger->debug( "payload_list param is incomplete in AppSession::send()", ERROR );
389                 return undef; 
390         }
391
392         my $doc = OpenSRF::DOM->createDocument();
393
394         $logger->debug( "In send2", INTERNAL );
395
396         my $disconnect = 0;
397         my $connecting = 0;
398
399         while( @payload_list ) {
400
401                 my ($msg_type, $payload) = ( shift(@payload_list), shift(@payload_list) ); 
402
403                 if ($msg_type eq 'DISCONNECT' ) {
404                         $disconnect++;
405                         if( $self->state == DISCONNECTED) {
406                                 next;
407                         }
408                 }
409
410                 if( $msg_type eq "CONNECT" ) { $connecting++; }
411
412
413                 if( $payload ) {
414                         $logger->debug( "Payload is ".$payload->toString, INTERNAL );
415                 }
416         
417         
418                 my $msg = OpenSRF::DomainObject::oilsMessage->new();
419                 $logger->debug( "AppSession after creating oilsMessage $msg", INTERNAL );
420                 
421                 $msg->type($msg_type);
422                 $logger->debug( "AppSession after adding type" . $msg->toString(), INTERNAL );
423         
424                 #$msg->userAuth($self->client_auth) if ($self->endpoint == CLIENT && $msg_type eq 'CONNECT');
425         
426                 no warnings;
427                 $msg->threadTrace( $tT || int($self->session_threadTrace) || int($self->last_threadTrace) );
428                 use warnings;
429         
430                 if ($msg->type eq 'REQUEST') {
431                         if ( !defined($tT) || $self->last_threadTrace != $tT ) {
432                                 $msg->update_threadTrace;
433                                 $self->session_threadTrace( $msg->threadTrace );
434                                 $tT = $self->session_threadTrace;
435                                 OpenSRF::AppRequest->new($self, $payload);
436                         }
437                 }
438         
439                 $msg->api_level($self->api_level);
440                 $msg->payload($payload) if $payload;
441         
442                 $doc->documentElement->appendChild( $msg );
443
444         
445                 $logger->debug( "AppSession sending ".$msg->type." to ".$self->remote_id.
446                         " with threadTrace [".$msg->threadTrace."]", INFO );
447
448         }
449         
450         if ($self->endpoint == CLIENT and ! $disconnect) {
451                 $self->queue_wait(0);
452                 unless ($self->state == CONNECTED || ($self->state == CONNECTING && $connecting )) {
453                         my $v = $self->connect();
454                         if( ! $v ) {
455                                 $logger->debug( "Unable to connect to remote service in AppSession::send()", ERROR );
456                                 return undef;
457                         }
458                         if( ref($v) and $v->can("class") and $v->class->isa( "OpenSRF::EX" ) ) {
459                                 return $v;
460                         }
461                 }
462         } 
463
464         $logger->debug( "AppSession sending doc: " . $doc->toString(), INTERNAL );
465
466
467         $self->{peer_handle}->send( 
468                                         to     => $self->remote_id,
469                                    thread => $self->session_id,
470                                    body   => $doc->toString );
471
472         return $self->app_request( $tT );
473 }
474
475 sub app_request {
476         my $self = shift;
477         my $tT = shift;
478         
479         return undef unless (defined $tT);
480         my ($req) = grep { $_->threadTrace == $tT } @{ $self->{request_queue} };
481
482         return $req;
483 }
484
485 sub remove_app_request {
486         my $self = shift;
487         my $req = shift;
488         
489         my @list = grep { $_->threadTrace != $req->threadTrace } @{ $self->{request_queue} };
490
491         $self->{request_queue} = \@list;
492 }
493
494 sub endpoint {
495         return $_[0]->{endpoint};
496 }
497
498
499 sub session_id {
500         my $self = shift;
501         return $self->{session_id};
502 }
503
504 sub push_queue {
505         my $self = shift;
506         my $resp = shift;
507         my $req = $self->app_request($resp->[1]);
508         return $req->push_queue( $resp->[0] ) if ($req);
509         push @{ $self->{recv_queue} }, $resp->[0];
510 }
511
512 sub last_threadTrace {
513         my $self = shift;
514         my $new_last_threadTrace = shift;
515
516         my $old_last_threadTrace = $self->{last_threadTrace};
517         if (defined $new_last_threadTrace) {
518                 $self->{last_threadTrace} = $new_last_threadTrace;
519                 return $new_last_threadTrace unless ($old_last_threadTrace);
520         }
521
522         return $old_last_threadTrace;
523 }
524
525 sub session_threadTrace {
526         my $self = shift;
527         my $new_last_threadTrace = shift;
528
529         my $old_last_threadTrace = $self->{session_threadTrace};
530         if (defined $new_last_threadTrace) {
531                 $self->{session_threadTrace} = $new_last_threadTrace;
532                 return $new_last_threadTrace unless ($old_last_threadTrace);
533         }
534
535         return $old_last_threadTrace;
536 }
537
538 sub last_message_type {
539         my $self = shift;
540         my $new_last_message_type = shift;
541
542         my $old_last_message_type = $self->{last_message_type};
543         if (defined $new_last_message_type) {
544                 $self->{last_message_type} = $new_last_message_type;
545                 return $new_last_message_type unless ($old_last_message_type);
546         }
547
548         return $old_last_message_type;
549 }
550
551 sub last_message_api_level {
552         my $self = shift;
553         my $new_last_message_api_level = shift;
554
555         my $old_last_message_api_level = $self->{last_message_api_level};
556         if (defined $new_last_message_api_level) {
557                 $self->{last_message_api_level} = $new_last_message_api_level;
558                 return $new_last_message_api_level unless ($old_last_message_api_level);
559         }
560
561         return $old_last_message_api_level;
562 }
563
564 sub remote_id {
565         my $self = shift;
566         my $new_remote_id = shift;
567
568         my $old_remote_id = $self->{remote_id};
569         if (defined $new_remote_id) {
570                 $self->{remote_id} = $new_remote_id;
571                 return $new_remote_id unless ($old_remote_id);
572         }
573
574         return $old_remote_id;
575 }
576
577 sub client_auth {
578         return undef;
579         my $self = shift;
580         my $new_ua = shift;
581
582         my $old_ua = $self->{client_auth};
583         if (defined $new_ua) {
584                 $self->{client_auth} = $new_ua;
585                 return $new_ua unless ($old_ua);
586         }
587
588         return $old_ua->cloneNode(1);
589 }
590
591 sub state {
592         my $self = shift;
593         my $new_state = shift;
594
595         my $old_state = $self->{state};
596         if (defined $new_state) {
597                 $self->{state} = $new_state;
598                 return $new_state unless ($old_state);
599         }
600
601         return $old_state;
602 }
603
604 sub DESTROY {
605         my $self = shift;
606         delete $$self{$_} for keys %$self;
607         return undef;
608 }
609
610 sub recv {
611         my $self = shift;
612         my @proto_args = @_;
613         my %args;
614
615         if ( @proto_args ) {
616                 if ( !(@proto_args % 2) ) {
617                         %args = @proto_args;
618                 } elsif (@proto_args == 1) {
619                         %args = ( timeout => @proto_args );
620                 }
621         }
622
623         #$logger->debug( ref($self). " recv_queue before wait: " . $self->_print_queue(), INTERNAL );
624
625         if( exists( $args{timeout} ) ) {
626                 $args{timeout} = int($args{timeout});
627         } else {
628                 $args{timeout} = 10;
629         }
630
631         #$args{timeout} = 0 if ($self->complete);
632
633         $logger->debug( ref($self) ."->recv with timeout " . $args{timeout}, INTERNAL );
634
635         $args{count} ||= 1;
636
637         my $avail = @{ $self->{recv_queue} };
638         my $time_remaining = $args{timeout};
639
640         while ( $avail < $args{count} and $time_remaining > 0 ) {
641                 last if $self->complete;
642                 my $starttime = time;
643                 $self->queue_wait($time_remaining);
644                 my $endtime = time;
645                 $time_remaining -= ($endtime - $starttime);
646                 $avail = @{ $self->{recv_queue} };
647         }
648
649         #$logger->debug( ref($self)." queue after wait: " . $self->_print_queue(), INTERNAL );
650                 
651         my @list;
652         while ( my $msg = shift @{ $self->{recv_queue} } ) {
653                 push @list, $msg;
654                 last if (scalar(@list) >= $args{count});
655         }
656
657 #       $self->{recv_queue} = [@unlist, @{ $self->{recv_queue} }];
658         $logger->debug( "Number of matched responses: " . @list, DEBUG );
659
660         $self->queue_wait(0); # check for statuses
661         
662         return $list[0] unless (wantarray);
663         return @list;
664 }
665
666 sub push_resend {
667         my $self = shift;
668         push @OpenSRF::AppSession::_RESEND_QUEUE, @_;
669 }
670
671 sub flush_resend {
672         my $self = shift;
673         $logger->debug( "Resending..." . @_RESEND_QUEUE, DEBUG );
674         while ( my $req = shift @OpenSRF::AppSession::_RESEND_QUEUE ) {
675                 $req->resend;
676         }
677 }
678
679
680 sub queue_wait {
681         my $self = shift;
682         if( ! $self->{peer_handle} ) { return 0; }
683         my $timeout = shift || 0;
684         $logger->debug( "Calling queue_wait($timeout)" , DEBUG );
685         $logger->debug( "Timestamp before process($timeout) : " . $logger->format_time(), INTERNAL );
686         my $o = $self->{peer_handle}->process($timeout);
687         $logger->debug( "Timestamp after  process($timeout) : " . $logger->format_time(), INTERNAL );
688         $self->flush_resend;
689         return $o;
690 }
691
692 sub _print_queue {
693         my( $self ) = @_;
694         my $string = "";
695         foreach my $msg ( @{$self->{recv_queue}} ) {
696                 $string = $string . $msg->toString(1) . "\n";
697         }
698         return $string;
699 }
700
701 sub status {
702         my $self = shift;
703         $self->send( 'STATUS', @_ );
704 }
705
706 #-------------------------------------------------------------------------------
707
708 package OpenSRF::AppRequest;
709 use base qw/OpenSRF::AppSession/;
710 use OpenSRF::Utils::Logger qw/:level/;
711 use OpenSRF::DomainObject::oilsResponse qw/:status/;
712
713 sub new {
714         my $class = shift;
715         $class = ref($class) || $class;
716
717         my $session = shift;
718         my $threadTrace = $session->session_threadTrace || $session->last_threadTrace;
719         my $payload = shift;
720         
721         my $self = {    session     => $session,
722                         threadTrace => $threadTrace,
723                         payload     => $payload,
724                         complete    => 0,
725                         recv_queue  => [],
726         };
727
728         bless $self => $class;
729
730         push @{ $self->session->{request_queue} }, $self;
731
732         return $self;
733 }
734
735 sub queue_size {
736         my $size = @{$_[0]->{recv_queue}};
737         return $size;
738 }
739         
740 sub send {
741         shift()->session->send(@_);
742 }
743
744 sub finish {
745         my $self = shift;
746         $self->session->remove_app_request($self);
747         delete($$self{$_}) for (keys %$self);
748 }
749
750 sub session {
751         return shift()->{session};
752 }
753
754 sub complete {
755         my $self = shift;
756         my $complete = shift;
757         return $self->{complete} if ($self->{complete});
758         if (defined $complete) {
759                 $self->{complete} = $complete;
760         } else {
761                 $self->session->queue_wait(0);
762         }
763         return $self->{complete};
764 }
765
766 sub wait_complete {
767         my $self = shift;
768         my $timeout = shift || 1;
769         my $time_remaining = $timeout;
770
771         while ( ! $self->complete  and $time_remaining > 0 ) {
772                 my $starttime = time;
773                 $self->queue_wait($time_remaining);
774                 my $endtime = time;
775                 $time_remaining -= ($endtime - $starttime);
776         }
777
778         return $self->complete;
779 }
780
781 sub threadTrace {
782         return shift()->{threadTrace};
783 }
784
785 sub push_queue {
786         my $self = shift;
787         my $resp = shift;
788         if( !$resp ) { return 0; }
789         push @{ $self->{recv_queue} }, $resp;
790         OpenSRF::Utils::Logger->debug( "AppRequest pushed ".$resp->toString(), INTERNAL );
791 }
792
793 sub queue_wait {
794         my $self = shift;
795         OpenSRF::Utils::Logger->debug( "Calling queue_wait(@_)", DEBUG );
796         return $self->session->queue_wait(@_)
797 }
798
799 sub payload { return shift()->{payload}; }
800
801 sub resend {
802         my $self = shift;
803         OpenSRF::Utils::Logger->debug(
804                 "I'm resending the request for threadTrace ". $self->threadTrace, DEBUG);
805         if($self->payload) {
806                 OpenSRF::Utils::Logger->debug($self->payload->toString,INTERNAL);
807         }
808         return $self->session->send('REQUEST', $self->payload, $self->threadTrace );
809 }
810
811 sub status {
812         my $self = shift;
813         my $msg = shift;
814         $self->session->send( 'STATUS',$msg, $self->threadTrace );
815 }
816
817 sub respond {
818         my $self = shift;
819         my $msg = shift;
820
821         my $response;
822         if (ref($msg) && UNIVERSAL::can($msg, 'getAttribute') && $msg->getAttribute('name') =~ /oilsResult/) {
823                 $response = $msg;
824         } else {
825                 $response = new OpenSRF::DomainObject::oilsResult;
826                 $response->content($msg);
827         }
828
829         $self->session->send('RESULT', $response, $self->threadTrace);
830 }
831
832 sub respond_complete {
833         my $self = shift;
834         my $msg = shift;
835
836         my $response;
837         if (ref($msg) && UNIVERSAL::can($msg, 'getAttribute') && $msg->getAttribute('name') =~ /oilsResult/) {
838                 $response = $msg;
839         } else {
840                 $response = new OpenSRF::DomainObject::oilsResult;
841                 $response->content($msg);
842         }
843
844         my $stat = OpenSRF::DomainObject::oilsConnectStatus->new(
845                 statusCode => STATUS_COMPLETE(),
846                 status => 'Request Complete' );
847
848         $self->session->send( 'RESULT' => $response, 'STATUS' => $stat, $self->threadTrace);
849
850 }
851
852
853 package OpenSRF::AppSubrequest;
854
855 sub respond {
856         my $self = shift;
857         my $resp = shift;
858         push @{$$self{resp}}, $resp;
859 }
860
861 sub new {
862         return bless({resp => []}, 'OpenSRF::AppSubrequest');
863 }
864
865 sub responses { @{$_[0]->{resp}} }
866
867 sub status {}
868
869
870 1;
871