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 fatal_handler( "Server sessions not implemented yet ..." );
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 );
32 /* XXX ERROR CHECKING, BAD XML, ETC... */
34 for( i = 0; i != num_msgs; i++ ) {
35 osrf_stack_message_handler( session, arr[i] );
41 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
42 if(session == NULL || msg == NULL)
45 osrf_message* ret_msg;
46 if( session->type == OSRF_SESSION_CLIENT )
47 ret_msg = _do_client( session, msg );
49 ret_msg= _do_server( session, msg );
52 osrf_stack_application_handler( session, msg );
58 /** If we return a message, that message should be passed up the stack,
59 * if we return NULL, we're finished for now...
61 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
62 if(session == NULL || msg == NULL)
65 //osrf_message* new_msg;
67 if( msg->m_type == STATUS ) {
69 switch( msg->status_code ) {
72 session->state = OSRF_SESSION_CONNECTED;
75 case OSRF_STATUS_COMPLETE:
76 osrf_app_session_set_complete( session, msg->thread_trace );
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 );
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 );
91 case OSRF_STATUS_INTERNALSERVERERROR: /*XXX we need to propogate these... */
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",
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 );
111 warning_handler("We don't know what to do with the provided message code: %d", msg->status_code );
116 } else if( msg->m_type == RESULT )
124 /** If we return a message, that message should be passed up the stack,
125 * if we return NULL, we're finished for now...
127 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
128 if(session == NULL || msg == NULL)
131 warning_handler( "We dont' do servers yet !!" );
139 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
140 if(session == NULL || msg == NULL)
143 if(msg->m_type == RESULT) {
144 osrf_app_session_push_queue( session, msg );
148 warning_handler( "application_handler can't handle whatever you sent, type %d", msg->m_type);