update ElementContainer & finished component: ReadOnlyField

This commit is contained in:
free will
2021-03-09 20:31:36 +08:00
parent c9b2d830df
commit 416f7a6498
3 changed files with 200 additions and 67 deletions
+114
View File
@@ -0,0 +1,114 @@
package component;
import encoding.*;
/*
* @Author: Wang Feng
* @Description: 表示一个MIN包格式中的只读区
* @Version: 1.0.0
* @Date: 19:58 2021/3/9
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
private ElementContainer blocks;
/**
* 往只读区添加一个Block
* @param block
*/
public void addBlock(Block block){
blocks.addElement(block);
}
/**
* 获取只读区指定位置的 Block
* @return
*/
public Block getBlock(int index){
return this.blocks.getBlock(index);
}
/**
* 清空只读区中,所有的子 Block
*/
public void clearBlocks(){
this.blocks.clear();
}
/**
* 获取只读区所有的 Block
* @return
*/
public ElementContainer getBlocks(){
return this.blocks;
}
/**
* 获取只读区中,第一个指定TLV-TYPE的子 TLV Block
* @return
*/
public Block getBlockByType(VlInt tlvType){
return this.blocks.getElement(tlvType);
}
/**
* 将只读区线速编码为一个 TLV
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
// 如果只读区没有任何子 TLV,则可以去掉只读区 TLV
if(this.blocks.length()==0){
return 0;
}
int totalLength=0;
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen=encoder.prependBlock(this.blocks.getBlock(i));
if(tmplen==0){
return 0;
}
totalLength+=tmplen;
}
// 编码 TLV-LENGTH
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
if(tmplen==0){
return 0;
}
totalLength+=tmplen;
return totalLength;
}
/**
* 从 TLV Block 中解码出一个 ReadOnlyField
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvReadOnlyField))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
}
this.blocks=block.getSubElements();
return true;
}
}
-67
View File
@@ -13,73 +13,6 @@ import java.util.List;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
// TLV 组件容器
class ElementContainer{
private List<Block> elementContainer;
public ElementContainer(){
elementContainer = new LinkedList<>();
}
/**
* 获取所有Block
* @return
*/
public List<Block> getElements(){
return elementContainer;
}
/**
* 获取block个数
* @return
*/
public int length(){
return elementContainer.size();
}
/**
* 清除 elements 中所有的子TLV
*/
public void clear(){
elementContainer.clear();
}
/**
* 往 elements 中添加一个子tlv
* @param block
*/
public void addElement(Block block){
elementContainer.add(block);
}
/**
* 移除所有指定type的子tlv
* @param tlvType
*/
public void removeElements(VlInt tlvType){
List<Block> elementContainer=new LinkedList<>();
for(Block block: this.elementContainer){
if(!block.getTlvType().isEqual(tlvType)){
elementContainer.add(block);
}
}
this.elementContainer =elementContainer;
}
/**
* 从 elements 中找到第一个指定type的子TLV
* @param tlvType
* @return
*/
public Block getElement(VlInt tlvType){
for(Block block: this.elementContainer){
if(block.getTlvType().isEqual(tlvType)){
return block;
}
}
return null;
}
}
public class Block {
private VlInt tlvType; // TLV-TYPE
private VlInt length; // TLV-LENGTH
@@ -0,0 +1,86 @@
package encoding;
import java.util.LinkedList;
import java.util.List;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 20:01 2021/3/9
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
// TLV 组件容器
public class ElementContainer{
private List<Block> elementContainer;
public ElementContainer(){
elementContainer = new LinkedList<>();
}
/**
* 获取所有Block
* @return
*/
public List<Block> getElements(){
return elementContainer;
}
/**
* 获取block个数
* @return
*/
public int length(){
return elementContainer.size();
}
/**
* 清除 elements 中所有的子TLV
*/
public void clear(){
elementContainer.clear();
}
/**
* 往 elements 中添加一个子tlv
* @param block
*/
public void addElement(Block block){
elementContainer.add(block);
}
/**
* 移除所有指定type的子tlv
* @param tlvType
*/
public void removeElements(VlInt tlvType){
List<Block> elementContainer=new LinkedList<>();
for(Block block: this.elementContainer){
if(!block.getTlvType().isEqual(tlvType)){
elementContainer.add(block);
}
}
this.elementContainer =elementContainer;
}
/**
* 从 elements 中找到第一个指定type的子TLV
* @param tlvType
* @return
*/
public Block getElement(VlInt tlvType){
for(Block block: this.elementContainer){
if(block.getTlvType().isEqual(tlvType)){
return block;
}
}
return null;
}
public Block getBlock(int index){
if(index>=this.length()){
return null;
}
return this.elementContainer.get(index);
}
}