/* OS/400 Data Queue Example This small program that is furnished by IBM is a simple example to provide an illustration. This example has not been thoroughly tested under all conditions. IBM, therefore, cannot guarantee or imply reliability, serviceability, or function of this program. All programs contained herein are provided to you "AS IS". THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PART ICULAR PURPOSE ARE EXPRESSLY DISCLAIMED. This examples consists of TWO programs. C source code for both programs is contained in this file. */ /*********************************************************************/ /* */ /* test case: dataq_client.c */ /* */ /* objective: Data Queue example - client program */ /* */ /* description: This program serves as a client to the server pgm */ /* below. */ /* */ /* */ /* usage notes: Compile this program using CRTBNDC */ /* Call it after the server prpgram below has been */ /* started. It needs one parameter, the number of */ /* requests: */ /* Usage: CALL QGPL/CLIENT 'number_of_requests' */ /* Example: CALL QGPL/CLIENT '1000' */ /* Example: CALL QGPL/CLIENT '-1' */ /* Specify a negative number to end the central */ /* process(es). */ /* */ /*********************************************************************/ #include #include #include #include #include #include "QSYSINC/H(QSNDDTAQ)" #include "QSYSINC/H(QRCVDTAQ)" #include typedef struct{ int id; char symbol[5]; char reply[32]; } request_t; static char mylib[10] = "QGPL "; static char myqueue[10] = "MY_TESTQ "; int main (int argc, char* argv[]) { decimal(5,0) msglen; decimal(5,0) duration; decimal(3,0) keylen; decimal(3,0) senderlen; request_t request; char reply[50]; int request_key=0; int i; if (argc != 2) { printf(" Usage: CALL %s 'number_of_requests' \n", argv[0]); printf("Example: CALL %s '1000' \n", argv[0]); printf("Example: CALL %s '-1' \n", argv[0]); printf("Specify a negative number to end the central process(es).\n"); exit(1); } i=atoi(argv[1]); msglen = (decimal(5,0)) sizeof(request); keylen = (decimal(3,0)) sizeof(request_key); senderlen = 0.0D; /* do not return any bytes about sender */ duration = -1.0D; /* wait forever - if negative */ /* This is used to stop the central process(es). */ /* Send a request message with return id = 0. */ if (i < 0) { request.id=0; QSNDDTAQ (myqueue, /* Data queue name */ mylib, /* Library name */ msglen, /* Length of data */ &request, /* Data */ keylen, /* key kength (4 bytes) */ &request_key); /* key used is the request key */ exit(0); } request.id = getpid(); /* set the return key value */ strcpy(request.symbol, "IBM"); /* fill in the data request */ for(; i>0; i--) { memset(request.reply, 32, 0); /* clear the return space */ usleep(20000); QSNDDTAQ (myqueue, /* Data queue name */ mylib, /* Library name */ msglen, /* Length of data */ &request, /* Data */ keylen, /* key size (4 bytes) */ &request_key); /* key data (request key) */ QRCVDTAQ (myqueue, /* Data queue name */ mylib, /* Library name */ &msglen, /* Length of data */ &request, /* Data */ duration, /* wait time */ "EQ", /* match key exactly */ keylen, /* key size (4 bytes) */ &request.id, /* key data (process id) */ senderlen, /* */ NULL); /* return no data about sender */ printf("%3d: '%s'\n",i, request.reply); } } /*********************************************************************/ /* */ /* test case: dataq_svr.c */ /* */ /* objective: Data Queue example - server program */ /* */ /* description: This program serves as a server to the client pgm */ /* above. */ /* This program reads a request from the keyed data */ /* queue and fills in the reply block, to send back */ /* to the original requester. */ /* */ /* */ /* usage notes: Compile this program using CRTBNDC */ /* Call it with no parameters before calling the */ /* client program above */ /*********************************************************************/ #include #include #include #include #include "QSYSINC/H(QCLRDTAQ)" #include "QSYSINC/H(QSNDDTAQ)" #include "QSYSINC/H(QRCVDTAQ)" typedef struct{ int id; char symbol[5]; char reply[32]; } request_t; static char mylib[10] = "QGPL "; static char myqueue[10] = "MY_TESTQ "; int main (void) { decimal(5,0) msglen; decimal(5,0) waittime; decimal(3,0) keylen; decimal(3,0) senderlen; request_t request; int i=0, r; int request_key=0; float p=125; msglen = (decimal(5,0)) sizeof(request); keylen = (decimal(3,0)) sizeof(request_key); senderlen = 0.0D; waittime = -1.0D; /* wait for ever - if negative */ system("CRTDTAQ DTAQ(QGPL/MY_TESTQ) MAXLEN(50) SEQ(*KEYED) KEYLEN(4)"); request.id = 1; while(request.id != 0) /* sending 0 for return id stops server */ { QRCVDTAQ (myqueue, /* Data queue name */ mylib, /* Library name */ &msglen, /* Length of data */ &request, /* Data */ waittime, /* wait time */ "EQ", /* match key exactly */ keylen, /* key size (4 bytes) */ &request_key, /* key data (request key) */ senderlen, /* */ NULL); /* return no data about sender */ if (request.id) { /***** Fill in data for request ****************************/ r = rand(); p = p + (float)(r & 400) /1000 - 0.15; sprintf(request.reply, "Price for %s: $%6.3f", request.symbol, p); /***********************************************************/ QSNDDTAQ (myqueue, /* Data queue name */ mylib, /* Library name */ msglen, /* Length of data */ &request, /* Data */ keylen, /* key size (4 bytes) */ &request.id); /* key data (process id) */ i++; if ((i % 10) == 0) printf("Serviced %d requests.\n", i); } } printf("Serviced %d requests.\n", i); /* You can clear the entire queue with the following calls. */ QCLRDTAQ(myqueue,mylib); system(" DLTDTAQ DTAQ(QGPL/MY_TESTQ)"); exit(0); }