1 #include "osrf_app_session.h"
4 /* the global app_session cache */
5 osrfHash* osrfAppSessionCache = NULL;
8 // --------------------------------------------------------------------------
9 // --------------------------------------------------------------------------
11 // --------------------------------------------------------------------------
13 /** Allocation and initializes a new app_request object */
14 osrf_app_request* _osrf_app_request_init(
15 osrf_app_session* session, osrf_message* msg ) {
17 osrf_app_request* req =
18 (osrf_app_request*) safe_malloc(sizeof(osrf_app_request));
20 req->session = session;
21 req->request_id = msg->thread_trace;
31 void osrfAppSessionCleanup() {
32 osrfHashFree(osrfAppSessionCache);
35 /** Frees memory used by an app_request object */
36 void _osrf_app_request_free( void * req ){
37 if( req == NULL ) return;
38 osrfAppRequest* r = (osrfAppRequest*) req;
40 if( r->payload ) osrf_message_free( r->payload );
43 osrf_message* cur_msg = req->result;
44 while( cur_msg != NULL ) {
45 osrf_message* next_msg = cur_msg->next;
46 osrf_message_free( cur_msg );
49 osrf_message_free( req->payload );
55 /** Pushes the given message onto the list of 'responses' to this request */
56 void _osrf_app_request_push_queue( osrf_app_request* req, osrf_message* result ){
57 if(req == NULL || result == NULL) return;
58 osrfLogDebug( OSRF_LOG_MARK, "App Session pushing request [%d] onto request queue", result->thread_trace );
59 if(req->result == NULL) {
64 osrf_message* ptr = req->result;
65 osrf_message* ptr2 = req->result->next;
73 result->next = req->result;
79 /** Removes this app_request from our session request set */
80 void osrf_app_session_request_finish(
81 osrf_app_session* session, int req_id ){
83 if(session == NULL) return;
84 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
85 if(req == NULL) return;
86 osrfListRemove( req->session->request_queue, req->request_id );
90 void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id ) {
91 if(session == NULL) return;
92 osrfLogDebug( OSRF_LOG_MARK, "Resetting request timeout %d", req_id );
93 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
94 if(req == NULL) return;
95 req->reset_timeout = 1;
98 /** Checks the receive queue for messages. If any are found, the first
99 * is popped off and returned. Otherwise, this method will wait at most timeout
100 * seconds for a message to appear in the receive queue. Once it arrives it is returned.
101 * If no messages arrive in the timeout provided, null is returned.
103 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) {
105 if(req == NULL) return NULL;
107 if( req->result != NULL ) {
108 /* pop off the first message in the list */
109 osrf_message* tmp_msg = req->result;
110 req->result = req->result->next;
114 time_t start = time(NULL);
115 time_t remaining = (time_t) timeout;
117 while( remaining >= 0 ) {
118 /* tell the session to wait for stuff */
119 osrfLogDebug( OSRF_LOG_MARK, "In app_request receive with remaining time [%d]", (int) remaining );
121 osrf_app_session_queue_wait( req->session, 0 );
123 if( req->result != NULL ) { /* if we received anything */
124 /* pop off the first message in the list */
125 osrfLogDebug( OSRF_LOG_MARK, "app_request_recv received a message, returning it");
126 osrf_message* ret_msg = req->result;
127 osrf_message* tmp_msg = ret_msg->next;
128 req->result = tmp_msg;
135 osrf_app_session_queue_wait( req->session, (int) remaining );
137 if( req->result != NULL ) { /* if we received anything */
138 /* pop off the first message in the list */
139 osrfLogDebug( OSRF_LOG_MARK, "app_request_recv received a message, returning it");
140 osrf_message* ret_msg = req->result;
141 osrf_message* tmp_msg = ret_msg->next;
142 req->result = tmp_msg;
148 if(req->reset_timeout) {
149 remaining = (time_t) timeout;
150 req->reset_timeout = 0;
151 osrfLogDebug( OSRF_LOG_MARK, "Recevied a timeout reset");
153 remaining -= (int) (time(NULL) - start);
157 osrfLogInfo( OSRF_LOG_MARK, "Returning NULL from app_request_recv after timeout");
161 /** Resend this requests original request message */
162 int _osrf_app_request_resend( osrf_app_request* req ) {
163 if(req == NULL) return 0;
165 osrfLogDebug( OSRF_LOG_MARK, "Resending request [%d]", req->request_id );
166 return _osrf_app_session_send( req->session, req->payload );
173 // --------------------------------------------------------------------------
174 // --------------------------------------------------------------------------
176 // --------------------------------------------------------------------------
178 /** returns a session from the global session hash */
179 osrf_app_session* osrf_app_session_find_session( char* session_id ) {
180 if(session_id) return osrfHashGet(osrfAppSessionCache, session_id);
185 /** adds a session to the global session cache */
186 void _osrf_app_session_push_session( osrf_app_session* session ) {
188 if( osrfAppSessionCache == NULL ) osrfAppSessionCache = osrfNewHash();
189 if( osrfHashGet( osrfAppSessionCache, session->session_id ) ) return;
190 osrfHashSet( osrfAppSessionCache, session, session->session_id );
193 /** Allocates a initializes a new app_session */
195 osrf_app_session* osrfAppSessionClientInit( char* remote_service ) {
196 return osrf_app_client_session_init( remote_service );
199 osrf_app_session* osrf_app_client_session_init( char* remote_service ) {
201 osrf_app_session* session = safe_malloc(sizeof(osrf_app_session));
203 session->transport_handle = osrf_system_get_transport_client();
204 if( session->transport_handle == NULL ) {
205 osrfLogWarning( OSRF_LOG_MARK, "No transport client for service 'client'");
209 char target_buf[512];
210 memset(target_buf,0,512);
212 osrfStringArray* arr = osrfNewStringArray(8);
213 osrfConfigGetValueList(NULL, arr, "/domains/domain");
214 char* domain = osrfStringArrayGetString(arr, 0);
215 char* router_name = osrfConfigGetValue(NULL, "/router_name");
217 sprintf( target_buf, "%s@%s/%s", router_name, domain, remote_service );
218 osrfStringArrayFree(arr);
222 session->request_queue = osrfNewList();
223 session->request_queue->freeItem = &_osrf_app_request_free;
224 session->remote_id = strdup(target_buf);
225 session->orig_remote_id = strdup(session->remote_id);
226 session->remote_service = strdup(remote_service);
228 #ifdef ASSUME_STATELESS
229 session->stateless = 1;
230 osrfLogDebug( OSRF_LOG_MARK, "%s session is stateless", remote_service );
232 session->stateless = 0;
233 osrfLogDebug( OSRF_LOG_MARK, "%s session is NOT stateless", remote_service );
236 /* build a chunky, random session id */
240 sprintf(id, "%lf.%d%d", get_timestamp_millis(), (int)time(NULL), getpid());
241 session->session_id = strdup(id);
242 osrfLogDebug( OSRF_LOG_MARK, "Building a new client session with id [%s] [%s]",
243 session->remote_service, session->session_id );
245 session->thread_trace = 0;
246 session->state = OSRF_SESSION_DISCONNECTED;
247 session->type = OSRF_SESSION_CLIENT;
248 //session->next = NULL;
249 _osrf_app_session_push_session( session );
253 osrf_app_session* osrf_app_server_session_init(
254 char* session_id, char* our_app, char* remote_id ) {
256 osrfLogInfo( OSRF_LOG_MARK, "Initing server session with session id %s, service %s,"
257 " and remote_id %s", session_id, our_app, remote_id );
259 osrf_app_session* session = osrf_app_session_find_session( session_id );
260 if(session) return session;
262 session = safe_malloc(sizeof(osrf_app_session));
264 session->transport_handle = osrf_system_get_transport_client();
265 if( session->transport_handle == NULL ) {
266 osrfLogWarning( OSRF_LOG_MARK, "No transport client for service '%s'", our_app );
271 char* statel = osrf_settings_host_value("/apps/%s/stateless", our_app );
272 if(statel) stateless = atoi(statel);
276 session->request_queue = osrfNewList();
277 session->request_queue->freeItem = &_osrf_app_request_free;
278 session->remote_id = strdup(remote_id);
279 session->orig_remote_id = strdup(remote_id);
280 session->session_id = strdup(session_id);
281 session->remote_service = strdup(our_app);
282 session->stateless = stateless;
284 #ifdef ASSUME_STATELESS
285 session->stateless = 1;
288 session->thread_trace = 0;
289 session->state = OSRF_SESSION_DISCONNECTED;
290 session->type = OSRF_SESSION_SERVER;
292 _osrf_app_session_push_session( session );
299 /** frees memory held by a session */
300 void _osrf_app_session_free( osrf_app_session* session ){
304 if( session->userDataFree && session->userData )
305 session->userDataFree(session->userData);
307 free(session->remote_id);
308 free(session->orig_remote_id);
309 free(session->session_id);
310 free(session->remote_service);
311 osrfListFree(session->request_queue);
315 int osrfAppSessionMakeRequest(
316 osrf_app_session* session, jsonObject* params,
317 char* method_name, int protocol, string_array* param_strings ) {
319 return osrf_app_session_make_req( session, params,
320 method_name, protocol, param_strings );
323 int osrf_app_session_make_req(
324 osrf_app_session* session, jsonObject* params,
325 char* method_name, int protocol, string_array* param_strings ) {
326 if(session == NULL) return -1;
328 osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
329 osrf_message_set_method(req_msg, method_name);
331 osrf_message_set_params(req_msg, params);
337 for(i = 0; i!= param_strings->size ; i++ ) {
338 osrf_message_add_param(req_msg,
339 string_array_get_string(param_strings,i));
344 osrf_app_request* req = _osrf_app_request_init( session, req_msg );
345 if(_osrf_app_session_send( session, req_msg ) ) {
346 osrfLogWarning( OSRF_LOG_MARK, "Error sending request message [%d]", session->thread_trace );
350 osrfLogDebug( OSRF_LOG_MARK, "Pushing [%d] onto requeust queue for session [%s] [%s]",
351 req->request_id, session->remote_service, session->session_id );
352 osrfListSet( session->request_queue, req, req->request_id );
353 return req->request_id;
356 void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
360 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id );
361 if(req) req->complete = 1;
364 int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
367 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id );
369 return req->complete;
374 /** Resets the remote connection id to that of the original*/
375 void osrf_app_session_reset_remote( osrf_app_session* session ){
379 free(session->remote_id);
380 osrfLogDebug( OSRF_LOG_MARK, "App Session [%s] [%s] resetting remote id to %s",
381 session->remote_service, session->session_id, session->orig_remote_id );
383 session->remote_id = strdup(session->orig_remote_id);
386 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) {
389 if( session->remote_id )
390 free(session->remote_id );
391 session->remote_id = strdup( remote_id );
394 /** pushes the given message into the result list of the app_request
395 with the given request_id */
396 int osrf_app_session_push_queue(
397 osrf_app_session* session, osrf_message* msg ){
398 if(session == NULL || msg == NULL) return 0;
400 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, msg->thread_trace );
401 if(req == NULL) return 0;
402 _osrf_app_request_push_queue( req, msg );
407 int osrfAppSessionConnect( osrf_app_session* session ) {
408 return osrf_app_session_connect(session);
412 /** Attempts to connect to the remote service */
413 int osrf_app_session_connect(osrf_app_session* session){
418 if(session->state == OSRF_SESSION_CONNECTED) {
422 int timeout = 5; /* XXX CONFIG VALUE */
424 osrfLogDebug( OSRF_LOG_MARK, "AppSession connecting to %s", session->remote_id );
426 /* defaulting to protocol 1 for now */
427 osrf_message* con_msg = osrf_message_init( CONNECT, session->thread_trace, 1 );
428 osrf_app_session_reset_remote( session );
429 session->state = OSRF_SESSION_CONNECTING;
430 int ret = _osrf_app_session_send( session, con_msg );
431 osrf_message_free(con_msg);
434 time_t start = time(NULL);
435 time_t remaining = (time_t) timeout;
437 while( session->state != OSRF_SESSION_CONNECTED && remaining >= 0 ) {
438 osrf_app_session_queue_wait( session, remaining );
439 remaining -= (int) (time(NULL) - start);
442 if(session->state == OSRF_SESSION_CONNECTED)
443 osrfLogDebug( OSRF_LOG_MARK, " * Connected Successfully to %s", session->remote_service );
445 if(session->state != OSRF_SESSION_CONNECTED)
453 /** Disconnects from the remote service */
454 int osrf_app_session_disconnect( osrf_app_session* session){
458 if(session->state == OSRF_SESSION_DISCONNECTED)
461 if(session->stateless && session->state != OSRF_SESSION_CONNECTED) {
462 osrfLogDebug( OSRF_LOG_MARK,
463 "Exiting disconnect on stateless session %s",
464 session->session_id);
468 osrfLogDebug(OSRF_LOG_MARK, "AppSession disconnecting from %s", session->remote_id );
470 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
471 _osrf_app_session_send( session, dis_msg );
472 session->state = OSRF_SESSION_DISCONNECTED;
474 osrf_message_free( dis_msg );
475 osrf_app_session_reset_remote( session );
479 int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) {
480 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
481 return _osrf_app_request_resend( req );
485 int osrfAppSessionSendBatch( osrfAppSession* session, osrf_message* msgs[], int size ) {
487 if( !(session && msgs && size > 0) ) return 0;
491 osrfMessage* msg = msgs[0];
495 osrf_app_session_queue_wait( session, 0 );
497 if(session->state != OSRF_SESSION_CONNECTED) {
499 if(session->stateless) { /* stateless session always send to the root listener */
500 osrf_app_session_reset_remote(session);
504 /* do an auto-connect if necessary */
505 if( ! session->stateless &&
506 (msg->m_type != CONNECT) &&
507 (msg->m_type != DISCONNECT) &&
508 (session->state != OSRF_SESSION_CONNECTED) ) {
510 if(!osrf_app_session_connect( session ))
517 char* string = osrfMessageSerializeBatch(msgs, size);
521 transport_message* t_msg = message_init(
522 string, "", session->session_id, session->remote_id, NULL );
524 osrfLogDebug(OSRF_LOG_MARK, "Session [%s] [%s] sending to %s \nData: %s",
525 session->remote_service, session->session_id, t_msg->recipient, string );
527 retval = client_send_message( session->transport_handle, t_msg );
529 if( retval ) osrfLogError(OSRF_LOG_MARK, "client_send_message failed");
532 message_free( t_msg );
541 int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){
542 if( !(session && msg) ) return 0;
545 return osrfAppSessionSendBatch( session, a, 1 );
551 /** Waits up to 'timeout' seconds for some data to arrive.
552 * Any data that arrives will be processed according to its
553 * payload and message type. This method will return after
554 * any data has arrived.
556 int osrf_app_session_queue_wait( osrf_app_session* session, int timeout ){
557 if(session == NULL) return 0;
559 osrfLogDebug(OSRF_LOG_MARK, "AppSession in queue_wait with timeout %d", timeout );
560 ret_val = osrf_stack_entry_point(session->transport_handle, timeout);
564 /** Disconnects (if client) and removes the given session from the global session cache
565 * ! This free's all attached app_requests !
567 void osrfAppSessionFree( osrfAppSession* ses ) {
568 osrf_app_session_destroy( ses );
572 void osrf_app_session_destroy( osrf_app_session* session ){
573 if(session == NULL) return;
575 osrfLogDebug(OSRF_LOG_MARK, "AppSession [%s] [%s] destroying self and deleting requests",
576 session->remote_service, session->session_id );
577 if(session->type == OSRF_SESSION_CLIENT
578 && session->state != OSRF_SESSION_DISCONNECTED ) { /* disconnect if we're a client */
579 osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 );
580 _osrf_app_session_send( session, dis_msg );
581 osrf_message_free(dis_msg);
584 osrfHashRemove( osrfAppSessionCache, session->session_id );
585 _osrf_app_session_free( session );
588 osrf_message* osrfAppSessionRequestRecv(
589 osrf_app_session* session, int req_id, int timeout ) {
590 return osrf_app_session_request_recv( session, req_id, timeout );
592 osrf_message* osrf_app_session_request_recv(
593 osrf_app_session* session, int req_id, int timeout ) {
594 if(req_id < 0 || session == NULL)
596 osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
597 return _osrf_app_request_recv( req, timeout );
602 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data ) {
603 if(!ses || ! data ) return -1;
605 osrf_message* msg = osrf_message_init( RESULT, requestId, 1 );
606 char* json = jsonObjectToJSON( data );
607 osrf_message_set_result_content( msg, json );
608 _osrf_app_session_send( ses, msg );
611 osrf_message_free( msg );
617 int osrfAppRequestRespondComplete(
618 osrfAppSession* ses, int requestId, jsonObject* data ) {
620 osrf_message* payload = osrf_message_init( RESULT, requestId, 1 );
621 osrf_message_set_status_info( payload, NULL, "OK", OSRF_STATUS_OK );
623 osrf_message* status = osrf_message_init( STATUS, requestId, 1);
624 osrf_message_set_status_info( status, "osrfConnectStatus", "Request Complete", OSRF_STATUS_COMPLETE );
627 char* json = jsonObjectToJSON( data );
628 osrf_message_set_result_content( payload, json );
635 osrfAppSessionSendBatch( ses, ms, 2 );
637 osrf_message_free( payload );
639 osrfAppSessionSendBatch( ses, &status, 1 );
642 osrf_message_free( status );
649 int osrfAppSessionStatus( osrfAppSession* ses, int type, char* name, int reqId, char* message ) {
652 osrf_message* msg = osrf_message_init( STATUS, reqId, 1);
653 osrf_message_set_status_info( msg, name, message, type );
654 _osrf_app_session_send( ses, msg );
655 osrf_message_free( msg );