cmpppacket.java

来自「短信网关发送接受平台。」· Java 代码 · 共 138 行

JAVA
138
字号
import java.lang.*;
import java.net.*;
import java.io.*;

import CMPPBuffer;
import CMPPCommandID;

public class CMPPPacket extends CMPPBuffer
{
	final static int IMPOSIBLE_MAX_LENGTH		= 4096;
	final static int MESSAGE_HEADER_LENGTH		= 12;
	
	public int total_length = MESSAGE_HEADER_LENGTH;
	public int command_id = CMPPCommandID.CMPP_NACK_RESPONSE;
	public int sequence_id = 1;
	
	public CMPPPacket()
	{
	}

	public CMPPPacket(int command_id,int sequence_id)
	{
		this.command_id = command_id;
		this.sequence_id = sequence_id;
	}
	
	public CMPPPacket(CMPPPacket packet)
	{
		super(packet);
		
		total_length = packet.total_length;
		command_id = packet.command_id;
		sequence_id = packet.sequence_id;
	}
	
	public boolean isPacketValid()
	{
		if(total_length < MESSAGE_HEADER_LENGTH || total_length > IMPOSIBLE_MAX_LENGTH)
		{
			CMPPLog.log("CMPPPacket.isPacketValid : invalid total_length !",
				CMPPLog.LOG_CMPP_PACKET | CMPPLog.LOG_ISVALID_METHOD);
			
			return false;
		}
		
		if(!CMPPCommandID.isValid(command_id))
		{
			CMPPLog.log("CMPPPacket.isPacketValid : invalid command_id !",
				CMPPLog.LOG_CMPP_PACKET | CMPPLog.LOG_ISVALID_METHOD);
				
			return false;
		}
		
		if(sequence_id <= 0)
		{
			CMPPLog.log("CMPPPacket.isPacketValid : invalid sequence_id !",
				CMPPLog.LOG_CMPP_PACKET | CMPPLog.LOG_ISVALID_METHOD);
			
			return false;
		}
		
		return true;
	}
	
	public void dumpPacket(long lMethod)
	{
		CMPPLog.log("\ttotal_length = " + total_length,
			CMPPLog.LOG_CMPP_PACKET | lMethod);

		CMPPLog.log("\tcommand_id = 0x" + CMPPUtility.toHexString(command_id) 
			+ " [" + CMPPCommandID.getDescription(command_id) + "]",
			CMPPLog.LOG_CMPP_PACKET | lMethod);
			
		CMPPLog.log("\tsequence_id = " + sequence_id,
			CMPPLog.LOG_CMPP_PACKET | lMethod);
	}
	
	public void wrapPacket()
		throws CMPPException
	{
		//计算整个数据包的长度
		total_length = getLength() + MESSAGE_HEADER_LENGTH;
		
		dumpPacket(CMPPLog.LOG_WRAP_METHOD);
		
		insertInteger(sequence_id);
		insertInteger(command_id);
		insertInteger(total_length);
	}
	
	public void unwrapPacket()
		throws CMPPException
	{
		total_length = getInteger();
		command_id = getInteger();
		sequence_id = getInteger();

		dumpPacket(CMPPLog.LOG_UNWRAP_METHOD);
	}

	public void inputPacket(InputStream is)
		throws IOException, CMPPException
	{
		inputBuffer(is,MESSAGE_HEADER_LENGTH);
		
		CMPPLog.log("CMPPPacket.inputPacket : receive a packet !",
			CMPPLog.LOG_CMPP_PACKET | CMPPLog.LOG_INPUT_METHOD);
			
		unwrapPacket();
		
		if(!isPacketValid())
		{
			throw new CMPPException("CMPPPacket.input : invalid CMPP message header !");
		}
		
		int body_length = total_length - MESSAGE_HEADER_LENGTH;
		if(body_length > 0)
		{
			inputBuffer(is,body_length);
		}
	}
	
	public void outputPacket(OutputStream os)
		throws IOException, CMPPException
	{
		CMPPLog.log("CMPPPacket.outputPacket : transmit a packet !",
			CMPPLog.LOG_CMPP_PACKET | CMPPLog.LOG_OUTPUT_METHOD);
		
		if(!isPacketValid())
		{
			throw new CMPPException("CMPPPacket.output : invalid CMPP message header !");
		}
		
		wrapPacket();
		
		outputBuffer(os);
	}
}

⌨️ 快捷键说明

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