]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/osrf_app_session.c
initial sender_locale support ... probably going to break stuff; also, patch from...
[OpenSRF.git] / src / libopensrf / osrf_app_session.c
1 #include <opensrf/osrf_app_session.h>
2 #include <time.h>
3
4 /* the global app_session cache */
5 osrfHash* osrfAppSessionCache = NULL;
6
7 // --------------------------------------------------------------------------
8 // --------------------------------------------------------------------------
9 // Request API
10 // --------------------------------------------------------------------------
11
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 ) {
15
16         osrf_app_request* req = 
17                 (osrf_app_request*) safe_malloc(sizeof(osrf_app_request));
18
19         req->session            = session;
20         req->request_id = msg->thread_trace;
21         req->complete           = 0;
22         req->payload            = msg;
23         req->result                     = NULL;
24
25         return req;
26
27 }
28
29
30 void osrfAppSessionCleanup() {
31         osrfHashFree(osrfAppSessionCache);      
32 }
33
34
35
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 );
41         free( r );
42 }
43
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) {
49                 req->result = result;
50
51         } else {
52                 
53                 osrf_message* ptr = req->result;
54                 osrf_message* ptr2 = req->result->next;
55                 while( ptr2 ) {
56                         ptr = ptr2;
57                         ptr2 = ptr2->next;
58                 }
59                 ptr->next = result;
60         }
61 }
62
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 ){
66
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 );
71 }
72
73
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;
80 }
81
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.
86   */
87 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) {
88
89         if(req == NULL) return NULL;
90
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;
95                 return tmp_msg;
96         }
97
98         time_t start = time(NULL);      
99         time_t remaining = (time_t) timeout;
100
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 );
104
105                 osrf_app_session_queue_wait( req->session, 0, NULL );
106
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);
117                         }
118                         return ret_msg;
119                 }
120
121                 if( req->complete )
122                         return NULL;
123
124                 osrf_app_session_queue_wait( req->session, (int) remaining, NULL );
125
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);
136                         }
137                         return ret_msg;
138                 }
139                 if( req->complete )
140                         return NULL;
141
142                 if(req->reset_timeout) {
143                         remaining = (time_t) timeout;
144                         req->reset_timeout = 0;
145                         osrfLogDebug( OSRF_LOG_MARK, "Recevied a timeout reset");
146                 } else {
147                         remaining -= (int) (time(NULL) - start);
148                 }
149         }
150
151         osrfLogInfo( OSRF_LOG_MARK, "Returning NULL from app_request_recv after timeout");
152         return NULL;
153 }
154
155 /** Resend this requests original request message */
156 int _osrf_app_request_resend( osrf_app_request* req ) {
157         if(req == NULL) return 0;
158         if(!req->complete) {
159                 osrfLogDebug( OSRF_LOG_MARK,  "Resending request [%d]", req->request_id );
160                 return _osrf_app_session_send( req->session, req->payload );
161         }
162         return 1;
163 }
164
165
166
167 // --------------------------------------------------------------------------
168 // --------------------------------------------------------------------------
169 // Session API
170 // --------------------------------------------------------------------------
171
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)
175                 return NULL;
176
177         if(session->session_locale)
178                 free(session->session_locale);
179
180         session->session_locale = strdup( locale );
181         return session->session_locale;
182 }
183
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);
187         return NULL;
188 }
189
190
191 /** adds a session to the global session cache */
192 void _osrf_app_session_push_session( osrf_app_session* session ) {
193         if(!session) return;
194         if( osrfAppSessionCache == NULL ) osrfAppSessionCache = osrfNewHash();
195         if( osrfHashGet( osrfAppSessionCache, session->session_id ) ) return;
196         osrfHashSet( osrfAppSessionCache, session, session->session_id );
197 }
198
199 /** Allocates a initializes a new app_session */
200
201 osrf_app_session* osrfAppSessionClientInit( char* remote_service ) {
202         return osrf_app_client_session_init( remote_service );
203 }
204
205 osrf_app_session* osrf_app_client_session_init( char* remote_service ) {
206
207         if (!remote_service) {
208                 osrfLogWarning( OSRF_LOG_MARK, "No remote service specified in osrf_app_client_session_init");
209                 return NULL;
210         }
211
212         osrf_app_session* session = safe_malloc(sizeof(osrf_app_session));      
213
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'");
217                 free( session );
218                 return NULL;
219         }
220
221         osrfStringArray* arr = osrfNewStringArray(8);
222         osrfConfigGetValueList(NULL, arr, "/domains/domain");
223         char* domain = osrfStringArrayGetString(arr, 0);
224
225         if (!domain) {
226                 osrfLogWarning( OSRF_LOG_MARK, "No domains specified in the OpenSRF config file");
227                 free( session );
228                 osrfStringArrayFree(arr);
229                 return NULL;
230         }
231
232         char* router_name = osrfConfigGetValue(NULL, "/router_name");
233         if (!router_name) {
234                 osrfLogWarning( OSRF_LOG_MARK, "No router name specified in the OpenSRF config file");
235                 free( session );
236                 osrfStringArrayFree(arr);
237                 return NULL;
238         }
239
240         char target_buf[512];
241         target_buf[ 0 ] = '\0';
242
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);
248         //free(domain);
249         free(router_name);
250
251         if( len >= sizeof( target_buf ) ) {
252                 osrfLogWarning( OSRF_LOG_MARK, "Buffer overflow for remote_id");
253                 free( session );
254                 return NULL;
255         }
256
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;
263
264         #ifdef ASSUME_STATELESS
265         session->stateless = 1;
266         osrfLogDebug( OSRF_LOG_MARK, "%s session is stateless", remote_service );
267         #else
268         session->stateless = 0;
269         osrfLogDebug( OSRF_LOG_MARK, "%s session is NOT stateless", remote_service );
270         #endif
271
272         /* build a chunky, random session id */
273         char id[256];
274         memset(id,0,256);
275
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 );
280
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 );
286         return session;
287 }
288
289 osrf_app_session* osrf_app_server_session_init( 
290                 char* session_id, char* our_app, char* remote_id ) {
291
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 );
294
295         osrf_app_session* session = osrf_app_session_find_session( session_id );
296         if(session) return session;
297
298         session = safe_malloc(sizeof(osrf_app_session));        
299
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 );
303                 return NULL;
304         }
305
306         int stateless = 0;
307         char* statel = osrf_settings_host_value("/apps/%s/stateless", our_app );
308         if(statel) stateless = atoi(statel);
309         free(statel);
310
311
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;
319
320         #ifdef ASSUME_STATELESS
321         session->stateless = 1;
322         #endif
323
324         session->thread_trace = 0;
325         session->state = OSRF_SESSION_DISCONNECTED;
326         session->type = OSRF_SESSION_SERVER;
327
328         _osrf_app_session_push_session( session );
329         return session;
330
331 }
332
333
334
335 /** frees memory held by a session */
336 void _osrf_app_session_free( osrf_app_session* session ){
337         if(session==NULL)
338                 return;
339
340         if( session->userDataFree && session->userData ) 
341                 session->userDataFree(session->userData);
342         
343         if(session->session_locale)
344                 free(session->session_locale);
345
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);
351         free(session);
352 }
353
354 int osrfAppSessionMakeRequest(
355                 osrf_app_session* session, jsonObject* params, 
356                 char* method_name, int protocol, string_array* param_strings ) {
357
358         return osrf_app_session_make_locale_req( session, params, 
359                         method_name, protocol, param_strings, NULL );
360 }
361
362 int osrfAppSessionMakeLocaleRequest(
363                 osrf_app_session* session, jsonObject* params, 
364                 char* method_name, int protocol, string_array* param_strings, char* locale ) {
365
366         return osrf_app_session_make_locale_req( session, params, 
367                         method_name, protocol, param_strings, locale );
368 }
369
370 int osrf_app_session_make_req( 
371                 osrf_app_session* session, jsonObject* params, 
372                 char* method_name, int protocol, string_array* param_strings) {
373
374         return osrf_app_session_make_locale_req(session, params,
375                         method_name, protocol, param_strings, NULL);
376 }
377
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;
382
383         osrfLogMkXid();
384
385         osrf_message* req_msg = osrf_message_init( REQUEST, ++(session->thread_trace), protocol );
386         osrf_message_set_method(req_msg, method_name);
387
388         if (locale) {
389                 osrf_message_set_locale(req_msg, locale);
390         } else if (session->session_locale) {
391                 osrf_message_set_locale(req_msg, session->session_locale);
392         }
393
394         if(params) {
395                 osrf_message_set_params(req_msg, params);
396
397         } else {
398
399                 if(param_strings) {
400                         int i;
401                         for(i = 0; i!= param_strings->size ; i++ ) {
402                                 osrf_message_add_param(req_msg,
403                                         string_array_get_string(param_strings,i));
404                         }
405                 }
406         }
407
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 );
411                 return -1;
412         }
413
414         osrfLogDebug( OSRF_LOG_MARK,  "Pushing [%d] onto requeust 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;
418 }
419
420 void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) {
421         if(session == NULL)
422                 return;
423
424         osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id );
425         if(req) req->complete = 1;
426 }
427
428 int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) {
429         if(session == NULL)
430                 return 0;
431         osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id );
432         if(req)
433                 return req->complete;
434         return 0;
435 }
436
437
438 /** Resets the remote connection id to that of the original*/
439 void osrf_app_session_reset_remote( osrf_app_session* session ){
440         if( session==NULL )
441                 return;
442
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 );
446
447         session->remote_id = strdup(session->orig_remote_id);
448 }
449
450 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) {
451         if(session == NULL)
452                 return;
453         if( session->remote_id )
454                 free(session->remote_id );
455         session->remote_id = strdup( remote_id );
456 }
457
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;
463
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 );
467
468         return 0;
469 }
470
471 int osrfAppSessionConnect( osrf_app_session* session ) { 
472         return osrf_app_session_connect(session);
473 }
474
475
476 /** Attempts to connect to the remote service */
477 int osrf_app_session_connect(osrf_app_session* session){
478         
479         if(session == NULL)
480                 return 0;
481
482         if(session->state == OSRF_SESSION_CONNECTED) {
483                 return 1;
484         }
485
486         int timeout = 5; /* XXX CONFIG VALUE */
487
488         osrfLogDebug( OSRF_LOG_MARK,  "AppSession connecting to %s", session->remote_id );
489
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);
496         if(ret) return 0;
497
498         time_t start = time(NULL);      
499         time_t remaining = (time_t) timeout;
500
501         while( session->state != OSRF_SESSION_CONNECTED && remaining >= 0 ) {
502                 osrf_app_session_queue_wait( session, remaining, NULL );
503                 remaining -= (int) (time(NULL) - start);
504         }
505
506         if(session->state == OSRF_SESSION_CONNECTED)
507                 osrfLogDebug( OSRF_LOG_MARK, " * Connected Successfully to %s", session->remote_service );
508
509         if(session->state != OSRF_SESSION_CONNECTED)
510                 return 0;
511
512         return 1;
513 }
514
515
516
517 /** Disconnects from the remote service */
518 int osrf_app_session_disconnect( osrf_app_session* session){
519         if(session == NULL)
520                 return 1;
521
522         if(session->state == OSRF_SESSION_DISCONNECTED)
523                 return 1;
524
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);
529                 return 1;
530         }
531
532         osrfLogDebug(OSRF_LOG_MARK,  "AppSession disconnecting from %s", session->remote_id );
533
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;
537
538         osrf_message_free( dis_msg );
539         osrf_app_session_reset_remote( session );
540         return 1;
541 }
542
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 );
546 }
547
548
549 int osrfAppSessionSendBatch( osrfAppSession* session, osrf_message* msgs[], int size ) {
550
551         if( !(session && msgs && size > 0) ) return 0;
552         int retval = 0;
553
554         osrfMessage* msg = msgs[0];
555
556         if(msg) {
557
558                 osrf_app_session_queue_wait( session, 0, NULL );
559
560                 if(session->state != OSRF_SESSION_CONNECTED)  {
561
562                         if(session->stateless) { /* stateless session always send to the root listener */
563                                 osrf_app_session_reset_remote(session);
564
565                         } else { 
566
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) ) {
572
573                                         if(!osrf_app_session_connect( session )) 
574                                                 return 0;
575                                 }
576                         }
577                 }
578         }
579
580         char* string = osrfMessageSerializeBatch(msgs, size);
581
582         if( string ) {
583
584                 transport_message* t_msg = message_init( 
585                                 string, "", session->session_id, session->remote_id, NULL );
586       message_set_osrf_xid( t_msg, osrfLogGetXid() );
587
588                 retval = client_send_message( session->transport_handle, t_msg );
589
590                 if( retval ) osrfLogError(OSRF_LOG_MARK, "client_send_message failed");
591
592                 osrfLogInfo(OSRF_LOG_MARK, "[%s] sent %d bytes of data to %s",
593                         session->remote_service, strlen(string), t_msg->recipient );
594
595                 osrfLogDebug(OSRF_LOG_MARK, "Sent: %s", string );
596
597                 free(string);
598                 message_free( t_msg );
599         }
600
601         return retval; 
602 }
603
604
605
606 int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){
607         if( !(session && msg) ) return 0;
608         osrfMessage* a[1];
609         a[0] = msg;
610         return osrfAppSessionSendBatch( session, a, 1 );
611 }
612
613
614
615
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.
620   */
621 int osrf_app_session_queue_wait( osrf_app_session* session, int timeout, int* recvd ){
622         if(session == NULL) return 0;
623         int ret_val = 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);
626         return ret_val;
627 }
628
629 /** Disconnects (if client) and removes the given session from the global session cache 
630   * ! This free's all attached app_requests ! 
631   */
632 void osrfAppSessionFree( osrfAppSession* ses ) {
633         osrf_app_session_destroy( ses );
634 }
635
636
637 void osrf_app_session_destroy( osrf_app_session* session ){
638         if(session == NULL) return;
639
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);
647         }
648
649         osrfHashRemove( osrfAppSessionCache, session->session_id );
650         _osrf_app_session_free( session );
651 }
652
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 );
656 }
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)
660                 return NULL;
661         osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id );
662         return _osrf_app_request_recv( req, timeout );
663 }
664
665
666
667 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data ) {
668         if(!ses || ! data ) return -1;
669
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 );
673
674         osrf_message_set_result_content( msg, json );
675         _osrf_app_session_send( ses, msg ); 
676
677         free(json);
678         osrf_message_free( msg );
679
680         return 0;
681 }
682
683
684 int osrfAppRequestRespondComplete( 
685                 osrfAppSession* ses, int requestId, jsonObject* data ) {
686
687         osrf_message* payload = osrf_message_init( RESULT, requestId, 1 );
688         osrf_message_set_status_info( payload, NULL, "OK", OSRF_STATUS_OK );
689
690         osrf_message* status = osrf_message_init( STATUS, requestId, 1);
691         osrf_message_set_status_info( status, "osrfConnectStatus", "Request Complete", OSRF_STATUS_COMPLETE );
692         
693         if (data) {
694                 char* json = jsonObjectToJSON( data );
695                 osrf_message_set_result_content( payload, json );
696                 free(json);
697
698                 osrfMessage* ms[2];
699                 ms[0] = payload;
700                 ms[1] = status;
701
702                 osrfAppSessionSendBatch( ses, ms, 2 );
703
704                 osrf_message_free( payload );
705         } else {
706                 osrfAppSessionSendBatch( ses, &status, 1 );
707         }
708
709         osrf_message_free( status );
710
711         return 0;
712 }
713
714 int osrfAppSessionStatus( osrfAppSession* ses, int type, char* name, int reqId, char* message ) {
715
716         if(ses) {
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 );
721                 return 0;
722         }
723         return -1;
724 }
725
726
727
728
729
730