⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcs_rcv_zfjb.pc

📁 unix 下用pro*c tuxedo 开发的东西
💻 PC
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	File name		: Pcs_Rcv_ZFJB.pc				*				
*	Module ID		:						*
*	Module Name		:                               		*
*	Purpose			: Get ZFJB Data From MQ And Insert Into DB 	*
*	Author          	: Liu Yan Qiu (DIMPT)                		*
*	Date Created		: 1999,10,27                         		*
*	Version			: Version 1.0                       		*
*	Environment		: Digital UNIX                       		*
*	Portability		: UNIX Platform                     		*
*	Warnings		:                                  		*
*	References		:                                  		*
*	Calling Syntax		:                                  		*
*	Parameters		: None                               		*
*	Returns			: void                                 		*
*	Calling Function	:                                        	*
*	Called Functions	: When system startup                   	*
*	Datastores and usages	:                                   		*
*       	(a) Input                                              		*
* 	   	(b) OutPut                                           		*       
*	Report			: None                                 		*
*	Screens			: None                              		*
*	Messages Files		: None                                		*
*	Change Log		:                                       	*
*	Change No. 	Date	Author	Reason For Change    			*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/


#include "/usr/bkfx/include/Pcs_Rcv_ZFJB/Pcs_Rcv_ZFJB.h"


void main()
{

   	signal(SIGTERM, Free_Mem);
   
   	EXEC SQL INCLUDE SQLCA;

	ProcessError(LOG_NORMAL, "Pcs_Rcv_ZFJB.x Is Begin!",END);  

	while(1)
	{
		rtn_code = MQBKInit("ZFJ");
    		if ( rtn_code < 0 )
      		{
       			ProcessError(LOG_ERROR,"Calling Funtion MQBKInit() Failed!",END);
			sleep(InitInterval);
     		}
		else
			break;
	} 	

   	rtn_code=InitRcvZFJB();
   	if ( rtn_code < 0 )
     	{
      		ProcessError(LOG_ERROR,"PCS_RCV_ZFJB.X: Calling Function InitRcvZFJB() \
      					Failed!",END);
     	}

    	rtn_code = PCSRcvZFJB();
     	if ( rtn_code < 0 )
      	{
       		ProcessError(LOG_ERROR,"PCS_RCV_ZFJB.X: Calling Function PCSRcvZFJB() \
       					Failed!",END);
      	}
  
   	
}    	
    	
    	
  int PCSRcvZFJB()
  {    	
  	int i=0;
  	
  	while (1)
        {
                rtn_code = ReadZFJBMsg();
                if ( rtn_code == -2 )  /* no message arrived */
                {
                        sleep( interval );
                }

                else if ( rtn_code == -3 ) /* received an error message */
                {

                        ProcessError(LOG_ERROR,"PCS_RCV_ZFJB.X: Can't Get Correct Message \
                        			From ZFJB MQ! Now Skip The Current Message \
                        			And Try To Read Next One!",END);
			sleep( interval );
                }
 
 		else
                {
                        rtn_code = CreateRcvLogStatus();
                        if ( rtn_code < 0 ) continue;

                        rtn_code = InsertRcvZFJB();
                        if ( rtn_code < 0 ) continue;

                        rtn_code = InsertRcvZFJBNR();
                        if ( rtn_code < 0 ) continue;

                        rtn_code = Snd_HZ_MQ();
                        if ( rtn_code < 0 ) continue;
                        
                        rtn_code = UpdateRcvLogStatus();
     			if ( rtn_code < 0 ) continue;

/*			sleep( interval ); */
                }
        }

        return(1);
 
}               
  

void Free_Mem()
{
	ProcessError(LOG_NORMAL, "Pcs_Rcv_ZFJB.x Is Terminated By User!",END);
 	if ( tmp != NULL ) free( tmp );
        if ( buf_hz != NULL ) free( buf_hz);
        if ( data_type != NULL ) free( data_type );
        MQBKClear();
        EXEC SQL RollBack RELEASE;
	exit(0);

}

int InitRcvZFJB()
{
	

        EXEC SQL BEGIN DECLARE SECTION;
                VARCHAR  userid[20];
                VARCHAR  password[20];
        EXEC SQL END DECLARE SECTION;

	
        EXEC SQL WHENEVER SQLERROR GOTO sqlerror;

        strcpy( userid.arr, "pcsdba" );
        userid.len = strlen( userid.arr );
        strcpy( password.arr, "dba" );
        password.len = strlen( password.arr );

        EXEC SQL CONNECT :userid IDENTIFIED BY :password;

        interval = GetTimeInterval("PCS_RCV_ZFJB");

	return(0);

	sqlerror:
	EXEC SQL WHENEVER SQLERROR CONTINUE;
	ProcessError(LOG_ERROR,   "DataBase Error:%s!", sqlca.sqlerrm.sqlerrmc );
	exit(-1);
}


