📄 cmpp_dr.java
字号:
package com.hoten.cmpp.type;import com.hoten.cmpp.message.*;import com.hoten.cmpp.socket.*;import com.hoten.cmpp.util.*;import java.util.*;import java.io.*;import java.net.*;import com.hoten.cmpp.*;/** * <p>Title: 通信主类</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class CMPP_DR implements CMPP_Service{ private CMPP_InitMessage initMsg=CMPP_InitMessage.getInstance(); private SocketManager socketManger=null; private String logFile=initMsg.logFile; private Socket s=null; private String poolName=null; private int flag=-1; private long time=initMsg.timeout; private Vector deliverMsgList=null; private DataInputStream in=null; private DataOutputStream out=null; private String user = null; private int C=initMsg.C; private int N=initMsg.N; private int sendOut=1; private String icp=null; private int checkTime=(int)(System.currentTimeMillis()/1000); public CMPP_DR() { } //初始化CMPP public int initCMPP(String poolName,String host,String icp_ID,String user,String auth,String timeStamp,int version,int port,String loginFlag,Vector list){ int stat=-1; int timeout = 1000*10; icp=icp_ID; this.user=user; try { socketManger=new SocketManager(host,user,auth,timeStamp,version,port,timeout); if(loginFlag==null) flag=2; else{ if(loginFlag.equals("MO")) flag=1; if(loginFlag.equals("MT")) flag=0; if(loginFlag.equals("MOMT")) flag=2; } this.poolName=poolName; deliverMsgList=list; stat = initCMPP(); } catch (Exception ex) { socketManger.freeSocket(); s=null; stat=-1; } return stat; } private int initCMPP()throws Exception{ sendOut=1; int stat = CMPP_SUCCESS; try { CMPPConnectMessage cMsg=new CMPPConnectMessage(); sendOut=cMsg.setSequenceID(sendOut); int timeStamp = (int)(System.currentTimeMillis()/1000); byte[] auth =getAuthString(timeStamp); MD5 md5 = new MD5(); byte md5Msg[]=md5.getMD5ofBytes(auth,auth.length); stat=login(cMsg,md5Msg,flag,timeStamp); if(stat!=CMPP_SUCCESS){ Log.printEvent(Thread.currentThread().getName()+"初使化DR_CMPP发生错误,resp stat:"+stat,logFile); }else{ Log.printEvent("初使化DR_CMPP成功:"+Thread.currentThread().getName(),logFile); } } catch (Exception ex){ stat=CONNECT_INIT_ERROR; Log.printError(ex,"初使化DR_CMPP发生错误:"+Thread.currentThread().getName(),logFile); } try { Thread.currentThread().sleep(1000); } catch (Exception ex) { } return stat; } private byte[] getAuthString(int timeStamp)throws Exception{//取得登录认证码(转换前) byte[] user=socketManger.getUSER(); byte[] auth = socketManger.getAuth(); if(user==null||auth==null) return null; byte abyte2[] = new byte[100]; System.arraycopy(user, 0, abyte2, 0, user.length);//icp int k = user.length+ 9; System.arraycopy(auth, 0, abyte2, k, auth.length);//keys k += auth.length; System.arraycopy(Tools.int2byte(timeStamp), 0, abyte2, k, 4);//keys k += 4; byte auths[] = new byte[k]; System.arraycopy(abyte2, 0,auths, 0, k);//keys return auths; } private int login(CMPPConnectMessage cMsg,byte[] auth,int loginFlag,int timeStamp)throws IOException,Exception{ int stat = CMPP_SUCCESS; int length=0; byte[] inMsg = new byte[100]; System.arraycopy(Tools.int2byte(cMsg.nCommandID),0,inMsg,4,4); System.arraycopy(Tools.int2byte(cMsg.nSequenceID),0,inMsg,12,4); length=16; System.arraycopy(icp.getBytes(),0,inMsg,length,icp.getBytes().length); length+=icp.getBytes().length+1; System.arraycopy(auth,0,inMsg,length,auth.length); length+=auth.length; inMsg[length]=(byte)loginFlag; length++; inMsg[length]=(byte)socketManger.getVersion(); length++; System.arraycopy(Tools.int2byte(timeStamp),0,inMsg,length,4); length+=4; cMsg.nMsgSize=length; System.arraycopy(Tools.int2byte(cMsg.nMsgSize),0,inMsg,0,4); byte[] send = new byte[length]; System.arraycopy(inMsg,0,send,0,length); s = socketManger.getSocket(); ///发送认证码 send(send); //接收认证码 CMPPHead head= Deliver(); if(head.nCommandID==this.COMMAND_CMPP_CONNECT_RESP){ stat=head.stat; }else{ Log.printEvent("DR:登陆连接反馈发生错误!",logFile); stat=CONNECT_MSG_RESP_NOT_FOUNT_ERROR; } //关闭接收发送通道 if(stat!=CMPP_SUCCESS){ close(); } return stat; } //关闭与网关连接的SOCKET在调用了logout后调用 //发送断开连接消息给网关 public void quit(){ CMPPHead logout = new CMPPHead(); sendOut=logout.setSequenceID(sendOut); logout.nCommandID=2; logout.nMsgSize=16; try { sendHead(logout); } catch (Exception ex) { } close(); } //信息提交 public int submit(SubmitMsg msg)throws IOException{ try { if(msg==null){ if(!activeTest("auto")){ throw new IOException(); } return 0; } CMPPSubmitMessage submitMessage = new CMPPSubmitMessage(msg); boolean splitFlag=true; Vector vMsg=null; try { if(submitMessage.picAndRing!=null){ submitMessage.sMsgContent="1"; splitFlag=false; } vMsg = SplitMsg.split(submitMessage.sMsgContent,splitFlag); //信息分割队列 } catch (Exception ex) { return -1; } int size=vMsg.size(); int stat[] = new int[size]; int i; for(i=0;i<size;i++){ sendOut=submitMessage.setSequenceID(sendOut); submitMessage.sMsgContent = (String)(vMsg.remove(0)); //信息内容 submitMessage.nPkTotal=size; submitMessage.nPkNumber=i+1; stat[i]=sendMsg(submitMessage); if(stat[i]!=0) return stat[i]; } } catch (IOException ex) { close(); throw ex; } return 0; } private int sendMsg(CMPPSubmitMessage msg)throws IOException{ int stat = CMPP_SUCCESS; byte[] sendMsg=null; try { sendMsg = getSendMsg(msg); } catch (Exception ex) { sendOut--; return SUBMIT_MSG_FORMAT_ERROR; } int i=N;//重新发送次数 while(i!=0){ try { sendPacket(msg,sendMsg); } catch (IOException ex) { sendOut--; throw ex; } CMPPHead back= Deliver(); if(back.stat==DELIVER_MSG_TIME_OUT){ i--; if(i==0) stat=SUBMIT_MSG_TIME_OUT; continue; } if(back.nCommandID==this.COMMAND_CMPP_SUBMIT_RESP){ return back.stat; } if(back.nCommandID==this.COMMAND_CMPP_TERMINATE){ throw new IOException(); } if(back.nCommandID==this.COMMAND_CMPP_DELIVER){ deliverMsgList.addElement(((CMPPDeliverMessage)back).getDeliverMsg()); continue; } else return this.CMPP_UNKNOWN_PACKAGE_ERROR; } return stat; } private byte[] getSendMsg(CMPPSubmitMessage msg)throws Exception{ int length=0; byte send[]= new byte[1024*3]; System.arraycopy(icp.getBytes(),0,send,0,icp.getBytes().length); length+=icp.getBytes().length+1; if(msg.sServiceId!=null){ System.arraycopy(msg.sServiceId.getBytes(),0,send,length,msg.sServiceId.getBytes().length); length+=msg.sServiceId.getBytes().length+1; }else length++; send[length]=(byte)Integer.parseInt(msg.sFeeType); length++; System.arraycopy(Tools.int2byte(Integer.parseInt(msg.sFeeCode)),0,send,length,4); length+=4; send[length]=(byte)msg.nTPpId; length++; send[length]=(byte)msg.nNeedReply; length++; send[length]=(byte)msg.nMsgLevel; length++; if(msg.sValidTime!=null){ System.arraycopy(msg.sValidTime.getBytes(),0,send,length,msg.sValidTime.getBytes().length); length+=msg.sValidTime.getBytes().length+1; }else{ length++; } if(msg.sAtTime!=null){ System.arraycopy(msg.sAtTime.getBytes(),0,send,length,msg.sAtTime.getBytes().length); length+=msg.sAtTime.getBytes().length+1; }else{ length++; } send[length]=(byte)msg.nFeeUserType; length++; if(msg.sFeeMobile!=null){ System.arraycopy(msg.sFeeMobile.getBytes(),0,send,length,msg.sFeeMobile.getBytes().length); length+=msg.sFeeMobile.getBytes().length+1; }else{ length++; } System.arraycopy(msg.sSrcId.getBytes(),0,send,length,msg.sSrcId.getBytes().length); length+=msg.sSrcId.getBytes().length+1; length++; System.arraycopy(msg.getDestBytes_DR(),0,send,length,msg.getDestBytes_DR().length); send[length-1]=(byte)msg.nDestUsrTl; length+=msg.getDestBytes_DR().length; send[length]=(byte)msg.nMsgFormat; length++; if(msg.sMsgContent==null && msg.nNeedReply==2) msg.sMsgContent="JF"; if(msg.picAndRing!=null){ send[length]=(byte)msg.picAndRing.length; length++; System.arraycopy(msg.picAndRing,0,send,length,msg.picAndRing.length); length+=msg.sMsgContent.getBytes().length; }else{ byte[] sMsgContent=null; try { if(msg.nMsgFormat==8) sMsgContent=msg.sMsgContent.getBytes("iso-10646-ucs-2"); else if(msg.nMsgFormat==15) sMsgContent=msg.sMsgContent.getBytes("gb2312"); else sMsgContent=msg.sMsgContent.getBytes("iso-8859-1"); } catch (Exception ex) { } send[length]=(byte)sMsgContent.length; length++; System.arraycopy(sMsgContent,0,send,length,sMsgContent.length); length+=sMsgContent.length+1; } byte[] msgs = new byte[length]; System.arraycopy(send,0,msgs,0,length); return msgs; } //信息删除 public int Cancel(){ CMPPCancelMessage cancelMessage = new CMPPCancelMessage(); CMPPCancelMessageResp cancelResp = new CMPPCancelMessageResp(); return 1; } private CMPPHead dealMsg(CMPPHead head,byte[] msg)throws IOException,Exception{ CMPPHead backMsg = new CMPPHead(); int stat=head.stat; switch (head.nCommandID){ case COMMAND_CMPP_NACK_RESP: break; case COMMAND_CMPP_CONNECT_RESP: CMPPConnectMessageResp cResp = new CMPPConnectMessageResp(); if(msg==null){ cResp.nStatus=stat; backMsg=cResp; break; } try { cResp.sISMG=new String(msg,0,16); cResp.nVersion=msg[16]; } catch (Exception ex) { stat=CONNECT_MSG_FORMAT_ERROR; } cResp.nStatus=stat; backMsg=cResp; break; case COMMAND_CMPP_DELIVER: CMPPDeliverMessage back=new CMPPDeliverMessage(); stat = getDeliverMessage(back,msg); CMPPDeliverMessageResp resp = new CMPPDeliverMessageResp(); resp.nSequenceID=back.nSequenceID; resp.stat=stat; resp.nMsgSize=16; try { sendHead(resp); } catch (IOException ex) { throw ex; } back.stat=stat; backMsg=back; break; case COMMAND_CMPP_SUBMIT_RESP: CMPPSubmitMessageResp sResp = new CMPPSubmitMessageResp(); if(msg!=null){ try { sResp.un_count= msg[0]; if(sResp.un_count!=0){ byte[] count = new byte[msg.length-1]; System.arraycopy(msg,1,count,0,msg.length-1); sResp.fail_Index=count; } } catch (Exception ex) { stat=SUBMIT_MSG_FORMAT_ERROR; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -