/*******************************************************************************
*        : UtiLibs.h
* ес :
*          :
*        :
*        : 2018.07.13
*      :
*******************************************************************************/

#ifndef	__UTI_DEFINITIONS__
#define	__UTI_DEFINITIONS__

/******************************************************************************/
/* include files */

#include	"ExDefs.h"

#ifdef	__cplusplus
extern "C" {
#endif	/*__cplusplus*/

	/**************************************************************************/
	/* literal constants */

	#define	SYSTEM_INFO_SHMKEY			(0x30010522) /* 0x20010522 to 0x30010522 Modify by shwoo 2012-11-20 */
	#define	TNAME_LEN					(16)
	#define	ALIAS_LEN					(8)
	#define	ARGC_MAX					(4)
	#define	ARGV_LEN					(TNAME_LEN)
	#define HASH_KEY_LEN				(64)
	/* TCP_BUFSIZ : HP-UX default buffer size 32768, maximum 1073725440 */
	#define	TCP_BUFSIZ					(1024*1024)
	/* UDP_BUFSIZ : HP-UX default send buffer size 65535, maximum 2147483647 */
	#define	UDP_BUFSIZ					(1024*1024)

	enum {
		INIT_TASK = 300,
		DEBUG_TASK,
		SCHEDULER_TASK,
		EPSBILL_TASK,
		EPSFILE_TASK,
		EPSMSG_TASK,
		TCSMSG_TASK,
		RCVEPS_TASK,
		TASK_MAX = 500 /*UCHAR_MAX*/
	};

	#define	INVALID_TASK(t)				((t)<INIT_TASK||(t)>=TASK_MAX)
	#define	TOUT						TaskIndex(__FILE__,__LINE__),TaskPrint
	#define	TDUMP						TaskIndex(__FILE__,__LINE__),TaskDump

	enum {
		MSGDEFS_DEBUG_STRING = 1,			/* debug string message */
		MSGDEFS_DEBUG_DUMP,					/* debug hexa-decimal dump data */

		MSGDEFS_VDS_CONNECT,
		MSGDEFS_VDS_DISCONNECT,
		MSGDEFS_VDS_DATA_RECEIVED,

		MSGDEFS_EXT_NOTIFY,
		MSGDEFS_TNS_NOTIFY,
		MSGDEFS_DML_INSERT,

		MSGDEFS_VMS_NOTIFY,
		MSGDEFS_WAS_NOTIFY,
		MSGDEFS_INC_NOTIFY,

		MSGDEFS_MAX
	};

	#define	FileReadLock(f, s, l)	FileControl(f, F_SETLKW, F_RDLCK, s, l)
	#define	FileWriteLock(f, s, l)	FileControl(f, F_SETLKW, F_WRLCK, s, l)
	#define	FileUnlock(f, s, l)		FileControl(f, F_SETLK, F_UNLCK, s, l)

	/**************************************************************************/
	/* type definitions */

	typedef struct {
		INT32	id;							/* task id */
		INT8	name[TNAME_LEN];			/* task name */
		INT8	alias[ALIAS_LEN];			/* task alias */
		INT32	argc;						/* count of arguments */
		INT8	argv[ARGC_MAX][ARGV_LEN];	/* arguments */
		pid_t	pid;						/* current process id */
		pid_t	opid;						/* older process id */
		time_t	start;						/* when start */
		time_t	run;						/* when running */
		UINT32	debug;						/* debug flag (0:off, 1:on) */
		UINT32	restart;					/* count of restart */
		UINT32	reboot;						/* 0:man, 1:auto */
		UINT32	dummy[CHAR_BIT];
	} ST_TASK_INFO, *PST_TASK_INFO;

	typedef struct {
		key_t			shmkey;						/* shared memory key */
		INT32			shmid;						/* shared memory id */
		INT32			id;							/* system id */
		INT8			inet[INET_ADDRSTRLEN + 4];	/* system ip address */
		INT8			bcast[INET_ADDRSTRLEN + 4];	/* broadcast ip address */
		INT32			debug;						/* system debug flag */
		INT16			target;						/* target id */
		INT16			command;					/* command */
		ST_TASK_INFO	task[TASK_MAX];
	} ST_SYSTEM_INFO, *PST_SYSTEM_INFO;

	typedef struct {
		INT16			srcSystem;
		INT32			srcTask;
		INT16			dstSystem;
		INT32			dstTask;
		UINT16			target;
		UINT16			command;
		UINT16			dsize;
		INT16			dummy;
	} ST_MSG_HEAD, *PST_MSG_HEAD;

	typedef struct {
		ST_MSG_HEAD		head;
		INT8			data[BUFSIZ - sizeof(ST_MSG_HEAD)];
	} ST_MSG_ANY, *PST_MSG_ANY;

	typedef ST_MSG_ANY	ST_MSG_DEBUG_STRING, *PST_MSG_DEBUG_STRING; 
	
	typedef struct {
		ST_MSG_HEAD		head;
		INT8			title[64];
		INT32			size;
		INT8			buf[BUFSIZ - sizeof(ST_MSG_HEAD) - sizeof(INT32) - 64];
	} ST_MSG_DEBUG_DUMP, *PST_MSG_DEBUG_DUMP;

	typedef struct {
		UINT32			hash;
		CHAR			key[HASH_KEY_LEN + 1];
		PVOID			value;
	} ST_HASH_REC, *PST_HASH_REC;

	typedef struct {
		PST_HASH_REC	records;
		UINT32			count;
		UINT32			sind;
		UINT8			type;
	} ST_HASH, *PST_HASH;

	/**************************************************************************/
	/* function proto-types */

	/* Task Functions */
	INT32 TaskConfRead(PST_TASK_INFO, INT32);
	VOID  TaskSleep(INT32);
	INT32 TaskInit(BOOL);
	VOID  TaskClose(INT32);
	INT32 TaskOpen();
	INT32 TaskChildOpen();
	pid_t TaskSpawn(PST_TASK_INFO);
	pid_t TaskRespawn(INT32);
	INT32 TaskIdGet(pid_t);
	BOOL  TaskIsAlive(INT32);
	VOID  TaskIndex(INT8 *, INT32);
	VOID  TaskPrint(const INT8 *, ...);
	VOID  TaskDump(const INT8 *, const INT8 *, const INT32);

	/* Msg Functions */
	VOID  MsgClose();
	INT32 MsgOpen(INT32);
	VOID  MsgTarget(struct sockaddr_in *, UINT16);
	INT32 MsgSend(INT32, const VOID *, const INT32);
	INT32 MsgRecv(INT32, VOID *, INT32);
	INT32 MsgRecvTime(INT32, VOID *, INT32, LONG);

	/* Tcp Functions */
	VOID  TcpClose();
	INT32 TcpServerOpen(UINT16);
	INT32 TcpAccept(struct sockaddr_in *);
	INT32 TcpConnect(const INT8 *, UINT16);
	INT32 TcpConnectTime(const INT8 *, UINT16, LONG);
	INT32 TcpSend(const VOID *, const INT32);
	INT32 TcpRecv(VOID *, INT32);
	INT32 TcpRecvTime(VOID *, INT32, LONG);

	/* Shm Functions */
	VOID  ShmClose(INT32);
	PVOID ShmOpen(key_t, size_t);

	/* File Functions */
	INT32 FileGetSqlFmt(INT8 *, INT8 *, ...);
	INT32 FileControl(INT32, INT32, INT16, off_t, off_t );

	/* Ip Functions */
	INT32 IpConfigGet();

	/* Time Functions */
	CHAR* TimeTm2Str(time_t, INT32);
	time_t TimeStr2Tm(CHAR *);
	CHAR* TimeGetStrMsec();
	CHAR* TimeGetDayCode(time_t);

	/* Str Functions */
	VOID  StrSpDelete(CHAR *);
	VOID  StrValueHeadSet(PCHAR, INT32, INT32, const PCHAR, ...);
	VOID  StrValueTailSet(PCHAR, INT32, INT32, const PCHAR, ...);

	/* Hash Functions */
	PST_HASH HashNew(UINT32);
	INT32 HashNew2(PST_HASH, UINT32, key_t, INT32);
	VOID  HashPrint(PST_HASH);
	VOID  HashDestroy(PST_HASH);
	INT32 HashAdd(PST_HASH, const CHAR *, VOID *);
	PVOID HashGet(PST_HASH, const CHAR *);
	PVOID HashRemove(PST_HASH, const CHAR *);
	UINT32 HashSize(PST_HASH);

	/**************************************************************************/
	/* variables */

	extern PST_SYSTEM_INFO	pstSysInfo;
	extern INT32			i32TaskId;

#ifdef	__cplusplus
}
#endif	/*__cplusplus*/

/******************************************************************************/
#endif	/*__UTI_DEFINITIONS__*/