int ReadZFJBMsg()  
{
	char 	c_nf[4];
	

	while(1)
	{

		if ( (( buf = (char *)malloc( LEN_ZFJB_BUF ))==NULL) || \
             		(( data_type = (char *)malloc( LEN_DATA_TYPE ))==NULL) )
        	{
                	ProcessError(LOG_ERROR,"PCS_RCV_ZFJB.X: Malloc ZFJB Buffer Error!",END);
			sleep(InitInterval);
     		}
		else
			break;
	}

        tmp = buf; 
	
	rtn_code = MQBKReceiveSyn_Wait( &buf_length, buf, data_type );
	if ( rtn_code < 0 )  /* no message arrived */
        {
                free( tmp );
                free( data_type );
                return(-2);
        }

   	else
        {

        #ifdef DEBUG
                printf("recv code and data_type is %d %s\n",rtn_code,data_type);

        #endif
                if (( data_type[0] == 'Z' ) && ( data_type[1]=='F' ) && \
                    ( data_type[2] == 'J' ))
                {
   		
   	 		strncpy( c_fsxh, buf, 10);
 			c_fsxh[10]='\0';       
 			fsxh = atoi( c_fsxh);
 			buf = buf + 10;
 
 			strncpy( fssjbh, buf, 2);
 			fssjbh[2]='\0';
 			buf = buf + 2;
 
 			strncpy( jssjbh,buf,2);
 			jssjbh[2]='\0';
 			buf = buf + 2;

 			strncpy( c_nf,buf,4);
 			c_nf[4]='\0';
 			nf = atoi(c_nf);
 			buf = buf + 4;
 
 			#ifdef DEBUG
                        printf("fsxh is %s \n",c_fsxh);
                        printf("fssjbh, jssjbh, nf is: %s,%s,%s\n",fssjbh,jssjbh,nf);
                        #endif
        
                        free( data_type );

                }
                else            /* received an error message */
 		{
                        free( tmp );
                        free( data_type );
                        return(-3);
                }
        
        }
        return(1);      
}


int CreateRcvLogStatus()
{

	EXEC SQL WHENEVER SQLERROR GOTO sqlerror;

	EXEC SQL INSERT INTO T_JSRZ 
         		VALUES( :fsxh, '4', '1', :fssjbh, sysdate );
  
	return(1);

	sqlerror:
	EXEC SQL WHENEVER SQLERROR CONTINUE;
	ProcessError(LOG_ERROR,   "DataBase Error:%s!", sqlca.sqlerrm.sqlerrmc );
   	ProcessError(LOG_ERROR,"PCS_RCV_ZFJB.X: Running Function CreateRcvLogStatus() \
   				SQL Error!",END);
   	free( tmp );
   	EXEC SQL RollBack;
   	return (-1);
}


int InsertRcvZFJB()
{

	EXEC SQL WHENEVER SQLERROR GOTO sqlerror;

/*Delete the duplicate records first, the sql do nothing if no duplicate. 
by Mr. zjk 2000.05.12*/
	EXEC SQL DELETE FROM t_jszfjbnr WHERE jsxh IN (\
		SELECT jsxh FROM t_jszfjb WHERE sjbh=:fssjbh and nf=:nf);
	EXEC SQL DELETE FROM t_jszfjb WHERE sjbh=:fssjbh and nf=:nf;

/* This paragraph was cut from function: InsertRcvZFJBNR(), by Mr. zjk 2000.05.12

	EXEC SQL select count(*) into :count_zfj from t_jszfjb where sjbh=:fssjbh and nf=:nf;
	if ( count_zfj > 0 ) 
	{
		EXEC SQL delete t_jszfjbnr where sjbh=:fssjbh and t_jszfjb.nf=:nf and  \
				t_jszfjb.jsxh=t_jszfjbnr.jsxh and t_jszfjb.sjbh=t_jszfjbnr.sjbh;
		EXEC SQL delete t_jszfjb where sjbh=:fssjbh and nf=:nf;
		EXEC SQL COMMIT WORK;
	}	
*/


	EXEC SQL INSERT INTO T_JSZFJB VALUES( :nf, :fssjbh, :fsxh );

	return(1);

	sqlerror:
	EXEC SQL WHENEVER SQLERROR CONTINUE;
	ProcessError(LOG_ERROR,   "DataBase Error:%s!", sqlca.sqlerrm.sqlerrmc );
  	ProcessError(LOG_ERROR,"PCS_RCV_ZFJB.X: Running Function InsertRcvZFJB() \
  				SQL Error!",END);
  	free( tmp );
  	EXEC SQL RollBack;
  	return (-1);
  	
}

