idl_request.c

Go to the documentation of this file.
00001 
00007 /* $Id: idl_request.c,v 1.5 2007/03/01 22:52:31 seymour Exp $ */
00008 /* $UTK_Copyright: $ */
00009 
00010 #include <stdio.h>
00011 #include "idl_request.h"
00012 #include "translate.h"
00013 
00014 GS_IDL_Request requests[MAX_GRPC_REQUESTS];
00015 
00020 void
00021 gs_init_req()
00022 {
00023   int i;
00024 
00025   for(i=0;i<MAX_GRPC_REQUESTS;i++) {
00026     requests[i].gs_req_id = GS_IDL_NULL_ID;
00027     requests[i].argc = 0;
00028     requests[i].pd = NULL;
00029     requests[i].argv_idl = NULL; 
00030   }
00031 }
00032 
00041 void 
00042 gs_requests(int argc, IDL_VPTR* argv_idl) {
00043   int i;
00044 
00045   if(argc == 0) {
00046     for(i=0;i<MAX_GRPC_REQUESTS;i++) {
00047       if(requests[i].gs_req_id != GS_IDL_NULL_ID) {
00048         if(requests[i].pd)
00049           printf("%d: %s\n", i, requests[i].pd->name);
00050         else
00051           printf("%d: Error: null problem description.\n", i);
00052       }
00053     }
00054   }
00055   else {
00056     IDL_ENSURE_SCALAR(argv_idl[0]);
00057 
00058     i = argv_idl[0]->value.l;
00059 
00060     if((i < 0) || (i >= MAX_GRPC_REQUESTS)) {
00061       IDL_Message(IDL_M_GENERIC,IDL_MSG_INFO, "Error: bad request id\n");
00062       return;
00063     }
00064 
00065     if(requests[i].gs_req_id != GS_IDL_NULL_ID) {
00066       if(requests[i].pd)
00067         printf("%d: %s\n", i, requests[i].pd->name);
00068       else
00069         printf("%d: Error: null problem description.\n", i);
00070     }
00071     else 
00072       printf("%d: no request using this id\n", i);
00073   }
00074 
00075   return;
00076 }
00077 
00091 IDL_VPTR *
00092 argv_idl_dup(IDL_VPTR* argv_idl, int num) {
00093   int        i;
00094   IDL_VPTR*  argv_ret;
00095   IDL_VPTR   arg;
00096 
00097   argv_ret = (IDL_VPTR*) malloc(sizeof(IDL_VPTR)*num);
00098 
00099   if(!argv_ret)
00100     return NULL;
00101 
00102   for(i = 0; i < num; i++) {
00103     arg = argv_idl[i];
00104     /* clone if it is a temp var */
00105     if((arg->flags & IDL_V_TEMP) || (arg->flags & IDL_V_CONST)) {
00106       argv_ret[i] = (IDL_VPTR) malloc(sizeof(IDL_VARIABLE));
00107 
00108       if(!argv_ret[i])
00109         return NULL;
00110 
00111       *argv_ret[i] = *argv_idl[i];
00112 
00113       if((arg->flags & IDL_V_ARR) != 0) {
00114     argv_ret[i]->value.arr    = (IDL_ARRAY*) malloc(sizeof(IDL_ARRAY));
00115         if(!argv_ret[i]->value.arr)
00116           return NULL;
00117 
00118     *(argv_ret[i]->value.arr) = *(argv_idl[i]->value.arr);
00119       }      
00120     } else {
00121       argv_ret[i] = arg; /* keep the original non-tmp var */
00122     }
00123 
00124   }
00125 
00126   return argv_ret;
00127 }
00128 
00136 void
00137 argv_idl_free(IDL_VPTR* argv_idl, int num) {
00138   int i;
00139 
00140   for (i = 0; i < num; i++) {
00141     if((argv_idl[i]->flags & IDL_V_TEMP) || (argv_idl[i]->flags & IDL_V_CONST)) {
00142       /* malloc happened only when temp idl_vptr was used */
00143       if ((argv_idl[i]->flags & IDL_V_ARR) != 0) {
00144     free(argv_idl[i]->value.arr);
00145       }
00146       free(argv_idl[i]);
00147     }
00148   }
00149 
00150   free(argv_idl);
00151 }
00152 
00167 int
00168 add_req(grpc_sessionid_t id, gs_problem_t* pd, int argc, 
00169         IDL_VPTR* argv_idl)
00170 {
00171   if((id < 0) || (id >= MAX_GRPC_REQUESTS))
00172     return -1;
00173 
00174   requests[id].gs_req_id = id;
00175   requests[id].pd        = pd;
00176   requests[id].argc      = argc;
00177   requests[id].argv_idl  = argv_idl_dup(argv_idl, argc);
00178 
00179   if(!requests[id].argv_idl)
00180     return -1;
00181 
00182   return 0;
00183 }
00184 
00196 int
00197 del_req(grpc_sessionid_t id)
00198 {
00199   if((id < 0) || (id >= MAX_GRPC_REQUESTS))
00200     return -1;
00201 
00202   /* post processing */
00203   postproc_argv_c(requests[id].pd, requests[id].argv_idl);
00204 
00205   /* free those cloned argv_idl */
00206   argv_idl_free(requests[id].argv_idl, requests[id].argc);
00207 
00208   requests[id].gs_req_id = GS_IDL_NULL_ID;
00209   requests[id].pd = NULL;
00210   requests[id].argv_idl = NULL;
00211 
00212   return 0;
00213 }