diff --git a/src/main/java/component/InterestLifeTime.java b/src/main/java/component/InterestLifeTime.java new file mode 100644 index 0000000..b7ec99f --- /dev/null +++ b/src/main/java/component/InterestLifeTime.java @@ -0,0 +1,94 @@ +package component; + +import encoding.*; + +/* + * @Author: Wang Feng + * @Description: + * @Version: 1.0.0 + * @Date: 19:46 2021/3/9 + * @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室 + */ +public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncodingAble { + // 变量 + private long interestLifeTime; + + // 方法 + public long getInterestLifeTime() { + return interestLifeTime; + } + + public void setInterestLifeTime(long interestLifeTime) { + this.interestLifeTime = interestLifeTime; + this.doInitial(); + } + + // 接口实现-变量区 + private boolean initial=false; + + // 接口实现-方法区 + @Override + public void doInitial() { + initial=true; + } + + @Override + public boolean isInitial() { + return initial; + } + + /** + * 将 InterestLifeTime 线速编码为一个 TLV + * @param encoder + * @return + */ + @Override + public int wireEncode(Encoder encoder) { + int totalLength=0; + + // 编码 TLV-VALUE + int tmplen=encoder.prependNonNegativeInteger(this.interestLifeTime); + if(tmplen==0){ + return 0; + } + totalLength+=tmplen; + + // 编码 TLV-LENGTH + tmplen+=encoder.prependVarNumber(new VlInt(totalLength)); + if(tmplen==0){ + return 0; + } + totalLength+=tmplen; + + // 编码 TLV-TYPE + tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvInterestLifeTime)); + if(tmplen==0){ + return 0; + } + totalLength+=tmplen; + + return totalLength; + } + + /** + * 从 TLV Block 中解码出一个 InterestLifeTime + * @param block + * @return + */ + @Override + public boolean wireDecode(Block block) { + // 检查 Type 是否正确 + if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvInterestLifeTime))){ + return false; + } + + // 读取 value + long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int()); + if(value<0){ + return false; + } + this.setInterestLifeTime(value); + + return true; + } +}