1 package OpenSRF::Transport;
2 use strict; use warnings;
4 use Time::HiRes qw/time/;
5 use OpenSRF::AppSession;
6 use OpenSRF::Utils::Logger qw(:level);
7 use OpenSRF::DomainObject::oilsResponse qw/:status/;
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::Transport::SlimJabber::MessageWrapper;
12 # --- These must be implemented by all Transport subclasses
13 # -------------------------------------------
17 Returns the package name of the package the system will use to
18 gather incoming requests
22 sub get_listener { shift()->alert_abstract(); }
24 =head2 get_peer_client
26 Returns the name of the package responsible for client communication
30 sub get_peer_client { shift()->alert_abstract(); }
32 =head2 get_msg_envelope
34 Returns the name of the package responsible for parsing incoming messages
38 sub get_msg_envelope { shift()->alert_abstract(); }
40 # -------------------------------------------
42 our $message_envelope;
43 my $logger = "OpenSRF::Utils::Logger";
47 =head2 message_envelope( [$envelope] );
49 Sets the message envelope class that will allow us to extract
50 information from the messages we receive from the low
55 sub message_envelope {
56 my( $class, $envelope ) = @_;
58 $message_envelope = $envelope;
59 eval "use $envelope;";
62 "Error loading message_envelope: $envelope -> $@", ERROR);
65 return $message_envelope;
68 =head2 handler( $data )
70 Creates a new MessageWrapper, extracts the remote_id, session_id, and message body
71 from the message. Then, creates or retrieves the AppSession object with the session_id and remote_id.
72 Finally, creates the message document from the body of the message and calls
73 the handler method on the message document.
78 my $start_time = time();
79 my( $class, $service, $data ) = @_;
81 $logger->transport( "Transport handler() received $data", INTERNAL );
83 # pass data to the message envelope
84 my $helper = OpenSRF::Transport::SlimJabber::MessageWrapper->new( $data );
86 # Extract message information
87 my $remote_id = $helper->get_remote_id();
88 my $sess_id = $helper->get_sess_id();
89 my $body = $helper->get_body();
90 my $type = $helper->get_msg_type();
93 if (defined($type) and $type eq 'error') {
94 throw OpenSRF::EX::Session ("$remote_id IS NOT CONNECTED TO THE NETWORK!!!");
99 "Transport building/retrieving session: $service, $remote_id, $sess_id", DEBUG );
101 # See if the app_session already exists. If so, make
102 # sure the sender hasn't changed if we're a server
103 my $app_session = OpenSRF::AppSession->find( $sess_id );
104 if( $app_session and $app_session->endpoint == $app_session->SERVER() and
105 $app_session->remote_id ne $remote_id ) {
106 $logger->transport( "Backend Gone or invalid sender", INTERNAL );
107 my $res = OpenSRF::DomainObject::oilsBrokenSession->new();
108 $res->status( "Backend Gone or invalid sender, Reconnect" );
109 $app_session->status( $res );
113 # Retrieve or build the app_session as appropriate (server_build decides which to do)
114 $logger->transport( "AppSession is valid or does not exist yet", INTERNAL );
115 $app_session = OpenSRF::AppSession->server_build( $sess_id, $remote_id, $service );
117 if( ! $app_session ) {
118 throw OpenSRF::EX::Session ("Transport::handler(): No AppSession object returned from server_build()");
121 # Create a document from the JSON contained within the message
123 eval { $doc = JSON->JSON2perl($body); };
126 $logger->transport( "Received bogus JSON: $@", INFO );
127 $logger->transport( "Bogus JSON data: \n $body \n", INTERNAL );
128 my $res = OpenSRF::DomainObject::oilsXMLParseError->new( status => "JSON Parse Error --- $body\n\n$@" );
130 $app_session->status($res);
131 #$app_session->kill_me;
135 $logger->transport( "Transport::handler() creating \n$body", INTERNAL );
137 # We need to disconnect the session if we got a jabber error on the client side. For
138 # server side, we'll just tear down the session and go away.
139 if (defined($type) and $type eq 'error') {
141 if( $app_session->endpoint == $app_session->SERVER() ) {
142 $app_session->kill_me;
146 $app_session->state( $app_session->DISCONNECTED );
147 # below will lead to infinite looping, should return an exception
148 #$app_session->push_resend( $app_session->app_request(
149 # $doc->documentElement->firstChild->threadTrace ) );
151 "Got Jabber error on client connection $remote_id, nothing we can do..", ERROR );
157 # cycle through and pass each oilsMessage contained in the message
158 # up to the message layer for processing.
159 for my $msg (@$doc) {
161 $logger->transport( "Transport::handler()passing to message handler", DEBUG );
163 $logger->transport( "Transport passing up ".$msg->type." from ".
164 $app_session->remote_id . " with threadTrace [" . $msg->threadTrace."]", INFO );
166 next unless ( $msg && UNIVERSAL::isa($msg => 'OpenSRF::DomainObject::oilsMessage'));
168 if( $app_session->endpoint == $app_session->SERVER() ) {
172 if( ! $msg->handler( $app_session ) ) { return 0; }
174 $logger->debug("Successfully handled message", DEBUG);
179 my $res = OpenSRF::DomainObject::oilsServerError->new();
180 $res->status( $res->status . "\n$e");
181 $app_session->status($res) if $res;
182 $app_session->kill_me;
189 if( ! $msg->handler( $app_session ) ) { return 0; }
190 $logger->debug("Successfully handled message", DEBUG);
196 $logger->debug(sprintf("Message processing duration: %.3fs",(time() - $start_time)), DEBUG);