Hitachi

OpenTP1 Version 7 分散トランザクション処理機能 OpenTP1 クライアント使用の手引 TP1/Client/W,TP1/Client/P編


3.3.1 CUPとSPPの作成

CUPとSPPの構成例を次の図に示します。例題では,Windows環境以外の場合を示します。

図3‒2 CUPとSPPの構成例

[図データ]

この例題で使用するCUPのコーディング例を次に示します。

000010  #include <stdio.h>
000020  #include <stdlib.h>
000030  #include <string.h>
000040  #include <dcvclt.h>
000050  #include <dcvrpc.h>
000060
000070  #define BUFSIZE    512
000080  #define SERVICE    "spp01"
000090
000100  main()
000110  {
000120    char    in[BUFSIZE];
000130    DCULONG    in_len;
000140    char    out[BUFSIZE];
000150    DCULONG    out_len;
000160    char    indata[BUFSIZE];
000170    DCLONG      rc;
000180    DCCLT_ID  cltid;
000190    char    clt_flag = 0;
000200    char    rpc_flag = 0;
000210
000220    /*
000230     *  クライアントユーザの認証要求
000240     */
000250    if((rc = dc_clt_cltin_s(NULL, &cltid, NULL, NULL, "user01", "puser01",
000260        NULL, DCNOFLAGS)) != DC_OK){
000270      printf("cup01: dc_clt_cltin_sに失敗しました。CODE=%d\n", rc);
000280      goto PROG_EXIT;
000290    }
000300    clt_flag = 1;
000310
000320    /*
000330     *  RPC-OPEN(RPC環境の初期設定)
000340     */
000350    if((rc = dc_rpc_open_s(cltid, DCNOFLAGS)) != DC_OK) {
000360      printf("cup01: dc_rpc_open_sに失敗しました。CODE=%d\n", rc);
000370      goto PROG_END;
000380    }
000390    rpc_flag = 1;
000400
000410    while (1) {
000420      printf("****** 伝言板メニュー ******\n");
000430      printf("伝言の取り出し ... [1]    伝言の書き込み ... [2]\n");
000440      printf("終了 ............. [9]\n");
000450      printf("番号を入力して下さい。=>");
000460      gets(indata);
000470
000480      if(indata[0] == '1') {
000490
000500        /*
000510         *  RPC-CALL(RPCの実行)
000520         */
000530        strcpy(in, "cup01");
000540        in_len = strlen(in) + 1;
000550        out_len = sizeof(out);
000560        if((rc = dc_rpc_call_s(cltid, SERVICE, "get", in, &in_len, out,
000570            &out_len, DCNOFLAGS)) != DC_OK) {
000580          printf("cup01: dc_rpc_call_sに失敗しました。CODE=%d\n", rc);
000590          goto PROG_END;
000600        }
000610        printf("伝言の内容: %s\n", out);
000620      }
000630
000640      else if(indata[0] == '2') {  
000650        printf("伝言を入力して下さい =>");
000660        gets(indata);
000670        if(indata[0] == '\0') {
000680          strcpy(indata, "伝言はありません。\n");
000690        }
000700
000710        /*
000720         *  RPC-CALL(RPCの実行)
000730         */
000740        strcpy(in, indata);
000750        in_len = strlen(in) + 1;
000760        out_len = sizeof(out);
000770        if((rc = dc_rpc_call_s(cltid, SERVICE, "put", in, &in_len, out,
000780            &out_len, DCNOFLAGS)) != DC_OK) {
000790          printf("cup01: dc_rpc_call_sに失敗しました。CODE=%d\n", rc);
000800          goto PROG_END;
000810        }
000820        printf("%s\n", out);
000830      }
000840
000850      else if(indata[0] == '9') {
000860        break;
000870      }
000880
000890      else {
000900        continue;
000910      }
000920    }
000930
000940  PROG_END:
000950    /*
000960     *  RPC-CLOSE(RPC環境の解除)
000970     */
000980    if(rpc_flag) {
000990        dc_rpc_close_s(cltid, DCNOFLAGS);
001000    }
001010
001020  PROG_EXIT:
001030    if(clt_flag) {
001040        dc_clt_cltout_s(cltid, DCNOFLAGS);
001050    }
001060    exit(0);
001070  }

この例題で使用する,SPPのコーディング例(メイン関数 DAMアクセス)を次に示します。

