]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perlmods/OpenSRF/Transport.pm
4ee322c3ba69d056a4e59b65e60c9edc4468b800
[OpenSRF.git] / src / perlmods / OpenSRF / Transport.pm
1 package OpenSRF::Transport;
2 use strict; use warnings;
3 use base 'OpenSRF';
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;
10
11 #------------------ 
12 # --- These must be implemented by all Transport subclasses
13 # -------------------------------------------
14
15 =head2 get_listener
16
17 Returns the package name of the package the system will use to 
18 gather incoming requests
19
20 =cut
21
22 sub get_listener { shift()->alert_abstract(); }
23
24 =head2 get_peer_client
25
26 Returns the name of the package responsible for client communication
27
28 =cut
29
30 sub get_peer_client { shift()->alert_abstract(); } 
31
32 =head2 get_msg_envelope
33
34 Returns the name of the package responsible for parsing incoming messages
35
36 =cut
37
38 sub get_msg_envelope { shift()->alert_abstract(); } 
39
40 # -------------------------------------------
41
42 our $message_envelope;
43 my $logger = "OpenSRF::Utils::Logger"; 
44
45
46
47 =head2 message_envelope( [$envelope] );
48
49 Sets the message envelope class that will allow us to extract
50 information from the messages we receive from the low 
51 level transport
52
53 =cut
54
55 sub message_envelope {
56         my( $class, $envelope ) = @_;
57         if( $envelope ) {
58                 $message_envelope = $envelope;
59                 $envelope->use;
60                 if( $@ ) {
61                         $logger->error( 
62                                         "Error loading message_envelope: $envelope -> $@", ERROR);
63                 }
64         }
65         return $message_envelope;
66 }
67
68 =head2 handler( $data )
69
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.
74
75 =cut
76
77 sub handler {
78         my $start_time = time();
79         my( $class, $service, $data ) = @_;
80
81         $logger->transport( "Transport handler() received $data", INTERNAL );
82
83         # pass data to the message envelope 
84         my $helper = OpenSRF::Transport::SlimJabber::MessageWrapper->new( $data );
85
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();
91
92    $logger->set_osrf_xid($helper->get_osrf_xid);
93
94         if (defined($type) and $type eq 'error') {
95                 throw OpenSRF::EX::Session ("$remote_id IS NOT CONNECTED TO THE NETWORK!!!");
96
97         }
98
99         # See if the app_session already exists.  If so, make 
100         # sure the sender hasn't changed if we're a server
101         my $app_session = OpenSRF::AppSession->find( $sess_id );
102         if( $app_session and $app_session->endpoint == $app_session->SERVER() and
103                         $app_session->remote_id ne $remote_id ) {
104                 $logger->transport( "Backend Gone or invalid sender", INTERNAL );
105                 my $res = OpenSRF::DomainObject::oilsBrokenSession->new();
106                 $res->status( "Backend Gone or invalid sender, Reconnect" );
107                 $app_session->status( $res );
108                 return 1;
109         } 
110
111         # Retrieve or build the app_session as appropriate (server_build decides which to do)
112         $logger->transport( "AppSession is valid or does not exist yet", INTERNAL );
113         $app_session = OpenSRF::AppSession->server_build( $sess_id, $remote_id, $service );
114
115         if( ! $app_session ) {
116                 throw OpenSRF::EX::Session ("Transport::handler(): No AppSession object returned from server_build()");
117         }
118
119         # Create a document from the JSON contained within the message 
120         my $doc; 
121         eval { $doc = JSON->JSON2perl($body); };
122         if( $@ ) {
123
124                 $logger->transport( "Received bogus JSON: $@", INFO );
125                 $logger->transport( "Bogus JSON data: \n $body \n", INTERNAL );
126                 my $res = OpenSRF::DomainObject::oilsXMLParseError->new( status => "JSON Parse Error --- $body\n\n$@" );
127
128                 $app_session->status($res);
129                 #$app_session->kill_me;
130                 return 1;
131         }
132
133         $logger->transport( "Transport::handler() creating \n$body", INTERNAL );
134
135         # We need to disconnect the session if we got a jabber error on the client side.  For
136         # server side, we'll just tear down the session and go away.
137         if (defined($type) and $type eq 'error') {
138                 # If we're a server
139                 if( $app_session->endpoint == $app_session->SERVER() ) {
140                         $app_session->kill_me;
141                         return 1;
142                 } else {
143                         $app_session->reset;
144                         $app_session->state( $app_session->DISCONNECTED );
145                         # below will lead to infinite looping, should return an exception
146                         #$app_session->push_resend( $app_session->app_request( 
147                         #               $doc->documentElement->firstChild->threadTrace ) );
148                         $logger->debug(
149                                 "Got Jabber error on client connection $remote_id, nothing we can do..", ERROR );
150                         return 1;
151                 }
152         }
153
154
155         # cycle through and pass each oilsMessage contained in the message
156         # up to the message layer for processing.
157         for my $msg (@$doc) {
158
159                 next unless (   $msg && UNIVERSAL::isa($msg => 'OpenSRF::DomainObject::oilsMessage'));
160
161                 if( $app_session->endpoint == $app_session->SERVER() ) {
162
163                         try {  
164
165                                 if( ! $msg->handler( $app_session ) ) { return 0; }
166
167                                 $logger->debug("Successfully handled message", DEBUG);
168
169                         } catch Error with {
170
171                                 my $e = shift;
172                                 my $res = OpenSRF::DomainObject::oilsServerError->new();
173                                 $res->status( $res->status . "\n$e");
174                                 $app_session->status($res) if $res;
175                                 $app_session->kill_me;
176                                 return 0;
177
178                         };
179
180                 } else { 
181
182                         if( ! $msg->handler( $app_session ) ) { return 0; } 
183                         $logger->info("Successfully handled message", DEBUG);
184
185                 }
186
187         }
188
189         $logger->debug(sprintf("Message processing duration: %.3fs",(time() - $start_time)), DEBUG);
190
191         return $app_session;
192 }
193
194 1;