]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libstack/osrf_stack.c
OK. This is the first early C version of the OpenSRF stack. It's highly
[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                 fatal_handler( "Server sessions not implemented yet ..." );
26
27         osrf_app_session_set_remote( session, msg->sender );
28         osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET];
29         memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET );
30         int num_msgs = osrf_message_from_xml( msg->body, arr );
31
32         /* XXX ERROR CHECKING, BAD XML, ETC... */
33         int i;
34         for( i = 0; i != num_msgs; i++ ) {
35                 osrf_stack_message_handler( session, arr[i] );
36         }
37
38         return 1;
39 }
40
41 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
42         if(session == NULL || msg == NULL)
43                 return 0;
44
45         osrf_message* ret_msg;
46         if( session->type ==  OSRF_SESSION_CLIENT )
47                  ret_msg = _do_client( session, msg );
48         else
49                 ret_msg= _do_server( session, msg );
50
51         if(ret_msg)
52                 osrf_stack_application_handler( session, msg );
53
54         return 1;
55
56
57
58 /** If we return a message, that message should be passed up the stack, 
59   * if we return NULL, we're finished for now...
60   */
61 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
62         if(session == NULL || msg == NULL)
63                 return NULL;
64
65         //osrf_message* new_msg;
66
67         if( msg->m_type == STATUS ) {
68                 
69                 switch( msg->status_code ) {
70
71                         case OSRF_STATUS_OK:
72                                 session->state = OSRF_SESSION_CONNECTED;
73                                 return NULL;
74
75                         case OSRF_STATUS_COMPLETE:
76                                 osrf_app_session_set_complete( session, msg->thread_trace );
77                                 return NULL;
78
79                         case OSRF_STATUS_REDIRECTED:
80                                 osrf_app_session_reset_remote( session );
81                                 session->state = OSRF_SESSION_DISCONNECTED;
82                                 osrf_app_session_request_resend( session, msg->thread_trace );
83                                 return NULL;
84
85                         case OSRF_STATUS_EXPFAILED: 
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_INTERNALSERVERERROR: /*XXX we need to propogate these... */
92                                 /*
93                                 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
94                                 osrf_message_set_status_info( new_msg, 
95                                                 msg->status_name, msg->status_text, msg->status_code );
96                                 osrf_message_set_result_content( new_msg, json_object_new_string("HELP") );
97                                 warning_handler( "Received an INTERNAL SERVER ERROR from the server for tt %d", 
98                                                 msg->thread_trace);
99                                 return new_msg;
100                                 */
101                                 return NULL;
102
103                         case OSRF_STATUS_TIMEOUT:
104                                 osrf_app_session_reset_remote( session );
105                                 session->state = OSRF_SESSION_DISCONNECTED;
106                                 osrf_app_session_request_resend( session, msg->thread_trace );
107                                 return NULL;
108
109
110                         default:
111                                 warning_handler("We don't know what to do with the provided message code: %d", msg->status_code );
112                 }
113
114                 return NULL;
115
116         } else if( msg->m_type == RESULT ) 
117                 return msg;
118
119         return NULL;
120
121 }
122
123
124 /** If we return a message, that message should be passed up the stack, 
125   * if we return NULL, we're finished for now...
126   */
127 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
128         if(session == NULL || msg == NULL)
129                 return NULL;
130
131         warning_handler( "We dont' do servers yet !!" );
132
133         return msg;
134 }
135
136
137
138
139 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
140         if(session == NULL || msg == NULL)
141                 return 0;
142
143         if(msg->m_type == RESULT) {
144                 osrf_app_session_push_queue( session, msg ); 
145                 return 1;
146         }
147
148         warning_handler( "application_handler can't handle whatever you sent, type %d", msg->m_type);
149
150         return 1;
151
152 }