6.2.4 Example: Sending a Synchronous Message with TPNOTRAN Set

The following listing illustrates a communication call that suppresses transaction mode. The call is made to a service that is not affiliated with a resource manager; it would be an error to allow the service to participate in the transaction. The application prints an accounts receivable report, accrcv, generated from information obtained from a database named accounts.

The service routine REPORT interprets the specified parameters and sends the byte stream for the completed report as a reply. The client uses tpcall() to send the byte stream to a service called PRINTER, which, in turn, sends the byte stream to a printer that is conveniently close to the client. The reply is printed. Finally, the PRINTER service notifies the client that the hard copy is ready to be picked up.

Note: The example Sending an Asynchronous Message with TPNOREPLY | TPNOTRAN shows a similar example using an asynchronous message call.

Listing Sending a Synchronous Message with TPNOTRAN Set

#include <stdio.h>
#include "atmi.h"

main()

{
char *rbuf;                            /* report buffer */
long r1len, r2len, r3len;             /* buffer lengths of send, 1st reply,
                                      and 2nd reply buffers for report */ 
join application

if (rbuf = tpalloc("STRING", NULL, 0) == NULL) /* allocate space for report 
*/
    leave application and exit program
(void)strcpy(rbuf,
    "REPORT=accrcv DBNAME=accounts"); /* send parms of report */
r1len = strlen(rbuf)+1;              /* length of request */

start transaction

if (tpcall("REPORT", rbuf, r1len, &rbuf,
   &r2len, 0) == -1)                    /* get report print stream */
   error routine
if (tpcall("PRINTER", rbuf, r2len, &rbuf,
   &r3len, TPNOTRAN) == -1)             /* send report to printer */
   error routine
(void)printf("Report sent to %s printer\n",
rbuf);                                 /* indicate which printer */

terminate transaction
free buffer
leave application
}

Note:

In the preceding example, the term error routine indicates that the following tasks are performed: an error message is printed, the transaction is aborted, allocated buffers are freed, the client leaves the application, and the program is exited.