000010  #include <stdio.h>
000020  #include <dcrpc.h>
000030  #include <dcdam.h>
000040  #define DAMFILE "damfile0"
000050  
000060  int damfd;
000070  
000080  main()
000090  {
000100    int rc;
000110  
000120    /*
000130     *  RPC-OPEN(UAPの開始)
000140     */
000150    if ((rc = dc_rpc_open(DCNOFLAGS)) != DC_OK) {
000160        printf("spp01: dc_rpc_openに失敗しました。CODE=%d\n", rc);
000170        goto PROG_END;
000180    }
000190    /*
000200     *  DAM-OPEN(論理ファイルのオープン)
000210     */
000220    if ((rc = dc_dam_open(DAMFILE,DCDAM_BLOCK_EXCLUSIVE)) < 0) {
000230        printf("spp01: dc_dam_openに失敗しました。CODE=%d\n", rc);
000240        goto PROG_END;
000250    }
000260    damfd = rc;
000270    /*
000280     *  RPC-MAINLOOP(SPPのサービス開始)
000290     */
000300    printf("spp01: mainloopに入ります。\n");
000310    if ((rc = dc_rpc_mainloop(DCNOFLAGS)) != DC_OK) {
000320        printf("spp01: dc_rpc_mainloopに失敗しました。CODE=%d\n",rc);
000330    }
000340    /*
000350     *  DAM-CLOSE(論理ファイルのクローズ)
000360     */
000370    if ((rc = dc_dam_close(damfd, DCNOFLAGS)) != DC_OK) {
000380        printf("spp01: dc_dam_closeに失敗しました。CODE=%d\n", rc);
000390    }
000400  PROG_END:
000410    /*
000420     *  RPC-CLOSE(UAPの終了)
000430     */
000440    dc_rpc_close(DCNOFLAGS);
000450    printf("spp01: SPPのサービス処理を終了します。\n");
000460    exit(0);
000470  }

この例題で使用する,SPPのコーディング例(サービス関数 DAMアクセス)を次に示します。

000010  #include <stdio.h>
000020  #include <string.h>
000030  #include <dcrpc.h>
000040  #include <dctrn.h>
000050  #include <dcdam.h>
000060  #define DAMBLKSIZE  504
000070  
000080  extern int  damfd;
000090  static char damblk[DAMBLKSIZE];
000100  
000110  void get(in, in_len, out, out_len)
000120      char              *in;
000130      DCULONG           *in_len;
000140      char              *out;
000150      DCULONG           *out_len;
000160  {
000170      int               rc;
000180      struct DC_DAMKEY  keyptr;
000190      static char       *service = "get";
000200  
000210    printf("%s: %sからのサービス要求を受け付けました。\n", service, in);
000220  
000230    /*
000240     *  TRN-BEGIN(トランザクションの開始)
000250     */
000260    if ((rc = dc_trn_begin()) != DC_OK) {
000270      sprintf(out, "%s: dc_trn_beginに失敗しました。CODE=%d\n",
000280              service,rc);
000290      printf("%s", out);
000300      goto PROG_END;
000310    }
000320    /*
000330     *  DAM_READ(DAMファイルの読み込み)
000340     */
000350    keyptr.fstblkno = 0;
000360    keyptr.endblkno = 0;
000370    if ((rc = dc_dam_read(damfd, &keyptr, 1, damblk, DAMBLKSIZE,
000380                DCDAM_REFERENCE | DCDAM_NOWAIT)) != DC_OK) {
000390        sprintf(out, "%s: dc_dam_readに失敗しました。CODE=%d\n",
000400                service,rc);
000410        printf("%s", out);
000420        goto TRN_COMMIT;
000430    }
000440    strncpy(out, damblk, *out_len);
000450  
000460  TRN_COMMIT:
000470    /*
000480     *  TRN_UNCHAINED_COMMIT(非連鎖モードのコミット)
000490     */
000500    if ((rc = dc_trn_unchained_commit()) != DC_OK) {
000510        sprintf(out, "%s: dc_trn_unchained_commitに失敗しました。CODE=%d\n",
000520                service, rc);
000530        printf("%s", out);
000540    }
000550  PROG_END:
000560      *out_len = strlen(out) + 1;
000570      return;
000580  }
000590  
000600  void put(in, in_len, out, out_len)
000610      char              *in;
000620      DCULONG           *in_len;
000630      char              *out;
000640      DCULONG           *out_len;
000650  {
000660      int               rc;
000670      struct DC_DAMKEY  keyptr;
000680      static char  *service = "put";
000690  
000700    printf("%s: サービス要求を受け付けました。\n", service);
000710  
000720    /*
000730     *  TRN-BEGIN(トランザクションの開始)
000740     */
000750    if ((rc = dc_trn_begin()) != DC_OK) {
000760        sprintf(out, "%s: dc_trn_beginに失敗しました。CODE=%d\n",
000770                service,rc);
000780        printf("%s", out);
000790        goto PROG_END;
000800    }
000810    /*
000820     *  DAM_WRITE(DAMファイルへ書き込み)
000830     */
000840    keyptr.fstblkno = 0;
000850    keyptr.endblkno = 0;
000860    strncpy(damblk, in, DAMBLKSIZE);
000870    if ((rc = dc_dam_write(damfd, &keyptr, 1, damblk,
000880              DAMBLKSIZE, DCDAM_WAIT)) != DC_OK) {
000890      sprintf(out, "%s: dc_dam_writeに失敗しました。CODE=%d\n",
000900              service, rc);
000910      printf("%s", out);
000920      dc_trn_unchained_rollback();
000930      goto PROG_END;
000940    }
000950    sprintf(out, "%s: 正常に処理を終了しました。\n", service);
000960    /*
000970     *  TRN_UNCHAINED_COMMIT(非連鎖モードのコミット)
000980     */
000990    if ((rc = dc_trn_unchained_commit()) != DC_OK) {
001000        sprintf(out, "%s: dc_trn_unchained_commitに失敗しました。CODE=%d\n",
001010                service, rc);
001020        printf("%s", out);
001030    }
001040  PROG_END:
001050    *out_len = strlen(out) + 1;
001060    return;
001070  }