5.4.2 例: サービス・リクエストの優先度の確認
この項で示すサンプル・コードは、PRINTERという名前のサービスがtpgprio()関数を使用して、受信したばかりのリクエストの優先度を確認する方法を示しています。その後、その優先度に基づいて印刷ジョブが適切なプリンタに送られ、そのプリンタにpbuf−>dataの内容がパイプされています。
               
アプリケーションはpbuf−>flagsに対して問合せを実行し、応答が必要かどうかを判定します。その場合は、宛先プリンタ名をクライアントに返します。tpreturn()関数の詳細は、「サービス・ルーチンの終了」を参照してください。
               
受信したリクエストの優先度の確認のリスト
#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++]);
}親トピック: サービスの定義