5.4.2 Example: Checking the Priority of the Service Request

Note:

The tpgprio() and tpsprio() functions, used for getting and setting priorities, respectively, are described in detail in Setting and Getting Message Priorities.

The example code in this section shows how a service called PRINTER tests the priority level of the request just received using the tpgprio() function. Then, based on the priority level, the application routes the print job to the appropriate destination printer and pipes the contents of pbuf−>data to that printer.

The application queries pbuf−>flags to determine whether a reply is expected. If so, it returns the name of the destination printer to the client. For more information on the tpreturn() function, refer to Terminating a Service Routine.

Listing Checking the Priority of a Received Request

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

char *roundrobin();

PRINTER(pbuf)

TPSVCINFO *pbuf;             /* print buffer */

{
char prname[20], ocmd[30];        /* printer name, output command */
long rlen;                        /* return buffer length */
int prio;                         /* priority of request */
FILE *lp_pipe;                    /* pipe file pointer */
prio=tpgprio();
if (prio <= 20)
   (void)strcpy(prname,"bigjobs"); /* send low priority (verbose)
                                      jobs to big comp. center
                                      laser printer where operator
                                      sorts output and puts it
                                      in a bin */
else if (prio <= 60)
    (void)strcpy(prname,roundrobin()); /* assign printer on a
                                       rotating basis to one of
                                       many local small laser printers
                                       where output can be picked
                                       up immediately; roundrobin() cycles
                                       through list of printers */

else
   (void)strcpy(prname,"hispeed");
                                    /* assign job to high-speed laser
                                    printer; reserved for those who
                                    need verbose output on a daily,
                                    frequent basis */

(void)sprintf(ocmd, "lp -d%s", prname);     /* output lp(1) command */
lp_pipe = popen(ocmd, "w");                 /* create pipe to command */
(void)fprintf(lp_pipe, "%s", pbuf->data);   /* print output there */
(void)pclose(lp_pipe);                      /* close pipe */

if ((pbuf->flags & TPNOREPLY))
    tpreturn(TPSUCCESS, 0, NULL, 0, 0);
rlen = strlen(prname) + 1;
pbuf->data = tprealloc(pbuf->data, rlen); /* ensure enough space for name */
(void)strcpy(pbuf->data, prname);
tpreturn(TPSUCCESS, 0, pbuf->data, rlen, 0);

char *
roundrobin()

{
static char *printers[] = {"printer1", "printer2", "printer3", "printer4"};
static int p = 0;

if (p > 3)
    p=0;
return(printers[p++]);
}