]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_stack.c
pass up internal server errors as user exceptions
[OpenSRF.git] / src / libstack / osrf_stack.c
1 #include "opensrf/osrf_stack.h"
2
3 osrf_message* _do_client( osrf_app_session*, osrf_message* );
4 osrf_message* _do_server( osrf_app_session*, osrf_message* );
5
6 int osrf_stack_process( transport_client* client, int timeout ) {
7         transport_message* msg = client_recv( client, timeout );
8         if(msg == NULL) return 0;
9         return osrf_stack_transport_handler( msg );
10 }
11
12
13
14 // -----------------------------------------------------------------------------
15 // Entry point into the stack
16 // -----------------------------------------------------------------------------
17 int osrf_stack_transport_handler( transport_message* msg ) { 
18
19         debug_handler( "Transport handler received new message \nfrom %s "
20                         "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body );
21
22         osrf_app_session* session = osrf_app_session_find_session( msg->thread );
23
24         if( session == NULL ) {  /* we must be a server, build a new session */
25                 info_handler( "Server sessions not implemented yet ..." );
26                 return 0;
27         }
28
29         debug_handler("Session [%s] found, building message", msg->thread );
30
31         osrf_app_session_set_remote( session, msg->sender );
32         osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET];
33         memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET );
34         int num_msgs = osrf_message_from_xml( msg->body, arr );
35
36         debug_handler( "We received %d messages from %s", num_msgs, msg->sender );
37
38         /* XXX ERROR CHECKING, BAD XML, ETC... */
39         int i;
40         for( i = 0; i != num_msgs; i++ ) {
41                 osrf_stack_message_handler( session, arr[i] );
42         }
43
44         return 1;
45 }
46
47 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
48         if(session == NULL || msg == NULL)
49                 return 0;
50
51         osrf_message* ret_msg = NULL;
52         if( session->type ==  OSRF_SESSION_CLIENT )
53                  ret_msg = _do_client( session, msg );
54         else
55                 ret_msg= _do_server( session, msg );
56
57         if(ret_msg)
58                 osrf_stack_application_handler( session, ret_msg );
59
60         return 1;
61
62
63
64 /** If we return a message, that message should be passed up the stack, 
65   * if we return NULL, we're finished for now...
66   */
67 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
68         if(session == NULL || msg == NULL)
69                 return NULL;
70
71         osrf_message* new_msg;
72
73         if( msg->m_type == STATUS ) {
74                 
75                 switch( msg->status_code ) {
76
77                         case OSRF_STATUS_OK:
78                                 session->state = OSRF_SESSION_CONNECTED;
79                                 return NULL;
80
81                         case OSRF_STATUS_COMPLETE:
82                                 osrf_app_session_set_complete( session, msg->thread_trace );
83                                 return NULL;
84
85                         case OSRF_STATUS_REDIRECTED:
86                                 osrf_app_session_reset_remote( session );
87                                 session->state = OSRF_SESSION_DISCONNECTED;
88                                 osrf_app_session_request_resend( session, msg->thread_trace );
89                                 return NULL;
90
91                         case OSRF_STATUS_EXPFAILED: 
92                                 osrf_app_session_reset_remote( session );
93                                 session->state = OSRF_SESSION_DISCONNECTED;
94                                 osrf_app_session_request_resend( session, msg->thread_trace );
95                                 return NULL;
96
97                         case OSRF_STATUS_TIMEOUT:
98                                 osrf_app_session_reset_remote( session );
99                                 session->state = OSRF_SESSION_DISCONNECTED;
100                                 osrf_app_session_request_resend( session, msg->thread_trace );
101                                 return NULL;
102
103
104                         default:
105                                 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
106                                 osrf_message_set_status_info( new_msg, 
107                                                 msg->status_name, msg->status_text, msg->status_code );
108                                 warning_handler("The stack doesn't know what to do with " 
109                                                 "the provided message code: %d, name %s. Passing UP.", 
110                                                 msg->status_code, msg->status_name );
111                                 new_msg->is_exception = 1;
112                                 osrf_app_session_set_complete( session, msg->thread_trace );
113                                 osrf_message_free(msg);
114                                 return new_msg;
115                 }
116
117                 return NULL;
118
119         } else if( msg->m_type == RESULT ) 
120                 return msg;
121
122         return NULL;
123
124 }
125
126
127 /** If we return a message, that message should be passed up the stack, 
128   * if we return NULL, we're finished for now...
129   */
130 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
131         if(session == NULL || msg == NULL)
132                 return NULL;
133
134         warning_handler( "We dont' do servers yet !!" );
135
136         return msg;
137 }
138
139
140
141
142 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
143         if(session == NULL || msg == NULL)
144                 return 0;
145
146         if(msg->m_type == RESULT) {
147                 osrf_app_session_push_queue( session, msg ); 
148                 return 1;
149         }
150
151         warning_handler( "application_handler can't handle whatever you sent, type %d", msg->m_type);
152
153         return 1;
154
155 }