gs_wait.cpp

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*    gs_wait.cpp                                                             */
00004 /*    source code that implements the function of                             */
00005 /*    waiting for a GridSolve job to complete                                 */
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_wait, args, nargout, "Octave client for GridSolve") {
00023     octave_value_list retvals;      //return value
00024     octave_value_list bad_retval;   //bad return value
00025     grpc_function_handle_t *handle; //GridRPC function handle
00026     grpc_error_t status;            //GridRPC status/error code
00027     grpc_sessionid_t req_id;        //GridRPC non-blocking call session id
00028 
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     //number of input arguments
00037     int nargin = args.length();
00038 
00039     if (nargin <= 0) {
00040         fprintf(stderr, "No input service name found. Aborted.\n");
00041         return bad_retval;
00042     }
00043 
00044     if (nargin > 1) {
00045         fprintf(stderr, "Exactly one argument (session id) is expected. Aborted.\n");
00046         return bad_retval;
00047     }
00048 
00049     //otherwise, the input is correct
00050 
00051     //initialize the GridSolve environment
00052     status = grpc_initialize(NULL);
00053     if (status != GRPC_NO_ERROR && status != GRPC_ALREADY_INITIALIZED) {
00054         fprintf(stderr, "%s\n", grpc_error_string(status));
00055         return bad_retval;
00056     }
00057         
00058     req_id = args(0).int_value();
00059 
00060     //get the GRPC handle
00061     status = grpc_get_handle(&handle, req_id);
00062     if (status != GRPC_NO_ERROR) {
00063         fprintf(stderr, "Failed to get the GRPC handle. Aborted.\n");
00064         return bad_retval;
00065     }
00066 
00067     status = grpc_wait(req_id);
00068     if (status != GRPC_NO_ERROR) {
00069         fprintf(stderr, "%s\n", grpc_error_string(status));
00070         return bad_retval;
00071     }
00072 
00073     //the job completed
00074 
00075     retvals = octave_value_list();
00076 
00077     //convert object back
00078     if (convert_output_objects(handle->problem_desc, retvals) < 0) {
00079         fprintf(stderr, "Failed to convert output arguments. Aborted.\n");
00080         grpc_function_handle_destruct(handle);
00081         if (handle != NULL) free(handle);
00082         grpc_finalize();
00083         return bad_retval;
00084     }
00085 
00086     //finalize and clean up
00087     grpc_function_handle_destruct(handle);
00088     if (handle != NULL) free(handle);
00089     grpc_finalize();
00090 
00091     return retvals;
00092 }