1 package OpenSRF::DomainObject::oilsMessage;
2 use OpenSRF::Utils::JSON;
3 use OpenSRF::AppSession;
4 use OpenSRF::DomainObject::oilsResponse qw/:status/;
5 use OpenSRF::Utils::Logger qw/:level/;
6 use warnings; use strict;
7 use OpenSRF::EX qw/:try/;
9 OpenSRF::Utils::JSON->register_class_hint(hint => 'osrfMessage', name => 'OpenSRF::DomainObject::oilsMessage', type => 'hash');
13 return OpenSRF::Utils::JSON->perl2JSON($self);
18 my $class = ref($self) || $self;
20 return bless \%args => $class;
26 OpenSRF::DomainObject::oilsMessage
30 use OpenSRF::DomainObject::oilsMessage;
32 my $msg = OpenSRF::DomainObject::oilsMessage->new( type => 'CONNECT' );
34 $msg->payload( $domain_object );
38 OpenSRF::DomainObject::oilsMessage is used internally to wrap data sent
39 between client and server. It provides the structure needed to authenticate
40 session data, and also provides the logic needed to unwrap session data and
41 pass this information along to the Application Layer.
45 my $log = 'OpenSRF::Utils::Logger';
49 =head2 OpenSRF::DomainObject::oilsMessage->type( [$new_type] )
53 Used to specify the type of message. One of
54 B<CONNECT, REQUEST, RESULT, STATUS, ERROR, or DISCONNECT>.
63 $self->{type} = $val if (defined $val);
67 =head2 OpenSRF::DomainObject::oilsMessage->api_level( [$new_api_level] )
71 Used to specify the api_level of message. Currently, only api_level C<1> is
72 supported. This will be used to check that messages are well-formed, and as
73 a hint to the Application as to which version of a method should fulfill a
83 $self->{api_level} = $val if (defined $val);
84 return $self->{api_level};
87 =head2 OpenSRF::DomainObject::oilsMessage->sender_locale( [$locale] );
91 Sets or gets the current message locale hint. Useful for telling the
92 server how you see the world.
101 $self->{locale} = $val if (defined $val);
102 return $self->{locale};
105 =head2 OpenSRF::DomainObject::oilsMessage->threadTrace( [$new_threadTrace] );
109 Sets or gets the current message sequence identifier, or thread trace number,
110 for a message. Useful as a debugging aid, but that's about it.
119 $self->{threadTrace} = $val if (defined $val);
120 return $self->{threadTrace};
123 =head2 OpenSRF::DomainObject::oilsMessage->update_threadTrace
127 Increments the threadTrace component of a message. This is automatic when
128 using the normal session processing stack.
134 sub update_threadTrace {
136 my $tT = $self->threadTrace;
141 $log->debug("Setting threadTrace to $tT",DEBUG);
143 $self->threadTrace($tT);
148 =head2 OpenSRF::DomainObject::oilsMessage->payload( [$new_payload] )
152 Sets or gets the payload of a message. This should be exactly one object
153 of (sub)type domainObject or domainObjectCollection.
162 $self->{payload} = $val if (defined $val);
163 return $self->{payload};
166 =head2 OpenSRF::DomainObject::oilsMessage->handler( $session_id )
170 Used by the message processing stack to set session state information from the current
171 message, and then sends control (via the payload) to the Application layer.
181 my $mtype = $self->type;
182 my $locale = $self->sender_locale || '';
183 my $api_level = $self->api_level || 1;
184 my $tT = $self->threadTrace;
186 $log->debug("Message locale is $locale", DEBUG);
188 $session->last_message_type($mtype);
189 $session->last_message_api_level($api_level);
190 $session->last_threadTrace($tT);
191 $session->session_locale($locale);
193 $log->debug(" Received api_level => [$api_level], MType => [$mtype], ".
194 "from [".$session->remote_id."], threadTrace[".$self->threadTrace."]");
197 if ( $session->endpoint == $session->SERVER() ) {
198 $val = $self->do_server( $session, $mtype, $api_level, $tT );
200 } elsif ($session->endpoint == $session->CLIENT()) {
201 $val = $self->do_client( $session, $mtype, $api_level, $tT );
205 return OpenSRF::Application->handler($session, $self->payload);
207 $log->debug("Request was handled internally", DEBUG);
216 # handle server side message processing
218 # !!! Returning 0 means that we don't want to pass ourselves up to the message layer !!!
220 my( $self, $session, $mtype, $api_level, $tT ) = @_;
222 # A Server should never receive STATUS or RESULT messages. If so, we drop them.
223 # This is to keep STATUS/RESULT's from dead client sessions from creating new server
224 # sessions which send mangled session exceptions to backends for messages
225 # that they are not aware of any more.
226 if( $mtype eq 'STATUS' or $mtype eq 'RESULT' ) { return 0; }
229 if ($mtype eq 'DISCONNECT') {
230 $session->disconnect;
235 if ($session->state == $session->CONNECTING()) {
237 if($mtype ne "CONNECT" and $session->stateless) {
238 return 1; #pass the message up the stack
241 # the transport layer thinks this is a new connection. is it?
242 unless ($mtype eq 'CONNECT') {
243 $log->error("Connection seems to be mangled: Got $mtype instead of CONNECT");
245 my $res = OpenSRF::DomainObject::oilsBrokenSession->new(
246 status => "Connection seems to be mangled: Got $mtype instead of CONNECT",
249 $session->status($res);
255 my $res = OpenSRF::DomainObject::oilsConnectStatus->new;
256 $session->status($res);
257 $session->state( $session->CONNECTED );
268 # Handle client side message processing. Return 1 when the the message should be pushed
269 # up to the application layer. return 0 otherwise.
272 my( $self, $session , $mtype, $api_level, $tT) = @_;
275 if ($mtype eq 'STATUS') {
277 if ($self->payload->statusCode == STATUS_OK) {
278 $session->state($session->CONNECTED);
279 $log->debug("We connected successfully to ".$session->app);
283 if ($self->payload->statusCode == STATUS_TIMEOUT) {
284 $session->state( $session->DISCONNECTED );
287 $session->push_resend( $session->app_request($self->threadTrace) );
288 $log->debug("Disconnected because of timeout");
291 } elsif ($self->payload->statusCode == STATUS_REDIRECTED) {
292 $session->state( $session->DISCONNECTED );
295 $session->push_resend( $session->app_request($self->threadTrace) );
296 $log->debug("Disconnected because of redirect", WARN);
299 } elsif ($self->payload->statusCode == STATUS_EXPFAILED) {
300 $session->state( $session->DISCONNECTED );
301 $log->debug("Disconnected because of mangled session", WARN);
303 $session->push_resend( $session->app_request($self->threadTrace) );
306 } elsif ($self->payload->statusCode == STATUS_CONTINUE) {
307 $session->reset_request_timeout($self->threadTrace);
310 } elsif ($self->payload->statusCode == STATUS_COMPLETE) {
311 my $req = $session->app_request($self->threadTrace);
312 $req->complete(1) if ($req);
316 # add more STATUS handling code here (as 'elsif's), for Message layer status stuff
318 #$session->state( $session->DISCONNECTED() );
321 } elsif ($session->state == $session->CONNECTING()) {
322 # This should be changed to check the type of response (is it a connectException?, etc.)
325 if( $self->payload and $self->payload->isa( "ERROR" ) ) {
326 if ($session->raise_remote_errors) {
327 $self->payload->throw();
331 $log->debug("oilsMessage passing to Application: " . $self->type." : ".$session->remote_id );