📄 cmpp.java
字号:
package cmpp.v2_0;
import java.io.*;
import java.security.NoSuchAlgorithmException;
public class CMPP
{
public static final int CMPP_VERSION = 2*16 + 0;
public static final int LEN_CMPP_HEADER = 12;
public static final int ID_CMPP_CONNECT = 0x00000001;
public static final int ID_CMPP_CONNECT_RESP = 0x80000001;
public static final int ID_CMPP_TERMINATE = 0x00000002;
public static final int ID_CMPP_TERMINATE_RESP = 0x80000002;
public static final int ID_CMPP_SUBMIT = 0x00000004;
public static final int ID_CMPP_SUBMIT_RESP = 0x80000004;
public static final int ID_CMPP_DELIVER = 0x00000005;
public static final int ID_CMPP_DELIVER_RESP = 0x80000005;
public static final int ID_CMPP_QUERY = 0x00000006;
public static final int ID_CMPP_QUERY_RESP = 0x80000006;
public static final int ID_CMPP_CANCEL = 0x00000007;
public static final int ID_CMPP_CANCEL_RESP = 0x80000007;
public static final int ID_CMPP_ACTIVE_TEST = 0x00000008;
public static final int ID_CMPP_ACTIVE_TEST_RESP= 0x80000008;
protected static Seq seq = new Seq();
protected int totalLength;
protected MsgHead head;
protected int bodyLength;
protected byte bodybytes[];
public CMPP()
{
totalLength = 0;
head = new MsgHead();
bodyLength = 0;
bodybytes = null;
}
public CMPP(int commandId) {
totalLength = 0;
head = new MsgHead();
head.setCommandID(commandId);
bodyLength = 0;
bodybytes = null;
}
/**
*
* @param msghead
*/
public CMPP(MsgHead msghead)
{
totalLength = 0;
head = msghead;
bodyLength = 0;
bodybytes = null;
}
/**
*
* @param cmpp
*/
public CMPP(CMPP cmpp)
{
head = cmpp.head;
bodyLength = cmpp.bodyLength;
bodybytes = cmpp.bodybytes;
totalLength = cmpp.totalLength;
}
/**
* byte to integer
* @param byte0
* @return
*/
protected static int ByteToInt(byte byte0)
{
return byte0;
}
/**
* byte to integer
* @param byte0
* @return
*/
protected static int BytesToInt(byte abyte0[])
{
return ((0xff & abyte0[0]) << 8) + abyte0[1];
}
/**
* 4 bytes to integer
* @param abyte0
* @return
*/
protected static int Bytes4ToInt(byte abyte0[])
{
return (0xff & abyte0[0]) << 24 | (0xff & abyte0[1]) << 16 | (0xff & abyte0[2]) << 8 | 0xff & abyte0[3];
}
/**
* 8 bytes to long
* @param abyte0
* @return
*/
protected static long Bytes8ToLong(byte abyte0[])
{
long ret = 0;
ret = (0xffL & abyte0[0]) << 56 | (0xffL & abyte0[1]) << 48 | (0xffL & abyte0[2]) << 40 | (0xffL & abyte0[3]) << 32 | (0xffL & abyte0[4]) << 24 | (0xffL & abyte0[5]) << 16 | (0xffL & abyte0[6]) << 8 | 0xffL & abyte0[7];
return ret;
}
/**
*
* @param srcAbyte
* @param destAbyte
* @param srcFrom: srcindex
* @param srcTo
* @param destFrom
*/
protected static void BytesCopy(byte srcAbyte[], byte destAbyte[], int srcFrom, int srcTo, int destFrom)
{
// check null
if( srcAbyte == null || destAbyte == null ) {
return;
}
// copy
int i1 = 0;
for(int l = srcFrom; l <= srcTo; l++ )
{
if( destFrom + i1 >= destAbyte.length ) {
break;
}
if( l >= srcAbyte.length ) {
break;
}
destAbyte[destFrom + i1] = srcAbyte[l];
i1++;
}
}
/**
*
* @param i
* @return
*/
protected static byte IntToByte(int i)
{
return (byte)i;
}
/**
*
* @param i
* @return
*/
protected static byte[] IntToBytes(int i)
{
byte abyte0[] = new byte[2];
abyte0[1] = (byte)(0xff & i);
abyte0[0] = (byte)((0xff00 & i) >> 8);
return abyte0;
}
/**
*
* @param i
* @param abyte0
*/
protected static void IntToBytes(int i, byte abyte0[])
{
abyte0[1] = (byte)(0xff & i);
abyte0[0] = (byte)((0xff00 & i) >> 8);
}
/**
*
* @param i
* @return
*/
protected static byte[] IntToBytes4(int i)
{
byte abyte0[] = new byte[4];
abyte0[3] = (byte)(0xff & i);
abyte0[2] = (byte)((0xff00 & i) >> 8);
abyte0[1] = (byte)((0xff0000 & i) >> 16);
abyte0[0] = (byte)((0xff000000 & i) >> 24);
return abyte0;
}
/**
*
* @param i
* @param abyte0
*/
protected static void IntToBytes4(int i, byte abyte0[])
{
abyte0[3] = (byte)(0xff & i);
abyte0[2] = (byte)((0xff00 & i) >> 8);
abyte0[1] = (byte)((0xff0000 & i) >> 16);
abyte0[0] = (byte)(int)((0xffffffffff000000L & (long)i) >> 24);
}
/**
*
* @param i
* @return
*/
protected static byte[] LongToBytes8(long i)
{
byte abyte0[] = new byte[8];
abyte0[7] = (byte)(0xffL & i);
abyte0[6] = (byte)((0xff00L & i) >> 8);
abyte0[5] = (byte)((0xff0000L & i) >> 16);
abyte0[4] = (byte)((0xff000000L & i) >> 24);
abyte0[3] = (byte)((0xff00000000L & i) >> 32);
abyte0[2] = (byte)((0xff0000000000L & i) >> 40);
abyte0[1] = (byte)((0xff000000000000L & i) >> 48);
abyte0[0] = (byte)((0xff00000000000000L & i) >> 56);
return abyte0;
}
public int getCommandID()
{
return head.getCommandID();
}
public int getSeqno()
{
return head.getSeq();
}
public int getTotalLength()
{
return head.getTotalLength();
}
/**
*
* @param inputstream
* @return
* @throws IOException
* @throws Exception
*/
public CMPP read(InputStream inputstream)
throws IOException, Exception
{
CMPP cmpp = null;
head.read(inputstream);
totalLength = head.getTotalLength();
if( totalLength < LEN_CMPP_HEADER ) { //add by wanglin
bodybytes = new byte[0];
} else {
bodybytes = new byte[totalLength - LEN_CMPP_HEADER];
}
inputstream.read(bodybytes);
bodyLength = bodybytes.length;
switch(head.getCommandID())
{
case CMPP.ID_CMPP_CONNECT: // '\001'
cmpp = new Connect(this);
break;
case CMPP.ID_CMPP_CONNECT_RESP:
cmpp = new ConnectResp(this);
break;
case CMPP.ID_CMPP_TERMINATE: // '\002'
cmpp = new Terminate(this);
break;
case CMPP.ID_CMPP_TERMINATE_RESP:
cmpp = new TerminateResp(this);
break;
case CMPP.ID_CMPP_SUBMIT: // '\004'
cmpp = new Submit(this);
break;
case CMPP.ID_CMPP_SUBMIT_RESP:
cmpp = new SubmitResp(this);
break;
case CMPP.ID_CMPP_DELIVER: // '\005'
cmpp = new Deliver(this);
break;
case CMPP.ID_CMPP_DELIVER_RESP:
cmpp = new DeliverResp(this);
break;
case CMPP.ID_CMPP_QUERY: // '\006'
cmpp = new Query(this);
break;
case CMPP.ID_CMPP_QUERY_RESP:
cmpp = new QueryResp(this);
break;
case CMPP.ID_CMPP_CANCEL: // '\007'
cmpp = new Cancel(this);
break;
case CMPP.ID_CMPP_CANCEL_RESP:
cmpp = new CancelResp(this);
break;
case CMPP.ID_CMPP_ACTIVE_TEST: // '\008'
cmpp = new ActiveTest(this);
break;
case CMPP.ID_CMPP_ACTIVE_TEST_RESP:
cmpp = new ActiveTestResp(this);
break;
default:
cmpp = null;
}
if( cmpp != null ) {
if( cmpp.parseBody() < 0 ) {
cmpp = null;
}
}
return cmpp;
}
public void setSeqno(int i)
{
head.setSeq(i);
}
// add by wanglin for don't auto generate seqno
public int write(OutputStream outputstream, boolean isAutoSeq )
{
try
{
//get bodybytes from subclass
if( makeBody() < 0 ) {
return -1;
}
//total length
totalLength = CMPP.LEN_CMPP_HEADER + bodyLength;
head.setTotalLength(totalLength);
if( isAutoSeq ) { //Auto Seq, compute Sequence
synchronized(Seq.seqclass)
{
Seq.computeSequence();
head.setSeq(seq.getsn());
}
}
outputstream.write(head.Head,0,CMPP.LEN_CMPP_HEADER);
outputstream.write(bodybytes,0,bodyLength);
return 0;
}
catch(Exception exception)
{
return -1;
}
}
protected int parseBody() {
// subclass to do..
return 0;
}
protected int makeBody() {
// subclass to do..
return 0;
}
public int write(OutputStream outputstream)
{
return write(outputstream, true);
}
/**
*
* @param source
* @return
*/
protected static byte[] getMD5(byte[] source) {
byte[] tmp = null;
java.security.MessageDigest md;
try {
md = java.security.MessageDigest.getInstance( "MD5" );
md.update(source);
tmp = md.digest(); // MD5 的计算结果是一个 128 位的长整数, 用字节表示就是 16 个字节
} catch (NoSuchAlgorithmException e) {
tmp = null;
}
return tmp;
}
// add by wanglin for don't auto generate seqno end
/*
public static void main(String args[]) {
byte b[] = new byte[0];
try {
System.out.write(b);
} catch (IOException e) {
e.printStackTrace();
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -