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] );
47 int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) {
48 if(session == NULL || msg == NULL)
51 osrf_message* ret_msg = NULL;
52 if( session->type == OSRF_SESSION_CLIENT )
53 ret_msg = _do_client( session, msg );
55 ret_msg= _do_server( session, msg );
58 osrf_stack_application_handler( session, ret_msg );
64 /** If we return a message, that message should be passed up the stack,
65 * if we return NULL, we're finished for now...
67 osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) {
68 if(session == NULL || msg == NULL)
71 osrf_message* new_msg;
73 if( msg->m_type == STATUS ) {
75 switch( msg->status_code ) {
78 session->state = OSRF_SESSION_CONNECTED;
81 case OSRF_STATUS_COMPLETE:
82 osrf_app_session_set_complete( session, msg->thread_trace );
85 case OSRF_STATUS_REDIRECTED:
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_EXPFAILED:
92 osrf_app_session_reset_remote( session );
93 session->state = OSRF_SESSION_DISCONNECTED;
94 osrf_app_session_request_resend( session, msg->thread_trace );
97 case OSRF_STATUS_TIMEOUT:
98 osrf_app_session_reset_remote( session );
99 session->state = OSRF_SESSION_DISCONNECTED;
100 osrf_app_session_request_resend( session, msg->thread_trace );
105 new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol );
106 osrf_message_set_status_info( new_msg,
107 msg->status_name, msg->status_text, msg->status_code );
108 warning_handler("The stack doesn't know what to do with "
109 "the provided message code: %d, name %s. Passing UP.",
110 msg->status_code, msg->status_name );
111 new_msg->is_exception = 1;
112 osrf_app_session_set_complete( session, msg->thread_trace );
113 osrf_message_free(msg);
119 } else if( msg->m_type == RESULT )
127 /** If we return a message, that message should be passed up the stack,
128 * if we return NULL, we're finished for now...
130 osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) {
131 if(session == NULL || msg == NULL)
134 warning_handler( "We dont' do servers yet !!" );
142 int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg ) {
143 if(session == NULL || msg == NULL)
146 if(msg->m_type == RESULT) {
147 osrf_app_session_push_queue( session, msg );
151 warning_handler( "application_handler can't handle whatever you sent, type %d", msg->m_type);