📄 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 String spNum;
private String rpSPNum;
private int sendOut=1;
private int checkTime=(int)(System.currentTimeMillis()/1000);
private String icp=null;
public CMPP() {
}
private static class Factory extends CMPPFactory{
protected CMPP_Service creat(){
return new CMPP();
}
}
static{
CMPPFactory.addFactory("com.hoten.cmpp.type.CMPP",new Factory());
}
//初始化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,String spNum,String rpSPNum){
int stat=-1;
int timeout=1000*10;
this.spNum=spNum;
this.rpSPNum=rpSPNum;
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).substring(2);
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 CMPPHead submit(SubmitMsg msg)throws IOException{
CMPPHead returnHead =new CMPPHead();
try {
if(msg==null){
if(!activeTest("auto")){
throw new IOException();
}
return returnHead;
}
if(msg.sSrcId.startsWith(rpSPNum)){
msg.sSrcId=spNum+msg.sSrcId.substring(rpSPNum.length());
msg.sSrcId=msg.sSrcId.trim();
}
CMPPSubmitMessage submitMessage = new CMPPSubmitMessage(msg);
boolean splitFlag=submitMessage.isSplit;
Vector vMsg=null;
try {
vMsg = SplitMsg.split(submitMessage.sMsgContent, splitFlag,submitMessage.isPicOrRing); //信息分割队列
}
catch (Exception ex) {
returnHead.stat=-1;
return returnHead;
}
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;
returnHead =sendMsg(submitMessage);
if(returnHead.stat!=0) return returnHead;
}
}
catch (IOException ex) {
close();
throw ex;
}
return returnHead;
}
private CMPPHead sendMsg(CMPPSubmitMessage msg)throws IOException{
CMPPHead cHead = new CMPPHead();
cHead.stat = CMPP_SUCCESS;
byte[] sendMsg=null;
try {
sendMsg = getSendMsg(msg);
}
catch (Exception ex) {
sendOut--;
cHead.stat =SUBMIT_MSG_FORMAT_ERROR;
return cHead;
}
int i=N;//重新发送次数
if(msg.nNeedReply==2) i=1;
boolean flag=false;
while(i!=0){
try {
if(!flag)
sendPacket(msg,sendMsg);
}
catch (IOException ex) {
sendOut--;
throw ex;
}
CMPPHead back= Deliver();
if(back.stat==DELIVER_MSG_TIME_OUT){
i--;
if(i==0)
cHead.stat=SUBMIT_MSG_TIME_OUT;
flag=false;
continue;
}
if(back.nCommandID==this.COMMAND_CMPP_SUBMIT_RESP){
return back;
}
if(back.nCommandID==this.COMMAND_CMPP_TERMINATE){
throw new IOException();
}
if(back.nCommandID==this.COMMAND_CMPP_DELIVER){
deliverMsgList.addElement(((CMPPDeliverMessage)back).getDeliverMsg());
flag=true;
continue;
}
else{
cHead.stat=this.CMPP_UNKNOWN_PACKAGE_ERROR;
return cHead;
}
}
return cHead;
}
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){
send[22]=(byte)3;
System.arraycopy(msg.sFeeMobile.getBytes(), 0, send, 23,
msg.sFeeMobile.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){
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyMMddHHmmss");
System.arraycopy((dateFormat.format(new java.util.Date(System.currentTimeMillis()+Integer.parseInt(msg.sValidTime)*60*1000))+"032+").getBytes(),0,send,61,16);
}
if(msg.sAtTime!=null){
java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyMMddHHmmss");
System.arraycopy((dateFormat.format(new java.util.Date(System.currentTimeMillis()+Integer.parseInt(msg.sAtTime)*60*1000))+"032+").getBytes(),0,send,78,16);
}
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;
byte[] tmp = null;
if(msg.isPicOrRing){
tmp = Tools.getBytesRingORPic(msg.sMsgContent);
send[size]=(byte)tmp.length;
System.arraycopy(tmp,0,send,size+1,tmp.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.isPicOrRing)
length = 118+msg.nDestUsrTl*21+msg.sMsgContent.getBytes().length;
else
length = 118+msg.nDestUsrTl*21+tmp.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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -