Files
min-dev-java/src/main/java/component/LpPacketFragmentId.java
T

110 lines
2.7 KiB
Java

package component;
import encoding.*;
/*
* @Author: Wang Feng
* @Description: 表示 LpPacket 中的分片id TLV
* @Version: 1.0.0
* @Date: 20:45 2021/4/5
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class LpPacketFragmentId implements TlvComponentBase,InitialAble,IEncodingAble{
// 成员变量
private long id;
public LpPacketFragmentId(){
}
public LpPacketFragmentId(long id){
this.setId(id);
}
// 函数
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
this.doInitial();
}
// 接口实现-变量区
private boolean initial=false;
// 接口实现-方法区
@Override
public void doInitial() {
initial=true;
}
@Override
public boolean isInitial() {
return initial;
}
/**
* 将 LpPacketFragmentId 线速编码为一个 TLV
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
try {
int totalLength=0;
// 编码 TLV-VALUE
int tmpLen=encoder.prependNonNegativeInteger(this.id);
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvLpPacketFragmentId));
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("LpPacketFragmentId.wireEncode: "+e.getMessage());
}
}
/**
* 从 TLV Block 中解码一个 LpPacketFragmentId
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
try {
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvLpPacketFragmentId))) {
return false;
}
// 读取 value
long value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int());
if (value < 0) {
return false;
}
this.setId(value);
return true;
} catch (TLVException | VlIntException e) {
throw new ComponentException("LpPacketFragmentId.wireDecode: "+e.getMessage());
}
}
}