gs_info.cpp

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*    gridsolve.cpp                                                           */
00004 /*    source file that implements the function of                             */
00005 /*    getting the arguments and calling sequence                              */
00006 /*    description of a GridSolve service                                      */
00007 /*                                                                            */
00008 /*    Copyright (C)                                                           */
00009 /*    Innovative Computing Laboratory (ICL)                                   */
00010 /*    Univesity of Tennessee, Knoxville                                       */
00011 /*                                                                            */
00012 /*    Feburary 22, 2007                                                       */
00013 /******************************************************************************/
00014 
00015 
00016 #include <oct.h>
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 #include "grpc.h"
00020 #include "gs_oct.h"
00021 
00022 
00023 DEFUN_DLD (gs_info, args, nargout, "Octave client for GridSolve") {
00024     octave_value_list bad_retval;   //bad return value
00025     gs_problem_t* pd;               //GridSolve problem description
00026     gs_argument_t* argp;            //GridRPC argument pointer
00027     grpc_function_handle_t *handle; //GridRPC function handle
00028     grpc_error_t status;            //GridRPC status/error code
00029 
00030         //meaningless return value for error condition
00031     //or those calls that do not make grpc function call
00032     for (int i = 0; i < nargout; i++) {
00033         bad_retval(i) = Matrix(0, 0);
00034     }
00035 
00036     //initialize the GridSolve environment
00037     status = grpc_initialize(NULL);
00038     if (status != GRPC_NO_ERROR && status != GRPC_ALREADY_INITIALIZED) {
00039         fprintf(stderr, "%s\n", grpc_error_string(status));
00040         return bad_retval;
00041     }
00042 
00043     //number of input arguments
00044     int nargin = args.length();
00045     
00046     //input error checking
00047     if (nargin <= 0) {
00048         fprintf(stderr, "No input service name found. Aborted.\n");
00049         return bad_retval;
00050     }
00051 
00052     if (nargin > 1) {
00053         fprintf(stderr, "Exactly one argument (service name) is expected. Aborted.\n");
00054         return bad_retval;
00055     }
00056 
00057     if (!args(0).is_string()) {
00058         fprintf(stderr, "Invalid first parameter: should be a string. Aborted.\n");
00059         grpc_finalize();
00060         return bad_retval;
00061     }
00062     
00063     //otherwise, the input is correct
00064 
00065     int strlen = args(0).string_value().length();
00066 
00067     //get the problem name
00068     char *pname = new char[strlen + 1];
00069     for (int i = 0; i < strlen; i++)
00070         pname[i] = (args(0).string_value())[i];
00071     pname[strlen] = '\0';
00072 
00073     //initialize the GridRPC function handle
00074     handle = (grpc_function_handle_t *) malloc(sizeof(grpc_function_handle_t));
00075     if (handle == NULL) {
00076         perror("malloc");
00077         delete pname;
00078         grpc_finalize();
00079         return bad_retval;
00080     }
00081 
00082     status = grpc_function_handle_default(handle, pname);
00083     if (status != GRPC_NO_ERROR) {
00084         fprintf(stderr, "%s\n", grpc_error_string(status));
00085         delete pname;
00086         grpc_function_handle_destruct(handle);
00087         if (handle != NULL) free(handle);
00088         grpc_finalize();
00089         return bad_retval;
00090     }
00091 
00092     //get the GridSolve problem description
00093     pd = handle->problem_desc;
00094 
00095     if (pd == NULL) {
00096         fprintf(stderr, "Empty GridSolve problem description. Aborted.\n");
00097         delete pname;
00098         grpc_function_handle_destruct(handle);
00099         if (handle != NULL) free(handle);
00100         grpc_finalize();
00101         return bad_retval;
00102     }
00103 
00104     //print out the problem description
00105     gs_problem_dump(pd);
00106     
00107     //generate the function call prototype
00108     printf("\nOctave call prototype:\n");
00109     printf("[");
00110     bool first_arg = true;
00111     //output arguments first (right hand side)
00112     for (argp = pd->arglist; argp != NULL; argp = argp->next) {
00113         if (argp->inout == GS_INOUT ||
00114             argp->inout == GS_OUT || 
00115             argp->inout == GS_VAROUT) {
00116             if (first_arg) {
00117                 printf("%s", argp->name);
00118                 first_arg = false;
00119             } else printf(", %s", argp->name);
00120         }
00121     }
00122     printf("] = gs_call(");
00123     first_arg = true;
00124     //and then input arguments (left hand side)
00125     for (argp = pd->arglist; argp != NULL; argp = argp->next) {
00126         if (argp->inout == GS_IN ||
00127             argp->inout == GS_INOUT) {
00128             if (first_arg) {
00129                 printf("%s", argp->name);
00130                 first_arg = false;
00131             } else printf(", %s", argp->name);
00132         }
00133     }
00134     printf(")\n\n");
00135     
00136     //finalize and clean up
00137     grpc_function_handle_destruct(handle);
00138     if (handle != NULL) free(handle);
00139     delete pname;
00140     grpc_finalize();
00141 }
00142