int recv_from_channel(char *chnlname) { SysLog(LOG_SYS_SHOW,"开始接收渠道[%s]返回报文\n",chnlname); _msgbuf *mbuf ; int msgidi,msgido,msgidr; int iret ; _tran *tranbuf; mbuf = (_msgbuf *)malloc(sizeof(_msgbuf)); if(mbuf == NULL) { printf("get mbuf error\n"); return -1; } memset(mbuf,0,sizeof(mbuf)); if(getmsgid(chnlname,&msgidi,&msgido,&msgidr)!=0) { SysLog(LOG_SYS_ERR,"获取渠道[%s]消息队列失败\n",chnlname); free(mbuf); return -1; } iret = msgrcv(msgidr,mbuf,sizeof(mbuf->tranbuf),innerid,0); if(iret == -1) { SysLog(LOG_SYS_ERR,"接收渠道[%s]返回报文失败:%s\n",chnlname,strerror(errno)); free(mbuf); return -1; } SysLog(LOG_SYS_SHOW,"接收渠道[%s]返回报文成功\n",chnlname); /** 开始解析返回报文**/ tranbuf = (_tran *)malloc(sizeof(_tran)); if(tranbuf == NULL) { SysLog(LOG_SYS_ERR,"申请交易数据内存失败[%s]\n",strerror(errno)); return -1; } memset(mbuf->tranbuf.chnlname,0,sizeof(mbuf->tranbuf.chnlname)); sprintf(mbuf->tranbuf.chnlname,"%s返回",chnlname); if((get_shm_hash(mbuf->innerid,tranbuf))!=-1) { SysLog(LOG_SYS_SHOW,"交易跟踪号[%ld]\t[%s]渠道返回信息信息[%s]\n",mbuf->innerid,chnlname,tranbuf->intran); if(unpack(mbuf->tranbuf.chnlname,tranbuf->intran,"|")==-1) { SysLog(LOG_SYS_ERR,"解[%s]包失败\t传入交易信息[%s]\n",mbuf->tranbuf.chnlname,tranbuf->intran); seterr("EEEEEEEE","解包失败"); } } free(tranbuf); free(mbuf); return 0; }
/** 发送报文到渠道**/ int send_to_channel(char *chnlname) { SysLog(LOG_SYS_SHOW,"开始发送报文到渠道[%s]\n",chnlname); _msgbuf *mbuf ; int msgidi,msgido,msgidr; int iret ; mbuf = (_msgbuf *)malloc(sizeof(_msgbuf)); if(mbuf == NULL) { SysLog(LOG_SYS_ERR,"申请msgbuf内存失败[%s]\n",strerror(errno)); return -1; } memset(mbuf,0,sizeof(mbuf)); mbuf->innerid = innerid; memset(mbuf->tranbuf.trancode,0,sizeof(mbuf->tranbuf.trancode)); strcpy(mbuf->tranbuf.trancode,"核心服务"); memset(mbuf->tranbuf.chnlname,0,sizeof(mbuf->tranbuf.chnlname)); mbuf->tranbuf.buffsize=123; if(getmsgid(chnlname,&msgidi,&msgido,&msgidr)!=0) { SysLog(LOG_SYS_ERR,"获取渠道[%s]消息队列失败\n",chnlname); free(mbuf); return -1; } iret = msgsnd(msgidi,mbuf,sizeof(mbuf->tranbuf),IPC_NOWAIT); if(iret == -1) { SysLog(LOG_SYS_ERR,"发送到渠道[%s]失败\n",chnlname); free(mbuf); return -1; } free(mbuf); SysLog(LOG_SYS_ERR,"发送到渠道[%s]成功\n",chnlname); return 0; }
/* ** setMQMD ** ** Convert the name=value pair string into an MQMD structure. ** The delimiter between the name=value pairs is '\3'. ** ** Inputs: ** input name=value pair string ** ** Outputs: ** output pointer to MQMD structure ** ** Returns: ** None */ void setMQMD(char *input, MQMD *output) { int i,pos,flag; char str_mqmd[MAX_MQMD_SIZE]; char msg_id[50]; char *mqmd_elmnt = NULL; char option[30],value[170]; char s_mqmd[200]; char value1[31]; char delim[2]; long lvalue; int msgflag = 0; delim[0] = '\3'; delim[1] = '\0'; #ifdef DEBUG fprintf(stdout,"setMQMD: input message - %s\nLength - %d\n",input,strlen(input)); fflush(stdout); #endif /* DEBUG */ memcpy((char *) str_mqmd,input,strlen(input)); mqmd_elmnt = strtok(str_mqmd,delim); flag = 0; for (i = 0; (i < MAX_MQMD_ELEMENTS)&&(mqmd_elmnt != NULL);i++) { lvalue = 0, pos = 0; memset(option,0,30); memset(value,0,170); sscanf(mqmd_elmnt,"%[^=]=%s",option,value); #ifdef DEBUG fprintf(stdout,"setMQMD: Option = %s | Value = %s\n",option,value); fflush(stdout); #endif /* DEBUG */ switch (getvalue(option)) { case 1: strcpy(output->StrucId,MQMD_STRUC_ID); break; case 2: lvalue = strtol(value,NULL,10); output->Version = lvalue; break; case 3: lvalue = strtol(value,NULL,10); output->Report = lvalue; break; case 4: lvalue = strtol(value,NULL,10); output->MsgType = lvalue; break; case 5: lvalue = strtol(value,NULL,10); output->Expiry = lvalue; break; case 6: lvalue = strtol(value,NULL,10); output->Feedback = lvalue; break; case 7: lvalue = strtol(value,NULL,10); output->Encoding = lvalue; break; case 8: lvalue = strtol(value,NULL,10); output->CodedCharSetId = lvalue; break; case 9: sprintf(output->Format,"%-8s\0",value); break; case 10: lvalue = strtol(value,NULL,10); output->Priority = lvalue; break; case 11: lvalue = strtol(value,NULL,10); output->Persistence = lvalue; break; case 12: memcpy(msg_id,mqmd_elmnt,strlen(mqmd_elmnt)); msgflag = 1; break; case 13: sprintf(output->CorrelId,"%-24s\0",value); break; case 14: lvalue = strtol(value,NULL,10); output->BackoutCount = lvalue; break; case 15: sprintf(output->ReplyToQ,"%-48s\0",value); break; case 16: sprintf(output->ReplyToQMgr,"%-48s\0",value); break; case 17: sprintf(output->UserIdentifier,"%-12s\0",value); break; case 18: sprintf(output->AccountingToken,"%-32s\0",value); break; case 19: sprintf(output->ApplIdentityData,"%-32s\0",value); break; case 20: lvalue = strtol(value,NULL,10); output->PutApplType = lvalue; break; case 21: sprintf(output->PutApplName,"%-28s\0",value); break; case 22: sprintf(output->PutDate,"%-8s\0",value); break; case 23: sprintf(output->PutTime,"%-8s\0",value); break; case 24: strcpy(output->ApplOriginData,value); break; case 25: strcpy(output->GroupId,value); break; case 26: lvalue = strtol(value,NULL,10); output->MsgSeqNumber = lvalue; break; case 27: lvalue = strtol(value,NULL,10); output->Offset = lvalue; break; case 28: lvalue = strtol(value,NULL,10); output->MsgFlags = lvalue; break; case 29: lvalue = strtol(value,NULL,10); output->OriginalLength = lvalue; break; default: #ifdef DEBUG fprintf(stdout,"setMQMD: Invalid MQMD element name - %s\n",option); fflush(stdout); #endif /* DEBUG */ break; } mqmd_elmnt = strtok(NULL,delim); } if (msgflag) { getmsgid(msg_id,value1); memcpy(output->MsgId,value1,24); } return; }
int main(int argc, char **argv) { /** 初始化全局共享内存前,先获取ups根路径 **/ if(setupshome()==-1) { printf("设置全局变量upshome错误,请检查UPSHOME环境变量是否设置\n"); return -1; } char startcmd[200]; int i=0; memset(startcmd,0x00,sizeof(startcmd)); /* Declare file and character for sample input */ /* Declare MQI structures needed */ MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */ MQMD md = {MQMD_DEFAULT}; /* Message Descriptor */ MQPMO pmo = {MQPMO_DEFAULT}; /* put message options */ /** note, sample uses defaults where it can **/ MQHCONN Hcon; /* connection handle */ MQHOBJ Hobj; /* object handle */ MQLONG O_options; /* MQOPEN options */ MQLONG C_options; /* MQCLOSE options */ MQLONG CompCode; /* completion code */ MQLONG OpenCode; /* MQOPEN completion code */ MQLONG Reason; /* reason code */ MQLONG CReason; /* reason code for MQCONN */ MQLONG messlen; /* message length */ int iret = 0; //pid_t pid; memset(chnlname,0,sizeof(chnlname)); strcpy(chnlname,"MQ发送渠道"); if(getchnlcfg(chnlname)!=0) { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d] 获取渠道[%s]配置失败\n",__FILE__,__LINE__,chnlname); return -1; } SysLog(LOG_CHNL_SHOW,"FILE[%s] LINE[%d] 远程队列管理器[%s]\n",__FILE__,__LINE__,QMName); SysLog(LOG_CHNL_SHOW,"FILE[%s] LINE[%d] 远程队列[%s]\n",__FILE__,__LINE__,RQName); mbuf = (_msgbuf *)malloc(sizeof(_msgbuf)); if(mbuf == (void *)-1) { SysLog(LOG_CHNL_ERR,"FILE [%s] LINE [%d]:MALLOC MSGBUF ERROR[%s]\n",__FILE__,__LINE__,strerror(errno)); return -1; } tranbuf = (_tran *)malloc(sizeof(_tran)); if(tranbuf == (void *)-1) { SysLog(LOG_CHNL_ERR,"FILE [%s] LINE [%d]:MALLOC TRANBUF ERROR[%s]\n",__FILE__,__LINE__,strerror(errno)); free(mbuf); return -1; } if(getmsgid(chnlname,&msgidi,&msgido,&msgidr)==-1) { SysLog(LOG_CHNL_ERR,"FILE [%s] LINE [%d]:GET CHANNEL[%s] MSGID ERROR[%s]\n",__FILE__,__LINE__,chnlname,strerror(errno)); return -1; } /** 设置忽略SIGPIPE信号,防止因socket写的时候客户端关闭导致的SIGPIPE信号 **/ signal(SIGPIPE,SIG_IGN); signal(35,child_exit); signal(SIGCHLD,child_exit); //signal(SIGCHLD,child_exit); /******************************************************************/ /* */ /* Connect to queue manager */ /* */ /******************************************************************/ //QMName[0] = 0; /* default */ //strncpy(QMName, "dev01", (size_t)MQ_Q_MGR_NAME_LENGTH); MQCONN(QMName, /* queue manager */ &Hcon, /* connection handle */ &CompCode, /* completion code */ &CReason); /* reason code */ /* report reason and stop if it failed */ if (CompCode == MQCC_FAILED) { SysLog(LOG_CHNL_ERR,"FILE [%s] LINE[%d] MQCONN MQ链接失败,错误码%d\n",__FILE__,__LINE__,CReason); exit( (int)CReason ); } /******************************************************************/ /* */ /* Use parameter as the name of the target queue */ /* */ /******************************************************************/ //strncpy(od.ObjectName, "RTEST", (size_t)MQ_Q_NAME_LENGTH); strncpy(od.ObjectName, RQName, (size_t)MQ_Q_NAME_LENGTH); SysLog(LOG_CHNL_SHOW,"FILE[%s] LINE[%d] target queue is %s\n",__FILE__,__LINE__, od.ObjectName); //strncpy(od.ObjectQMgrName, argv[5], (size_t) MQ_Q_MGR_NAME_LENGTH); //SysLog(LOG_CHNL_ERR,"target queue manager is %s\n", od.ObjectQMgrName); //strncpy(od.DynamicQName, argv[6], (size_t) MQ_Q_NAME_LENGTH); //SysLog(LOG_CHNL_ERR,"dynamic queue name is %s\n", od.DynamicQName); /******************************************************************/ /* */ /* Open the target message queue for output */ /* */ /******************************************************************/ if (argc > 3) { O_options = atoi( argv[3] ); SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d]open options are %d\n",__FILE__,__LINE__, O_options); } else { O_options = MQOO_OUTPUT /* open queue for output */ | MQOO_FAIL_IF_QUIESCING /* but not if MQM stopping */ ; /* = 0x2010 = 8208 decimal */ } MQOPEN(Hcon, /* connection handle */ &od, /* object descriptor for queue */ O_options, /* open options */ &Hobj, /* object handle */ &OpenCode, /* MQOPEN completion code */ &Reason); /* reason code */ /* report reason, if any; stop if failed */ if (Reason != MQRC_NONE) { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d]MQOPEN ended with reason code %d\n",__FILE__,__LINE__, Reason); } if (OpenCode == MQCC_FAILED) { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d]unable to open queue for output\n",__FILE__,__LINE__); } /******************************************************************/ /* */ /* Read lines from the file and put them to the message queue */ /* Loop until null line or end of file, or there is a failure */ /* */ /******************************************************************/ CompCode = OpenCode; /* use MQOPEN result for initial test */ memcpy(md.Format, /* character string format */ MQFMT_STRING, (size_t)MQ_FORMAT_LENGTH); pmo.Options = MQPMO_NO_SYNCPOINT | MQPMO_FAIL_IF_QUIESCING; /******************************************************************/ /* Use these options when connecting to Queue Managers that also */ /* support them, see the Application Programming Reference for */ /* details. */ /* The MQPMO_NEW_MSG_ID option causes the MsgId to be replaced, */ /* so that there is no need to reset it before each MQPUT. */ /* The MQPMO_NEW_CORREL_ID option causes the CorrelId to be */ /* replaced. */ /******************************************************************/ /* pmo.Options |= MQPMO_NEW_MSG_ID; */ /* pmo.Options |= MQPMO_NEW_CORREL_ID; */ signal(SIGUSR1,SIG_IGN); /** 添加该进程到进程控制表中,方便做统一处理 **/ for(i=0;i<argc;i++) { if(strlen(argv[i])==0) break; strcat(startcmd," "); strcat(startcmd,argv[i]); } strcat(startcmd," "); strcat(startcmd,"&"); if(insert_chnlreg(startcmd,chnlname)!=0) { SysLog(LOG_CHNL_ERR,"FILE [%s] LINE [%d]:添加渠道[%s]到监控内存失败\n",__FILE__,__LINE__,chnlname); return -1; } /** 从消息队列读取数据,进行后续处理 **/ while(1) { SysLog(LOG_CHNL_DEBUG,"FILE [%s] LINE [%d]:渠道[%s]开始服务\n",__FILE__,__LINE__,chnlname); memset(mbuf,0,sizeof(mbuf)); if(msgrcv(msgidi,mbuf,sizeof(mbuf->tranbuf),0,0)==-1) { if(errno == EINTR) { continue; }else { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d] 获取渠道[%s]消息队列消息失败[%s]\n",__FILE__,__LINE__,chnlname,strerror(errno)); sleep (5); continue; } } SysLog(LOG_CHNL_SHOW,"FILE[%s] LINE[%d] 渠道[%s]获取到跟踪号[%ld]\n",__FILE__,__LINE__,chnlname,mbuf->innerid); if(sendprocess(mbuf->innerid,&messlen)!=0) { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d] 处理失败\n",__FILE__,__LINE__); /** 返回交易信息到服务端**/ iret = shm_hash_update(mbuf->innerid,"EEEEEEE|发送失败",NULL); if(iret == -1) { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d] 放置打包信息到共享内存失败 \n",__FILE__,__LINE__); msgsnd(msgidr,mbuf,sizeof(mbuf->tranbuf),IPC_NOWAIT); }else { msgsnd(msgidr,mbuf,sizeof(mbuf->tranbuf),IPC_NOWAIT); } }else { /****************************************************************/ /* */ /* Put each buffer to the message queue */ /* */ /****************************************************************/ SysLog(LOG_CHNL_SHOW,"FILE[%s] LINE[%d]buffer is [%s] len is [%d]\n",__FILE__,__LINE__,buffer,messlen); if (messlen > 0) { /**************************************************************/ /* The following statement is not required if the */ /* MQPMO_NEW_MSG_ID option is used. */ /**************************************************************/ memcpy(md.MsgId, /* reset MsgId to get a new one */ MQMI_NONE, sizeof(md.MsgId) ); MQPUT(Hcon, /* connection handle */ Hobj, /* object handle */ &md, /* message descriptor */ &pmo, /* default options (datagram) */ messlen, /* message length */ buffer, /* message buffer */ &CompCode, /* completion code */ &Reason); /* reason code */ /* report reason, if any */ if (Reason != MQRC_NONE) { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d]MQPUT ended with reason code %d\n",__FILE__,__LINE__, Reason); /** 返回交易信息到服务端**/ iret = shm_hash_update(mbuf->innerid,"EEEEEEE|发送失败",NULL); if(iret == -1) { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d]放置打包信息到共享内存失败 \n",__FILE__,__LINE__); msgsnd(msgidr,mbuf,sizeof(mbuf->tranbuf),IPC_NOWAIT); }else { msgsnd(msgido,mbuf,sizeof(mbuf->tranbuf),IPC_NOWAIT); } } /** 返回交易信息到服务端**/ iret = shm_hash_update(mbuf->innerid,"AAAAAAA|发送成功",NULL); if(iret == -1) { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d]放置打包信息到共享内存失败 \n",__FILE__,__LINE__); msgsnd(msgidr,mbuf,sizeof(mbuf->tranbuf),IPC_NOWAIT); }else { msgsnd(msgidr,mbuf,sizeof(mbuf->tranbuf),IPC_NOWAIT); } } else /* satisfy end condition when empty line is read */ CompCode = MQCC_FAILED; } } /******************************************************************/ /* */ /* Close the target queue (if it was opened) */ /* */ /******************************************************************/ if (OpenCode != MQCC_FAILED) { if (argc > 4) { C_options = atoi( argv[4] ); SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d]close options are %d\n",__FILE__,__LINE__, C_options); } else { C_options = MQCO_NONE; /* no close options */ } MQCLOSE(Hcon, /* connection handle */ &Hobj, /* object handle */ C_options, &CompCode, /* completion code */ &Reason); /* reason code */ /* report reason, if any */ if (Reason != MQRC_NONE) { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d]MQCLOSE ended with reason code %d\n",__FILE__,__LINE__,Reason); } } /******************************************************************/ /* */ /* Disconnect from MQM if not already connected */ /* */ /******************************************************************/ if (CReason != MQRC_ALREADY_CONNECTED) { MQDISC(&Hcon, /* connection handle */ &CompCode, /* completion code */ &Reason); /* reason code */ /* report reason, if any */ if (Reason != MQRC_NONE) { SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d]MQDISC ended with reason code %d\n",__FILE__,__LINE__, Reason); } } /******************************************************************/ /* */ /* END OF AMQSPUT0 */ /* */ /******************************************************************/ SysLog(LOG_CHNL_ERR,"FILE[%s] LINE[%d]Sample AMQSPUT0 end\n",__FILE__,__LINE__); return(0); }