mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
Merge & identity comment & keyChain
This commit is contained in:
@@ -76,7 +76,7 @@ public class CanBePrefix implements TlvComponentBase,InitialAble, IEncodingAble
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvCanBePrefix))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvCanBePrefix))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAb
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvCongestionMark))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvCongestionMark))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvFreshnessPeriod))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvFreshnessPeriod))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvHopLimit))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvHopLimit))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,311 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 标识一个MIN网络标识
|
||||
// 1. 身份标识为由单个名称组件组成的平面标识
|
||||
// 2. 内容标识为一到多个名称组件组成的层级化标识
|
||||
// 3. 包格式如下:
|
||||
// Identifier = 101 TLV-LENGTH
|
||||
// *IdentifierComponent
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:34 2021/3/10
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
class IdentifierComponentContainer{
|
||||
private List<IdentifierComponent> identifierComponents;
|
||||
|
||||
public IdentifierComponentContainer(){
|
||||
identifierComponents=new LinkedList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有名称组件
|
||||
* @return
|
||||
*/
|
||||
public List<IdentifierComponent> getElements(){
|
||||
return identifierComponents;
|
||||
}
|
||||
|
||||
/**
|
||||
* 组件个数
|
||||
* @return
|
||||
*/
|
||||
public int length(){
|
||||
return identifierComponents.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空组件
|
||||
*/
|
||||
public void clear(){
|
||||
identifierComponents.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个组件
|
||||
* @param identifierComponent
|
||||
*/
|
||||
public void addElement(IdentifierComponent identifierComponent){
|
||||
identifierComponents.add(identifierComponent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定索引位置的组件
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponent getElement(int index){
|
||||
if(index>=this.length()){
|
||||
return null;
|
||||
}
|
||||
return this.identifierComponents.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前 num 个名称组件构成的标识
|
||||
* @param num
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponentContainer getPrefix(int num){
|
||||
if(num>this.length()){
|
||||
return null;
|
||||
}
|
||||
IdentifierComponentContainer identifierComponentContainer=new IdentifierComponentContainer();
|
||||
for (int i = 0; i < num; i++) {
|
||||
identifierComponentContainer.addElement(this.getElement(i));
|
||||
}
|
||||
return identifierComponentContainer;
|
||||
}
|
||||
|
||||
public IdentifierComponentContainer getSubIdentifierContainer(int startIndex,int num){
|
||||
if((startIndex+num)>this.length()){
|
||||
return null;
|
||||
}
|
||||
|
||||
IdentifierComponentContainer identifierComponentContainer=new IdentifierComponentContainer();
|
||||
for (int i = startIndex; i < startIndex+num; i++) {
|
||||
identifierComponentContainer.addElement(this.getElement(i));
|
||||
}
|
||||
return identifierComponentContainer;
|
||||
}
|
||||
}
|
||||
|
||||
public class Identifier implements TlvComponentBase, IEncodingAble {
|
||||
private IdentifierComponentContainer components;
|
||||
|
||||
/**
|
||||
* 根据一个 TLV Block 创建一个Identifier
|
||||
* @param block
|
||||
*/
|
||||
public Identifier(Block block){
|
||||
this.buildIdentifierByBlock(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标识的字符串标识创建一个标识
|
||||
* @param identifierString
|
||||
*/
|
||||
public Identifier(String identifierString){
|
||||
this.buildIdentifierByString(identifierString);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据components构造一个Identifier
|
||||
* @param container
|
||||
*/
|
||||
public Identifier(IdentifierComponentContainer container){
|
||||
this.buildIdentifierByComponents(container);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据一个 TLV Block 初始化当前Identifier
|
||||
* @param block
|
||||
*/
|
||||
public boolean buildIdentifierByBlock(Block block){
|
||||
// 如果不存在子 TLV, 则不可能是一个 Identifier
|
||||
if(!block.parseSubElements()){
|
||||
return false;
|
||||
}
|
||||
if(block.getSubElements().length()<1){
|
||||
return false;
|
||||
}
|
||||
return this.wireDecode(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标识字符串初始化当前Identifier
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public boolean buildIdentifierByString(String identifierString){
|
||||
if((identifierString.length()<=0)||(!identifierString.startsWith("/"))){
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] componentStrings=identifierString.split("/");
|
||||
this.components.clear();
|
||||
for (int i = 1; i < componentStrings.length; i++) {
|
||||
this.components.addElement(new IdentifierComponent(componentStrings[i]));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 components 初始化当前 Identifier
|
||||
* @param container
|
||||
* @return
|
||||
*/
|
||||
public boolean buildIdentifierByComponents(IdentifierComponentContainer container){
|
||||
if(container.length()<=0){
|
||||
return false;
|
||||
}
|
||||
|
||||
this.components.clear();
|
||||
for (int i = 0; i < container.length(); i++) {
|
||||
this.components.addElement(container.getElement(i));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对 Identifier 进行线速编码
|
||||
* // @Description:
|
||||
* // 1. 需要注意,本函数只对 Identifier 进行编码,不包含外层区分不同标识的Type,编码的格式如下:
|
||||
* // Identifier = 101 TLV-LENGTH
|
||||
* // *IdentifierComponent
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
// 编码 TLV-VALUE
|
||||
for (int i = 0; i < components.length(); i++) {
|
||||
int tmpLen=components.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-TYPE
|
||||
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifier));
|
||||
if(tmpLen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmpLen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 TLV Block 进行线速解码为一个 Identifier
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvIdentifier))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析子 TLV
|
||||
if(!block.parseSubElements()){
|
||||
return false;
|
||||
}
|
||||
this.components.clear();
|
||||
ElementContainer elementContainer=block.getSubElements();
|
||||
for (int i = 0; i < elementContainer.length(); i++) {
|
||||
this.components.addElement(new IdentifierComponent(elementContainer.getBlock(i)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Identifier表示成Uri字符串
|
||||
* // @Description:
|
||||
* // 1. https://named-data.net/doc/NDN-packet-spec/current/name.html#ndn-uri-scheme
|
||||
* @return
|
||||
*/
|
||||
public String toUri(){
|
||||
String res="";
|
||||
for (int i = 0; i < components.length(); i++) {
|
||||
res+=("/"+components.getElement(i).toUri());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否名称组件数量为空
|
||||
* @return
|
||||
*/
|
||||
public boolean empty(){
|
||||
return components.length()==0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本标识包含的名称组件数量
|
||||
* @return
|
||||
*/
|
||||
public int size(){
|
||||
return components.length();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本标识包含的第 index 个名称组件
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponent get(int index){
|
||||
return this.components.getElement(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标识中所有的名称组件
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponentContainer getComponents(){
|
||||
return this.components;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前 num 个名称组件构成的标识
|
||||
* @param num
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponentContainer getPrefix(int num){
|
||||
return this.components.getPrefix(num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回从第 startIndex 个组件开始往后的 num 个组件组成的新标识
|
||||
* @return
|
||||
*/
|
||||
public Identifier getSubIdentifier(int startIndex,int num){
|
||||
return new Identifier(this.components.getSubIdentifierContainer(startIndex,num));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前标识是否有效
|
||||
* @return
|
||||
*/
|
||||
public boolean isValid(){
|
||||
return this.components.length()>0;
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,15 @@ package component;
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 名称组件
|
||||
* todo: toUri()函数待完善
|
||||
* @Version: 1.0.0
|
||||
* @Date: 21:26 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
import encoding.Block;
|
||||
import encoding.Encoder;
|
||||
import encoding.IEncodingAble;
|
||||
import encoding.*;
|
||||
import util.ByteHelper;
|
||||
import util.StringHelper;
|
||||
|
||||
// @Description:
|
||||
// 1. 参考 => https://named-data.net/doc/tech-memos/naming-conventions.pdf ,借鉴但不是按这个实现的
|
||||
@@ -25,23 +26,199 @@ import encoding.IEncodingAble;
|
||||
// 0x01 非负整数(可以用1、2、4、8个字节表示)
|
||||
// 0x02 任意字节数组
|
||||
public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
|
||||
private Block block;
|
||||
|
||||
private Block identifierBlock; // IdentifierComponent数据以TLV形式存储下来
|
||||
private byte marker; // 标记 => 用来区分不同类型的 IdentifierComponent
|
||||
private String stringValue; // 如果 Marker == 0x00,则本组件存储一个字符串
|
||||
private long intValue; // 如果 Marker == 0x01,则本组件存储一个非负整型
|
||||
private byte[] byteArrayValue; // 如果 Marker == 0x02,则本组件存储一个字节数组
|
||||
|
||||
public static final byte MarkerString = 0x00;
|
||||
public static final byte MarkerNonNegativeInteger = 0x01;
|
||||
public static final byte MarkerByteArray = 0x02;
|
||||
private static final byte MarkerString = 0x00;
|
||||
private static final byte MarkerNonNegativeInteger = 0x01;
|
||||
private static final byte MarkerByteArray = 0x02;
|
||||
|
||||
/**
|
||||
* 用于判断某个字节是否在已分配的Marker列表当中
|
||||
* @param marker
|
||||
* @return
|
||||
*/
|
||||
private boolean isInMarker(byte marker){
|
||||
if((marker==MarkerString)||(marker==MarkerNonNegativeInteger)||(marker==MarkerByteArray)){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据一个字符串构造一个 IdentifierComponent
|
||||
* @param nameComponent
|
||||
*/
|
||||
public IdentifierComponent(String nameComponent){
|
||||
this.identifierBlock=new Block();
|
||||
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
|
||||
this.identifierBlock.setLength(new VlInt(nameComponent.length()+1));
|
||||
byte[] value=new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
|
||||
value[0]=MarkerString;
|
||||
System.arraycopy(nameComponent.getBytes(),0,value,1,nameComponent.getBytes().length);
|
||||
this.identifierBlock.setValue(value);
|
||||
this.marker=MarkerString;
|
||||
this.stringValue=nameComponent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据一个非负整数构造一个 IdentifierComponent
|
||||
* @param intValue
|
||||
*/
|
||||
public IdentifierComponent(long intValue){
|
||||
this.identifierBlock=new Block();
|
||||
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
|
||||
this.identifierBlock.setLength(new VlInt(new VlInt(intValue).size()+1));
|
||||
byte[] value=new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
|
||||
value[0]=MarkerNonNegativeInteger;
|
||||
System.arraycopy(new VlInt(intValue).getVlIntBytes(),0,value,1,new VlInt(intValue).size());
|
||||
this.identifierBlock.setValue(value);
|
||||
this.marker=MarkerNonNegativeInteger;
|
||||
this.intValue=intValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据一个字节数组构造一个 IdentifierComponent
|
||||
* @param arr
|
||||
*/
|
||||
public IdentifierComponent(byte[] arr){
|
||||
this.identifierBlock=new Block();
|
||||
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
|
||||
this.identifierBlock.setLength(new VlInt(arr.length+1));
|
||||
byte[] value=new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
|
||||
value[0]=MarkerByteArray;
|
||||
System.arraycopy(arr,0,value,1,arr.length);
|
||||
this.identifierBlock.setValue(value);
|
||||
this.marker=MarkerByteArray;
|
||||
this.byteArrayValue=arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据一个 TLV Block 创建一个 IdentifierComponent
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponent(Block block){
|
||||
this.wireDecode(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以字符串形式获取本 IdentifierComponent 的值
|
||||
* @return
|
||||
*/
|
||||
public String getString(){
|
||||
return this.stringValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以非负整数的形式获取本 IdentifierComponent 的值
|
||||
* @return
|
||||
*/
|
||||
public long getNonNegativeInteger(){
|
||||
return this.intValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以字节数组的形式获取本 IdentifierComponent 的值
|
||||
* @return
|
||||
*/
|
||||
public byte[] getByteArray(){
|
||||
return this.byteArrayValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前 IdentifierComponent 是否是存储字符串的类型
|
||||
* @return
|
||||
*/
|
||||
public boolean isString(){
|
||||
return this.marker==MarkerString;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前 IdentifierComponent 是否是存储非负整数的类型
|
||||
* @return
|
||||
*/
|
||||
public boolean isNonNegativeInteger(){
|
||||
return this.marker==MarkerNonNegativeInteger;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前 IdentifierComponent 是否是存储字节数组的类型
|
||||
* @return
|
||||
*/
|
||||
public boolean isByteArray(){
|
||||
return this.marker==MarkerByteArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 线速编码,将 IdentifierComponent 编码成网络中传输的字节数组格式
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
return 0;
|
||||
return this.identifierBlock.encode(encoder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 线速解码,将TLV Block解码成 IdentifierComponent
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
return false;
|
||||
this.identifierBlock=new Block(block);
|
||||
|
||||
//todo:越界问题
|
||||
int length=this.identifierBlock.getLength().getVlIntValue2Int();
|
||||
byte[] value=this.identifierBlock.getValue();
|
||||
|
||||
// 要求TLV-LENGTH至少为1,因为 IdentifierComponent 使用 TLV-VALUE 中的第一个字节作为Marker
|
||||
if(length<1){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 判断Marker是否有效
|
||||
if(!isInMarker(value[0])){
|
||||
return false;
|
||||
}
|
||||
this.marker=value[0];
|
||||
|
||||
// 根据不同类型的Marker,给不同的成员赋值
|
||||
byte[] v=ByteHelper.getTailBytes(value,1);
|
||||
if(isString()){
|
||||
this.stringValue=new String(v);
|
||||
}else if(isNonNegativeInteger()){
|
||||
// 如果Marker指定是非负整型,则TLV-LENGTH的有效值为2、3、5、9
|
||||
if((length!=2)&&(length!=3)&&(length!=5)&&(length!=9)){
|
||||
return false;
|
||||
}
|
||||
this.intValue=TLV.readNonNegativeInteger(v,0,v.length);
|
||||
}else{ // byte[]
|
||||
this.byteArrayValue=v;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 IdentifierComponent 使用百分号编码规则编码
|
||||
* @return
|
||||
*/
|
||||
public String toUri(){
|
||||
if(isString()){
|
||||
return StringHelper.escape(this.stringValue);
|
||||
}else if(isNonNegativeInteger()){
|
||||
// return StringHelper.FormatUint(this.intValue,10);
|
||||
// todo: 需要对long类型按照指定编码规则编码成字符串
|
||||
return "";
|
||||
}else{
|
||||
return StringHelper.escape(new String(this.byteArrayValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 10:48 2021/3/11
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
|
||||
private Identifier identifier;
|
||||
private VlInt tlvType;
|
||||
|
||||
public IdentifierWrapper(){
|
||||
}
|
||||
|
||||
public VlInt getTlvType(){
|
||||
return this.tlvType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从一个 IdentifierWrapper 编码成的 TLV Block 中解析出一个 IdentifierWrapper
|
||||
*/
|
||||
public IdentifierWrapper(Block block){
|
||||
if(!block.parseSubElements()){
|
||||
// todo: 错误处理
|
||||
}
|
||||
// 检查是否包含有且仅有一个 Identifier TLV
|
||||
if((block.getSubElements().length()!=1)&&block.getSubElements().getBlock(0).getType().isEqual(TLV.TlvIdentifier)){
|
||||
// todo: 错误处理
|
||||
}
|
||||
this.tlvType=block.getType();
|
||||
this.identifier.buildIdentifierByBlock(block.getSubElements().getBlock(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义标识类型,根据标识的字符串标识创建一个自定义的标识
|
||||
* @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CustomIdentifier = <自定义标识类型> TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param tlvType
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper(VlInt tlvType,String identifierString){
|
||||
if(TLV.isValidIdentifierType(tlvType)){
|
||||
// todo: 报错
|
||||
}
|
||||
this.tlvType=tlvType;
|
||||
this.identifier.buildIdentifierByString(identifierString);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义标识类型,根据 components 创建一个自定义的标识
|
||||
* @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CustomIdentifier = <自定义标识类型> TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param tlvType
|
||||
* @param container
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper(VlInt tlvType,IdentifierComponentContainer container){
|
||||
if(TLV.isValidIdentifierType(tlvType)){
|
||||
// todo: 报错
|
||||
}
|
||||
this.tlvType=tlvType;
|
||||
this.identifier.buildIdentifierByComponents(container);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义标识类型,根据 TLV Block 创建一个自定义标识
|
||||
* @param tlvType
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper(VlInt tlvType,Block block){
|
||||
if(TLV.isValidIdentifierType(tlvType)){
|
||||
// todo: 报错
|
||||
}
|
||||
this.tlvType=tlvType;
|
||||
this.identifier.buildIdentifierByBlock(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标识的字符串表示创建一个通用的身份标识
|
||||
* //
|
||||
* // @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CommonIdentifier = 102 TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper createCommonIdentifierByString(String identifierString){
|
||||
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
|
||||
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierCommon);
|
||||
identifierWrapper.identifier.buildIdentifierByString(identifierString);
|
||||
return identifierWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据components构造一个通用的身份标识
|
||||
* //
|
||||
* // @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CommonIdentifier = 102 TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param container
|
||||
*/
|
||||
public IdentifierWrapper createCommonIdentifierByComponents(IdentifierComponentContainer container){
|
||||
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
|
||||
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierCommon);
|
||||
identifierWrapper.identifier.buildIdentifierByComponents(container);
|
||||
return identifierWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 TLV Block 构造一个通用的身份标识
|
||||
* //
|
||||
* // @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CommonIdentifier = 102 TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* // @param block 传递 Identifier Block,而非 IdentifierWrapper Block
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper createCommonIdentifierByBlock(Block block){
|
||||
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
|
||||
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierCommon);
|
||||
identifierWrapper.identifier.buildIdentifierByBlock(block);
|
||||
return identifierWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标识的字符串表示创建一个内容兴趣标识
|
||||
* //
|
||||
* // @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CommonIdentifier = 103 TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper createContentInterestIdentifierByString(String identifierString){
|
||||
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
|
||||
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentInterest);
|
||||
identifierWrapper.identifier.buildIdentifierByString(identifierString);
|
||||
return identifierWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据components构造一个内容兴趣标识
|
||||
* //
|
||||
* // @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CommonIdentifier = 103 TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param container
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper createContentInterestIdentifierByComponents(IdentifierComponentContainer container){
|
||||
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
|
||||
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentInterest);
|
||||
identifierWrapper.identifier.buildIdentifierByComponents(container);
|
||||
return identifierWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 TLV Block 构造一个内容兴趣标识
|
||||
* //
|
||||
* // @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CommonIdentifier = 103 TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param block 传递 Identifier Block,而非 IdentifierWrapper Block
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper createContentInterestIdentifierByBlock(Block block){
|
||||
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
|
||||
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentInterest);
|
||||
identifierWrapper.identifier.buildIdentifierByBlock(block);
|
||||
return identifierWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标识的字符串表示创建一个内容数据标识
|
||||
* //
|
||||
* // @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CommonIdentifier = 104 TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper createContentDataIdentifierByString(String identifierString){
|
||||
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
|
||||
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentData);
|
||||
identifierWrapper.identifier.buildIdentifierByString(identifierString);
|
||||
return identifierWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据components构造一个内容数据标识
|
||||
* //
|
||||
* // @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CommonIdentifier = 104 TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param container
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper createContentDataIdentifierByComponents(IdentifierComponentContainer container){
|
||||
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
|
||||
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentData);
|
||||
identifierWrapper.identifier.buildIdentifierByComponents(container);
|
||||
return identifierWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 TLV Block 构造一个内容数据标识
|
||||
* //
|
||||
* // @Description:
|
||||
* // 1. 格式如下:
|
||||
* // CommonIdentifier = 104 TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param block 传递 Identifier Block,而非 IdentifierWrapper Block
|
||||
* @return
|
||||
*/
|
||||
public static IdentifierWrapper createContentDataIdentifierByBlock(Block block){
|
||||
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
|
||||
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentData);
|
||||
identifierWrapper.identifier.buildIdentifierByBlock(block);
|
||||
return identifierWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否有效
|
||||
* @return
|
||||
*/
|
||||
public boolean isValid(){
|
||||
return TLV.isValidIdentifierType(this.tlvType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 identifier 对象
|
||||
* @return
|
||||
*/
|
||||
public Identifier getIdentifier(){
|
||||
return this.identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对 IdentifierWrapper 执行线速编码
|
||||
* //
|
||||
* // @Description:
|
||||
* // 1. 格式如下:
|
||||
* // IdentifierWrapper = TLV-TYPE TLV-LENGTH
|
||||
* // <Identifier>
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen=this.identifier.wireEncode(encoder);
|
||||
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(this.tlvType);
|
||||
if(tmplen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 线速解码,从 TLV Block 中解码一个 IdentifierWrapper
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查TLV值是否有效
|
||||
if(!TLV.isValidIdentifierType(block.getType())){
|
||||
return false;
|
||||
}
|
||||
// 解析子 TLV
|
||||
if(!block.parseSubElements()){
|
||||
return false;
|
||||
}
|
||||
// 判断是否有且仅有一个子TLV
|
||||
if(block.getSubElements().length()!=1){
|
||||
return false;
|
||||
}
|
||||
// 尝试将唯一的子TLV Block解析成一个 Identifier
|
||||
if(!this.identifier.buildIdentifierByBlock(block.getSubElements().getBlock(0))){
|
||||
return false;
|
||||
}
|
||||
this.tlvType=block.getType();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 IdentifierWrapper 表示成Uri 字符串
|
||||
* @return
|
||||
*/
|
||||
public String toUri(){
|
||||
return this.identifier.toUri();
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvInterestLifeTime))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvInterestLifeTime))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示一个用于签名的证书或公钥的位置
|
||||
// 1. 格式如下:
|
||||
// KeyLocator = KEYLOCATOR-TYPE TLV-TYPE <Identifier>
|
||||
// 2. 详情参见:http://gitea.qjm253.cn/PKUSZ-future-network-lab/minlib/src/branch/master/docs/PacketFormat.md#4-signature
|
||||
* @Version: 1.0.0
|
||||
* @Date: 13:20 2021/3/12
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class KeyLocator implements TlvComponentBase, IEncodingAble {
|
||||
private Identifier identifier;
|
||||
|
||||
/**
|
||||
* 根据一个 TLV Block 创建一个 KeyLocator
|
||||
* @param block
|
||||
*/
|
||||
public KeyLocator(Block block){
|
||||
this.buildKeyLocatorByBlock(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据标识字符串 创建一个 KeyLocator
|
||||
* @param identifierString
|
||||
*/
|
||||
public KeyLocator(String identifierString){
|
||||
this.buildKeyLocatorByString(identifierString);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 components 创建一个 KeyLocator
|
||||
* @param container
|
||||
*/
|
||||
public KeyLocator(IdentifierComponentContainer container){
|
||||
this.buildKeyLocatorByComponents(container);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 Identifier 创建一个 KeyLocator
|
||||
* @param identifier
|
||||
*/
|
||||
public KeyLocator(Identifier identifier){
|
||||
this.buildKeyLocatorByIdentifier(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据一个 TVL Block 初始化当前 KeyLocator
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
public boolean buildKeyLocatorByBlock(Block block){
|
||||
return this.wireDecode(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据一个标识字符串初始化当前 KeyLocator
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public boolean buildKeyLocatorByString(String identifierString){
|
||||
return this.identifier.buildIdentifierByString(identifierString);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 components 初始化当前 KeyLocator
|
||||
* @param container
|
||||
* @return
|
||||
*/
|
||||
public boolean buildKeyLocatorByComponents(IdentifierComponentContainer container){
|
||||
return this.identifier.buildIdentifierByComponents(container);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 Identifier 初始化当前 KeyLocator
|
||||
* @param identifier
|
||||
* @return
|
||||
*/
|
||||
public boolean buildKeyLocatorByIdentifier(Identifier identifier){
|
||||
return this.identifier.buildIdentifierByComponents(identifier.getComponents());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用于签名的证书或公钥的标识名(指示了位置)
|
||||
* @return
|
||||
*/
|
||||
public Identifier getIdentifier(){
|
||||
return this.identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前 KeyLocator 是否有效
|
||||
* @return
|
||||
*/
|
||||
public boolean isValid(){
|
||||
return this.identifier.isValid();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 KeyLocator 线速编码为 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen=this.identifier.wireEncode(encoder);
|
||||
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(totalLength));
|
||||
if(tmplen<=0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 KeyLocator
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查Type是否正确
|
||||
if(!TLV.isValidIdentifierType(block.getType())){
|
||||
return false;
|
||||
}
|
||||
// 解析子 TLV
|
||||
if(!block.parseSubElements()){
|
||||
return false;
|
||||
}
|
||||
// 判断是否有且仅有一个子TLV
|
||||
if(block.getSubElements().length()!=1){
|
||||
return false;
|
||||
}
|
||||
// 解析子TLV为一个Identifier
|
||||
return this.identifier.wireDecode(block.getSubElements().getBlock(0));
|
||||
}
|
||||
}
|
||||
@@ -76,7 +76,7 @@ public class MustBeRefresh implements TlvComponentBase,InitialAble, IEncodingAbl
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvMustBeRefresh))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvMustBeRefresh))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvReadOnlyField))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ public class MutableField implements TlvComponentBase,IEncodingAble {
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvMutableField))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvMutableField))){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -80,11 +80,11 @@ public class MutableField implements TlvComponentBase,IEncodingAble {
|
||||
|
||||
for (int i = 0; i < block.getSubElements().length(); i++) {
|
||||
Block subBlock=block.getSubElements().getBlock(i);
|
||||
if(subBlock.getTlvType().isEqual(TLV.TlvMutableProtectField)){
|
||||
if(subBlock.getType().isEqual(TLV.TlvMutableProtectField)){
|
||||
if(!this.mutableProtectField.wireDecode(subBlock)){
|
||||
return false;
|
||||
}
|
||||
}else if(subBlock.getTlvType().isEqual(TLV.TlvMutableDangerousField)){
|
||||
}else if(subBlock.getType().isEqual(TLV.TlvMutableDangerousField)){
|
||||
if(!this.mutableDangerousField.wireDecode(subBlock)){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvReadOnlyField))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvNackHeader))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvNackHeader))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvNonce))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvNonce))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ public class Payload implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvPayload))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvPayload))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvReadOnlyField))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ public class TTL implements IEncodingAble {
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查Type是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvTTL))){
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvTTL))){
|
||||
return false;
|
||||
}
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
|
||||
@@ -20,6 +20,10 @@ public class Block {
|
||||
private ElementContainer elements; // TLV-sub-elements
|
||||
private byte[] raw; // TLV编码后的字节数组
|
||||
|
||||
public Block(){
|
||||
this.tlvType=new VlInt(TLV.TlvInvalid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从一个包含TLV的字节数组中构造一个 Block 对象
|
||||
* @param buffer
|
||||
@@ -27,7 +31,7 @@ public class Block {
|
||||
* 则会对比解析到的 TLV-LENGTH 与 TLV-VALUE的实际长度是否匹配
|
||||
* @return
|
||||
*/
|
||||
public Block createBlockByBuffer(byte[] buffer,boolean verifyLength){
|
||||
public static Block createBlockByBuffer(byte[] buffer,boolean verifyLength){
|
||||
if(buffer.length==0){
|
||||
return null;
|
||||
}
|
||||
@@ -57,7 +61,7 @@ public class Block {
|
||||
* @param verifyLength
|
||||
* @return
|
||||
*/
|
||||
public Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength){
|
||||
public static Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength){
|
||||
return new Block(tlvType,tlvLength,buffer,verifyLength);
|
||||
}
|
||||
|
||||
@@ -103,14 +107,18 @@ public class Block {
|
||||
}
|
||||
|
||||
public Block(Block block){
|
||||
|
||||
this.tlvType=block.tlvType;
|
||||
this.length=block.length;
|
||||
this.value=block.value;
|
||||
this.elements=block.elements;
|
||||
this.raw=block.raw;
|
||||
}
|
||||
|
||||
public VlInt getTlvType() {
|
||||
public VlInt getType() {
|
||||
return tlvType;
|
||||
}
|
||||
|
||||
public void setTlvType(VlInt tlvType) {
|
||||
public void setType(VlInt tlvType) {
|
||||
this.tlvType = tlvType;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ public class ElementContainer{
|
||||
public void removeElements(VlInt tlvType){
|
||||
List<Block> elementContainer=new LinkedList<>();
|
||||
for(Block block: this.elementContainer){
|
||||
if(!block.getTlvType().isEqual(tlvType)){
|
||||
if(!block.getType().isEqual(tlvType)){
|
||||
elementContainer.add(block);
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,7 @@ public class ElementContainer{
|
||||
*/
|
||||
public Block getElement(VlInt tlvType){
|
||||
for(Block block: this.elementContainer){
|
||||
if(block.getTlvType().isEqual(tlvType)){
|
||||
if(block.getType().isEqual(tlvType)){
|
||||
return block;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,7 +256,7 @@ public class Encoder {
|
||||
* @return
|
||||
*/
|
||||
public int prependBlock(Block block){
|
||||
return this.prependByteArrayBlock(block.getTlvType(),block.getValue(),new SizeT(block.getLength()));
|
||||
return this.prependByteArrayBlock(block.getType(),block.getValue(),new SizeT(block.getLength()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,7 +265,7 @@ public class Encoder {
|
||||
* @return
|
||||
*/
|
||||
public int appendBlock(Block block){
|
||||
return this.appendByteArrayBlock(block.getTlvType(),block.getValue(),new SizeT(block.getLength()));
|
||||
return this.appendByteArrayBlock(block.getType(),block.getValue(),new SizeT(block.getLength()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,6 +12,19 @@ import encoding.VlInt;
|
||||
*/
|
||||
public class ByteHelper {
|
||||
|
||||
/**
|
||||
* 获取从start到末尾的字节数组
|
||||
* @param buffer
|
||||
* @param startIndex
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getTailBytes(byte[] buffer,int startIndex){
|
||||
int len=buffer.length-startIndex;
|
||||
byte[] newBytes=new byte[len];
|
||||
System.arraycopy(buffer,startIndex,newBytes,0,len);
|
||||
return newBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从byte[]数组指定开始位置获取第一个字节
|
||||
* todo: start位置可能是大于int类型的数值
|
||||
@@ -197,7 +210,7 @@ public class ByteHelper {
|
||||
if(uint32.length!=4){
|
||||
return Long.parseLong(null);
|
||||
}
|
||||
byte[] bytes=new byte[64];
|
||||
byte[] bytes=new byte[8];
|
||||
for(int i=0;i<4;i++) {
|
||||
bytes[i] = 0x00;
|
||||
}
|
||||
@@ -232,6 +245,7 @@ public class ByteHelper {
|
||||
return byteArrayToLong(uint64);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 一个uint64转为的long,通过本函数再次转为byte[]
|
||||
* todo: 理论上可能发生越界
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package util;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 11:27 2021/3/10
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class StringHelper {
|
||||
|
||||
/**
|
||||
* 对字符串实施百分号编码
|
||||
* // @Description:
|
||||
* // 1. 具体编码的定义参见:RFC 3986 section 2
|
||||
* // 2. 将除了字母('a'~'z', 'A'~'Z')、数字('0' ~ '9')和指定特殊字符('-', '.', '_', '~')
|
||||
* // 之外的所有字符都进行百分号编码。
|
||||
* // 3. 对字符串中的每个UTF-8字符c(用八位一个字节表示)进行如下规则的编码:
|
||||
* // a. 首先将一个八位字节分为两个部分,hi = (c % 0xf0) >> 4 ; lo = c % 0xf。hi 和 lo 均为两个字节表示的数字,范围为(0~15)
|
||||
* // b. 然后分别用 hi 和 lo 作为索引,从 "0123456789ABCDEF" 字符串取指定位置的字符,最后编码的结果如下表示:
|
||||
* // '%' + "0123456789ABCDEF"[hi] + "0123456789ABCDEF"[lo]
|
||||
* // c. 相当于每个需要编码的字符都编码成了一个%开头的三个字符表示
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String escape(String str){
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 对字符串实施百分号解码
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String unescape(String str){
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package encoding;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/*
|
||||
* @Author: feng Zhao
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 12:28 2021/3/12
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
public class VlIntTest {
|
||||
|
||||
// @Test
|
||||
// public void size() {
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void createVlInt() {
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void createVlIntByBigInteger() {
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void getVlIntBytes() {
|
||||
byte[] a = new byte[]{1,2,3,4};
|
||||
VlInt vlInt_byte = new VlInt(a);
|
||||
byte[] b = new byte[]{1,2,3,5};
|
||||
assertArrayEquals(vlInt_byte.getVlIntBytes(),a);
|
||||
assertFalse(vlInt_byte.getVlIntBytes().equals(b));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getVlIntValue2Int() {
|
||||
//vlint(int)的size小于4时,测试是否能准确转为int输出
|
||||
int a = 222;
|
||||
int b = 22222222;
|
||||
VlInt vlInt_int1 = new VlInt(a);
|
||||
VlInt vlInt_int2 = new VlInt(b);
|
||||
//测试size大小
|
||||
assertTrue(vlInt_int1.size()<4);
|
||||
assertFalse(vlInt_int2.size()<4);
|
||||
//测试VlIntValue是否转为int
|
||||
assertEquals(vlInt_int1.getVlIntValue2Int(),a);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getVlIntValue2Long() {
|
||||
long l1 = new Long(0);
|
||||
long l2 = new Long(1);
|
||||
long l3 = new Long(4294967295L); //2^32-1
|
||||
long l4 = new Long(4294967296L); //2^32
|
||||
|
||||
VlInt vlInt_long1 = new VlInt(l1); // size = 1
|
||||
VlInt vlInt_long2 = new VlInt(l2); // size = 1
|
||||
VlInt vlInt_long3 = new VlInt(l3); // size = 5
|
||||
VlInt vlInt_long4 = new VlInt(l4); // size = 9
|
||||
|
||||
assertTrue(vlInt_long3.size()<8);
|
||||
assertFalse(vlInt_long4.size()<8);
|
||||
assertEquals(vlInt_long1.getVlIntValue2Long(),l1);
|
||||
assertEquals(vlInt_long2.getVlIntValue2Long(),l2);
|
||||
assertEquals(vlInt_long3.getVlIntValue2Long(),l3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getVlIntValue() {
|
||||
long l1 = new Long(4294967295L); //2^32-1
|
||||
long l2 = new Long(4294967296L); //2^32
|
||||
BigInteger b = BigInteger.valueOf(4294967296L);//2^32
|
||||
|
||||
VlInt vlInt_long = new VlInt(l1);
|
||||
VlInt vlInt_long2 = new VlInt(l2);
|
||||
VlInt vlInt_bigInteger = new VlInt(b);
|
||||
//判断size
|
||||
assertFalse(vlInt_long.size()>=9);
|
||||
assertTrue(vlInt_long2.size()>=9);
|
||||
assertTrue(vlInt_bigInteger.size()>=9);
|
||||
//判断值是否相等
|
||||
assertEquals(vlInt_bigInteger.getVlIntValue(),b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEqual() {
|
||||
int a1 = 1;
|
||||
int a2 = 65535;
|
||||
int a2_test = 66666;
|
||||
long a3 = 4294967295L;
|
||||
long a4 = 4294967296L;
|
||||
VlInt vlInt1 = new VlInt(a1); //size = 1
|
||||
VlInt vlInt_test = new VlInt(a1);
|
||||
VlInt vlInt2 = new VlInt(a2); //size = 3
|
||||
VlInt vlInt3 = new VlInt(a3); //size = 5
|
||||
VlInt vlInt4 = new VlInt(a4); //size = 9
|
||||
|
||||
assertTrue(vlInt1.isEqual(vlInt_test));
|
||||
assertTrue(vlInt2.isEqual(a2));
|
||||
assertFalse(vlInt2.isEqual(a2_test));
|
||||
assertTrue(vlInt3.isEqual(a3));
|
||||
assertTrue(vlInt4.isEqual(a4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareTo() {//小于:-1 大于:1 等于0
|
||||
//param:vlInt
|
||||
VlInt vlInt1 = new VlInt(1);
|
||||
VlInt vlInt2 = new VlInt(1);
|
||||
VlInt vlInt3 = new VlInt(4294967296L);
|
||||
assertEquals(vlInt1.compareTo(vlInt2),0);
|
||||
assertEquals(vlInt1.compareTo(vlInt3),-1);
|
||||
//param:int
|
||||
int a1 = 1,a2 = 2;
|
||||
assertEquals(vlInt1.compareTo(a1),0);
|
||||
assertEquals(vlInt1.compareTo(a2),-1);
|
||||
//param:long
|
||||
long l1=4294967295L,l2=4294967296L;
|
||||
VlInt v = new VlInt(l2);
|
||||
assertEquals(v.compareTo(l1),1);
|
||||
assertEquals(v.compareTo(l2),0);
|
||||
|
||||
}
|
||||
|
||||
//todo:暂没考虑两者差、两者和 越界int最大值的问题
|
||||
@Test
|
||||
public void subtract_add() {
|
||||
int a1 = 0,a2 = 65535;
|
||||
long l = 4294967296L;
|
||||
int s1 = a2-a1,s2 = a1-a2;
|
||||
// long r1 = l-a1,r2 = l-a2; //差值越界int最大值
|
||||
VlInt vlInt1 = new VlInt(a1);
|
||||
VlInt vlInt2 = new VlInt(a2);
|
||||
VlInt vlInt3 = new VlInt(l);
|
||||
assertEquals(vlInt2.subtract(vlInt1),s1);
|
||||
assertEquals(vlInt1.subtract(vlInt2),s2); //差为负数
|
||||
//error:差值越界int最大值
|
||||
// assertEquals(vlInt3.subtract(vlInt2),r1);
|
||||
|
||||
|
||||
int sum1 = a1+a2;
|
||||
long sum2 = a2+l;
|
||||
//param:VlInt
|
||||
assertEquals(vlInt1.add(vlInt2),sum1);
|
||||
// assertEquals(vlInt1.add(vlInt3),sum2); //两者之和为long,越界int最大值
|
||||
//param:int
|
||||
assertEquals(vlInt1.add(a2),sum1);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void bigInteger2vlintBytes(){
|
||||
// BigInteger a1 = BigInteger.valueOf(0);
|
||||
// BigInteger a2 = BigInteger.valueOf(240);
|
||||
// BigInteger a3 = BigInteger.valueOf(242);
|
||||
// BigInteger a4 = BigInteger.valueOf(256);
|
||||
// BigInteger a5 = BigInteger.valueOf(65535);
|
||||
// BigInteger a6 = BigInteger.valueOf(65536);
|
||||
// BigInteger a7 = BigInteger.valueOf(4294967295L);
|
||||
// BigInteger a8 = BigInteger.valueOf(4294967296L);
|
||||
// byte[] b1 = new byte[]{0x00}; //0:size=1
|
||||
// byte[] b2 = new byte[]{(byte)0xf0}; //240<241:size=1
|
||||
// byte[] b3 = new byte[]{(byte)0xf2}; //242:size=3
|
||||
// byte[] b4 =new byte[]{0x01, 0x00}; //256:size=3
|
||||
// byte[] b5 = new byte[]{(byte) 0xff, (byte) 0xff};//65535<2^16:size=3
|
||||
// byte[] b6 = new byte[]{0x00,0x01,0x00,0x00}; //65536:size=5
|
||||
// //4294967295<2^32:size=5
|
||||
// byte[] b7 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};
|
||||
// //4294967296>=2^32:size=9
|
||||
// byte[] b8 = new byte[]{0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
|
||||
//
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void vlintBytes2bigInteger(){
|
||||
//
|
||||
// }
|
||||
}
|
||||
@@ -19,8 +19,18 @@ public class HumanImpl implements God,Animal {
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
HumanImpl h=new HumanImpl();
|
||||
h.sayHi();
|
||||
h.makeWorld();
|
||||
// HumanImpl h=new HumanImpl();
|
||||
// h.sayHi();
|
||||
// h.makeWorld();
|
||||
// String ss="hello";
|
||||
// System.out.println(ss.length());
|
||||
// System.out.println(ss.getBytes().length);
|
||||
String[] i="/1/wefree/a.avi".split("/");
|
||||
System.out.println("-------------");
|
||||
for (int j = 0; j < i.length; j++) {
|
||||
System.out.println(i[j]);
|
||||
}
|
||||
System.out.println("-------------");
|
||||
// System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user