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){