?? trans_ser.ec
字號:
/*
** 功能描述:描述交易報文處理模塊
** 開發時間:2006-6-26
** 開發人員:郝憲祿、曾衍濤
** 運行環境:UNIX、ESQL
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <time.h>
EXEC SQL include sqlca;
#define MAX_TRACE_LEN 1024
#define MAX_FIELDS 126;
#define DEBUG
void WriteLog(char *DispStr,char *TraceStr,int len);int get_comm(char *rvcd);
int get_rcvtrcd( char *ogno,char *tran);
int up_pack(char *in_buf,int in_len,char *itid,char *flag);
int dw_pack(char flag,char *itid,char *out_buf,int *out_len);
int shift_data(sUP_DATA *data_from, sDW_DATA *data_to);
EXEC SQL begin declare section; EXEC SQL include "../include/comm_tb.h"; EXEC SQL include "../include/commcont.h"; EXEC SQL include "../include/rcvtrcd.h";
EXEC SQL include "../include/up_data.h";
EXEC SQL include "../include/dw_data.h"; COMM_TB sCOMM_TB; COMMCONT sCOMMCONT; RCVTRCD sRCVTRCD;
UP_DATA sUP_DATA[MAX_FIELDS];
DW_DATA sDW_DATA[MAX_FIELDS];
EXEC SQL end declare section;
/*
** 函數功能:轉換包文主函數
** 輸入參數:
** tag_flag :特征碼
** trans_id :交易碼
** up_buff :上傳報文
** 輸出參數:
** dw_buff :下傳報文
** dw_len :下傳報文長度
** 程 序 員:郝憲祿-hxl
** 開發日期:2006-06-26
*/
int trans_ser(char *tag_flag,char *trans_id,char *up_buff,char *dw_buff,int *dw_len)
{
EXEC SQL database dbfront;
WriteLog("上傳報文",up_buff,up_len);
memset((char *)&sCOMM_TB,0x00,sizeof(sCOMM_TB));
memset((char *)&sCOMMCONT,0x00,sizeof(sCOMMCONT)); memset((char *)&sRCVTRCD,0x00,sizeof(sRCVTRCD)); memset((char *)&sUP_DATA[MAX_FIELDS],0x00,sizeof(sUP_DATA)[MAX_FIELDS]);
memset((char *)&sDW_DATA[MAX_FIELDS],0x00,sizeof(sDW_DATA)[MAX_FIELDS]);
/*獲取COMM_TB結構體內容*/
if (get_comm(tag_flag)!= 0)
{
#ifdef DEBUG
WriteLog("驗證來源失敗",tag_flag,strlen(tag_flag));
printf("驗證來源失敗!!!\n");
#endif
goto err_exit; }
WriteLog("驗證來源成功",tag_flag,strlen(tag_flag));
WriteLog("單位ID",sCOMM_TB.UPTP,strlen(sCOMM_TB.UPTP));
/*獲取RCVTRCD結構體內容*/
if (get_rcvtrcd(sCOMM_TB.UPTP,trans_id))!= 0)
{
#ifdef DEBUG
WriteLog("交易映射失敗",trans_id,strlen(trans_id));
printf("交易映射失敗!!!\n");
#endif
goto err_exit; }
WriteLog("交易映射成功",trans_id,strlen(trans_id));
/*上傳報文解報*/
if(up_pack(up_buff,up_len,sRCVTRCD.ITID,sCOMM_TB.UPTY)!=0)
{
#ifdef DEBUG
WriteLog("分解上傳報文失敗",sRCVTRCD.ITID,strlen(sRCVTRCD.ITID));
printf("分解上傳報文失敗!!!\n");
#endif
goto err_exit;
}
WriteLog("組建下傳報文",ostr,outlen);
/*下傳報文組報*/
if(dw_pack(sRCVTRCD.ITID,sCOMM_TB.UPTY,ostr,&outlen)!=0)
{
#ifdef DEBUG
WriteLog("組建下傳報文失敗",sRCVTRCD.ITID,strlen(sRCVTRCD.ITID));
printf("組建下傳報文失敗!!!\n");
#endif
goto err_exit;
}
WriteLog("組建下傳報文",ostr,outlen);
/*返回上傳報文*/
*dw_len=outlen;
memcpy(dw_buff,ostr,dw_len);
EXEC SQL close database; return(0);
err_exit:
EXEC SQL close database; return(-1);
}
/*
** 函數功能:上傳報文解析處理
** 輸入參數:
** in_buf 上傳報文
** in_len 上傳報文長度
** itid_str 交易碼
** flag 報文格式
** 輸出參數:
**
** 程 序 員:郝憲祿-hxl
** 開發日期:2006-06-27
*/
int up_pack(char *in_buf,int in_len,char *itid,char *flag)
{
EXEC SQL begin declare section;
char itid_str[21];
EXEC SQL end declare section;
int ok=0;
int fd_num;
int i;
int r;
//*********************************
//*********added by Zeng Yantao****
char *buf_index1, *buf_index2;
int len_uptf;
int len_tok=0;
int len_roff;
int i_;
//*********************************
EXEC SQL declare commcont_cur cursor for select * from commcont where stcd = '0' and itid = itid_str ; EXEC SQL open commcont_cur; for(;;) { EXEC SQL fetch commcont_cur into :sCOMMCONT; if(sqlca.sqlcode == 100) break; if(sqlca.sqlcode < 0 ) { goto Error_exit; } ok++;
sUP_DATA[sCOMMCONT.SQNO].FDLI=sCOMMCONT.FDLI; memcpy(sUP_DATA[sCOMMCONT.SQNO].FDID,sCOMMCONT.FDID,sCOMMCONT.FDLI); sUP_DATA[sCOMMCONT.SQNO].FDLN=sCOMMCONT.FDLN; sUP_DATA[sCOMMCONT.SQNO].GTTP=sCOMMCONT.GTTP; sUP_DATA[sCOMMCONT.SQNO].CVTP=sCOMMCONT.CVTP;
fd_num++; } EXEC SQL close commcont_cur;
switch(flag[0]) {
/*定長解報文處理*/ case '0':
for(i=1;i<=fd_num;i++) {
if(sUP_DATA[i].FDLN<=0) goto Error_exit; memcpy(sUP_DATA[i].FDVL,in_buf+r,sUP_DATA[i].FDLN); sUP_DATA[i].FDVL[sUP_DATA[i].FDLN]=0x00;
r+=sUP_DATA[i].FDLN;
}
break;
/*分割符解報文處理*/
case '1':
//*********************************************************
//*************writen by Zeng Yantao***********************
len_roff=in_len;
len_uptf=strlen(sCOMM_TB.UPTF);
for(i_=1;i_<=fd_num;i_++,buf_index1=buf_index2+len_uptf){
/*
if(sUP_DATA[i_].FDLN<=0)
goto Error_exit;
*/
if( i_==1 )buf_index1 = in_buf;
if(buf_index1-in_buf>=in_len)break;
if( (buf_index2 = strstr(buf_index1 , sCOMM_TB.UPTF)) == NULL ){
buf_index2 = buf_index1+len_roff;
}
len_tok = buf_index2 - buf_index1;
memcpy(sUP_DATA[i_].FDVL,buf_index1,len_tok);
sUP_DATA[i_].FDVL[len_tok]='\0';
len_roff=len_roff-(len_tok+len_uptf);
}
//***********************************************************
//***********************************************************
/* for(i=1;i<=fd_num;i++) {
if(sUP_DATA[i].FDLN<=0) goto Error_exit;
/*
memset(in_buf_td,0x00,sizeof(in_buf_td));
memccpy(s_tmp,in_buf_ts,'|',strlen(in_buf_ts));
memcpy(sUP_DATA[i].FDVL,s_tmp,strlen(s_tmp)-1);
sUP_DATA[i].FDVL[sUP_DATA[i].FDLN]=0x00;
memcpy(in_buf_td,in_buf_ts+strlen(s_tmp),strlen(in_buf_ts)-strlen(s_tmp));
memset(in_buf_ts,0x00,sizeof(in_buf_ts));
memcpy(in_buf_ts,in_buf_td,strlen(in_buf_td));
*/
sUP_DATA[i].FDVL[sUP_DATA[i].FDLN]=0x00;
}
*/
//*****************************************************************
break;
/*標記解報文處理*/
case '2':
/*暫時不考慮*/
goto err_exit;
break;
/*錯誤報文*/
default:
goto err_exit; break; }
return 0;
err_exit:
return -1;
}
/*
** 函數功能:下傳報文組報處理
** 輸入參數:
** itid_str 交易碼
** flag 報文格式
** 輸出參數:
** out_buf 下傳報文
** out_len 下傳報文長度
** 程 序 員:郝憲祿-hxl
** 開發日期:2006-06-28
*/
int dw_pack(char *flag,char *itid,char *out_buf,int *out_len)
{
EXEC SQL begin declare section;
char itid_str[21];
EXEC SQL end declare section;
int ok=0;
int fd_num;
int i;
int j;
int r;
char tmpstr[61];
memset(itid_str,0x00,sizeof(itid_str));
memcpy(itid_str,itid,strlen(itid));
EXEC SQL declare commcont_cur cursor for select * from commcont where stcd = '0' and itid = itid_str ; EXEC SQL open commcont_cur; for(;;) { EXEC SQL fetch commcont_cur into :sCOMMCONT; if(sqlca.sqlcode == 100) break; if(sqlca.sqlcode < 0 ) { goto Error_exit; } ok++; sDW_DATA[sCOMMCONT.SQNO].FDLI=sCOMMCONT.FDLI; memcpy(sDW_DATA[sCOMMCONT.SQNO].FDID,sCOMMCONT.FDID,sCOMMCONT.FDLI); memcpy(sDW_DATA[sCOMMCONT.SQNO].FDVL,sCOMMCONT.FDVL, sCOMMCONT.FDLN);
sDW_DATA[sCOMMCONT.SQNO].FDLN=sCOMMCONT.FDLN; sDW_DATA[sCOMMCONT.SQNO].GTTP=sCOMMCONT.GTTP; sDW_DATA[sCOMMCONT.SQNO].CVTP=sCOMMCONT.CVTP; fd_num++; } EXEC SQL close commcont_cur;
/*上下報文交易碼映射*/
// memcpy(
/*組報處理*/
switch(flag[0]) {
/*定長組報文處理*/ case '0':
for(i=1;i<fd_num;i++) {
for(j=1;j<MAX_FIELDS;j++)
{ if(strncmp(sDW_DATA[i].FDID,sUP_DATA[j].FDID,sDW_DATA[i].FDLI)==0) {
/*memset(tmpstr,0x00,strlen(tmpstr)); 域值轉換函數
memcpy(tmpstr,sUP_DATA[j].FDVL, sUP_DATA[j].FDLN);
*/
if( shift_data(&sUP_DATA[j], &sDW_DATA[i])!=0)goto err_exit;//writen by Zeng Yantao. break; }
}
memcpy(out_buf+r,sDW_DATA[i].FDVL, sDW_DATA1[i].FDLN);
r+=sDW_DATA[i].FDLN; }
break;
/*分割符組報文處理*/
case '1':
for(i=1;i<fd_num;i++) {
for(j=1;j<MAX_FIELDS;j++)
{ if(strncmp(sDW_DATA[i].FDID,sUP_DATA[j].FDID,sDW_DATA[i].FDLI)==0) {
/*memset(tmpstr,0x00,strlen(tmpstr));
** **memcpy(tmpstr,sUP_DATA[j].FDVL, strlen(sUP_DATA[j].FDVL));
**strcat(tmpstr,sCOMM_TB.DWTF);*/
if( shift_data(&sUP_DATA[j], &sDW_DATA[i])!=0)goto err_exit;//writen by Zeng Yantao. break; }
}
strcat(sDW_DATA[i].FDVL,sCOMM_TB.DWTF);
memcpy(out_buf+r,sDW_DATA[i].FDVL,strlen(sDW_DATA[i].FDVL));
r+=strlen(sDW_DATA[i].FDVL); }
break;
/*標記組報文處理*/
case '2':
break;
/*錯誤報文*/
default:
goto err_exit; break; }
*out_len = r;
return 0;
err_exit:
return -1;
}
/*
** 函數功能:交易碼映射處理
** 輸入參數:
** ogno_str 單位ID
** 輸出參數:
** 獲取RCVTRCD結構體內容
** 程 序 員:郝憲祿-hxl
** 開發日期:2006-06-27
*/
int get_rcvtrcd( char *ogno,char *tran){
EXEC SQL begin declare section;
char ogno_str[5];
char tran_str[9];
EXEC SQL end declare section;
int ok=0;
memset(ogno_str,0x00,5);
memset(tran_str,0x00,9);
memcpy(ogno_str,ogno,strlen(ogno));
memcpy(tran_str,tran,strlen(tran));
EXEC SQL declare rcvtrcd_cur cursor for select * from rcvtrcd
where ogno=ogno_str and uptr=tran_str and stcd = '0'; EXEC SQL open rcvtrcd_cur; for(;;) { EXEC SQL fetch rcvtrcd_cur into :sRCVTRCD; if(sqlca.sqlcode == 100) break; if(sqlca.sqlcode < 0 ) {
#ifdef DEBUG printf("error code [%d]\n",sqlca.sqlcode);
#endif goto Error_exit; } ok++; break; } if(ok==0) {
#ifdef DEBUG printf("無特殊處理\n");
#endif goto Error_exit; }
EXEC SQL close rcvtrcd_cur;
return (0);
Error_exit: EXEC SQL close rcvtrcd_cur; return (-1);}
/*
** 函數功能:判斷來源包的標識
** 輸入參數:
** rvcd_str 特征碼
** 輸出參數:
** 獲取COMM_TB結構體內容
** 程 序 員:郝憲祿-hxl
** 開發日期:2006-06-27
*/int get_comm( char *rvcd ){
EXEC SQL begin declare section;
char rvcd_str[11];
EXEC SQL end declare section;
int ok=0;
memset(rvcd_str,0x00,11);
memcpy(rvcd_str,rvcd,strlen(rvcd));
EXEC SQL declare comm_cur cursor for select * from comm_tb where rvcd=rvcd_str and stcd = '0' order by cdln desc; EXEC SQL open comm_cur; for(;;) { EXEC SQL fetch comm_cur into :sCOMM_TB; if(sqlca.sqlcode == 100) break; if(sqlca.sqlcode < 0 ) {
#ifdef DEBUG printf("error code [%d]\n",sqlca.sqlcode);
#endif goto Error_exit; } ok++; break; } if(ok==0) {
#ifdef DEBUG printf("無特殊處理\n");
#endif goto Error_exit; }
EXEC SQL close comm_cur;
return (0);
Error_exit: EXEC SQL close comm_cur; return (-1);}
/*
** 函數功能:轉換包文
** 輸入參數:
** DispStr :提示信息
** TraceStr :日志信息
** len :日志信息長度
** 輸出參數:
** 無
** 程 序 員:郝憲祿-hxl
** 開發日期:2006-06-26
*/void WriteLog(char *DispStr,char *TraceStr,int len){ FILE *fp; time_t t1; struct tm *t2; int D_line,lineOK,i,line; char TraceFile[20],tmpbuf[81]; t1=time(0); t2=localtime(&t1); sprintf(TraceFile,"../Trace%02d%02d.log",t2->tm_mon+1,t2->tm_mday); fp=fopen(TraceFile,"a+"); if(fp==NULL) return; fprintf(fp,"[%04d/%02d/%02d %02d:%02d:%02d] ",t2->tm_year+1900,t2->tm_mon+1,t2->tm_mday,t2->tm_hour,t2->tm_min,t2->tm_sec); fprintf(fp,"%s len=%d\n",DispStr,len); fprintf(fp,"01 02 03 04 05 06 07 08-11 12 13 14 15 16 17 18\n"); fprintf(fp,"===============================================================================\n"); fprintf(fp,"[%s]\n",TraceStr); fprintf(fp,"===============================================================================\n"); fclose(fp);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -