mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
91 lines
2.0 KiB
Java
91 lines
2.0 KiB
Java
package component;
|
|
|
|
import encoding.*;
|
|
|
|
/*
|
|
* @Author: Wang Feng
|
|
* @Description:
|
|
* @Version: 1.0.0
|
|
* @Date: 16:38 2021/3/9
|
|
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
|
*/
|
|
public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
|
|
// 成员变量
|
|
private long hopLimit;
|
|
|
|
// 成员函数
|
|
public long getHopLimit() {
|
|
return hopLimit;
|
|
}
|
|
|
|
public void setHopLimit(long hopLimit) {
|
|
this.hopLimit = hopLimit;
|
|
this.doInitial();
|
|
}
|
|
|
|
// 接口实现-变量区
|
|
private boolean initial=false;
|
|
|
|
// 接口实现-方法区
|
|
@Override
|
|
public void doInitial() {
|
|
initial=true;
|
|
}
|
|
|
|
@Override
|
|
public boolean isInitial() {
|
|
return initial;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param encoder
|
|
* @return
|
|
*/
|
|
@Override
|
|
public int wireEncode(Encoder encoder) throws EncoderException {
|
|
int totalLength=0;
|
|
|
|
// 编码 TLV-VALUE
|
|
int tmplen=encoder.prependNonNegativeInteger(this.hopLimit);
|
|
if(tmplen<=0){
|
|
return -1;
|
|
}
|
|
totalLength+=tmplen;
|
|
|
|
// 编码 TLV-LENGTH
|
|
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
|
|
if(tmplen<=0){
|
|
return -1;
|
|
}
|
|
totalLength+=tmplen;
|
|
|
|
// 编码 TLV-TYPE
|
|
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvHopLimit));
|
|
if(tmplen<=0){
|
|
return -1;
|
|
}
|
|
totalLength+=tmplen;
|
|
|
|
return totalLength;
|
|
}
|
|
|
|
@Override
|
|
public boolean wireDecode(Block block) throws VlIntException {
|
|
// 检查 Type 是否正确
|
|
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvHopLimit))){
|
|
return false;
|
|
}
|
|
|
|
// 读取 HopLimit
|
|
long value= 0;
|
|
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
|
if(value<0){
|
|
return false;
|
|
}
|
|
this.setHopLimit(value);
|
|
|
|
return true;
|
|
}
|
|
}
|