OpenTP1 Version 7 Programming Reference C Language

[Contents][Index][Back][Next]

7.5.2 Examples of Files

This subsection gives examples of the following files:

Organization of this subsection
(1) Example of an IDL file
(2) Example of a client program
(3) Example of a manager program
(4) Example of an ACF file
(5) Template example of a server program
(6) Template example of a user service definition
(7) Template example of an environment definition

(1) Example of an IDL file

The following shows an example of an IDL file.

  10   /*
  20    *  (1) Example of IDL file (sample.idl)
  30    */
  40   [
  50   uuid(f990a82a-10e5-11ce-9b02-0000870000ff),
  60   version(1.0),
  70   transaction_mandatory
  80   ]
  90   interface sample_ope
 100   {
 110     const  long  NAME_LENGTH = 20;
 115                   /* size of name field in record */
 120     const  long  AGE_LEN = 3;
 125                   /* size of age field in record */
 130     const  long  MAXRECORD = 10;
 135                   /* max number of records in database */
 140   
 150     /* struct info:                                   */
 155     /* record format of customer information database */
 160     typedef struct  info{
 170       char  name[NAME_LENGTH];  /* name (20 bytes) */
 180       char  sex;                /* sex (1 byte) */
 190       char  age[AGE_LEN];       /* age (3 bytes) */
 200       long  sale;               /* sales (4 bytes) */
 210     }info_t;
 220   
 230      error_status_t  getinfo
 240     (
 250       [in] unsigned   char  name[NAME_LENGTH],
 255                               /* input parameter */
 260       [out] info_t    *ptr    /* output parameter */
 270     );
 280   }
 290   /* EOF */

(2) Example of a client program

The following shows an example of a client program.

  10   /*
  20    *  (2) Example of a client program
  30    *  Note: dc_rpc_open(), dc_adm_complete(),
  40    *        and dc_rpc_close() are required for
  45    *        the ndce type;
  50    *        dc_clt_cltin(), dc_rpc_open(),
  55    *        dc_rpc_close(), and dc_clt_cltin()
  60    *        are required for the wdce type.
  70    *        For the header file to be included,
  75    *        use the TP1/Client library.
  80    *  clt.c
  90    *  Functions = main() 
 100    */
 110   
 120   #include <stdio.h>
 130   #include <dcrpc.h>
 140   #include <dctrp.h>
 150   #include <dcadm.h>
 160   #include <tx.h>
 170   #include "sample.h"
 180   
 190   /*
 200    *  Program Specification
 210    *  Build customer information database. 
 215    *  Allow actions noting the following.
 220    *   * Reference processing
 230    *     Refer to information using "name" as the key.
 240    *
 250    *  Customer information database
 260    *  *-------------------------------------------*
 270    *  | Name     | Sex    | Age  | Sales   |
 280    *  |------------------------------------|
 290    *  | Smith    | Male   | 30   | 10,000  |
 300    *  | Johnson  | Female | 23   | 15,000  |
 310    *  | Williams | Female | 26   |  8,000  |
 320    *  | Jones    | Male   | 24   | 10,000  |
 330    *  | Brown    | Male   | 35   | 18,000  |
 340    *  | Davis    | Male   | 20   |  3,000  |
 350    *  | Miller   | Female | 28   | 10,000  |
 360    *  | Wilson   | Female | 27   | 21,000  |
 370    *  | Moore    | Male   | 25   |  6,000  |
 380    *  | Taylor   | Male   | 24   | 11,000  |
 390    *  *--------------------------------------------*
 400    *
 410    *  This program requires service.
 420    *    <refer>  refer Taylor's information.
 430    *
 440    */
 450   /*
 460    *  name = main()
 470    *  func = Client program for sample_ope interface
 480    *       (1) service requirement (reference)
 490    *       (2) output result of service requirement
 500    *  arg = nothing
 510    *  return = void
 520    */
 530   
 540   int  main()
 550   {
 560     static unsigned char name[] = "Taylor";
 565                              /* input parameter */
 570     info_t out_data;         /* output parameter */
 580     error_status_t status;   /* return code for server */
 590     int     rc;              /* return code */
 600   
 610   /*
 620    *  Start UAP
 630    */
 640     rc = dc_rpc_open(DCNOFLAGS);
 650     /* error processing */
 660     if(rc != DC_OK){
 670       fprintf(stderr,"client:dc_rpc_open failed. "
 675                      "rc = %d\n",rc);
 680       goto END;
 690     }
 700   
 710     /*
 720     **  Post completion of user process start processing
 730     */
 740     rc = dc_adm_complete(DCNOFLAGS);
 750     /* error processing */
 760     if(rc != DC_OK){
 770       fprintf(stderr,"client:dc_adm_complete failed. "
 775                      "rc = %d\n",rc);
 780       goto END;
 790     }
 800   
 810   /*
 820    *  Begin  transaction
 830    */
 840   
 850     rc = tx_begin();
 860     /* error processing */
 870     if(rc != DC_OK){
 880       fprintf(stderr,"client:tx_begin failed. "
 885                      "rc = %d\n",rc);
 890       goto END;
 900     }
 910   
 920   /*
 930    *  getinfo:
 940    *  get information for input parameter
 950    */
 960     status = getinfo(name,&out_data);
 970     if(status != 0){
 980       fprintf(stderr,"client:getinfo "
 985                      "failed.rc = %d\n",status);
 990     }else{
1000       fprintf(stdout,"NAME: %s SEX: %c AGE: %s "
1005                      "SALE:%ld\n",
1010           out_data.name,
1020           out_data.sex,
1030           out_data.age,
1040           out_data.sale);
1050     }
1060   /*
1070    *  commit  transaction
1080    */
1090   
1100     rc = tx_commit();  
1110     /* error processing */
1120     if(rc != DC_OK){
1130       fprintf(stderr,"client:tx_commit failed. "
1135                      "rc = %d\n",rc);
1140       goto END;
1150     }
1160   /*
1170    *  Termination processing
1180    */
1190   END:
1200     dc_rpc_close(DCNOFLAGS);
1210     return(0);
1220   }

