1 #include "opensrf/osrf_stack.h"
3 osrf_message* _do_client( osrf_app_session*, osrf_message* );
4 osrf_message* _do_server( osrf_app_session*, osrf_message* );
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 );
14 // -----------------------------------------------------------------------------
15 // Entry point into the stack
16 // -----------------------------------------------------------------------------
17 int osrf_stack_transport_handler( transport_message* msg ) {
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 );
22 osrf_app_session* session = osrf_app_session_find_session( msg->thread );
24 if( session == NULL ) { /* we must be a server, build a new session */
25 info_handler( "Server sessions not implemented yet ..." );
29 osrf_app_session_set_remote( session, msg->sender );
30 osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET];
31 memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET );
32 int num_msgs = osrf_message_from_xml( msg->body, arr );
34 /* XXX ERROR CHECKING, BAD XML, ETC... */
36 for( i = 0; i != num_msgs; i++ ) {
37 osrf_stack_message_handler( session, arr[i] );
43 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
44 if(session == NULL || msg == NULL)
47 osrf_message* ret_msg = NULL;
48 if( session->type == OSRF_SESSION_CLIENT )
49 ret_msg = _do_client( session, msg );
51 ret_msg= _do_server( session, msg );
54 osrf_stack_application_handler( session, ret_msg );
60 /** If we return a message, that message should be passed up the stack,
61 * if we return NULL, we're finished for now...
63 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
64 if(session == NULL || msg == NULL)
67 osrf_message* new_msg;
69 if( msg->m_type == STATUS ) {
71 switch( msg->status_code ) {
74 session->state = OSRF_SESSION_CONNECTED;
77 case OSRF_STATUS_COMPLETE:
78 osrf_app_session_set_complete( session, msg->thread_trace );
81 case OSRF_STATUS_REDIRECTED:
82 osrf_app_session_reset_remote( session );
83 session->state = OSRF_SESSION_DISCONNECTED;
84 osrf_app_session_request_resend( session, msg->thread_trace );
87 case OSRF_STATUS_EXPFAILED:
88 osrf_app_session_reset_remote( session );
89 session->state = OSRF_SESSION_DISCONNECTED;
90 osrf_app_session_request_resend( session, msg->thread_trace );
93 case OSRF_STATUS_INTERNALSERVERERROR: /*XXX we need to propogate these... */
95 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
96 osrf_message_set_status_info( new_msg,
97 msg->status_name, msg->status_text, msg->status_code );
98 osrf_message_set_result_content( new_msg, json_object_new_string("HELP") );
99 warning_handler( "Received an INTERNAL SERVER ERROR from the server for tt %d",
105 case OSRF_STATUS_TIMEOUT:
106 osrf_app_session_reset_remote( session );
107 session->state = OSRF_SESSION_DISCONNECTED;
108 osrf_app_session_request_resend( session, msg->thread_trace );
113 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
114 osrf_message_set_status_info( new_msg,
115 msg->status_name, msg->status_text, msg->status_code );
116 warning_handler("The stack doesn't know what to do with "
117 "the provided message code: %d, name %s. Passing UP.",
118 msg->status_code, msg->status_name );
119 new_msg->is_exception = 1;
120 osrf_app_session_set_complete( session, msg->thread_trace );
121 osrf_message_free(msg);
127 } else if( msg->m_type == RESULT )
135 /** If we return a message, that message should be passed up the stack,
136 * if we return NULL, we're finished for now...
138 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
139 if(session == NULL || msg == NULL)
142 warning_handler( "We dont' do servers yet !!" );
150 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
151 if(session == NULL || msg == NULL)
154 if(msg->m_type == RESULT) {
155 osrf_app_session_push_queue( session, msg );
159 warning_handler( "application_handler can't handle whatever you sent, type %d", msg->m_type);