mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
finished component: IdentifierField
This commit is contained in:
@@ -28,7 +28,7 @@ class IdentifierComponentContainer{
|
||||
* 获取所有名称组件
|
||||
* @return
|
||||
*/
|
||||
public List<IdentifierComponent> getElement(){
|
||||
public List<IdentifierComponent> getElements(){
|
||||
return identifierComponents;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示一个MIN包格式中的标识区
|
||||
* @Version: 1.0.0
|
||||
* @Date: 11:02 2021/3/12
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
class IdentifierContainer{
|
||||
private List<IdentifierWrapper> identifierWrappers;
|
||||
|
||||
public IdentifierContainer(){
|
||||
identifierWrappers=new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 组件个数
|
||||
* @return
|
||||
*/
|
||||
public int length(){
|
||||
return identifierWrappers.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有组件
|
||||
*/
|
||||
public void clear(){
|
||||
identifierWrappers.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加一个组件
|
||||
* @param identifierWrapper
|
||||
*/
|
||||
public void addElement(IdentifierWrapper identifierWrapper){
|
||||
identifierWrappers.add(identifierWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定索引位置的组件
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper getElement(int index){
|
||||
if(index>=this.length()){
|
||||
return null;
|
||||
}
|
||||
return this.identifierWrappers.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标识区中第一个指定类型标识
|
||||
* @param vlInt
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper getElementByType(VlInt vlInt){
|
||||
for (int i = 0; i < identifierWrappers.size(); i++) {
|
||||
if(identifierWrappers.get(i).getTlvType().isEqual(vlInt)){
|
||||
return identifierWrappers.get(i);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setElement(int index,IdentifierWrapper wrapper){
|
||||
if(index>=this.length()){
|
||||
return;
|
||||
}
|
||||
this.identifierWrappers.set(index,wrapper);
|
||||
}
|
||||
}
|
||||
|
||||
public class IdentifierField implements TlvComponentBase, IEncodingAble {
|
||||
private IdentifierContainer identifiers;
|
||||
|
||||
public IdentifierField(){
|
||||
identifiers=new IdentifierContainer();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个标识
|
||||
* @param wrapper
|
||||
*/
|
||||
public void addIdentifier(IdentifierWrapper wrapper){
|
||||
this.identifiers.addElement(wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标识区中第一个指定类型标识
|
||||
* @param tlvType
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper getIdentifierByType(VlInt tlvType){
|
||||
return this.identifiers.getElementByType(tlvType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定位置的标识
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper getIdentifier(int index){
|
||||
return this.identifiers.getElement(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置指定位置的标识
|
||||
* @param index
|
||||
* @param wrapper
|
||||
*/
|
||||
public void setIdentifier(int index,IdentifierWrapper wrapper){
|
||||
this.identifiers.setElement(index,wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空标识区所有的标识
|
||||
*/
|
||||
public void clearIdentifiers(){
|
||||
this.identifiers.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的标识
|
||||
* @return
|
||||
*/
|
||||
public IdentifierContainer getIdentifiers(){
|
||||
return this.identifiers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将标识区线速编码为 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
// 如果标识区不存在标识,则是一个无效的MIN包,直接抛出错误
|
||||
if(this.identifiers.length()==0){
|
||||
return 0;
|
||||
}
|
||||
int totalLength=0;
|
||||
// 编码 TLV-VALUE
|
||||
// 反向遍历,可以保证解码的时候正向输出
|
||||
for (int i = this.identifiers.length()-1; i >= 0; i--) {
|
||||
int tmplen=this.identifiers.getElement(i).wireEncode(encoder);
|
||||
if(tmplen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
}
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if(tmplen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifierField));
|
||||
if(tmplen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 IdentifierField
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 TLV-TYPE 是否正确
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvIdentifierField))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析子 TLV
|
||||
if(!block.parseSubElements()){
|
||||
return false;
|
||||
}
|
||||
IdentifierContainer identifierContainer=new IdentifierContainer();
|
||||
int len=block.getSubElements().length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
IdentifierWrapper newWrapper=new IdentifierWrapper(block.getSubElements().getBlock(i));
|
||||
if(newWrapper==null){
|
||||
return false;
|
||||
}
|
||||
identifierContainer.addElement(newWrapper);
|
||||
}
|
||||
this.identifiers=identifierContainer;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,10 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
|
||||
public IdentifierWrapper(){
|
||||
}
|
||||
|
||||
public VlInt getTlvType(){
|
||||
return this.tlvType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从一个 IdentifierWrapper 编码成的 TLV Block 中解析出一个 IdentifierWrapper
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user