5.4.1 Example: Checking the Buffer Type

The code example in this section shows how a service can access the data buffer defined in the TPSVCINFO structure to determine its type by using the tptypes() function. (This process is described in “Checking for Buffer Type” on page 2‑16.) The service also checks the maximum size of the buffer to determine whether or not to reallocate space for the buffer.

This example is derived from the ABAL service that is part of the banking application provided with the Oracle Tuxedo software. It shows how the service is written to accept a request either as an aud VIEW or an FML buffer. If its attempt to determine the message type fails, the service returns a string with an error message plus an appropriate return code; otherwise it executes the segment of code that is appropriate for the buffer type. For more information on the tpreturn() function, refer to “Terminating a Service Routine” on page 5‑16.

Listing Checking for Buffer Type

#define TMTYPERR 1 /* return code indicating tptypes failed */
#define INVALMTY 2 /* return code indicating invalid message type */

void
ABAL(transb)
TPSVCINFO *transb;

{
    struct aud *transv; /* view message */
    FBFR *transf; /* fielded buffer message */
    int repc; /* tpgetrply return code */
    char typ[TMTYPELEN+1], subtyp[TMSTYPELEN+1]; /* type, subtype of message */
    char *retstr; /* return string if tptypes fails */

/* find out what type of buffer sent */
   if (tptypes((char *)transb->data, typ, subtyp) == -1) {
      retstr=tpalloc("STRING", NULL, 100);
      (void)sprintf(retstr,
       "Message garbled; tptypes cannot tell what type message\n");
        tpreturn(TPFAIL, TMTYPERR, retstr, 100, 0);
   }
/* Determine method of processing service request based on type */
   if (strcmp(typ, "FML") == 0) {
      transf = (FBFR *)transb->data;
... code to do abal service for fielded buffer ...
tpreturn succeeds and sends FML buffer in reply 
}
else if (strcmp(typ, "VIEW") == 0 && strcmp(subtyp, "aud") == 0) {
    transv = (struct aud *)transb->data;
... code to do abal service for aud struct ...
tpreturn succeeds and sends aud view buffer in reply
 }
 else {
    retstr=tpalloc("STRING", NULL, 100);
    (void)sprintf(retstr,
    "Message garbled; is neither FML buffer nor aud view\n");
     tpreturn(TPFAIL, INVALMTY, retstr, 100, 0);
  }
}