1 #include <opensrf/osrf_app_session.h>
4 /* the global app_session cache */
5 osrfHash* osrfAppSessionCache = NULL;
7 // --------------------------------------------------------------------------
8 // --------------------------------------------------------------------------
10 // --------------------------------------------------------------------------
12 /** Allocation and initializes a new app_request object */
13 osrf_app_request* _osrf_app_request_init(
14 osrf_app_session* session, osrf_message* msg ) {
16 osrf_app_request* req =
17 (osrf_app_request*) safe_malloc(sizeof(osrf_app_request));
19 req->session = session;
20 req->request_id = msg->thread_trace;
30 void osrfAppSessionCleanup() {
31 osrfHashFree(osrfAppSessionCache);
36 /** Frees memory used by an app_request object */
37 void _osrf_app_request_free( void * req ){
38 if( req == NULL ) return;
39 osrfAppRequest* r = (osrfAppRequest*) req;
40 if( r->payload ) osrf_message_free( r->payload );
44 /** Pushes the given message onto the list of 'responses' to this request */
45 void _osrf_app_request_push_queue( osrf_app_request* req, osrf_message* result ){
46 if(req == NULL || result == NULL) return;
47 osrfLogDebug( OSRF_LOG_MARK, "App Session pushing request [%d] onto request queue", result->thread_trace );
48 if(req->result == NULL) {
53 osrf_message* ptr = req->result;
54 osrf_message* ptr2 = req->result->next;
63 /** Removes this app_request from our session request set */
64 void osrf_app_session_request_finish(
65 osrf_app_session* session, int req_id ){
67 if(session == NULL) return;
68 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
69 if(req == NULL) return;
70 osrfListRemove( req->session->request_queue, req->request_id );
74 void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id ) {
75 if(session == NULL) return;
76 osrfLogDebug( OSRF_LOG_MARK, "Resetting request timeout %d", req_id );
77 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
78 if(req == NULL) return;
79 req->reset_timeout = 1;
82 /** Checks the receive queue for messages. If any are found, the first
83 * is popped off and returned. Otherwise, this method will wait at most timeout
84 * seconds for a message to appear in the receive queue. Once it arrives it is returned.
85 * If no messages arrive in the timeout provided, null is returned.
87 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) {
89 if(req == NULL) return NULL;
91 if( req->result != NULL ) {
92 /* pop off the first message in the list */
93 osrf_message* tmp_msg = req->result;
94 req->result = req->result->next;
98 time_t start = time(NULL);
99 time_t remaining = (time_t) timeout;
101 while( remaining >= 0 ) {
102 /* tell the session to wait for stuff */
103 osrfLogDebug( OSRF_LOG_MARK, "In app_request receive with remaining time [%d]", (int) remaining );
105 osrf_app_session_queue_wait( req->session, 0, NULL );
107 if( req->result != NULL ) { /* if we received anything */
108 /* pop off the first message in the list */
109 osrfLogDebug( OSRF_LOG_MARK, "app_request_recv received a message, returning it");
110 osrf_message* ret_msg = req->result;
111 osrf_message* tmp_msg = ret_msg->next;
112 req->result = tmp_msg;
113 if (ret_msg->sender_locale) {
114 if (req->session->session_locale)
115 free(req->session->session_locale);
116 req->session->session_locale = strdup(ret_msg->sender_locale);
124 osrf_app_session_queue_wait( req->session, (int) remaining, NULL );
126 if( req->result != NULL ) { /* if we received anything */
127 /* pop off the first message in the list */
128 osrfLogDebug( OSRF_LOG_MARK, "app_request_recv received a message, returning it");
129 osrf_message* ret_msg = req->result;
130 osrf_message* tmp_msg = ret_msg->next;
131 req->result = tmp_msg;
132 if (ret_msg->sender_locale) {
133 if (req->session->session_locale)
134 free(req->session->session_locale);
135 req->session->session_locale = strdup(ret_msg->sender_locale);
142 if(req->reset_timeout) {
143 remaining = (time_t) timeout;
144 req->reset_timeout = 0;
145 osrfLogDebug( OSRF_LOG_MARK, "Received a timeout reset");
147 remaining -= (int) (time(NULL) - start);
151 osrfLogInfo( OSRF_LOG_MARK, "Returning NULL from app_request_recv after timeout");
155 /** Resend this requests original request message */
156 int _osrf_app_request_resend( osrf_app_request* req ) {
157 if(req == NULL) return 0;
159 osrfLogDebug( OSRF_LOG_MARK, "Resending request [%d]", req->request_id );
160 return _osrf_app_session_send( req->session, req->payload );
167 // --------------------------------------------------------------------------
168 // --------------------------------------------------------------------------
170 // --------------------------------------------------------------------------
172 /** returns a session from the global session hash */
173 char* osrf_app_session_set_locale( osrf_app_session* session, const char* locale ) {
174 if (!session || !locale)
177 if(session->session_locale)
178 free(session->session_locale);
180 session->session_locale = strdup( locale );
181 return session->session_locale;
184 /** returns a session from the global session hash */
185 osrf_app_session* osrf_app_session_find_session( char* session_id ) {
186 if(session_id) return osrfHashGet(osrfAppSessionCache, session_id);
191 /** adds a session to the global session cache */
192 void _osrf_app_session_push_session( osrf_app_session* session ) {
194 if( osrfAppSessionCache == NULL ) osrfAppSessionCache = osrfNewHash();
195 if( osrfHashGet( osrfAppSessionCache, session->session_id ) ) return;
196 osrfHashSet( osrfAppSessionCache, session, session->session_id );
199 /** Allocates a initializes a new app_session */
201 osrf_app_session* osrfAppSessionClientInit( char* remote_service ) {
202 return osrf_app_client_session_init( remote_service );
205 osrf_app_session* osrf_app_client_session_init( char* remote_service ) {
207 if (!remote_service) {
208 osrfLogWarning( OSRF_LOG_MARK, "No remote service specified in osrf_app_client_session_init");
212 osrf_app_session* session = safe_malloc(sizeof(osrf_app_session));
214 session->transport_handle = osrf_system_get_transport_client();
215 if( session->transport_handle == NULL ) {
216 osrfLogWarning( OSRF_LOG_MARK, "No transport client for service 'client'");
221 osrfStringArray* arr = osrfNewStringArray(8);
222 osrfConfigGetValueList(NULL, arr, "/domains/domain");
223 char* domain = osrfStringArrayGetString(arr, 0);
226 osrfLogWarning( OSRF_LOG_MARK, "No domains specified in the OpenSRF config file");
228 osrfStringArrayFree(arr);
232 char* router_name = osrfConfigGetValue(NULL, "/router_name");
234 osrfLogWarning( OSRF_LOG_MARK, "No router name specified in the OpenSRF config file");
236 osrfStringArrayFree(arr);
240 char target_buf[512];
241 target_buf[ 0 ] = '\0';
243 int len = snprintf( target_buf, 512, "%s@%s/%s",
244 router_name ? router_name : "(null)",
245 domain ? domain : "(null)",
246 remote_service ? remote_service : "(null)" );
247 osrfStringArrayFree(arr);
251 if( len >= sizeof( target_buf ) ) {
252 osrfLogWarning( OSRF_LOG_MARK, "Buffer overflow for remote_id");
257 session->request_queue = osrfNewList();
258 session->request_queue->freeItem = &_osrf_app_request_free;
259 session->remote_id = strdup(target_buf);
260 session->orig_remote_id = strdup(session->remote_id);
261 session->remote_service = strdup(remote_service);
262 session->session_locale = NULL;
264 #ifdef ASSUME_STATELESS
265 session->stateless = 1;
266 osrfLogDebug( OSRF_LOG_MARK, "%s session is stateless", remote_service );
268 session->stateless = 0;
269 osrfLogDebug( OSRF_LOG_MARK, "%s session is NOT stateless", remote_service );
272 /* build a chunky, random session id */
276 sprintf(id, "%f.%d%ld", get_timestamp_millis(), (int)time(NULL), (long) getpid());
277 session->session_id = strdup(id);
278 osrfLogDebug( OSRF_LOG_MARK, "Building a new client session with id [%s] [%s]",
279 session->remote_service, session->session_id );
281 session->thread_trace = 0;
282 session->state = OSRF_SESSION_DISCONNECTED;
283 session->type = OSRF_SESSION_CLIENT;
284 //session->next = NULL;
285 _osrf_app_session_push_session( session );
289 osrf_app_session* osrf_app_server_session_init(
290 char* session_id, char* our_app, char* remote_id ) {
292 osrfLogDebug( OSRF_LOG_MARK, "Initing server session with session id %s, service %s,"
293 " and remote_id %s", session_id, our_app, remote_id );
295 osrf_app_session* session = osrf_app_session_find_session( session_id );
296 if(session) return session;
298 session = safe_malloc(sizeof(osrf_app_session));
300 session->transport_handle = osrf_system_get_transport_client();
301 if( session->transport_handle == NULL ) {
302 osrfLogWarning( OSRF_LOG_MARK, "No transport client for service '%s'", our_app );
307 char* statel = osrf_settings_host_value("/apps/%s/stateless", our_app );
308 if(statel) stateless = atoi(statel);
312 session->request_queue = osrfNewList();
313 session->request_queue->freeItem = &_osrf_app_request_free;
314 session->remote_id = strdup(remote_id);
315 session->orig_remote_id = strdup(remote_id);
316 session->session_id = strdup(session_id);
317 session->remote_service = strdup(our_app);
318 session->stateless = stateless;
320 #ifdef ASSUME_STATELESS
321 session->stateless = 1;
324 session->thread_trace = 0;
325 session->state = OSRF_SESSION_DISCONNECTED;
326 session->type = OSRF_SESSION_SERVER;
328 _osrf_app_session_push_session( session );
335 /** frees memory held by a session */
336 void _osrf_app_session_free( osrf_app_session* session ){
340 if( session->userDataFree && session->userData )
341 session->userDataFree(session->userData);
343 if(session->session_locale)
344 free(session->session_locale);
346 free(session->remote_id);
347 free(session->orig_remote_id);
348 free(session->session_id);
349 free(session->remote_service);
350 osrfListFree(session->request_queue);
354 int osrfAppSessionMakeRequest(
355 osrf_app_session* session, jsonObject* params,
356 char* method_name, int protocol, string_array* param_strings ) {
358 return osrf_app_session_make_locale_req( session, params,
359 method_name, protocol, param_strings, NULL );
362 int osrfAppSessionMakeLocaleRequest(
363 osrf_app_session* session, jsonObject* params,
364 char* method_name, int protocol, string_array* param_strings, char* locale ) {
366 return osrf_app_session_make_locale_req( session, params,
367 method_name, protocol, param_strings, locale );
370 int osrf_app_session_make_req(
371 osrf_app_session* session, jsonObject* params,
372 char* method_name, int protocol, string_array* param_strings) {
374 return osrf_app_session_make_locale_req(session, params,
375 method_name, protocol, param_strings, NULL);
378 int osrf_app_session_make_locale_req(
379 osrf_app_session* session, jsonObject* params,
380 char* method_name, int protocol, string_array* param_strings, char* locale ) {
381 if(session == NULL) return -1;
385 osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
386 osrf_message_set_method(req_msg, method_name);
389 osrf_message_set_locale(req_msg, locale);
390 } else if (session->session_locale) {
391 osrf_message_set_locale(req_msg, session->session_locale);
395 osrf_message_set_params(req_msg, params);
401 for(i = 0; i!= param_strings->size ; i++ ) {
402 osrf_message_add_param(req_msg,
403 string_array_get_string(param_strings,i));
408 osrf_app_request* req = _osrf_app_request_init( session, req_msg );
409 if(_osrf_app_session_send( session, req_msg ) ) {
410 osrfLogWarning( OSRF_LOG_MARK, "Error sending request message [%d]", session->thread_trace );
414 osrfLogDebug( OSRF_LOG_MARK, "Pushing [%d] onto request queue for session [%s] [%s]",
415 req->request_id, session->remote_service, session->session_id );
416 osrfListSet( session->request_queue, req, req->request_id );
417 return req->request_id;
420 void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
424 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id );
425 if(req) req->complete = 1;
428 int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
431 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id );
433 return req->complete;
438 /** Resets the remote connection id to that of the original*/
439 void osrf_app_session_reset_remote( osrf_app_session* session ){
443 free(session->remote_id);
444 osrfLogDebug( OSRF_LOG_MARK, "App Session [%s] [%s] resetting remote id to %s",
445 session->remote_service, session->session_id, session->orig_remote_id );
447 session->remote_id = strdup(session->orig_remote_id);
450 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) {
453 if( session->remote_id )
454 free(session->remote_id );
455 session->remote_id = strdup( remote_id );
458 /** pushes the given message into the result list of the app_request
459 with the given request_id */
460 int osrf_app_session_push_queue(
461 osrf_app_session* session, osrf_message* msg ){
462 if(session == NULL || msg == NULL) return 0;
464 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, msg->thread_trace );
465 if(req == NULL) return 0;
466 _osrf_app_request_push_queue( req, msg );
471 int osrfAppSessionConnect( osrf_app_session* session ) {
472 return osrf_app_session_connect(session);
476 /** Attempts to connect to the remote service */
477 int osrf_app_session_connect(osrf_app_session* session){
482 if(session->state == OSRF_SESSION_CONNECTED) {
486 int timeout = 5; /* XXX CONFIG VALUE */
488 osrfLogDebug( OSRF_LOG_MARK, "AppSession connecting to %s", session->remote_id );
490 /* defaulting to protocol 1 for now */
491 osrf_message* con_msg = osrf_message_init( CONNECT, session->thread_trace, 1 );
492 osrf_app_session_reset_remote( session );
493 session->state = OSRF_SESSION_CONNECTING;
494 int ret = _osrf_app_session_send( session, con_msg );
495 osrf_message_free(con_msg);
498 time_t start = time(NULL);
499 time_t remaining = (time_t) timeout;
501 while( session->state != OSRF_SESSION_CONNECTED && remaining >= 0 ) {
502 osrf_app_session_queue_wait( session, remaining, NULL );
503 remaining -= (int) (time(NULL) - start);
506 if(session->state == OSRF_SESSION_CONNECTED)
507 osrfLogDebug( OSRF_LOG_MARK, " * Connected Successfully to %s", session->remote_service );
509 if(session->state != OSRF_SESSION_CONNECTED)
517 /** Disconnects from the remote service */
518 int osrf_app_session_disconnect( osrf_app_session* session){
522 if(session->state == OSRF_SESSION_DISCONNECTED)
525 if(session->stateless && session->state != OSRF_SESSION_CONNECTED) {
526 osrfLogDebug( OSRF_LOG_MARK,
527 "Exiting disconnect on stateless session %s",
528 session->session_id);
532 osrfLogDebug(OSRF_LOG_MARK, "AppSession disconnecting from %s", session->remote_id );
534 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
535 _osrf_app_session_send( session, dis_msg );
536 session->state = OSRF_SESSION_DISCONNECTED;
538 osrf_message_free( dis_msg );
539 osrf_app_session_reset_remote( session );
543 int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) {
544 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
545 return _osrf_app_request_resend( req );
549 int osrfAppSessionSendBatch( osrfAppSession* session, osrf_message* msgs[], int size ) {
551 if( !(session && msgs && size > 0) ) return 0;
554 osrfMessage* msg = msgs[0];
558 osrf_app_session_queue_wait( session, 0, NULL );
560 if(session->state != OSRF_SESSION_CONNECTED) {
562 if(session->stateless) { /* stateless session always send to the root listener */
563 osrf_app_session_reset_remote(session);
567 /* do an auto-connect if necessary */
568 if( ! session->stateless &&
569 (msg->m_type != CONNECT) &&
570 (msg->m_type != DISCONNECT) &&
571 (session->state != OSRF_SESSION_CONNECTED) ) {
573 if(!osrf_app_session_connect( session ))
580 char* string = osrfMessageSerializeBatch(msgs, size);
584 transport_message* t_msg = message_init(
585 string, "", session->session_id, session->remote_id, NULL );
586 message_set_osrf_xid( t_msg, osrfLogGetXid() );
588 retval = client_send_message( session->transport_handle, t_msg );
590 if( retval ) osrfLogError(OSRF_LOG_MARK, "client_send_message failed");
592 osrfLogInfo(OSRF_LOG_MARK, "[%s] sent %d bytes of data to %s",
593 session->remote_service, strlen(string), t_msg->recipient );
595 osrfLogDebug(OSRF_LOG_MARK, "Sent: %s", string );
598 message_free( t_msg );
606 int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){
607 if( !(session && msg) ) return 0;
610 return osrfAppSessionSendBatch( session, a, 1 );
616 /** Waits up to 'timeout' seconds for some data to arrive.
617 * Any data that arrives will be processed according to its
618 * payload and message type. This method will return after
619 * any data has arrived.
621 int osrf_app_session_queue_wait( osrf_app_session* session, int timeout, int* recvd ){
622 if(session == NULL) return 0;
624 osrfLogDebug(OSRF_LOG_MARK, "AppSession in queue_wait with timeout %d", timeout );
625 ret_val = osrf_stack_entry_point(session->transport_handle, timeout, recvd);
629 /** Disconnects (if client) and removes the given session from the global session cache
630 * ! This free's all attached app_requests !
632 void osrfAppSessionFree( osrfAppSession* ses ) {
633 osrf_app_session_destroy( ses );
637 void osrf_app_session_destroy( osrf_app_session* session ){
638 if(session == NULL) return;
640 osrfLogDebug(OSRF_LOG_MARK, "AppSession [%s] [%s] destroying self and deleting requests",
641 session->remote_service, session->session_id );
642 if(session->type == OSRF_SESSION_CLIENT
643 && session->state != OSRF_SESSION_DISCONNECTED ) { /* disconnect if we're a client */
644 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
645 _osrf_app_session_send( session, dis_msg );
646 osrf_message_free(dis_msg);
649 osrfHashRemove( osrfAppSessionCache, session->session_id );
650 _osrf_app_session_free( session );
653 osrf_message* osrfAppSessionRequestRecv(
654 osrf_app_session* session, int req_id, int timeout ) {
655 return osrf_app_session_request_recv( session, req_id, timeout );
657 osrf_message* osrf_app_session_request_recv(
658 osrf_app_session* session, int req_id, int timeout ) {
659 if(req_id < 0 || session == NULL)
661 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
662 return _osrf_app_request_recv( req, timeout );
667 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data ) {
668 if(!ses || ! data ) return -1;
670 osrf_message* msg = osrf_message_init( RESULT, requestId, 1 );
671 osrf_message_set_status_info( msg, NULL, "OK", OSRF_STATUS_OK );
672 char* json = jsonObjectToJSON( data );
674 osrf_message_set_result_content( msg, json );
675 _osrf_app_session_send( ses, msg );
678 osrf_message_free( msg );
684 int osrfAppRequestRespondComplete(
685 osrfAppSession* ses, int requestId, jsonObject* data ) {
687 osrf_message* payload = osrf_message_init( RESULT, requestId, 1 );
688 osrf_message_set_status_info( payload, NULL, "OK", OSRF_STATUS_OK );
690 osrf_message* status = osrf_message_init( STATUS, requestId, 1);
691 osrf_message_set_status_info( status, "osrfConnectStatus", "Request Complete", OSRF_STATUS_COMPLETE );
694 char* json = jsonObjectToJSON( data );
695 osrf_message_set_result_content( payload, json );
702 osrfAppSessionSendBatch( ses, ms, 2 );
704 osrf_message_free( payload );
706 osrfAppSessionSendBatch( ses, &status, 1 );
709 osrf_message_free( status );
714 int osrfAppSessionStatus( osrfAppSession* ses, int type, char* name, int reqId, char* message ) {
717 osrf_message* msg = osrf_message_init( STATUS, reqId, 1);
718 osrf_message_set_status_info( msg, name, message, type );
719 _osrf_app_session_send( ses, msg );
720 osrf_message_free( msg );