mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
finished component: MutableField
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user