gs_call_async.cpp

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*    gs_call_async.cpp                                                       */
00004 /*    source file that implements non-blocking                                */
00005 /*    call to GridSolve system from Octave                                    */
00006 /*                                                                            */
00007 /*    Copyright (C)                                                           */
00008 /*    Innovative Computing Laboratory (ICL)                                   */
00009 /*    Univesity of Tennessee, Knoxville                                       */
00010 /*                                                                            */
00011 /*    Feburary 22, 2007                                                       */
00012 /******************************************************************************/
00013 
00014 
00015 #include <oct.h>
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include "grpc.h"
00019 #include "gs_oct.h"
00020 
00021 
00022 DEFUN_DLD (gs_call_async, args, nargout, "Octave client for GridSolve") {
00023     octave_value_list retvals;      //return value
00024     octave_value_list bad_retval;   //bad return value
00025     gs_problem_t* pd;               //GridSolve problem description
00026     grpc_arg_stack* argstack;       //GridRPC argument stack
00027     gs_argument_t* argp;            //GridRPC argument pointer
00028     grpc_function_handle_t *handle; //GridRPC function handle
00029     grpc_error_t status;            //GridRPC status/error code
00030     grpc_sessionid_t req_id;        //GridRPC non-blocking call session id
00031     char *info[1];
00032 
00033 
00034     //meaningless return value for error condition
00035     //or those calls that do not make grpc function call
00036     for (int i = 0; i < nargout; i++) {
00037         bad_retval(i) = Matrix(0, 0);
00038     }
00039 
00040     //initialize the GridSolve environment
00041     status = grpc_initialize(NULL);
00042     if (status != GRPC_NO_ERROR && status != GRPC_ALREADY_INITIALIZED) {
00043         fprintf(stderr, "%s\n", grpc_error_string(status));
00044         return bad_retval;
00045     }
00046 
00047     //number of input arguments
00048     int nargin = args.length();
00049 
00050     //invalid first argument
00051     if (nargin >= 1 && !args(0).is_string()) {
00052         fprintf(stderr, "Invalid first parameter: should be a string\n");
00053         grpc_finalize();
00054         return bad_retval;
00055     }
00056 
00057     //do a grpc call to a remote service
00058     if (nargin >= 1) {
00059         int strlen = args(0).string_value().length();
00060 
00061         //get the problem name
00062         char *pname = new char[strlen + 1];
00063         for (int i = 0; i < strlen; i++)
00064             pname[i] = (args(0).string_value())[i];
00065         pname[strlen] = '\0';
00066 
00067         //initialize the GridRPC function handle
00068         handle = (grpc_function_handle_t *) malloc(sizeof(grpc_function_handle_t));
00069         if (handle == NULL) {
00070             delete pname;
00071             grpc_finalize();
00072             return bad_retval;
00073         }
00074 
00075         status = grpc_function_handle_default(handle, pname);
00076         if (status != GRPC_NO_ERROR) {
00077             fprintf(stderr, "%s\n", grpc_error_string(status));
00078             printf("%s\n", pname);
00079             delete pname;
00080             grpc_function_handle_destruct(handle);
00081             if (handle != NULL) free(handle);
00082             grpc_finalize();
00083             return bad_retval;
00084         }
00085 
00086         //get the GridSolve problem description
00087         pd = handle->problem_desc;
00088 
00089         if (pd == NULL) {
00090             fprintf(stderr, "Empty GridSolve problem description. Aborted.\n");
00091             delete pname;
00092             grpc_function_handle_destruct(handle);
00093             if (handle != NULL) free(handle);
00094             grpc_finalize();
00095             return bad_retval;
00096         }
00097 
00098         //convert object types before make the call
00099         if (convert_octave_arguments(pd, args) < 0) {
00100             fprintf(stderr, "Failure to convert input arguments. Aborted.\n");
00101             delete pname;
00102             grpc_function_handle_destruct(handle);
00103             if (handle != NULL) free(handle);
00104             grpc_finalize();
00105             return bad_retval;
00106         }
00107 
00108         int arg_stack_size = 0;
00109         //compute the argument stack size
00110         for (argp = pd->arglist; argp != NULL; argp = argp->next) {
00111             if (argp->inout != GS_WORKSPACE) arg_stack_size++;
00112         }
00113 
00114         //create a new argument stack for the grpc call
00115         if ((argstack = grpc_arg_stack_new(arg_stack_size)) == NULL) {
00116             fprintf(stderr, "Failed to create argument stack. Aborted.\n");
00117             delete pname;
00118             grpc_function_handle_destruct(handle);
00119             if (handle != NULL) free(handle);
00120             grpc_arg_stack_destruct(argstack);
00121             if (argstack != NULL) free(argstack);
00122             grpc_finalize();
00123             return bad_retval;
00124         }
00125 
00126         //push the converted arguments onto the stack
00127         for (argp = pd->arglist; argp != NULL; argp = argp->next) {
00128             if (argp->inout != GS_WORKSPACE) {
00129                 if (grpc_arg_stack_push_arg(argstack, argp->data) < 0) {
00130                     fprintf(stderr, "Error in pushing argument stack. Aborted\n");
00131                     delete pname;
00132                     grpc_function_handle_destruct(handle);
00133                     if (handle != NULL) free(handle);
00134                     grpc_arg_stack_destruct(argstack);
00135                     if (argstack != NULL) free(argstack);
00136                     grpc_finalize();
00137                     return bad_retval;
00138                 }
00139             }
00140         }
00141 
00142         //printf("Blocking GridRPC call starts...\n");
00143 
00144         grpc_set_default_major("C");
00145         grpc_set_client_major("C");
00146 
00147         //calling the remote service
00148         status = grpc_call_arg_stack_async(handle, &req_id, argstack);
00149         if (status == GRPC_NO_ERROR) {
00150             //printf("Non-blocking GridRPC call succeeded.\n");
00151         } else {
00152             fprintf(stderr, "%s\n", grpc_error_string(status));
00153             delete pname;
00154             grpc_function_handle_destruct(handle);
00155             if (handle != NULL) free(handle);
00156             grpc_arg_stack_destruct(argstack);
00157             if (argstack != NULL) free(argstack);
00158             grpc_finalize();
00159             return bad_retval;
00160         }
00161 
00162         //return the session id
00163         return octave_value(req_id);
00164     }
00165 }
00166 
00167 
00168 
00169 
00170