예제
본 장에서는 실제 Tmax SQ 시스템 예제를 통해서 사용법에 대해서 설명한다.
1. 클라이언트 프로그램
다음은 클라이언트 프로그램에 대한 예제이다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usrinc/atmi.h>
#include <usrinc/tmaxapi.h>
// Put할 Key의 개수
#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;
}
// Tmax 시스템에 접속
if (tpstart((TPSTART_T *)NULL) == -1){
printf("tpstart failed [%s]\n", tpstrerror(tperrno));
return -1;
}
// 세션 큐에 Put할 데이터 할당 (반드시 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]);
// tmax_sq_put 시 사용할 플래그 초기화
flags = 0L;
// 시스템 키 사용할 때
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);
}
// 키값이 중복될 경우, 기존의 데이터를 업데이트한다. 이 플래그가 설정되지 않으면
// 키값 중복 에러
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);
// 세션 큐에 데이터를 PUT 한다.
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);
}
// 키, 데이터 값 출력
printf("PUT : [%d] Key = [%s] , Data = [%s]\n", i, put_key[i], sqbuf);
}
printf("\n");
// Tmax 서비스 호출
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. 서버 프로그램
다음은 서버 프로그램에 대한 예제이다.
#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));
}
// SQ에 저장되어 있는 키 리스트를 가져온다.
sqh = tmax_sq_getkeylist(NULL, 0);
if(sqh != NULL)
{
// 키의 개수
count = tmax_keylist_count(sqh);
for(i=0; i<count; i++)
{
memset((char *)&keyinfo, 0x00, sizeof(keyinfo));
// i 순번에 해당하는 키값을 구하여 저장
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;
// 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");
// sqh 해제
tmax_keylist_free(sqh);
}
return i;
}