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 debug_handler("Session [%s] found, building message", msg->thread );
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 );
36 debug_handler( "We received %d messages from %s", num_msgs, msg->sender );
38 /* XXX ERROR CHECKING, BAD XML, ETC... */
40 for( i = 0; i != num_msgs; i++ ) {
41 osrf_stack_message_handler( session, arr[i] );
49 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
50 if(session == NULL || msg == NULL)
53 osrf_message* ret_msg = NULL;
54 if( session->type == OSRF_SESSION_CLIENT )
55 ret_msg = _do_client( session, msg );
57 ret_msg= _do_server( session, msg );
60 osrf_stack_application_handler( session, ret_msg );
66 /** If we return a message, that message should be passed up the stack,
67 * if we return NULL, we're finished for now...
69 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
70 if(session == NULL || msg == NULL)
73 osrf_message* new_msg;
75 if( msg->m_type == STATUS ) {
77 switch( msg->status_code ) {
80 session->state = OSRF_SESSION_CONNECTED;
83 case OSRF_STATUS_COMPLETE:
84 osrf_app_session_set_complete( session, msg->thread_trace );
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 );
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 );
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 );
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);
121 } else if( msg->m_type == RESULT )
129 /** If we return a message, that message should be passed up the stack,
130 * if we return NULL, we're finished for now...
132 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
133 if(session == NULL || msg == NULL)
136 warning_handler( "We dont' do servers yet !!" );
144 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
145 if(session == NULL || msg == NULL)
148 if(msg->m_type == RESULT) {
149 osrf_app_session_push_queue( session, msg );
153 warning_handler( "application_handler can't handle whatever you sent, type %d", msg->m_type);