Product Documentation

FairCom ISAM for C

Previous Topic

Next Topic

Writing to a Server-Side Queue

Data is placed on the queue by invoking ctSysQueueWrite() or ctSysQueueLIFOWrite() functions. ctSysQueueWrite() adds new data at the end of the queue, while the ctSysQueueLIFOWrite() function adds the data at the beginning of the queue, similar to a stack operation. The syntax for the server-side queue (system queue) write functions is shown here:

ctCONV NINT ctDECL ctSysQueueWrite(NINT qhandle,pVOID message,NINT msglen)

ctCONV NINT ctDECL ctSysQueueLIFOWrite(NINT qhandle,pVOID message,

NINT msglen)

Parameter qhandle is a system queue handle returned by a call to ctSysQueueOpen(). Parameter message is a pointer to a block of memory containing arbitrary data to be placed on the queue and msglen indicates how many bytes are pointed to by message. ctSysQueueWrite() and ctSysQueueLIFOWrite() return NO_ERROR (0) on success.

Example using ctSysQueueWrite:

#include <ctreep.h>


int main(void)

{

NINT eRet = 0;

NINT hQueue = -1;

TEXT buffer[256];

NINT isam_init = 0;


/* init ISAM */

eRet = (NINT)INTISAMX(6, 7, 4, 6, 0, "ADMIN", "ADMIN", "FAIRCOMS");

if (eRet != NO_ERROR)

{

printf("INTISAM failed with error %d\n", eRet);

goto Exit;

}

isam_init++;

/* create a new queue */

hQueue = ctSysQueueOpen("MyQueue", 0);

if (hQueue < 0)

{

eRet = -hQueue;

printf("ctSysQueueOpen failed with error %d\n", eRet);

goto Exit;

}

/* write text to a queue */

eRet = ctSysQueueWrite(hQueue, "This is the first line", 22);

if (eRet != NO_ERROR)

{

printf("ctSysQueueWrite failed with error %d\n", eRet);

goto Exit;

}

/* read the message in the queue */

eRet = ctSysQueueRead(hQueue, buffer, sizeof(buffer), ctWAITFOREVER);

/* check if read time-out */

if (eRet == NO_ERROR)

printf("Read: %s\n", buffer);

else if (eRet == NTIM_ERR)

printf("ctSysQueueRead time-out\n");

else

printf("ctSysQueueRead failed with error %d\n", eRet);


Exit:

/* close the queue */

if (hQueue >= 0)

{

eRet = ctSysQueueClose(hQueue);

if (eRet != NO_ERROR)

printf("ctSysQueueClose failed with error %d\n", eRet);

}

if (isam_init)

CLISAM();

return (int)eRet;

}

Example with ctSysQueueLIFOWrite

#include <ctreep.h>


int main(void)

{

NINT eRet = 0;

NINT hQueue = -1;

TEXT buffer[256];

NINT isam_init = 0;

NINT i;


/* init ISAM */

eRet = (NINT)INTISAMX(6, 7, 4, 6, 0, "ADMIN", "ADMIN", "FAIRCOMS");

if (eRet != NO_ERROR)

{

printf("INTISAM failed with error %d\n", eRet);

goto Exit;

}

isam_init++;

/* create a new queue */

hQueue = ctSysQueueOpen("MyQueue", 0);

if (hQueue < 0)

{

eRet = -hQueue;

printf("ctSysQueueOpen failed with error %d\n", eRet);

goto Exit;

}

/* write text to a queue using ctSysQueueWrite */

eRet = ctSysQueueWrite(hQueue, "This is the first line", 23);

if (eRet != NO_ERROR)

{

printf("ctSysQueueWrite failed with error %d\n", eRet);

goto Exit;

}

/* write text to a queue using ctSysQueueLIFOWrite */

eRet = ctSysQueueLIFOWrite(hQueue, "This line should be on top", 27);

if (eRet != NO_ERROR)

{

printf("ctSysQueueLIFOWrite failed with error %d\n", eRet);

goto Exit;

}

/* read two messages from the queue */

for (i = 0; i < 2; i++)

{

eRet = ctSysQueueRead(hQueue, buffer, sizeof(buffer), ctWAITFOREVER);

/* check if read time-out */

if (eRet == NO_ERROR)

printf("Read %d: %s\n", (i+1), buffer);

else if (eRet == NTIM_ERR)

printf("ctSysQueueRead time-out\n");

else

printf("ctSysQueueRead failed with error %d\n", eRet);

}


Exit:

/* close the queue */

if (hQueue >= 0)

{

eRet = ctSysQueueClose(hQueue);

if (eRet != NO_ERROR)

printf("ctSysQueueClose failed with error %d\n", eRet);

}

if (isam_init)

CLISAM();

return (int)eRet;

}

TOCIndex