⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tracemsg.java

📁 中国移动定位引擎的客户端
💻 JAVA
字号:
package ffcs.lbp.trace;

import java.nio.ByteBuffer;

import ffcs.lbp.ProtocolException;
import ffcs.lbp.LbpMessage;

/*
 * <p>Title: 流程追踪协议解析抽象基类</p>
 * <p>Description: 流程追踪协议解析抽象基类</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: 福富软件</p>
 * @author chenxin
 * @version 1.0 $Date 2007-11-02
 */

public abstract class TraceMsg extends LbpMessage {
	
	public final static int HEADER_LENGTH = 16;
	
	public final static int TRACE_SM_REQ=0x00000001;//流程追踪请求包
	public final static int TRACE_SM_RSP=0x80000001;//流程追踪请求应答包
	public final static int TRACE_CLEAR_REQ=0x00000002;//流程追踪请求清零包
	public final static int TRACE_CLEAR_RSP=0x80000002;//流程追踪请求清零应答包
    public final static int TRACE_SM_RESULT=0x00000004;//追踪结果消息
	
	public final static int ROK=0;//没有追踪号码
	
	public final static int ERR_FORMAT_ERROR=0x01;//通用错误码
	public final static int ERR_FAIL=0x02;//通用错误码
	public final static int ERR_MAX_TRACE=0x03;//超出追踪最大值
	public final static int ERR_MAX_SRC_TRACE=0x04;//超出追踪最大值
	public final static int ERR_MAX_DEST_TRACE=0x05;//超出追踪最大值
	public final static int ERR_TRACE_NULL=0x06;//没有追踪号码

	//自增序列
	public static int sequenceNum=1;
	
	protected int commandLength; //Set to overall length of PDU.

	protected int commandId; //The command_id field identifies the particular SMPP PDU, e.g., submit_sm, query_sm, etc.

	protected int commandStatus = 0; //Not used.Set to NULL.

	protected int sequence; //Set to a Unique sequencenumber. The associatedsubmit_sm_resp PDU willecho this sequence number.

	private boolean readHeader;

	private boolean writeHeader;

	protected TraceMsg(int commandID) {
		this(commandID, getNextSequnce());
	}

	protected TraceMsg(int command, int seqNum) {
		commandId = command;
		sequence = seqNum;
	}

	/**
	 * 读取协议包的包体
	 * @param buf ByteBuffer
	 * @throws MessageParseException
	 * @return boolean true读成功,false读取失败
	 */
	public abstract boolean readBody(ByteBuffer buf) throws ProtocolException;

	/**
	 * 写入协议包的包体
	 * @param buf ByteBuffer
	 * @return boolean true写成功,false写失败
	 */
	public abstract boolean writeBody(ByteBuffer buf);

