?? cmpp.java
字號:
package com.hoten.cmpp.type;import com.hoten.cmpp.message.*;import com.hoten.cmpp.*;import com.hoten.cmpp.socket.*;import com.hoten.cmpp.util.*;import java.util.*;import java.io.*;import java.net.*;/** * <p>Title: 通信主類</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class CMPP implements CMPP_Service{ private CMPP_InitMessage initMsg=CMPP_InitMessage.getInstance(); private SocketManager spm=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 int C=initMsg.C; private int N=initMsg.N; private int sendOut=1; private int checkTime=(int)(System.currentTimeMillis()/1000); private String icp=null; public CMPP() { } //初始化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; try { spm=new SocketManager(host,user,auth,timeStamp,version,port,timeout); this.poolName=poolName; deliverMsgList=list; stat = initCMPP(); } catch (Exception ex) { spm.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); String time=CTime.getTime(CTime.YYMMDDhhmmss); byte[] auth =getAuthString(time); MD5 md5 = new MD5(); byte md5Msg[]=md5.getMD5ofBytes(auth,auth.length); stat=login(cMsg,md5Msg,time); if(stat!=CMPP_SUCCESS){ Log.printEvent("初使化CMPP發生錯誤,resp stat:"+stat,logFile); }else{ Log.printEvent("初使化CMPP成功"+Thread.currentThread().getName(),logFile); } } catch (Exception ex){ stat=CONNECT_INIT_ERROR; Log.printError(ex,"初使化CMPP發生錯誤"+Thread.currentThread().getName(),logFile); } try { Thread.currentThread().sleep(1000); } catch (Exception ex) { } return stat; } private byte[] getAuthString(String time){//取得登錄認證碼(轉換前) byte[] user=spm.getUSER(); byte[] auth = spm.getAuth(); byte[] timeStamp = time.getBytes(); 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(timeStamp, 0, abyte2, k, 10);//keys k += 10; byte auths[] = new byte[k]; System.arraycopy(abyte2, 0,auths, 0, k);//keys return auths; } private int login(CMPPConnectMessage cMsg,byte[] auth,String time)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,8,4); length=12; System.arraycopy(icp.getBytes(),0,inMsg,length,icp.getBytes().length); length+=icp.getBytes().length; System.arraycopy(auth,0,inMsg,length,auth.length); length+=auth.length; inMsg[length]=(byte)spm.getVersion(); length++; System.arraycopy(Tools.int2byte(Integer.parseInt(time)),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 = spm.getSocket(); ///發送認證碼 send(send); //接收認證碼 CMPPHead head= Deliver(); if(head.nCommandID==this.COMMAND_CMPP_CONNECT_RESP){ stat=head.stat; }else{ Log.printEvent("標準:登陸連接反饋發生錯誤!",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=12; 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{ byte send[]= new byte[1024*3]; send[8]=(byte)msg.nPkTotal; send[9]=(byte)msg.nPkNumber; send[10]=(byte)msg.nNeedReply; send[11]=(byte)msg.nMsgLevel; System.arraycopy(msg.sServiceId.getBytes(),0,send,12,msg.sServiceId.getBytes().length); send[22]=(byte)msg.nFeeUserType; if(msg.sFeeMobile!=null) System.arraycopy(msg.sFeeMobile.getBytes(),0,send,23,msg.sFeeType.getBytes().length); send[44]=(byte)msg.nTPpId; send[45]=(byte)msg.nTPudhi; send[46]=(byte)msg.nMsgFormat; System.arraycopy(icp.getBytes(),0,send,47,icp.getBytes().length); System.arraycopy(msg.sFeeType.getBytes(),0,send,53,msg.sFeeType.getBytes().length); System.arraycopy(msg.sFeeCode.getBytes(),0,send,55,msg.sFeeCode.getBytes().length); if(msg.sValidTime!=null) System.arraycopy(msg.sValidTime.getBytes(),0,send,61,msg.sValidTime.getBytes().length); if(msg.sAtTime!=null) System.arraycopy(msg.sAtTime.getBytes(),0,send,78,msg.sAtTime.getBytes().length); System.arraycopy(msg.sSrcId.getBytes(),0,send,95,msg.sSrcId.getBytes().length); System.arraycopy(msg.getDestBytes(),0,send,117,msg.nDestUsrTl*21); send[116]=(byte)msg.nDestUsrTl; int size=117+msg.nDestUsrTl*21; byte[] sMsgContent=null; if(msg.picAndRing!=null){ send[size]=(byte)msg.picAndRing.length; System.arraycopy(msg.picAndRing,0,send,size+1,msg.picAndRing.length); }else{ 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[size]=(byte)sMsgContent.length; System.arraycopy(sMsgContent,0,send,size+1,sMsgContent.length); } int length=0; if(msg.picAndRing==null) length = 118+msg.nDestUsrTl*21+msg.sMsgContent.getBytes().length; else length = 118+msg.nDestUsrTl*21+msg.picAndRing.length; length++; byte[] msgBytes= new byte[length]; System.arraycopy(send,0,msgBytes,0,length); return msgBytes; } //信息刪除 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) stat=CONNECT_MSG_NULL_ERROR; else{ cResp.nStatus=msg[0]; try { stat= msg[0]; if(stat!=3) cResp.sISMG=new String(msg,1,16); cResp.nVersion=msg[msg.length-1]; } 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.nMsgId=back.msgID; resp.nSequenceID=back.nSequenceID; resp.nMsgSize=21; resp.nResult=stat; byte[] send = new byte[9]; System.arraycopy(Tools.long2byte(resp.nMsgId),0,send,0,8); send[8]=(byte)resp.nResult; try { sendPacket(resp,send); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -