]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_stack.c
fixed bug where session id's were always coming up the same
[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( "Received message for nonexistant session. Dropping..." );
26                 message_free( msg );
27                 return 1;
28         }
29
30         debug_handler("Session [%s] found, building message", msg->thread );
31
32         osrf_app_session_set_remote( session, msg->sender );
33         osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET];
34         memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET );
35         int num_msgs = osrf_message_from_xml( msg->body, arr );
36
37         debug_handler( "We received %d messages from %s", num_msgs, msg->sender );
38
39         /* XXX ERROR CHECKING, BAD XML, ETC... */
40         int i;
41         for( i = 0; i != num_msgs; i++ ) {
42                 osrf_stack_message_handler( session, arr[i] );
43         }
44
45         message_free( msg );
46
47         return 1;
48 }
49
50 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
51         if(session == NULL || msg == NULL)
52                 return 0;
53
54         osrf_message* ret_msg = NULL;
55         if( session->type ==  OSRF_SESSION_CLIENT )
56                  ret_msg = _do_client( session, msg );
57         else
58                 ret_msg= _do_server( session, msg );
59
60         if(ret_msg)
61                 osrf_stack_application_handler( session, ret_msg );
62         else
63                 osrf_message_free(msg);
64
65         return 1;
66
67
68
69 /** If we return a message, that message should be passed up the stack, 
70   * if we return NULL, we're finished for now...
71   */
72 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
73         if(session == NULL || msg == NULL)
74                 return NULL;
75
76         osrf_message* new_msg;
77
78         if( msg->m_type == STATUS ) {
79                 
80                 switch( msg->status_code ) {
81
82                         case OSRF_STATUS_OK:
83                                 debug_handler("We connected successfully");
84                                 session->state = OSRF_SESSION_CONNECTED;
85                                 debug_handler( "State: %x => %s => %d", session, session->session_id, session->state );
86                                 return NULL;
87
88                         case OSRF_STATUS_COMPLETE:
89                                 osrf_app_session_set_complete( session, msg->thread_trace );
90                                 return NULL;
91
92                         case OSRF_STATUS_REDIRECTED:
93                                 osrf_app_session_reset_remote( session );
94                                 session->state = OSRF_SESSION_DISCONNECTED;
95                                 osrf_app_session_request_resend( session, msg->thread_trace );
96                                 return NULL;
97
98                         case OSRF_STATUS_EXPFAILED: 
99                                 osrf_app_session_reset_remote( session );
100                                 session->state = OSRF_SESSION_DISCONNECTED;
101                                 osrf_app_session_request_resend( session, msg->thread_trace );
102                                 return NULL;
103
104                         case OSRF_STATUS_TIMEOUT:
105                                 osrf_app_session_reset_remote( session );
106                                 session->state = OSRF_SESSION_DISCONNECTED;
107                                 osrf_app_session_request_resend( session, msg->thread_trace );
108                                 return NULL;
109
110
111                         default:
112                                 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
113                                 osrf_message_set_status_info( new_msg, 
114                                                 msg->status_name, msg->status_text, msg->status_code );
115                                 warning_handler("The stack doesn't know what to do with " 
116                                                 "the provided message code: %d, name %s. Passing UP.", 
117                                                 msg->status_code, msg->status_name );
118                                 new_msg->is_exception = 1;
119                                 osrf_app_session_set_complete( session, msg->thread_trace );
120                                 osrf_message_free(msg);
121                                 return new_msg;
122                 }
123
124                 return NULL;
125
126         } else if( msg->m_type == RESULT ) 
127                 return msg;
128
129         return NULL;
130
131 }
132
133
134 /** If we return a message, that message should be passed up the stack, 
135   * if we return NULL, we're finished for now...
136   */
137 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
138         if(session == NULL || msg == NULL)
139                 return NULL;
140
141         warning_handler( "We dont' do servers yet !!" );
142
143         return msg;
144 }
145
146
147
148
149 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
150         if(session == NULL || msg == NULL)
151                 return 0;
152
153         if(msg->m_type == RESULT) {
154                 osrf_app_session_push_queue( session, msg ); 
155                 return 1;
156         }
157
158         warning_handler( "application_handler can't handle whatever you sent, type %d", msg->m_type);
159
160         return 1;
161
162 }