Examples
This chapter provides examples of client and server programs that use Tmax SQ system.
1. Client Program
The following is an example of a client program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usrinc/atmi.h>
#include <usrinc/tmaxapi.h>
// The number of keys to put
#define KEYCOUNT 3
int get_keylist(char [KEYCOUNT][SQ_SYSKEY_SIZE+1]);
int main(int argc, char *argv[])
{
char *sqbuf, *sndbuf, sqbuf_org[1024];
char put_key[KEYCOUNT][SQ_SYSKEY_SIZE+1];
char get_key[KEYCOUNT][SQ_SYSKEY_SIZE+1];
long sqbuf_len = 0, rlen, flags;
int ret, i, len=0, sys_flag=0;
char key[SQ_SYSKEY_SIZE+1];
if ( (ret = tmaxreadenv( "tmax.env","TMAX" )) == -1 ){
printf( "tmax read env failed[%s]\n", tpstrerror(tperrno) );
return -1;
}
// Connects to a Tmax system.
if (tpstart((TPSTART_T *)NULL) == -1){
printf("tpstart failed [%s]\n", tpstrerror(tperrno));
return -1;
}
// Allocates data to be put in SQ using tpalloc.
if ((sqbuf = (char *)tpalloc("STRING", NULL, 0)) == NULL) {
printf("sqbuf alloc failed [%s]\n", tpstrerror(tperrno));
tpend();
return -1;
}
if ((sndbuf = (char *)tpalloc("STRING", NULL, 0)) == NULL) {
printf("sndbuf alloc failed [%s]\n", tpstrerror(tperrno));
tpend();
return -1;
}
if (argc != 3)
{
printf("Usage : %s Key Data\n", argv[0]);
printf(" Key : \"SYSTEM\" or Specific Key Value\n");
exit(1);
}
strcpy(key, argv[1]);
strcpy(sqbuf_org, argv[2]);
// Initializes a flag used for tmax_sq_put.
flags = 0L;
// When using a system key
if(!strcmp(key, "SYSTEM"))
{
flags |= TPSQ_SYSKEY;
flags |= TPSQ_KEYGEN;
len = SQ_SYSKEY_SIZE;
sys_flag = 1;
}
else
{
strcpy(put_key[0], key);
len = strlen(key);
}
// If a key value is duplicated, the existing data is updated.
// If this flag is not set, duplication error occurs.
flags |= TPSQ_UPDATE;
for(i=0; i<KEYCOUNT; i++)
{
if( !sys_flag )
{
memset(put_key[i], 0x00, SQ_SYSKEY_SIZE+1);
sprintf(put_key[i], "%s%d", key, i);
}
memset(sqbuf, 0x00, 1024);
sprintf(sqbuf, "%s%d", sqbuf_org, i);
// Puts data in SQ.
ret = tmax_sq_put(put_key[i], len+1, sqbuf, strlen(sqbuf), flags);
if(ret < 0)
{
printf("tmax_sq_put error, key = %s [%s]\n", put_key[i],
tpstrerror(tperrno));
tpfree(sqbuf);
tpend();
exit(1);
}
// Displays key and data values.
printf("PUT : [%d] Key = [%s] , Data = [%s]\n", i, put_key[i], sqbuf);
}
printf("\n");
// Calls a Tmax service.
ret = tpcall("GET_SQ", (char *)sndbuf, 0, (char **)&sndbuf, &rlen,
TPNOFLAGS);
if(ret < 0)
{
printf("tpcall failed [%s]\n", tpstrerror(tperrno));
tpfree((char *)sndbuf);
tpfree((char *)sqbuf);
tpend();
exit(1);
}
printf("recv data: %s\n", sndbuf);
tpfree((char *)sqbuf);
tpfree((char *)sndbuf);
tpend();
return 0;
}
2. Server Program
The following is an example of a server program:
#include <stdio.h>
#include <usrinc/atmi.h>
#include <usrinc/tmaxapi.h>
#define MAXKEYCNT 10
GET_SQ(TPSVCINFO *msg)
{
char get_key[MAXKEYCNT][SQ_SYSKEY_SIZE+1];
int count;
count = get_keylist(get_key);
sprintf(msg->data, "GET_SQ is done, count=[%d]", count);
tpreturn(TPSUCCESS,0,(char *)msg->data, 0,0);
}
int get_keylist(char get_key[MAXKEYCNT][SQ_SYSKEY_SIZE])
{
char *sqbuf = NULL;
SQ_KEYLIST_T sqh;
SQ_KEYINFO_T keyinfo;
long sqbuf_len = 0;
int count, i, j, ret;
if ((sqbuf = (char *)tpalloc("STRING", NULL, 0)) == NULL) {
printf("sqbuf alloc failed [%s]\n", tpstrerror(tperrno));
}
// Gets a key list saved in SQ.
sqh = tmax_sq_getkeylist(NULL, 0);
if(sqh != NULL)
{
// The number of keys
count = tmax_keylist_count(sqh);
for(i=0; i<count; i++)
{
memset((char *)&keyinfo, 0x00, sizeof(keyinfo));
// Gets and saves an i-th key value.
tmax_keylist_getakey(sqh, i, &keyinfo);
memset(get_key[i], 0x00, SQ_SYSKEY_SIZE);
for(j=0; j< keyinfo.keylen; j++)
get_key[i][j] = keyinfo.key[j];
get_key[i][j] = 0;
// Retrieves data corresponding to the key value from SQ.
ret = tmax_sq_get(get_key[i], strlen(get_key[i]),
(char **)&sqbuf, &sqbuf_len, 0);
if(ret < 0)
{
printf("tmax_sq_get failed [%s]\n", t
pstrerror(tperrno));
tpfree(sqbuf);
return -1;
}
printf("GET : [%d] Key = [%s] , Data = [%s]\n", i,
get_key[i], sqbuf);
}
printf("\n");
// Releases sqh.
tmax_keylist_free(sqh);
}
return i;
}