/*
 * txcore.h - TxCore 공통 프레임워크 API
 *
 * TxCore 는 사내 표준 공통 프레임워크로, Tuxedo/ProFrame 계열 TP 모니터의
 * ATMI(tpcall/tpreturn/tpbegin) 및 FML/UBF(Fchg/Fget) 관용구를 얇게 감싼
 * 자체 구현 계층이다. 외부 TP 미들웨어 설치 없이 단독 빌드/링크된다.
 */
#ifndef TXCORE_H
#define TXCORE_H

#include <stddef.h>
#include <stdarg.h>

#ifdef __cplusplus
extern "C" {
#endif

#define TX_OK        0
#define TX_FAIL     (-1)
#define TX_ENOENT   (-2)
#define TX_EINVAL   (-3)
#define TX_EDB      (-4)
#define TX_ENOMEM   (-5)
#define TX_ENOKEY   (-6)

#define TX_LOG_DEBUG 0
#define TX_LOG_INFO  1
#define TX_LOG_WARN  2
#define TX_LOG_ERROR 3

#define TX_MAX_SLOTS 64
#define TX_KEY_LEN   32
#define TX_VAL_LEN   256

typedef struct {
    char key[TX_KEY_LEN];
    char val[TX_VAL_LEN];
    int  len;
    int  used;
} TXFIELD;

typedef struct {
    int     count;
    TXFIELD slots[TX_MAX_SLOTS];
} TXBUF;

typedef struct {
    char   name[TX_KEY_LEN];
    TXBUF *in;
    TXBUF *out;
    int    rcode;
    long   xid;
} TXSVCINFO;

typedef void (*tx_service_fn)(TXSVCINFO *ctx);

#define TX_SERVICE(name, ctx) void name(TXSVCINFO *ctx)

TXBUF *tx_buf_alloc(void);
void   tx_buf_free(TXBUF *buf);
void   tx_buf_reset(TXBUF *buf);
int    tx_buf_set(TXBUF *buf, const char *key, const char *val, int len);
int    tx_buf_sets(TXBUF *buf, const char *key, const char *val);
int    tx_buf_setlong(TXBUF *buf, const char *key, long val);
int    tx_buf_get(TXBUF *buf, const char *key, char *out, int outlen);
int    tx_buf_getlong(TXBUF *buf, const char *key, long *out);

int  tx_register(const char *name, tx_service_fn fn);
int  tx_call(const char *svc, TXBUF *in, TXBUF *out);
int  tx_return(TXSVCINFO *ctx, int rc, TXBUF *out);
int  tx_service_count(void);

int  tx_begin(void);
int  tx_commit(void);
int  tx_abort(void);
long tx_current_xid(void);

void tx_log(int level, const char *fmt, ...);
void tx_set_loglevel(int level);
const char *tx_strerror(int rc);

#ifdef __cplusplus
}
#endif

#endif /* TXCORE_H */