int InsertRcvZFJBNR()
{
  	
  	char zfjbh[9], zfjmc[17];
	int  count_zfj;
	long ll_zfjxh;  /* by Mr. zjk 2000.05.11 */
  
    EXEC SQL WHENEVER SQLERROR GOTO sqlerror;

	ll_zfjxh = 1; /* by Mr. zjk 2000.05.11 */

  	while ( *buf != '\0' )	
        {
	   	strncpy( zfjbh, buf, 8);
	   	zfjbh[8]='\0';
	   	buf = buf+8;
                       
	   	strncpy( zfjmc, buf, 16);
	   	zfjmc[16]='\0';
	   	buf = buf+16;
            
	  	EXEC SQL INSERT INTO T_JSZFJBNR 
         		   	VALUES (:fssjbh, rtrim(:zfjbh), rtrim(:zfjmc), :fsxh, :ll_zfjxh ); /* by Mr. zjk 2000.05.11 */
		EXEC SQL COMMIT WORK;
		ll_zfjxh++; /* by Mr. zjk 2000.05.11 */
         		   	                   
        }

        #ifdef DEBUG
                printf("the  zfjbh zfjmc is %s %s\n",zfjbh,zfjmc);
        #endif

        free( tmp );

        return(1);

 	sqlerror:
	EXEC SQL WHENEVER SQLERROR CONTINUE;
	ProcessError(LOG_ERROR,   "DataBase Error:%s!", sqlca.sqlerrm.sqlerrmc );
 	ProcessError(LOG_ERROR,"PCS_RCV_ZFJB.X: Running Function InsertRcvZFJBNR() \
 				SQL Error!",END);
        free( tmp );
	EXEC SQL select count(*) into :count_zfj from t_jszfjb where sjbh=:fssjbh and nf=:nf;
	if ( count_zfj > 0 ) 
	{
		EXEC SQL delete t_jszfjbnr where sjbh=:fssjbh and t_jszfjb.nf=:nf  and \
				t_jszfjb.jsxh=t_jszfjbnr.jsxh and t_jszfjb.sjbh=t_jszfjbnr.sjbh;
		EXEC SQL delete t_jszfjb where jsxh=:fsxh and sjbh=:fssjbh;
		EXEC SQL delete from t_jsrz where jsxh=:fsxh and sjbh=:fssjbh ;
		EXEC SQL COMMIT WORK;
	}			
	EXEC SQL RollBack;
        return (-1);

}


int Snd_HZ_MQ()
{

 	if ( (buf_hz = (char *) malloc (LEN_HZ)) == NULL )
        {
                ProcessError(LOG_ERROR,"PCS_RCV_GH.X: Malloc Send HZ Buffer Error!",END);
                return(-1);
        }
 	memset( buf_hz, '\0', LEN_HZ); 
 
 	strncat( buf_hz, c_fsxh, 11);
 	strncat( buf_hz, fssjbh, 3);
 	strncat( buf_hz, jssjbh, 3);
	
	#ifdef DEBUG
                printf("the hz msg is %s\n",buf_hz);
        #endif
        
	rtn_code = MQBKSend_Syn( strlen(buf_hz), buf_hz, 0, "HZ");
        free(buf_hz);
        if ( rtn_code <0 )
        {
                ProcessError(LOG_ERROR,"PCS_RCV_GH.X: Sendding HZ Message Error!",END);
                MQBKBack();
                return(-1);
        }

        rtn_code = MQBKCmit();
        if ( rtn_code < 0 )
        {
                ProcessError(LOG_ERROR,"PCS_RCV_GH.X: Commit HZ Message Error!",END);
                MQBKBack();
                return(-1);
        }
        
        return(1);
}


int UpdateRcvLogStatus()
{
	EXEC SQL WHENEVER SQLERROR GOTO sqlerror;

	EXEC SQL UPDATE T_JSRZ SET sjzt='2' WHERE jsxh=:fsxh;

	EXEC SQL COMMIT WORK ;

	return(1);

	sqlerror:
	EXEC SQL WHENEVER SQLERROR CONTINUE;
	ProcessError(LOG_ERROR,   "DataBase Error:%s!", sqlca.sqlerrm.sqlerrmc );
   	ProcessError(LOG_ERROR,"PCS_RCV_ZFJB.X: Running Function UpdateRcvLogStatus() \
   				SQL Error!",END);
   	EXEC SQL RollBack;
   	return (-1);
   	
}