	/**
	 * 得到包体的长度,因为包体的长度是动态的,不同包的计算方式不一样,所以在具体的子类中实现
	 * @return int
	 */
	protected abstract int getBodyLength();
	/**
	 * 读取消息
	 * @param buf
	 * @return true 成功 false 失败
	 * @throws ProtocolException
	 */
	public boolean readMsg(ByteBuffer buf) throws ProtocolException {
		if (!readHeader) {
			readHeader = readHeader(buf);
			if (!readHeader)
				return false;
		}
		if(buf.remaining()<(commandLength-HEADER_LENGTH)){
			throw new ProtocolException("消息长度不足,commandLength="+commandLength + " buf可读长度:"+buf.remaining());
		}
		// Header is read, now try to rf.ead body
		if (readBody(buf)) {
			// finished reading single complete message
			readHeader = false; // reset state
			return true;
		} else
			return false;
	}
	/**
	 * 写入消息
	 * @param buf 字节缓冲区
	 * @return true 成功写入 false 写入失败
	 */
	public boolean writeMsg(ByteBuffer buf) {
		// write a header if not written yet.
		if (!writeHeader) {
			writeHeader = writeHeader(buf);
			if (!writeHeader)
				return false; // buffer is almost full perhaps
		}

		// check if there is enough space to write body
		if (buf.remaining() < getBodyLength()) {
			return false;
		}

		// Header is written, now try to write body
		if (writeBody(buf)) {
			// finished writing single complete message
			writeHeader = false;
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 读取消息头
	 * @param buf
	 * @return
	 * @throws ProtocolException
	 */
	private boolean readHeader(ByteBuffer buf) throws ProtocolException {
		if (buf.remaining() < HEADER_LENGTH) {
			return false;
		}
		commandLength = buf.getInt();
		int tempID = buf.getInt();
		if (tempID != commandId) {
			throw new ProtocolException("流程追踪 COMMAND_ID与读到的包不匹配:ID:"
					+ Integer.toHexString(commandId) + " 收到ID:" + tempID);
		}
		commandStatus = buf.getInt();
		sequence = buf.getInt(); //
		return true;
	}

	/**
	 * 写入协议的包头
	 * @param buf ByteBuffer
	 * @return boolean
	 */
	protected boolean writeHeader(ByteBuffer buf) {
		// check if there is enough space to write header
		if (buf.remaining() < HEADER_LENGTH) {
			return false;
		}
		//
		buf.putInt(getCommandLength());
		buf.putInt(commandId);
		buf.putInt(commandStatus);
		buf.putInt(sequence);
		writeHeader = true;
		return true;
	}
	/**
	 * 根据消息ID,返回相应的消息包
	 * @param commandID 消息ID
	 * @return 返回消息包,如果为无效commandID,返回null;
	 */
	public static TraceMsg createPDU(int commandID){
		switch(commandID){
		case TRACE_SM_REQ :
			return new TraceSmReq();
		case TRACE_SM_RSP:
			return new TraceSmRsp();
		case TRACE_CLEAR_REQ :
			return new TraceClearReq();
		case TRACE_CLEAR_RSP:
			return new TraceClearRsp();
		default:
			return null;
		}
	}
	
	/**
	 * 根据请求消息,得到应答消息包
	 * @param tm 请求消息
	 * @return 返回消息包,如果为无效commandID,返回null;
	 */
	public static TraceResp createRespMsg(TraceMsg tm){
		if(tm==null){
			return null;
		}
		//在最高位加了8,得到该请求包,相应的应答包
		TraceResp resp = createRespMsg(tm.getCommandId());
		if(resp!=null){
			resp.setSequence(tm.getSequenceId());
		}
		return resp;
		
	}
	/**
	 * 根据请求消息,得到应答消息包
	 * @param tm 请求消息
	 * @return 返回消息包,如果为无效commandID,返回null;
	 */
	public static TraceResp createRespMsg(int commandID){
		//在最高位加了8,得到该请求包,相应的应答包
		return (TraceResp)createPDU(commandID | 0x80000000);
	}
	
	public int getCommandLength() {
		return HEADER_LENGTH+this.getBodyLength();
	}

	/**
	 *  消息的命令ID
	 * @return
	 */
	public int getCommandId() {
		return commandId;
	}

	/**
	 * 消息包的序列号
	 * @return
	 */
	public int getSequenceId() {
		return sequence;
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer(150);
		sb.append(" 长度:0x");
		sb.append(intToHex(this.getCommandLength()));

		sb.append(" 命令ID:0x");
		sb.append(intToHex(commandId));

		sb.append(" 序号:0x");
		sb.append(intToHex(sequence));
		
		sb.append(" 状态:0x");
		sb.append(intToHex(commandStatus));

		return sb.toString();
	}

	public int getCommandStatus() {
		return commandStatus;
	}

	public void setCommandStatus(int commandStatus) {
		this.commandStatus = commandStatus;
	}

	public void setSequence(int sequence) {
		this.sequence = sequence;
	}
	
	public static synchronized int getNextSequnce(){
		return (++ sequenceNum);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -