home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / external / rpc / idl_rpc_obsolete.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-08  |  11.2 KB  |  468 lines

  1. /*
  2.  *  $Id: idl_rpc_obsolete.c,v 1.2 1997/01/29 23:24:33 kirk Exp $
  3.  */
  4. /*
  5.   Copyright (c) 1992-1997, Research Systems Inc.  All rights reserved.
  6.   This software includes information which is proprietary to and a
  7.   trade secret of Research Systems, Inc.  It is not to be disclosed
  8.   to anyone outside of this organization. Reproduction by any means
  9.   whatsoever is  prohibited without express written permission.
  10.   */
  11.  
  12. /*
  13.  * This file contains routines that are used to emulate the obsolete 
  14.  * method of calling IDL rpc's.
  15.  *
  16.  * Note: All of the old rpc functionality is available to the user
  17.  *       except the ability to place the rpc server into *interactive*
  18.  *       mode. 
  19.  */
  20.  
  21. #include <string.h>
  22.  
  23. #include "idl_rpc.h"
  24. #include "idl_rpc_obsolete.h"
  25.  
  26.  
  27. /***************************************************************************
  28.  * free_idl_var()
  29.  * 
  30.  * Purpose:
  31.  *        This routine will free all dynamic memory associated with
  32.  *        a variable.
  33.  */
  34. void free_idl_var(varinfo_t * var)
  35. {
  36.    if(var->Variable == (IDL_VPTR)NULL || var->Length != 0)
  37.       return;
  38. /*
  39.  * If length equals 0, then this should be a dynamic variable
  40.  */
  41.    IDL_RPCDeltmp(var->Variable); /* will free all memory */
  42.    var->Variable = (IDL_VPTR)NULL;
  43. }
  44. /***************************************************************************
  45.  * get_idl_variable()
  46.  *
  47.  * Purpose:
  48.  *     This function is used to retrieve the value of the variable name
  49.  *     given. The routine also provides the ability to have the value 
  50.  *     returned converted to a desired type.
  51.  */
  52. int get_idl_variable(CLIENT *client, char *name, varinfo_t *var,
  53.              int typecode)
  54. {
  55.     int  status;        /* holds return status values */
  56.     char *funcName;        /* the name of the conversion name */
  57.     char buffer[248];        /* string buffer for commands */
  58.  
  59. /*
  60.  *  Check the input parameters
  61.  */
  62.     if(name == (char*)NULL || var == (varinfo_t*)NULL)
  63.        return -1;
  64. /*
  65.  *  If there is no conversion desired, just get the variable
  66.  */
  67.     if(typecode <= 0){
  68.        var->Variable = IDL_RPCGetMainVariable(client, name);
  69.        if(var->Variable == (IDL_VPTR)NULL){
  70.           return 0;
  71.        }
  72.     }else{            /* perform conversion */
  73.     /*
  74.      * what is the function name needed for the conversion?
  75.      */       
  76.        switch(typecode){
  77.        case IDL_TYP_BYTE:
  78.      funcName="byte";
  79.      break;
  80.        case IDL_TYP_INT:
  81.      funcName="fix";
  82.      break;
  83.        case IDL_TYP_LONG:
  84.      funcName="long";
  85.      break;
  86.        case IDL_TYP_FLOAT:
  87.      funcName="float";
  88.      break;
  89.        case IDL_TYP_DOUBLE:
  90.      funcName="double";
  91.      break;
  92.        case IDL_TYP_STRING:
  93.      funcName="string";
  94.      break;
  95.        case IDL_TYP_COMPLEX:
  96.      funcName="complex";
  97.      break;
  98.        case IDL_TYP_DCOMPLEX:
  99.      funcName="dcomplex";
  100.      break;
  101.        default:
  102.      funcName="long";
  103.      break;
  104.        }
  105.     /* 
  106.      * build a command line to do the conversion
  107.      */
  108.        sprintf(buffer, "if(n_elements(%s) gt 0)then %s=%s(%s)", 
  109.            name, IDL_RPC_CON_VAR, funcName, name);
  110.        status = IDL_RPCExecuteStr(client, buffer); /* perform conversion */
  111.        if(status != 0){
  112.      return 0;
  113.        }
  114.        var->Variable = IDL_RPCGetMainVariable(client, IDL_RPC_CON_VAR);
  115.        if(var->Variable == (IDL_VPTR)NULL){
  116.           return 0;
  117.        }
  118.     }
  119.     strncpy(var->Name, name, MAXIDLEN);    /* copy in the name  */
  120. /* 
  121.  *  Set the conversion varialbe to 0
  122.  */
  123.     sprintf(buffer, "%s = 0", IDL_RPC_CON_VAR);
  124.     status = IDL_RPCExecuteStr(client, buffer);
  125.  
  126.     return 1;
  127. }
  128. /***************************************************************************
  129.  * kill_server()
  130.  *
  131.  * This function is used to kill the IDL server
  132.  */
  133. int kill_server(CLIENT *client)
  134. {
  135.     return IDL_RPCCleanup(client, 1);    /* pretty easy */
  136. }
  137. /***************************************************************************
  138.  * register_idl_client()
  139.  *
  140.  * Used to register with the IDL server.
  141.  */
  142. CLIENT * register_idl_client(IDL_LONG server_id, char* hostname, 
  143.                  struct timeval* timeout)
  144. {
  145.   CLIENT *pClient;
  146.  
  147. /*
  148.  * Initialize the IDL RPC session
  149.  */   
  150.     pClient = IDL_RPCInit(server_id, hostname);
  151.  
  152. /*
  153.  * If we have a client struct and the time value looks sane, set the 
  154.  * timeout.
  155.  */ 
  156.    if(pClient != (CLIENT*)NULL && timeout->tv_sec > 0) 
  157.       (void)IDL_RPCTimeout( timeout->tv_sec);
  158.  
  159.    return pClient;
  160. }
  161. /***************************************************************************
  162.  * send_idl_command()
  163.  *
  164.  * Used to send a command string to IDL
  165.  */
  166. int send_idl_command(CLIENT *client, char *command)
  167. {
  168.    if(command == (char*)NULL || *command == (char)NULL)
  169.       return 0;
  170.  
  171.    return (IDL_RPCExecuteStr(client, command) == 0 ? 1 : -1); 
  172. }
  173. /***************************************************************************
  174.  * set_idl_timeout()
  175.  *
  176.  * Sets the rpc timeout. Only the second value is used.
  177.  */
  178. int set_idl_timout( struct timeval * timeout)
  179. {
  180.    return IDL_RPCTimeout(timeout->tv_sec); /* only use seconds with new API */
  181. }
  182. /***************************************************************************
  183.  * set_idl_variable()
  184.  *
  185.  * Used to set a variable at the main level of IDL.
  186.  */
  187. int set_idl_variable(CLIENT *client, varinfo_t * var)
  188. {
  189. /*
  190.  * Just call IDL_RPCSetMainVariable
  191.  */
  192.   return (IDL_RPCSetMainVariable(client, var->Name, var->Variable) 
  193.       == 0 ? 1 : 0 ); 
  194. }
  195. /***************************************************************************
  196.  * unregister_idl_client()
  197.  *
  198.  * Unregisters the IDL client, but doesnt kill the RPC server
  199.  */
  200. void unregister_idl_client(CLIENT *client)
  201. {
  202.    IDL_RPCCleanup(client, 0);
  203. }
  204. /***************************************************************************
  205.  * Now for the "helper" functions
  206.  *
  207.  */
  208. int v_make_byte( varinfo_t *var, char *name, unsigned int c)
  209. {
  210.    IDL_ALLTYPES   type_s;
  211.  
  212. /*
  213.  * Fill in the varinfo_t struct
  214.  */
  215.    strncpy( var->Name, name, MAXIDLEN );
  216.    var->Name[ MAXIDLEN ] = 0;
  217.  
  218. /*
  219.  * Get a temporary variable if we need it
  220.  */
  221.    if(var->Variable == (IDL_VPTR)NULL){
  222.       var->Variable = IDL_RPCGettmp();
  223.       if(var->Variable == (IDL_VPTR)NULL)
  224.      return 0;
  225.    }
  226. /*
  227.  * Store the scalar using the client side API.
  228.  */
  229.    type_s.c  = (UCHAR)c;    
  230.    IDL_RPCStoreScalar( var->Variable, IDL_TYP_BYTE, type_s);
  231.    return 1;
  232. }
  233. /***************************************************************************
  234.  * v_make_int()
  235.  *
  236.  */
  237. int v_make_int( varinfo_t *var, char *name, int value)
  238. {
  239.    IDL_ALLTYPES   type_s;
  240.  
  241. /*
  242.  * fill in the varinfo_t struct
  243.  */
  244.    strncpy( var->Name, name, MAXIDLEN );
  245.    var->Name[ MAXIDLEN ] = 0;
  246. /*
  247.  * Get a vptr if we need one.
  248.  */
  249.    if(var->Variable == (IDL_VPTR)NULL){
  250.       var->Variable = IDL_RPCGettmp();
  251.       if(var->Variable == (IDL_VPTR)NULL)
  252.      return 0;
  253.    }
  254. /*
  255.  * Store the value.
  256.  */
  257.    type_s.i  = value;
  258.    IDL_RPCStoreScalar( var->Variable, IDL_TYP_INT, type_s);
  259.    return 1;
  260. }
  261. /***************************************************************************
  262.  * v_make_long()
  263.  *
  264.  */
  265. int v_make_long( varinfo_t *var, char *name, IDL_LONG value)
  266. {
  267.    IDL_ALLTYPES   type_s;
  268.  
  269. /*
  270.  * fill in the varinfo_t struct
  271.  */
  272.    strncpy( var->Name, name, MAXIDLEN );
  273.    var->Name[ MAXIDLEN ] = 0;
  274. /*
  275.  * get a vptr if we need one
  276.  */
  277.    if(var->Variable == (IDL_VPTR)NULL){
  278.       var->Variable = IDL_RPCGettmp();
  279.       if(var->Variable == (IDL_VPTR)NULL)
  280.      return 0;
  281.    }
  282. /*
  283.  * Store the value.
  284.  */
  285.    type_s.l  = value;
  286.    IDL_RPCStoreScalar( var->Variable, IDL_TYP_LONG, type_s);
  287.    return 1;
  288. }
  289. /***************************************************************************
  290.  * v_make_float()
  291.  *
  292.  */
  293. int v_make_float( varinfo_t *var, char *name, double value)
  294. {
  295.    IDL_ALLTYPES   type_s;
  296.  
  297. /*
  298.  * fill in the varinfo_t struct.
  299.  */
  300.    strncpy( var->Name, name, MAXIDLEN );
  301.    var->Name[ MAXIDLEN ] = 0;
  302. /*
  303.  * Get a vptr if needed 
  304.  */
  305.    if(var->Variable == (IDL_VPTR)NULL){
  306.       var->Variable = IDL_RPCGettmp();
  307.       if(var->Variable == (IDL_VPTR)NULL)
  308.      return 0;
  309.    }
  310. /*
  311.  * Store the value in the variable
  312.  */
  313.    type_s.f  = value;
  314.    IDL_RPCStoreScalar( var->Variable, IDL_TYP_FLOAT, type_s);
  315.    return 1;
  316. }
  317. /***************************************************************************
  318.  * v_make_double()
  319.  *
  320.  */
  321. int v_make_double( varinfo_t *var, char *name, double value)
  322. {
  323.    IDL_ALLTYPES   type_s;
  324.  
  325. /*
  326.  * fill in the varinfo_t struct
  327.  */
  328.    strncpy( var->Name, name, MAXIDLEN );
  329.    var->Name[ MAXIDLEN ] = 0;
  330. /*
  331.  * Get a variable if needed 
  332.  */
  333.    if(var->Variable == (IDL_VPTR)NULL){
  334.       var->Variable = IDL_RPCGettmp();
  335.       if(var->Variable == (IDL_VPTR)NULL)
  336.      return 0;
  337.    }
  338. /*
  339.  * Store the scalar
  340.  */
  341.    type_s.d  = value;
  342.    IDL_RPCStoreScalar( var->Variable, IDL_TYP_DOUBLE, type_s);
  343.    return 1;
  344. }
  345. /***************************************************************************
  346.  * v_make_complex()
  347.  *
  348.  */
  349. int v_make_complex( varinfo_t *var, char *name, double r, double i)
  350. {
  351.    IDL_ALLTYPES   type_s;
  352.  
  353. /*
  354.  * Fill in the varinfo_t struct
  355.  */
  356.    strncpy( var->Name, name, MAXIDLEN );
  357.    var->Name[ MAXIDLEN ] = 0;
  358. /*
  359.  * get a vptr if needed
  360.  */
  361.    if(var->Variable == (IDL_VPTR)NULL){
  362.       var->Variable = IDL_RPCGettmp();
  363.       if(var->Variable == (IDL_VPTR)NULL)
  364.      return 0;
  365.    }
  366. /* 
  367.  * Store the value
  368.  */
  369.    type_s.cmp.r  = (float)r;
  370.    type_s.cmp.i  = (float)i;
  371.    IDL_RPCStoreScalar( var->Variable, IDL_TYP_COMPLEX, type_s);
  372.    return 1;
  373. }
  374. /***************************************************************************
  375.  * v_make_dcomplex()
  376.  *
  377.  */
  378. int v_make_dcomplex( varinfo_t *var, char *name, double r, double i)
  379. {
  380.    IDL_ALLTYPES   type_s;
  381.  
  382. /* 
  383.  * Fill in the varinfo_t struct
  384.  */
  385.    strncpy( var->Name, name, MAXIDLEN );
  386.    var->Name[ MAXIDLEN ] = 0;
  387. /*
  388.  * Get a vptr if we need it
  389.  */
  390.    if(var->Variable == (IDL_VPTR)NULL){
  391.       var->Variable = IDL_RPCGettmp();
  392.       if(var->Variable == (IDL_VPTR)NULL)
  393.      return 0;
  394.    }
  395. /*
  396.  * Store the values in the vptr
  397.  */
  398.    type_s.dcmp.r = r;
  399.    type_s.dcmp.i = i;
  400.    IDL_RPCStoreScalar( var->Variable, IDL_TYP_DCOMPLEX, type_s);
  401.    return 1;
  402. }
  403. /***************************************************************************
  404.  * v_make_string()
  405.  *
  406.  */
  407. int v_make_string( varinfo_t *var, char *name, char * value)
  408. {
  409.    IDL_ALLTYPES   type_s;
  410.  
  411. /*
  412.  * Fill in the varinfo_t struct
  413.  */
  414.    strncpy( var->Name, name, MAXIDLEN );
  415.    var->Name[ MAXIDLEN ] = 0;
  416. /* 
  417.  * Get a vptr if needed
  418.  */
  419.    if(var->Variable == (IDL_VPTR)NULL){
  420.       var->Variable = IDL_RPCGettmp();
  421.       if(var->Variable == (IDL_VPTR)NULL)
  422.      return 0;
  423.    }
  424. /*
  425.  * Get an IDL string and then store the value in the vptr
  426.  */
  427.    IDL_RPCStrStore(&type_s.str, value);
  428.    IDL_RPCStoreScalar( var->Variable, IDL_TYP_STRING, type_s);
  429.    IDL_RPCStrDelete(&type_s.str, 1L);
  430.    return 1;
  431. }
  432. /***************************************************************************
  433.  * v_fill_array()
  434.  *
  435.  * Purpose:
  436.  *        This routine is used to create an array and optionally import
  437.  *      the users data. 
  438.  */
  439. int v_fill_array(varinfo_t *var, char *name, int type, 
  440.          int n_dim, IDL_LONG dims[], UCHAR *value, IDL_LONG length)
  441. {
  442.    IDL_VPTR vTmp;
  443.    char    *pData;
  444.  
  445. /*
  446.  * Delete the old variable
  447.  */
  448.    free_idl_var(var);
  449.    bzero((char*)var, sizeof(varinfo_t)); /* zero out the struct */
  450.    strncpy( var->Name, name, MAXIDLEN );
  451. /*
  452.  * Should we import an array or create one?
  453.  */
  454.    if(value != (UCHAR*)NULL)
  455.       vTmp = IDL_RPCImportArray( n_dim, dims, type, value, 0);
  456.    else
  457.       pData = IDL_RPCMakeArray(type, n_dim, dims, IDL_BARR_INI_ZERO,
  458.                    &vTmp);
  459.    if(vTmp == (IDL_VPTR)NULL)
  460.       return 0;  
  461.    var->Variable = vTmp;
  462.    return 1;
  463. }
  464.       
  465.          
  466.  
  467.  
  468.