/*Get the Time_Interval specified in the file and return it */
int GetTimeInterval(char* cType)
{
	FILE *hTimeInterval;
	char cLine[MAX_LINE_LEN+1], cCaption[100], cValue[21];
	int iInterval, i;
	char* pLine;
	
	 
	hTimeInterval = fopen( CONFIG_FILE_FOR_TIMEINTERVAL, "r" );
	if( hTimeInterval == NULL )
	{
		ProcessError(LOG_ERROR, "GetTimeInterval: this is %s message:Cannot open file: %s,The file may not exist!",END);
		return(DEFAULT_TIMEINTERVAL);  
	}
	
	while( myapi_ReadALine( cLine, hTimeInterval ) )
	{
		if( cLine[0] == '#' ) continue;
		if( cLine[0] == NULL ) continue;
		
		pLine = cLine;
		while( (*pLine == ' ') || (*pLine == '\t') ) pLine++;
		i=0;
		while( (*pLine != ' ') && (*pLine != '\t') && (*pLine != '\0') )
		{
			cCaption[i] = *pLine;
			i++;
			pLine++;
		}
		
		cCaption[i] = '\0';
		if( strcmp( cCaption, cType ) == 0 ) /*Match*/
		{
			while( (*pLine == ' ') || (*pLine == '\t') ) pLine++;
			if( *pLine == '\0' )
			{	fclose( hTimeInterval );
				return(DEFAULT_TIMEINTERVAL);
			}
			i=0;
			while( (*pLine != ' ') && (*pLine != '\t') && (*pLine != '\0') )
			{
				cValue[i] = *pLine;
				i++;
				pLine++;
			}
			cValue[i] = '\0';
			iInterval = atoi(cValue);
			
			if(iInterval <= 0) 		
			{
				fclose( hTimeInterval );
				return(DEFAULT_TIMEINTERVAL);
			}
			else
				fclose( hTimeInterval );
				return(iInterval);
		}
		else
			continue;
	}
	fclose( hTimeInterval );
	return(DEFAULT_TIMEINTERVAL);
}

/*Read a line to a string from a file, \n is excluded.
  return 1 if sucessful, 0 otherwise
  Do not close the file.*/
int myapi_ReadALine( char* cLine, FILE* hTable )
{
	char cChar;
	int i=0;
	cLine[0] = '\0';
	while( (cChar = fgetc( hTable )) != '\n' )
	{
		if( cChar == EOF ) return(0);
		cLine[i++] = cChar;
		
		
		if( i >= MAX_LINE_LEN ) i=0;
		cLine[i] = '\0';
	}
	return(1);
}

void ProcessError( char *LOG, char *sErrorMessage1, char *sErrorMessage2 )
{
	FILE* hErrorFile;
	char cDateTime[100],tempstr[100];
	int i,j,len,n;
	
	tempstr[0]='\0';
	len=strlen(sErrorMessage1);
	for(i=0;i<=len;i++)
	{
		if(sErrorMessage1[i]=='%')
		{			
			n=i;
			if(len!=i+1)
			{
				for(j=0;j<200&&sErrorMessage1[i+2]!='\0';j++,i++)
				tempstr[j]=sErrorMessage1[i+2];
				tempstr[j]='\0';
			}	
			break;
		}
		if(sErrorMessage1[i]=='\0')
		{
			n=i;
			tempstr[0] = '\0';
			break;
		}
			
	}
	
	
	sErrorMessage1[n]='\0';
	hErrorFile = fopen( ERROR_LOG_FILE, "a" );
	if( hErrorFile == NULL )
	{
		printf( "Critical! Cannot open ERROR_LOG_FILE for append or cannot create!\n");
		return;
	}
	fseek( hErrorFile, 0 ,SEEK_END );
	fputs( "\n",hErrorFile);
	fputs( LOG ,hErrorFile);
	printf("\n%s",LOG);
	fputs( sErrorMessage1,hErrorFile );
	printf("%s",sErrorMessage1);
	fputs( sErrorMessage2,hErrorFile );
	printf("%s",sErrorMessage2);
	fputs( tempstr,hErrorFile );
	printf("%s\n",tempstr);
	fputs( "\n",hErrorFile);
	fclose( hErrorFile );
	sprintf( cDateTime, "date >> %s\n", ERROR_LOG_FILE );
	system( cDateTime );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -