]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_stack.c
memory leak cleaning, more to come.
[Evergreen.git] / OpenSRF / 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         message_free( msg );
45
46         return 1;
47 }
48
49 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
50         if(session == NULL || msg == NULL)
51                 return 0;
52
53         osrf_message* ret_msg = NULL;
54         if( session->type ==  OSRF_SESSION_CLIENT )
55                  ret_msg = _do_client( session, msg );
56         else
57                 ret_msg= _do_server( session, msg );
58
59         if(ret_msg)
60                 osrf_stack_application_handler( session, ret_msg );
61
62         return 1;
63
64
65
66 /** If we return a message, that message should be passed up the stack, 
67   * if we return NULL, we're finished for now...
68   */
69 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
70         if(session == NULL || msg == NULL)
71                 return NULL;
72
73         osrf_message* new_msg;
74
75         if( msg->m_type == STATUS ) {
76                 
77                 switch( msg->status_code ) {
78
79                         case OSRF_STATUS_OK:
80                                 session->state = OSRF_SESSION_CONNECTED;
81                                 return NULL;
82
83                         case OSRF_STATUS_COMPLETE:
84                                 osrf_app_session_set_complete( session, msg->thread_trace );
85                                 return NULL;
86
87                         case OSRF_STATUS_REDIRECTED:
88                                 osrf_app_session_reset_remote( session );
89                                 session->state = OSRF_SESSION_DISCONNECTED;
90                                 osrf_app_session_request_resend( session, msg->thread_trace );
91                                 return NULL;
92
93                         case OSRF_STATUS_EXPFAILED: 
94                                 osrf_app_session_reset_remote( session );
95                                 session->state = OSRF_SESSION_DISCONNECTED;
96                                 osrf_app_session_request_resend( session, msg->thread_trace );
97                                 return NULL;
98
99                         case OSRF_STATUS_TIMEOUT:
100                                 osrf_app_session_reset_remote( session );
101                                 session->state = OSRF_SESSION_DISCONNECTED;
102                                 osrf_app_session_request_resend( session, msg->thread_trace );
103                                 return NULL;
104
105
106                         default:
107                                 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
108                                 osrf_message_set_status_info( new_msg, 
109                                                 msg->status_name, msg->status_text, msg->status_code );
110                                 warning_handler("The stack doesn't know what to do with " 
111                                                 "the provided message code: %d, name %s. Passing UP.", 
112                                                 msg->status_code, msg->status_name );
113                                 new_msg->is_exception = 1;
114                                 osrf_app_session_set_complete( session, msg->thread_trace );
115                                 osrf_message_free(msg);
116                                 return new_msg;
117                 }
118
119                 return NULL;
120
121         } else if( msg->m_type == RESULT ) 
122                 return msg;
123
124         return NULL;
125
126 }
127
128
129 /** If we return a message, that message should be passed up the stack, 
130   * if we return NULL, we're finished for now...
131   */
132 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
133         if(session == NULL || msg == NULL)
134                 return NULL;
135
136         warning_handler( "We dont' do servers yet !!" );
137
138         return msg;
139 }
140
141
142
143
144 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
145         if(session == NULL || msg == NULL)
146                 return 0;
147
148         if(msg->m_type == RESULT) {
149                 osrf_app_session_push_queue( session, msg ); 
150                 return 1;
151         }
152
153         warning_handler( "application_handler can't handle whatever you sent, type %d", msg->m_type);
154
155         return 1;
156
157 }