static    char    sccsid[]="%Z% %M% %I% %E% %U%";
/********************************************************************/
/* <clanal.c>                                                       */
/*                                                                  */
/********************************************************************/

#include "colmn.h"

extern int giProgram[];
extern CLPRTBL *pCLprocTable;

int col_mn_analz(lInstanceHandle,cpInstanceData,tpRecvMsg)
long lInstanceHandle;
char *cpInstanceData;
AKAMSGCOM *tpRecvMsg;
{
	AKAMSGCOM    tMsgCom;
	char         *pFilev[2], filename[256], val[2];
	int          iRc, iDisp, init_sc, iWaitTime;
	qDbToCol *pDTD;		/* 送信パケット 共通部分   */
	TrmToSrv  qTTS;

DEBUGOUTL3(110,"col_mn_analz:Enter lInstanceHandle=%08x cpInstanceData=%08x tMsgCom=%08x",
lInstanceHandle,cpInstanceData,tMsgCom);

	iDisp = 0;
	switch (tpRecvMsg->msg_disp) {
	case AKA_REGIST_CLASS:
		/* aka_register_class()でこの関数を登録したときに呼ばれる。 */
		if (cpInstanceData && *cpInstanceData == 'i') {
			/* プロセス起動時に、コマンド0x0ffffff0のスクリプトを実行する */
			memset((char *)&qTTS,0,sizeof(TrmToSrv));
			qTTS.commandid = htonl(0x0ffffff0);
			memset((char *)&tMsgCom,0,sizeof(AKAMSGCOM));
			tMsgCom.msg_clid = lInstanceHandle & 0x0000ffff;
			tMsgCom.msg_mlen = sizeof(TrmToSrv);
			tMsgCom.msg_pmsg = (char *)&qTTS;
			if ((iRc = aka_post_msg(0,&tMsgCom)) < 0)
				ERROROUT1("col_mn_analz:AKA_REGIST_CLASS: aka_post_msg ret=%d",iRc);
		}
		break;
	case 0:
		/* 他プロセスからの発信メッセージの処理。 */
		if (tpRecvMsg->msg_pret) {
			/* 返信が共通プラットフォーム内でエラーになり返された。 */
			break;
		}
	case 1:
		memset((char *)&tMsgCom,0,sizeof(AKAMSGCOM));
		tMsgCom.msg_filv = pFilev;
		pFilev[0] = filename;
								/* Packet Check & process flag set */
		iRc = cl_packet_check(lInstanceHandle,cpInstanceData,tpRecvMsg);
		if (iRc == 0) {
			pCLprocTable->WrPacketp = (char *)&tMsgCom;
			iRc = cl_script_main(); /* Script process */
		}

DEBUGOUTL3(150,"col_mn_analz: tMsgCom.cpMsg=%08x usDisposition=%04x iThread=%d",
tMsgCom.msg_pmsg,tMsgCom.msg_disp,pCLprocTable->iThread);

		if (tMsgCom.msg_pmsg) {
			if (pCLprocTable->iThread>0) {
				if (tMsgCom.msg_disp & 0x1000) {
					tMsgCom.msg_disp &= ~0x1000;
					memcpy(&iWaitTime,tMsgCom.msg_pmsg+tMsgCom.msg_mlen,sizeof(int));
					iRc = aka_send_msg_wait_time(lInstanceHandle,&tMsgCom,iWaitTime);
				}
				else {
					pDTD = (qDbToCol *)tMsgCom.msg_pmsg;
					if (ntohs(pDTD->usCmdNo) == 30001) {	/* SLEEP command */
						iRc = aka_set_sleep(lInstanceHandle,1,ntohl(pDTD->lret));
					}
					else {
						tMsgCom.msg_disp = 1;
						iWaitTime = ntohl(pDTD->lret);
						pDTD->lret = 0;
						iRc = aka_send_msg_wait_time(lInstanceHandle,&tMsgCom,iWaitTime);
					}
				}

DEBUGOUTL1(150,"col_mn_analz: iRc=%d",iRc);

				if (iRc >= 0) {
					packet_save(cpInstanceData);
					iDisp = 1;
				}
				else {
					ERROROUT1("SendMsg rc=%d",iRc);
				}
			}
			Free(tMsgCom.msg_pmsg);
			tMsgCom.msg_pmsg = NULL;
		}
		if (!iDisp) {
			val[0] = AKA_SHUT_MODE_SHUT;
			aka_shut_control(AKA_SHUT_MODE,val);
		}
	}

DEBUGOUTL1(110,"col_mn_analz:Exit iDisp=%d",iDisp);

	return iDisp;
}
