7.4.2 TXインタフェースの例

X/OpenのTXインタフェースを使用した,SUPのコーディング例を次に示します。このSUPは,7.1で示すSUPの処理を,TXインタフェースでトランザクション制御したものです。処理の構成図と,サービス要求先のSPPについては,「7.1 クライアント/サーバ形態のUAPのコーディング例(SUP,SPP DAMアクセス)」を参照してください

 10  
 20  /*
 30   * SUP01
 40   */
 50  #include <stdio.h>  
 60  #include <string.h>  
 70  #include <dcrpc.h>  
 80  #include <tx.h>
 90  
100  main()
110  {
120  /*
130   * 変数の定義
140   */
150      static char in_buf [1024];
160      static long in_buf_len;
170      static char out_buf [1024];
180      static long out_buf_len;
190      int rc;
200    TRANSACTION_TIMEOUT trn_timeout = 180; /* 監視時間 180 秒*/
210    TXINFO info;
220  /*
230   * RPC-OPEN(UAPの開始)
240   */
250      rc = dc_rpc_open(DCNOFLAGS);
260      if(rc != DC_OK){
270          printf("SUP01:dc_rpc_openに失敗しました。CODE = %d ¥n",rc);
280          goto PROG_END;
290      }
300  /*
310   * TX-OPEN(リソースマネジャのオープン)
320   */
330      rc = tx_open();
340      if(rc != TX_OK){
350          printf("SUP01:tx_openに失敗しました。CODE = %d ¥n",rc);
360          goto PROG_END;
370      }
380  /*
390   * TX-SET-TRANSACTION-TIMEOUT(トランザクション監視時間の設定)
400   */
410      rc = tx_set_transaction_timeout(trn_timeout);
420      if(rc != TX_OK){
430          printf("SUP01:tx_set_transaction_timeoutに失敗しました。CODE = %d ¥n",rc);
440          goto PROG_END;
450      }
460  /*
470   * ADM-COMPLETE(ユーザサーバの開始処理完了の報告)
480   */
490      rc = dc_adm_complete(DCNOFLAGS);
500      if(rc != DC_OK){
510          printf("SUP01:dc_adm_completeに失敗しました。CODE = %d ¥n",rc);
520          goto PROG_END;
530      }
540  /*
550   * TX-BEGIN(トランザクションの開始)
560   */
570      rc = tx_begin();
580      if(rc != TX_OK){
590          printf("SUP01:tx_beginに失敗しました。CODE = %d ¥n",rc);
600          goto TRAN_END;
610      }
620  
630  /*
640   * TX-INFO(トランザクション情報の取得)
650   */
660      rc = tx_info(&info);
670      if(rc <= 0){
680          printf("SUP01:現在トランザクションモードではありません。CODE = %d ¥n",rc);
690          goto PROG_END;
700      }else if (rc == 1){
710      printf("SUP01:return=%d,control=%d,timeout=%d,state=%d¥n",
720        info.when_return,info.transaction_control,
730        info.transaction_timeout, info.transaction_state);
740    }
750  /*
760   * RPC-CALL(遠隔サービスの要求)
770   */
780      strcpy(in_buf,"SUP01:DATA OpenTP1!!");
790      in_buf_len = strlen(in_buf) + 1;
800      out_buf_len = 1024;
810      rc = dc_rpc_call("svr01","svr01",in_buf,&in_buf_len,
820      out_buf,&out_buf_len,DCNOFLAGS);
830      if(rc != DC_OK){
840          printf("SUP01:サービスの要求が失敗しました。 CODE = %d ¥n",rc);
850          goto TRAN_END;
860      }
870      printf("SUP01:SERVICE FUNCTION RETURN = %s¥n",out_buf);
880  /*
890   * TX-SET-TRANSACTION-CONTROL(非連鎖モード設定)
900   */
910      TRAN_END:
920      rc = tx_set_transaction_control(TX_UNCHAINED);
930      if(rc != TX_OK){
940          printf("SUP01:tx_set_transaction_controlに失敗しました。CODE = %d ¥n",rc);
950      }
960  /*
970   * TX-COMMIT(非連鎖モードのコミット)
980   */
990      rc = tx_commit();
1000      if(rc != TX_OK){
1010          printf("SUP01:tx_commitに失敗しました。CODE = %d ¥n",rc);
1020      }
1030  /*
1040   * TX-CLOSE(リソースマネジャのクローズ)
1050   */
1060      PROG_END:
1070      rc = tx_close();
1080      if(rc != TX_OK){
1090          printf("SUP01:tx_closeに失敗しました。CODE = %d ¥n",rc);
1100          goto PROG_END;
1110      }
1120  /*
1130   * RPC-CLOSE(UAPの終了)
1140   */
1150      dc_rpc_close(DCNOFLAGS);
1160      printf("SUP01:処理を終了しました。¥n");
1170      exit(0);
1180  }