(3) Example of a manager program

The following shows an example of a manager program.

  10   /*
  20    *
  30    *    (3) Example of manager program
  40    *  sv.c
  50    *  Data Table = customers
  60    *  Functions = main()
  70    *        getinfo()
  80    */
  90   
 100   #include <stdio.h>
 110   #include <string.h>
 120   #include "sample.h"
 130   
 140   /*
 150    *  name   = customers
 160    *  func   = customer information database
 170    *  field  = name (20 bytes)
 180    *      sex (1 byte)
 190    *      age (3 bytes)
 200    *      sales (4 bytes)
 210    *  record  = 10 records (1 record = 28 bytes)
 220    */
 230   static info_t customers[MAXRECORD] = 
 240              { {"Smith",   'M',"30",10000},
 250                {"Johnson", 'F',"23",15000},
 260                {"Williams",'F',"26", 8000},
 270                {"Jones",   'M',"24",10000},
 280                {"Brown",   'M',"35",18000},
 290                {"Davis",   'M',"20", 3000},
 300                {"Miller",  'F',"28",10000},
 310                {"Wilson",  'F',"27",21000},
 320                {"Moore",   'M',"25", 6000},
 330                {"Taylor",  'M',"24",11000}
 340            };
 350   
 360   /*
 370    *  name = getinfo()
 380    *  func = Manager routine for sample_ope interface
 390    *    (1) search suitable record.
 400    *    (2) set found record to output parameter.
 410    *  arg =   name   :i: name
 420    *      out_data:o: information for input parameter
 430    *  return = result
 440    *      0 : success getinfo
 450    */
 460   
 470   error_status_t  getinfo(name,out_data)
 480   unsigned char  *name;
 490   info_t  *out_data;
 500   {
 510     int i;          /* counter of for loop */
 520     info_t *ptr;    /* pointer for search record */
 530   
 540   /* point 1st record of database(customers) */
 550     ptr = customers;
 560   
 570   /* search until record found with same name */
 575   /* or end of database                       */
 580     for (i = 0; i < MAXRECORD; i++, ptr++) {
 590       /* compare name */
 600       if(strcmp(name,ptr->name) == 0) {
 610         memcpy(out_data,ptr,sizeof(info_t));
 620         return (0);  
 630         }
 640     }
 650     return(1);
 660   }

