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, "Recevied 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 osrf_app_session* session = safe_malloc(sizeof(osrf_app_session));
209 session->transport_handle = osrf_system_get_transport_client();
210 if( session->transport_handle == NULL ) {
211 osrfLogWarning( OSRF_LOG_MARK, "No transport client for service 'client'");
216 char target_buf[512];
217 target_buf[ 0 ] = '\0';
219 osrfStringArray* arr = osrfNewStringArray(8);
220 osrfConfigGetValueList(NULL, arr, "/domains/domain");
221 char* domain = osrfStringArrayGetString(arr, 0);
222 char* router_name = osrfConfigGetValue(NULL, "/router_name");
224 int len = snprintf( target_buf, 512, "%s@%s/%s",
225 router_name ? router_name : "(null)",
226 domain ? domain : "(null)",
227 remote_service ? remote_service : "(null)" );
228 osrfStringArrayFree(arr);
232 if( len >= sizeof( target_buf ) ) {
233 osrfLogWarning( OSRF_LOG_MARK, "Buffer overflow for remote_id");
238 session->request_queue = osrfNewList();
239 session->request_queue->freeItem = &_osrf_app_request_free;
240 session->remote_id = strdup(target_buf);
241 session->orig_remote_id = strdup(session->remote_id);
242 session->remote_service = strdup(remote_service);
243 session->session_locale = NULL;
245 #ifdef ASSUME_STATELESS
246 session->stateless = 1;
247 osrfLogDebug( OSRF_LOG_MARK, "%s session is stateless", remote_service );
249 session->stateless = 0;
250 osrfLogDebug( OSRF_LOG_MARK, "%s session is NOT stateless", remote_service );
253 /* build a chunky, random session id */
257 sprintf(id, "%f.%d%ld", get_timestamp_millis(), (int)time(NULL), (long) getpid());
258 session->session_id = strdup(id);
259 osrfLogDebug( OSRF_LOG_MARK, "Building a new client session with id [%s] [%s]",
260 session->remote_service, session->session_id );
262 session->thread_trace = 0;
263 session->state = OSRF_SESSION_DISCONNECTED;
264 session->type = OSRF_SESSION_CLIENT;
265 //session->next = NULL;
266 _osrf_app_session_push_session( session );
270 osrf_app_session* osrf_app_server_session_init(
271 char* session_id, char* our_app, char* remote_id ) {
273 osrfLogDebug( OSRF_LOG_MARK, "Initing server session with session id %s, service %s,"
274 " and remote_id %s", session_id, our_app, remote_id );
276 osrf_app_session* session = osrf_app_session_find_session( session_id );
277 if(session) return session;
279 session = safe_malloc(sizeof(osrf_app_session));
281 session->transport_handle = osrf_system_get_transport_client();
282 if( session->transport_handle == NULL ) {
283 osrfLogWarning( OSRF_LOG_MARK, "No transport client for service '%s'", our_app );
288 char* statel = osrf_settings_host_value("/apps/%s/stateless", our_app );
289 if(statel) stateless = atoi(statel);
293 session->request_queue = osrfNewList();
294 session->request_queue->freeItem = &_osrf_app_request_free;
295 session->remote_id = strdup(remote_id);
296 session->orig_remote_id = strdup(remote_id);
297 session->session_id = strdup(session_id);
298 session->remote_service = strdup(our_app);
299 session->stateless = stateless;
301 #ifdef ASSUME_STATELESS
302 session->stateless = 1;
305 session->thread_trace = 0;
306 session->state = OSRF_SESSION_DISCONNECTED;
307 session->type = OSRF_SESSION_SERVER;
309 _osrf_app_session_push_session( session );
316 /** frees memory held by a session */
317 void _osrf_app_session_free( osrf_app_session* session ){
321 if( session->userDataFree && session->userData )
322 session->userDataFree(session->userData);
324 if(session->session_locale)
325 free(session->session_locale);
327 free(session->remote_id);
328 free(session->orig_remote_id);
329 free(session->session_id);
330 free(session->remote_service);
331 osrfListFree(session->request_queue);
335 int osrfAppSessionMakeRequest(
336 osrf_app_session* session, jsonObject* params,
337 char* method_name, int protocol, string_array* param_strings ) {
339 return osrf_app_session_make_locale_req( session, params,
340 method_name, protocol, param_strings, NULL );
343 int osrfAppSessionMakeLocaleRequest(
344 osrf_app_session* session, jsonObject* params,
345 char* method_name, int protocol, string_array* param_strings, char* locale ) {
347 return osrf_app_session_make_locale_req( session, params,
348 method_name, protocol, param_strings, locale );
351 int osrf_app_session_make_req(
352 osrf_app_session* session, jsonObject* params,
353 char* method_name, int protocol, string_array* param_strings) {
355 return osrf_app_session_make_locale_req(session, params,
356 method_name, protocol, param_strings, NULL);
359 int osrf_app_session_make_locale_req(
360 osrf_app_session* session, jsonObject* params,
361 char* method_name, int protocol, string_array* param_strings, char* locale ) {
362 if(session == NULL) return -1;
366 osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
367 osrf_message_set_method(req_msg, method_name);
370 osrf_message_set_locale(req_msg, locale);
371 } else if (session->session_locale) {
372 osrf_message_set_locale(req_msg, session->session_locale);
376 osrf_message_set_params(req_msg, params);
382 for(i = 0; i!= param_strings->size ; i++ ) {
383 osrf_message_add_param(req_msg,
384 string_array_get_string(param_strings,i));
389 osrf_app_request* req = _osrf_app_request_init( session, req_msg );
390 if(_osrf_app_session_send( session, req_msg ) ) {
391 osrfLogWarning( OSRF_LOG_MARK, "Error sending request message [%d]", session->thread_trace );
395 osrfLogDebug( OSRF_LOG_MARK, "Pushing [%d] onto requeust queue for session [%s] [%s]",
396 req->request_id, session->remote_service, session->session_id );
397 osrfListSet( session->request_queue, req, req->request_id );
398 return req->request_id;
401 void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
405 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id );
406 if(req) req->complete = 1;
409 int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
412 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id );
414 return req->complete;
419 /** Resets the remote connection id to that of the original*/
420 void osrf_app_session_reset_remote( osrf_app_session* session ){
424 free(session->remote_id);
425 osrfLogDebug( OSRF_LOG_MARK, "App Session [%s] [%s] resetting remote id to %s",
426 session->remote_service, session->session_id, session->orig_remote_id );
428 session->remote_id = strdup(session->orig_remote_id);
431 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) {
434 if( session->remote_id )
435 free(session->remote_id );
436 session->remote_id = strdup( remote_id );
439 /** pushes the given message into the result list of the app_request
440 with the given request_id */
441 int osrf_app_session_push_queue(
442 osrf_app_session* session, osrf_message* msg ){
443 if(session == NULL || msg == NULL) return 0;
445 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, msg->thread_trace );
446 if(req == NULL) return 0;
447 _osrf_app_request_push_queue( req, msg );
452 int osrfAppSessionConnect( osrf_app_session* session ) {
453 return osrf_app_session_connect(session);
457 /** Attempts to connect to the remote service */
458 int osrf_app_session_connect(osrf_app_session* session){
463 if(session->state == OSRF_SESSION_CONNECTED) {
467 int timeout = 5; /* XXX CONFIG VALUE */
469 osrfLogDebug( OSRF_LOG_MARK, "AppSession connecting to %s", session->remote_id );
471 /* defaulting to protocol 1 for now */
472 osrf_message* con_msg = osrf_message_init( CONNECT, session->thread_trace, 1 );
473 osrf_app_session_reset_remote( session );
474 session->state = OSRF_SESSION_CONNECTING;
475 int ret = _osrf_app_session_send( session, con_msg );
476 osrf_message_free(con_msg);
479 time_t start = time(NULL);
480 time_t remaining = (time_t) timeout;
482 while( session->state != OSRF_SESSION_CONNECTED && remaining >= 0 ) {
483 osrf_app_session_queue_wait( session, remaining, NULL );
484 remaining -= (int) (time(NULL) - start);
487 if(session->state == OSRF_SESSION_CONNECTED)
488 osrfLogDebug( OSRF_LOG_MARK, " * Connected Successfully to %s", session->remote_service );
490 if(session->state != OSRF_SESSION_CONNECTED)
498 /** Disconnects from the remote service */
499 int osrf_app_session_disconnect( osrf_app_session* session){
503 if(session->state == OSRF_SESSION_DISCONNECTED)
506 if(session->stateless && session->state != OSRF_SESSION_CONNECTED) {
507 osrfLogDebug( OSRF_LOG_MARK,
508 "Exiting disconnect on stateless session %s",
509 session->session_id);
513 osrfLogDebug(OSRF_LOG_MARK, "AppSession disconnecting from %s", session->remote_id );
515 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
516 _osrf_app_session_send( session, dis_msg );
517 session->state = OSRF_SESSION_DISCONNECTED;
519 osrf_message_free( dis_msg );
520 osrf_app_session_reset_remote( session );
524 int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) {
525 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
526 return _osrf_app_request_resend( req );
530 int osrfAppSessionSendBatch( osrfAppSession* session, osrf_message* msgs[], int size ) {
532 if( !(session && msgs && size > 0) ) return 0;
535 osrfMessage* msg = msgs[0];
539 osrf_app_session_queue_wait( session, 0, NULL );
541 if(session->state != OSRF_SESSION_CONNECTED) {
543 if(session->stateless) { /* stateless session always send to the root listener */
544 osrf_app_session_reset_remote(session);
548 /* do an auto-connect if necessary */
549 if( ! session->stateless &&
550 (msg->m_type != CONNECT) &&
551 (msg->m_type != DISCONNECT) &&
552 (session->state != OSRF_SESSION_CONNECTED) ) {
554 if(!osrf_app_session_connect( session ))
561 char* string = osrfMessageSerializeBatch(msgs, size);
565 transport_message* t_msg = message_init(
566 string, "", session->session_id, session->remote_id, NULL );
567 message_set_osrf_xid( t_msg, osrfLogGetXid() );
569 retval = client_send_message( session->transport_handle, t_msg );
571 if( retval ) osrfLogError(OSRF_LOG_MARK, "client_send_message failed");
573 osrfLogInfo(OSRF_LOG_MARK, "[%s] sent %d bytes of data to %s",
574 session->remote_service, strlen(string), t_msg->recipient );
576 osrfLogDebug(OSRF_LOG_MARK, "Sent: %s", string );
579 message_free( t_msg );
587 int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){
588 if( !(session && msg) ) return 0;
591 return osrfAppSessionSendBatch( session, a, 1 );
597 /** Waits up to 'timeout' seconds for some data to arrive.
598 * Any data that arrives will be processed according to its
599 * payload and message type. This method will return after
600 * any data has arrived.
602 int osrf_app_session_queue_wait( osrf_app_session* session, int timeout, int* recvd ){
603 if(session == NULL) return 0;
605 osrfLogDebug(OSRF_LOG_MARK, "AppSession in queue_wait with timeout %d", timeout );
606 ret_val = osrf_stack_entry_point(session->transport_handle, timeout, recvd);
610 /** Disconnects (if client) and removes the given session from the global session cache
611 * ! This free's all attached app_requests !
613 void osrfAppSessionFree( osrfAppSession* ses ) {
614 osrf_app_session_destroy( ses );
618 void osrf_app_session_destroy( osrf_app_session* session ){
619 if(session == NULL) return;
621 osrfLogDebug(OSRF_LOG_MARK, "AppSession [%s] [%s] destroying self and deleting requests",
622 session->remote_service, session->session_id );
623 if(session->type == OSRF_SESSION_CLIENT
624 && session->state != OSRF_SESSION_DISCONNECTED ) { /* disconnect if we're a client */
625 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
626 _osrf_app_session_send( session, dis_msg );
627 osrf_message_free(dis_msg);
630 osrfHashRemove( osrfAppSessionCache, session->session_id );
631 _osrf_app_session_free( session );
634 osrf_message* osrfAppSessionRequestRecv(
635 osrf_app_session* session, int req_id, int timeout ) {
636 return osrf_app_session_request_recv( session, req_id, timeout );
638 osrf_message* osrf_app_session_request_recv(
639 osrf_app_session* session, int req_id, int timeout ) {
640 if(req_id < 0 || session == NULL)
642 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
643 return _osrf_app_request_recv( req, timeout );
648 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data ) {
649 if(!ses || ! data ) return -1;
651 osrf_message* msg = osrf_message_init( RESULT, requestId, 1 );
652 osrf_message_set_status_info( msg, NULL, "OK", OSRF_STATUS_OK );
653 char* json = jsonObjectToJSON( data );
655 osrf_message_set_result_content( msg, json );
656 _osrf_app_session_send( ses, msg );
659 osrf_message_free( msg );
665 int osrfAppRequestRespondComplete(
666 osrfAppSession* ses, int requestId, jsonObject* data ) {
668 osrf_message* payload = osrf_message_init( RESULT, requestId, 1 );
669 osrf_message_set_status_info( payload, NULL, "OK", OSRF_STATUS_OK );
671 osrf_message* status = osrf_message_init( STATUS, requestId, 1);
672 osrf_message_set_status_info( status, "osrfConnectStatus", "Request Complete", OSRF_STATUS_COMPLETE );
675 char* json = jsonObjectToJSON( data );
676 osrf_message_set_result_content( payload, json );
683 osrfAppSessionSendBatch( ses, ms, 2 );
685 osrf_message_free( payload );
687 osrfAppSessionSendBatch( ses, &status, 1 );
690 osrf_message_free( status );
695 int osrfAppSessionStatus( osrfAppSession* ses, int type, char* name, int reqId, char* message ) {
698 osrf_message* msg = osrf_message_init( STATUS, reqId, 1);
699 osrf_message_set_status_info( msg, name, message, type );
700 _osrf_app_session_send( ses, msg );
701 osrf_message_free( msg );