7.4.2 TX interface sample

This subsection shows a coding example for an SUP that uses the X/Open TX interface. This SUP uses TX-interfaced transaction control for processing that was described in 7.1 Coding samples for client/server configuration UAPs (SUP, SPP DAM access). See 7.1 Coding samples for client/server configuration UAPs (SUP, SPP DAM access) for the process configuration and details of the SPP to which the service request is addressed.

 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   * Define variables
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;
205                    /* Monitoring interval 180 seconds */
210    TXINFO info;
220  /*
230   * RPC-OPEN (start the UAP)
240   */
250      rc = dc_rpc_open(DCNOFLAGS);
260      if(rc != DC_OK){
270          printf("SUP01:dc_rpc_open failed. "
275                 "CODE = %d \n",rc);
280          goto PROG_END;
290      }
300  /*
310   * TX-OPEN (open the resource manager)
320   */
330      rc = tx_open();
340      if(rc != TX_OK){
350          printf("SUP01:tx_open failed. CODE = %d \n",rc);
360          goto PROG_END;
370      }
380  /*
390   * TX-SET-TRANSACTION-TIMEOUT (set the transaction
395   *                             monitoring interval)
400   */
410      rc = tx_set_transaction_timeout(trn_timeout);
420      if(rc != TX_OK){
430          printf("SUP01:tx_set_transaction_timeout "
435                 "failed. CODE = %d \n",rc);
440          goto PROG_END;
450      }
460  /*
470   * ADM-COMPLETE (report completion of user server
475   * start processing)
480   */
490      rc = dc_adm_complete(DCNOFLAGS);
500      if(rc != DC_OK){
510          printf(dc_adm_complete failed. CODE = %d \n",
515                 rc);
520          goto PROG_END;
530      }
540  /*
550   * TX-BEGIN (start the transaction)
560   */
570      rc = tx_begin();
580      if(rc != TX_OK){
590          printf("SUP01:tx_begin failed. CODE = %d \n",
595                  rc);
600          goto TRAN_END;
610      }
620  
630  /*
640   * TX-INFO (acquire transaction information)
650   */
660      rc = tx_info(&info);
670      if(rc <= 0){
680          printf("SUP01:Currently the system is not in "
685          "the transaction mode. CODE = %d \n",rc);
690          goto PROG_END;
700      }else if (rc == 1){
710      printf("SUP01:return=%d,control=%d,timeout=%d,"
715             "state=%d\n",
720        info.when_return,info.transaction_control,
730        info.transaction_timeout, info.transaction_state);
740    }
750  /*
760   * RPC-CALL (request a remote service)
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:The service request failed. "
845                 "CODE = %d \n",rc);
850          goto TRAN_END;
860      }
870      printf("SUP01:SERVICE FUNCTION RETURN = %s\n",
875              out_buf);
880  /*
890   * TX-SET-TRANSACTION-CONTROL (set the unchained mode)
900   */
910      TRAN_END:
920      rc = tx_set_transaction_control(TX_UNCHAINED);
930      if(rc != TX_OK){
940          printf("SUP01:tx_set_transaction_control "
945                 "failed. CODE = %d \n",rc);
950      }
960  /*
970   * TX-COMMIT (commit in unchained mode)
980   */
990      rc = tx_commit();
1000      if(rc != TX_OK){
1010          printf("SUP01:tx_commit failed. CODE = %d \n",
1015                  rc);
1020      }
1030  /*
1040   * TX-CLOSE (close the resource manager)
1050   */
1060      PROG_END:
1070      rc = tx_close();
1080      if(rc != TX_OK){
1090          printf("SUP01:tx_close failed. CODE = %d \n",
1095                  rc);
1100          goto PROG_END;
1110      }
1120  /*
1130   * RPC-CLOSE (terminate the UAP)
1140   */
1150      dc_rpc_close(DCNOFLAGS);
1160      printf("SUP01:Processing is finished.\n");
1170      exit(0);
1180  }