(4) Example of an ACF file

The following shows an example of an ACF file.

  10   /*
  20    *
  30    *  (4) Available only in the example of 
  40    *      ACF file RPC TxRPC sample.acf
  50    */
  60   
  70   [auto_handle] interface sample_ope
  80   {
  90     [comm_status, fault_status] getinfo();
 100   }

(5) Template example of a server program

The template example of a server program depends on the value specified for the argument of the txidl command. The following shows an example when the option specified.-sptype ndce is specified.

  10   /*
  20    *
  30    *    (5) Template for server program (name: serv.c)
  40    *    <For -sptype ndce>
  50    */
  60   
  70   #include <dctrp.h>
  80   
  90   main()
 100   {
 110     idl_long_int rc;
 120     rc = dc_rpc_open(DCNOFLAGS);
 130     if(rc != DC_OK) {
 140       printf("server : dc_rpc_open failed. rc=%d\n", rc);
 150       goto end_of_program;
 160     }
 170     rc=dc_rpc_mainloop(DCNOFLAGS);
 180     if(rc != DC_OK) {
 190       printf("server : dc_rpc_mainloop failed. "
 195              "rc=%d\n", rc);
 200     }
 210   end_of_program:
 220     dc_rpc_close(DCNOFLAGS);
 230     exit(0);
 240   }

(6) Template example of a user service definition

The template example of a user service definition depends on the value specified for the argument of the txidl command. The following shows an example when each option is specified.

  10   /*
  20    *  (6) Example of user service definition template
  30    *      <For -cptype ndce>
  40    */
  50   
  60   #Don't change the 2 definitions below.
  70   
  80   set atomic_update = Y
  90   
 100   set trn_expiration_time_suspend = Y
 110   
 120   # If this program is SUP, set none.
 125   # If other, set queue or socket.
 130   
 140   set receive_from = none
 150   
 160   #Set your modulename.
 170   
 180   set module = "modulename"
 190   
 200   #Set non-zero value.
 210   
 220   set trn_expiration_time = 180
 230   
 240   #Add any definition you need.
  10   /*
  20    *  (6) Example of user service definition template
  30    *      <For -sptype ndce>
  40    */
  50   
  60   #Don't change the 4 definitions below.
  70   
  80   
  90   set atomic_update = Y
 100   
 110   set trn_expiration_time_suspend = Y
 120   
 130   set service_group = "sample_ope"
 140   
 150   set service = "_getinfo=_getinfo"
 160   
 170   #Set your modulename.
 180   
 190   set module = "modulename"
 200   
 210   #Set non-zero value.
 220   
 230   set trn_expiration_time = 180
 240   
 250   #Add any definition you need.
  10   /*
  20    *  (6) Example of user service definition template
  30    *      <For -sptype wdce>
  40    */
  50   
  60   #Don't change the  4 definitions below.
  70   
  80   set atomic_update = N
  90   
 100   set receive_from = queue
 110   
 120   set service_group = "sample_ope"
 130   
 140   set service = "_getinfo=_getinfo"
 150   
 160   #Set your modulename.
 170   
 180   set module = "modulename"
 190   
 200   #Add any definition you need.

(7) Template example of an environment definition

The following shows an example of an environment definition template.

  10   /*
  20    *  (7) Example of an environment definition template
  30    *      <For -cptype wdce>
  40    */
  50   
  60   #Set the 2 definitions below
  70   
  80   #DCNAMPORT = 
  90   
 100   #DCHOST = 
 110   
 120   #Add any definition you need.