📄 cmpp_yx.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_YX
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 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_YX() {
}
private static class Factory
extends CMPPFactory {
protected CMPP_Service creat() {
return new CMPP_YX();
}
}
static {
CMPPFactory.addFactory("com.hoten.cmpp.type.CMPP_YX", 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 {
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); //设置消息流水号
byte[] auth = getAuthString(); //取得MD5加密初始值
MD5 md5 = new MD5();
byte md5Msg[] = md5.getMD5ofBytes(auth, auth.length); //进行MD5加密
stat = login(cMsg, md5Msg, flag); //登陆
if (stat != CMPP_SUCCESS) {
Log.printEvent("初使化YX_CMPP发生错误,resp stat:" + stat, logFile);
}
else {
Log.printEvent("初使化YX_CMPP成功" + Thread.currentThread().getName(),
logFile);
}
}
catch (Exception ex) {
stat = CONNECT_INIT_ERROR;
Log.printError(ex, "初使化YX_CMPP发生错误" + Thread.currentThread().getName(),
logFile);
}
try {
Thread.currentThread().sleep(1000);
}
catch (Exception ex) {
}
return stat;
}
private byte[] getAuthString() { //取得登录认证码(转换前)
byte[] user = socketManger.getUSER(); //取得icp_ID
byte[] auth = socketManger.getAuth(); //取得认证码
byte[] timeStamp = "0000000000".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; //空9个字节的0
System.arraycopy(auth, 0, abyte2, k, auth.length); //keys
k += auth.length;
System.arraycopy(timeStamp, 0, abyte2, k, 10); //此处时间戳为10位(东软为四位)
k += 10;
byte auths[] = new byte[k];
System.arraycopy(abyte2, 0, auths, 0, k); //取得25位的MD5加密前值
return auths;
}
private int login(CMPPConnectMessage cMsg, byte[] auth, int loginFlag) throws
IOException, Exception {
int stat = CMPP_SUCCESS;
int length = 12;
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);
System.arraycopy(icp.getBytes(), 0, inMsg, 12, icp.getBytes().length); //6
length += icp.getBytes().length; //18
System.arraycopy(auth, 0, inMsg, length, auth.length); //16
length += auth.length; //34
if (loginFlag == 1) {
inMsg[34] = (byte) 1;
}
else {
inMsg[34] = 0; //35
}
length += 5; //39
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(); //取得Socket
///发送认证码
send(send); //发送认证码
//接收认证码
CMPPHead head = Deliver(); //接收反馈消息
if (head.nCommandID == this.COMMAND_CMPP_CONNECT_RESP) {
stat = head.stat;
}
else {
Log.printEvent("YX:登陆连接反馈发生错误!", 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;
if (msg.sServiceId != null) {
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);
msg.sFeeType = "0" + Integer.parseInt(msg.sFeeType);
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);
}
if (msg.sSrcId != null) {
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.sMsgContent == null && msg.nNeedReply == 2) {
msg.sMsgContent = "JF";
}
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 + sMsgContent.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,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -