finished component: MutableField

This commit is contained in:
free will
2021-03-09 21:25:19 +08:00
parent 11858c2336
commit e9ce657092
+98
View File
@@ -0,0 +1,98 @@
package component;
import encoding.*;
/*
* @Author: Wang Feng
* @Description: 表示一个MIN包格式中的可变区
* @Version: 1.0.0
* @Date: 20:48 2021/3/9
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class MutableField implements TlvComponentBase,IEncodingAble {
private MutableProtectField mutableProtectField;
private MutableDangerousField mutableDangerousField;
public MutableField(){
mutableProtectField=new MutableProtectField();
mutableDangerousField=new MutableDangerousField();
}
/**
* 将可变区线速编码为一个 TLV
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
int totalLength=0;
// 编码 TLV—VALUE
int tmplen=this.mutableDangerousField.wireEncode(encoder);
if(tmplen==0){
return 0;
}
totalLength+=tmplen;
tmplen=this.mutableProtectField.wireEncode(encoder);
if(tmplen==0){
return 0;
}
totalLength+=tmplen;
// 如果可变保护区和可变非受保护区均为空,则可以去掉可变区
if(totalLength==0){
return 0;
}
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMutableField));
if(tmplen==0){
return 0;
}
totalLength+=tmplen;
return totalLength;
}
/**
* 从 TLV Block 中解码出一个 MutableField
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvMutableField))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
}
for (int i = 0; i < block.getSubElements().length(); i++) {
Block subBlock=block.getSubElements().getBlock(i);
if(subBlock.getTlvType().isEqual(TLV.TlvMutableProtectField)){
if(!this.mutableProtectField.wireDecode(subBlock)){
return false;
}
}else if(subBlock.getTlvType().isEqual(TLV.TlvMutableDangerousField)){
if(!this.mutableDangerousField.wireDecode(subBlock)){
return false;
}
}else{
return false;
}
}
return false;
}
}