maven管理项目 + junit单元测试

This commit is contained in:
ghy committed 2021-03-02 21:35:29 +08:00
1 parent b07bf8a9f9
commit 5938be8893
9 files changed
+47 -1

No files matched your search

+192
View File
@@ -0,0 +1,192 @@
package encoding;
import util.ByteHelper;
import java.util.LinkedList;
import java.util.List;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 14:19 2021/3/2
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
// TLV 组件容器
class ElementContainer{
private List<Block> ElementContainer;
public ElementContainer(){
ElementContainer= new LinkedList<>();
}
public int length(){
return ElementContainer.size();
}
}
public class Block {
private VlInt tlvType; // TLV-TYPE
private VlInt length; // TLV-LENGTH
private byte[] value; // TLV-VALUE
private ElementContainer elements; // TLV-sub-elements
private byte[] raw; // TLV编码后的字节数组
/**
* 从一个包含TLV的字节数组中构造一个 Block 对象
* @param buffer
* @param verifyLength 是否要验证TLV的长度是否匹配,如果true,
* 则会对比解析到的 TLV-LENGTH 与 TLV-VALUE的实际长度是否匹配
* @return
*/
public Block createBlockByBuffer(byte[] buffer,boolean verifyLength){
if(buffer.length==0){
return null;
}
// 读取type
VlInt tlvType=TLV.readType(buffer,new VlInt(0));
if(tlvType==null){
return null;
}
// 读取length
VlInt tlvLength=TLV.readVarNumber(buffer,new VlInt(tlvType.SizeOfVarNumber()));
if(tlvLength==null){
return null;
}
return createBlockByTypeLengthBuffer(tlvType,tlvLength,
ByteHelper.getLenBytes(buffer,new VlInt(tlvType.SizeOfVarNumber()+tlvLength.SizeOfVarNumber()),
buffer.length-(tlvType.SizeOfVarNumber()+tlvLength.SizeOfVarNumber())),verifyLength);
}
/**
* 传入 T、L、V 构造一个 TLV Block
* @param tlvType
* @param tlvLength
* @param buffer
* @param verifyLength
* @return
*/
public Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength){
return new Block(tlvType,tlvLength,buffer,verifyLength);
}
/**
* 构造函数
* @param tlvType
* @param tlvLength
* @param buffer
* @param verifyLength
*/
public Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength){
this.tlvType=tlvType;
this.length=tlvLength;
// 检查长度是否正确
if(verifyLength && (!this.length.isEqual(buffer.length))){
//todo: 抛出异常
}
// todo;设置 Raw
// todo 设置Value
}
public VlInt getTlvType() {
return tlvType;
}
public void setTlvType(VlInt tlvType) {
this.tlvType = tlvType;
}
public VlInt getLength() {
return length;
}
public void setLength(VlInt length) {
this.length = length;
}
public byte[] getValue() {
return value;
}
public void setValue(byte[] value) {
this.value = value;
}
public byte[] getRaw() {
return raw;
}
/**
* 整个TLV编码后的大小,包含T和L
* @return
*/
public SizeT Size(){
if(!this.isValid()){
return null;
}
return new SizeT(this.tlvType.SizeOfVarNumber()+this.length.SizeOfVarNumber()+this.value.length);
}
/**
* 判断当前的 Block 对象是否含有 value
* @return
*/
public boolean hasValue(){
return this.value.length!=0;
}
/**
* 判断当前的 Block 对象是否含有 raw数组
* @return
*/
public boolean hasRaw(){
return this.raw.length!=0;
}
// ************** Sub-Elements **************
/**
* 判断当前 TLV Block 是否包含子 TLV
* 1. 每次通过 CreateBlockBy* 系列工厂方法构造出Block之后,需要手动调用 ParseSubElements 方法,
* 才会去解析一级子TLV
* @return
*/
public boolean hasSubElement(){
return this.elements.length()!=0;
}
/**
* 获取所有的一级子TLV Block
* * 1. 每次通过 CreateBlockBy* 系列工厂方法构造出Block之后,需要手动调用 ParseSubElements 方法,
* * 才会去解析一级子TLV
* @return
*/
public ElementContainer getSubElements(){
return this.elements;
}
/**
* 从 value 字节数组中解析出所有的一级子 TLV Block
* 1. 这要求这个TLV确实存在嵌套的子TLV,或者为空;
* 2. 如果TLV的value不为空,且不存在嵌套的子TLV,则会抛出错误
* @return
*/
// public boolean parseSubElements(){
//
// }
/**
* 判断是否有效
* @return
*/
public boolean isValid(){
return true;
}
}
+181
View File
@@ -0,0 +1,181 @@
package encoding;
import java.math.BigInteger;
import static java.lang.Math.pow;
/*
* @Author: Wang Feng
* @Description:变长无符号整型,用于表示TLV中的T和L
* @Version: 1.0.0
* @Date: 18:13 2021/3/1
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class SizeT {
// 实际传输的数据
private byte[] VlIntBytes;
// 变长整数表示的真实数值
private BigInteger VlIntValue;
/**
* 测量一个变长整型实际用几个字节表示
* @return
*/
public int SizeOfVarNumber(){
return SizeOfVarNumber(this.VlIntValue);
}
private static int SizeOfVarNumber(BigInteger bigInteger){
if(bigInteger.compareTo(BigInteger.valueOf(Long.parseLong("241"))) < 0){
return 1;
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 16)).longValue())) < 0){
return 3;
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 32)).longValue())) < 0){
return 5;
}else{ //MIN1.0中,VlInt的size最大为9
return 9;
}
}
//todo:判断bytes是否是一个合法的变长非负整型
//todo:判断biginteger是否合法
public SizeT(byte[] bytes){
this.VlIntBytes=bytes;
this.VlIntValue=vlintBytes2bigInteger(bytes);
}
public SizeT(BigInteger bigInteger){
this.VlIntValue=bigInteger;
this.VlIntBytes=bigInteger2vlintBytes(bigInteger);
}
// 传入一个变长无符号字节数组byte[],将它解析成一个VlInt
public SizeT createVlInt(byte[] bytes){
return new SizeT(bytes);
}
// 传入一个BigInteger,将它解析成一个VlInt
public SizeT createVlIntByBigInteger(BigInteger bigInteger){
return new SizeT(bigInteger);
}
// todo:传入一个int,将它解析成一个VlInt
public SizeT(int i){
this.VlIntValue=new BigInteger(String.valueOf(i));
this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue);
}
// todo:传入一个long,将它解析成一个VlInt
// 将vlint转为一个int输出:需要判断size是不是小于4(1 or 3)
public Integer getVlIntValue2Int(){
if(this.SizeOfVarNumber()<4){
return VlIntValue.intValue();
}
return null;
}
// 将vlint转为一个long输出:需要判断size是不是小于8(5)
public Long getVlIntValue2Long(){
if(this.SizeOfVarNumber()<8){
return VlIntValue.longValue();
}
return null;
}
// 将vlint转为一个biginteger输出:需要判断size是不是等于9(9)
public BigInteger getVlIntValue(){
if(this.SizeOfVarNumber()>=9){
return VlIntValue;
}
return null;
}
// 判断vlint是否与某个int值相等
public boolean isEqual(Integer vlint){
return isEqual(vlint.intValue());
}
public boolean isEqual(int i){
if(this.VlIntValue.compareTo(new BigInteger(String.valueOf(i)))==0){
return true;
}
return false;
}
public boolean isEqual(SizeT vlInt){
if(this.VlIntValue.compareTo(vlInt.VlIntValue)==0){
return true;
}
return false;
}
// todo: 判断vlint是否与某个long值相等
/**
* 判断与另一个VlInt的数值孰大孰小
* @param vlInt
* @return
*/
public int compareTo(SizeT vlInt){
return this.VlIntValue.compareTo(vlInt.VlIntValue);
}
public int compareTo(int i){
BigInteger bigInteger=new BigInteger(String.valueOf(i));
return this.VlIntValue.compareTo(bigInteger);
}
/**
* 减去另一个VlInt
* todo:暂没考虑两者差值越界int最大值
* @param vlInt
* @return
*/
public int subtract(SizeT vlInt){
return this.VlIntValue.subtract(vlInt.VlIntValue).intValue();
}
// 将BigInteger转为byte[按照变长无符号整数方式转]
private byte[] bigInteger2vlintBytes(BigInteger bigInteger){
byte[] vlintbytes;
if(SizeOfVarNumber(bigInteger)==1){
vlintbytes=new byte[1];
//todo
}else if(SizeOfVarNumber(bigInteger)==3){
vlintbytes=new byte[3];
//todo
}else if(SizeOfVarNumber(bigInteger)==5){
vlintbytes=new byte[5];
//todo
}else{
vlintbytes=new byte[9];
//todo
}
return vlintbytes;
}
// 将变长无符号整数数组转为BigInteger
private BigInteger vlintBytes2bigInteger(byte[] vlIntBytes){
BigInteger vlintValue=null;
if(vlIntBytes.length==1){
//todo
}else if(vlIntBytes.length==3){
//todo
}else if(vlIntBytes.length==5){
//todo
}else if(vlIntBytes.length==9){
//todo
}else{
return null;
}
return vlintValue;
}
// 测试
public static void main(String[] args){
BigInteger bigInteger=new BigInteger("3");
SizeT vlInt=new SizeT(bigInteger);
System.out.println(vlInt.isEqual(2));
}
}
+157
View File
@@ -0,0 +1,157 @@
package encoding;
import static util.ByteHelper.getFirstByte;
import static util.ByteHelper.getLenBytes;
import static util.ByteHelper.unsignedbyte2int;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 17:54 2021/3/1
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class TLV {
////// 一级:网络分组级别TLV号分配(0 ~ 49)
public static final int TlvInvalid = 0; // 无效包
public static final int TlvPacketMINCommon = 5; // 通用MIN包
public static final int TlvPacketMINManagement = 6; // MIN管理包
public static final int TlvPacketMINIP = 7; // MIN支持IP包
////// 二级:网络包分区TLV号分配 (50 ~ 99)
public static final int TlvIdentifierField = 50; // 目的标识区
public static final int TlvSignatureField = 51; // 数字签名区
public static final int TlvReadOnlyField = 52; // 只读数据区
public static final int TlvMutableField = 53; // 可变数据区
public static final int TlvMutableProtectField = 54; // 可变数据区中的保护区
public static final int TlvMutableDangerousField = 55; // 可变数据区中的非受保护区
////// 组件 (100 -> 500)
// 标识分配
public static final int TlvIdentifierComponent = 100; // 标识名称组件
public static final int TlvIdentifier = 101; // 标识
public static final int TlvIdentifierCommon = 102; // 身份标识
public static final int TlvIdentifierContentInterest = 103; // 内容兴趣标识
public static final int TlvIdentifierContentData = 104; // 内容数据标识
// 数字签名
public static final int TlvSignatureValue = 200; // 签名值
public static final int TlvSignatureInfo = 201; // 签名参数
public static final int TlvSignatureType = 202; // 签名类型
public static final int TlvKeyLocator = 203; // 指示证书或公钥的位置
public static final int TlvPayload = 204; // 网络包负载
public static final int TlvCanBePrefix = 205; // 是否可以被较长的数据包前缀匹配
public static final int TlvMustBeRefresh = 206; // 是否强制只拉取新鲜的数据
public static final int TlvInterestLifeTime = 207; // 兴趣包生存期
public static final int TlvNonce = 208; // 随机数
public static final int TlvHopLimit = 209; // 跳数限制
public static final int TlvFreshnessPeriod = 210; // 数据包新鲜时间
public static final int TlvCongestionMark = 211; // 拥塞标记
public static final int TlvTTL = 212; // Time to live
public static final int TlvNackHeader = 213; // Nack header
// 管理通信协议
public static final int TlvSliceNumber = 211; // 分片数量
public static final int TlvSliceSize = 212; // 分片大小
public static final int TlvTlvLogicFaceId = 213; // 逻辑接口id
public static final int TlvRemoteAddr = 214; // 源端地址
public static final int TlvLocalAddr = 215; // 本地地址
public static final int TlvMtu = 216; // 最大传输单元
public static final int TlvRet = 217; // bool类型,成功或失败
public static final int TlvOrigin = 218; //
public static final int TlvCost = 219; // 链路开销
public static final int TlvExpires = 220; // LogicFace超时时间,-1为不超时(never)
// 目前已分配的最大的Tlv值,第一轮分配的最大值为500,超过该值则断定为不合法
// TODO: 目前TLV值的分配,第一轮分配都在500以内,如果以后分配更高值时,需要修改此处的限制
public static final int MaxTlvNum = 500;
/**
* 从字节数组中读出一个变长整型
* @param buffer
* @param start
* @return
*/
public static VlInt readVarNumber(byte[] buffer,VlInt start){
VlInt length=new VlInt(buffer.length);
if(start.compareTo(length)>=0){
return null;
}
int firstbyte=unsignedbyte2int(getFirstByte(buffer,start));
if(firstbyte<241){
return new VlInt(firstbyte);
}else if(firstbyte==241){
if(length.subtract(start)<3){
return null;
}
return new VlInt(getLenBytes(buffer,start,3));
}else if(firstbyte==242){
if(length.subtract(start)<5){
return null;
}
return new VlInt(getLenBytes(buffer,start,5));
}else if(firstbyte==243){
if(length.subtract(start)<9){
return null;
}
return new VlInt(getLenBytes(buffer,start,9));
}else{ //MIN1.0中算越界
return null;
}
}
/**
* 从字节数组的指定位置中读取一个变长的整型作为 TLV Type
* @param buffer
* @param start
* @return
*/
public static VlInt readType(byte[] buffer,VlInt start){
VlInt tlvType=readVarNumber(buffer,start);
if(tlvType==null||tlvType.isEqual(0)||tlvType.compareTo(MaxTlvNum)>0){
return null;
}
return tlvType;
}
// todo:从字节数组的指定位置中读取一个非负整型
/**
* 检查某个type值是否是指定的 TLV-TYPE
* @param current
* @param target
* @return
*/
public static boolean expectType(VlInt current,VlInt target){
if(current.isEqual(target)){
return true;
}
return false;
}
/**
* 根据 TLV 分配规则,判断某个tlvType是否是合规的标识类型
* TODO: 目前TLV的分配是500为一轮,如果标识范围扩展了,本方法需要对应修改
* @param tlvType
* @return
*/
public static boolean isValidIdentifierType(VlInt tlvType){
return tlvType.compareTo(102)>=0 && tlvType.compareTo(199)<=0;
}
/**
* 根据 TLV 分配规则,判断某个tlvType是否是合规的包类型
* TODO: 目前TLV的分配是500为一轮,如果标识范围扩展了,本方法需要对应修改
* @param tlvType
* @return
*/
public static boolean isValidPacketType(VlInt tlvType){
return tlvType.compareTo(0)>0 && tlvType.compareTo(50)<0;
}
//todo: 错误处理
}
+181
View File
@@ -0,0 +1,181 @@
package encoding;
import java.math.BigInteger;
import static java.lang.Math.pow;
/*
* @Author: Wang Feng
* @Description:变长无符号整型,用于表示TLV中的T和L
* @Version: 1.0.0
* @Date: 18:13 2021/3/1
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class VlInt {
// 实际传输的数据
private byte[] VlIntBytes;
// 变长整数表示的真实数值
private BigInteger VlIntValue;
/**
* 测量一个变长整型实际用几个字节表示
* @return
*/
public int SizeOfVarNumber(){
return SizeOfVarNumber(this.VlIntValue);
}
private static int SizeOfVarNumber(BigInteger bigInteger){
if(bigInteger.compareTo(BigInteger.valueOf(Long.parseLong("241"))) < 0){
return 1;
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 16)).longValue())) < 0){
return 3;
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 32)).longValue())) < 0){
return 5;
}else{ //MIN1.0中,VlInt的size最大为9
return 9;
}
}
//todo:判断bytes是否是一个合法的变长非负整型
//todo:判断biginteger是否合法
public VlInt(byte[] bytes){
this.VlIntBytes=bytes;
this.VlIntValue=vlintBytes2bigInteger(bytes);
}
public VlInt(BigInteger bigInteger){
this.VlIntValue=bigInteger;
this.VlIntBytes=bigInteger2vlintBytes(bigInteger);
}
// 传入一个变长无符号字节数组byte[],将它解析成一个VlInt
public VlInt createVlInt(byte[] bytes){
return new VlInt(bytes);
}
// 传入一个BigInteger,将它解析成一个VlInt
public VlInt createVlIntByBigInteger(BigInteger bigInteger){
return new VlInt(bigInteger);
}
// todo:传入一个int,将它解析成一个VlInt
public VlInt(int i){
this.VlIntValue=new BigInteger(String.valueOf(i));
this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue);
}
// todo:传入一个long,将它解析成一个VlInt
// 将vlint转为一个int输出:需要判断size是不是小于4(1 or 3)
public Integer getVlIntValue2Int(){
if(this.SizeOfVarNumber()<4){
return VlIntValue.intValue();
}
return null;
}
// 将vlint转为一个long输出:需要判断size是不是小于8(5)
public Long getVlIntValue2Long(){
if(this.SizeOfVarNumber()<8){
return VlIntValue.longValue();
}
return null;
}
// 将vlint转为一个biginteger输出:需要判断size是不是等于9(9)
public BigInteger getVlIntValue(){
if(this.SizeOfVarNumber()>=9){
return VlIntValue;
}
return null;
}
// 判断vlint是否与某个int值相等
public boolean isEqual(Integer vlint){
return isEqual(vlint.intValue());
}
public boolean isEqual(int i){
if(this.VlIntValue.compareTo(new BigInteger(String.valueOf(i)))==0){
return true;
}
return false;
}
public boolean isEqual(VlInt vlInt){
if(this.VlIntValue.compareTo(vlInt.VlIntValue)==0){
return true;
}
return false;
}
// todo: 判断vlint是否与某个long值相等
/**
* 判断与另一个VlInt的数值孰大孰小
* @param vlInt
* @return
*/
public int compareTo(VlInt vlInt){
return this.VlIntValue.compareTo(vlInt.VlIntValue);
}
public int compareTo(int i){
BigInteger bigInteger=new BigInteger(String.valueOf(i));
return this.VlIntValue.compareTo(bigInteger);
}
/**
* 减去另一个VlInt
* todo:暂没考虑两者差值越界int最大值
* @param vlInt
* @return
*/
public int subtract(VlInt vlInt){
return this.VlIntValue.subtract(vlInt.VlIntValue).intValue();
}
// 将BigInteger转为byte[按照变长无符号整数方式转]
private byte[] bigInteger2vlintBytes(BigInteger bigInteger){
byte[] vlintbytes;
if(SizeOfVarNumber(bigInteger)==1){
vlintbytes=new byte[1];
//todo
}else if(SizeOfVarNumber(bigInteger)==3){
vlintbytes=new byte[3];
//todo
}else if(SizeOfVarNumber(bigInteger)==5){
vlintbytes=new byte[5];
//todo
}else{
vlintbytes=new byte[9];
//todo
}
return vlintbytes;
}
// 将变长无符号整数数组转为BigInteger
private BigInteger vlintBytes2bigInteger(byte[] vlIntBytes){
BigInteger vlintValue=null;
if(vlIntBytes.length==1){
//todo
}else if(vlIntBytes.length==3){
//todo
}else if(vlIntBytes.length==5){
//todo
}else if(vlIntBytes.length==9){
//todo
}else{
return null;
}
return vlintValue;
}
// 测试
public static void main(String[] args){
BigInteger bigInteger=new BigInteger("3");
VlInt vlInt=new VlInt(bigInteger);
System.out.println(vlInt.isEqual(2));
}
}
+39
View File
@@ -0,0 +1,39 @@
package encoding;
import static java.lang.Math.pow;
/*
* @Author: Wang Feng
* @Description:变长无符号整型,用于表示TLV中的T和L
* @Version: 1.0.0
* @Date: 22:58 2021/3/1
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class VlInt_V1 {
// 实际传输的数据
private byte[] VlIntByte;
// 变长整数表示的真实数值:
// MIN1.0最大为(2^64-1)
private double VlIntValue;
// 传入一个变长无符号字节数组byte[],将它解析成一个VlInt
// 传入一个int,将它解析成一个VlInt
// 将vlint转为一个int输出
/**
* 测量一个变长整型实际用几个字节表示
* @return int
*/
public int SizeOfVarNumber(){
if(VlIntValue<241){
return 1;
}else if(VlIntValue<new Double(pow(2,16))){
return 3;
}else{
return 5;
}
}
}
+58
View File
@@ -0,0 +1,58 @@
package util;
import encoding.VlInt;
/*
* @Author: Wang Feng
* @Description:常用的数据类型转换。转无符号byte是重点
* @Version: 1.0.0
* @Date: 15:40 2021/3/2
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class ByteHelper {
/**
* 从byte[]数组指定开始位置获取第一个字节
* todo: start位置可能是大于int类型的数值
* @param buffer
* @param start
* @return
*/
public static byte getFirstByte(byte[] buffer, VlInt start){
int startInt=start.getVlIntValue2Int();
byte[] firstbyte=new byte[1];
System.arraycopy(buffer,startInt,firstbyte,0,1);
return firstbyte[0];
}
/**
* 从byte[]数组指定开始位置获取长度为len的字节数组
* todo: start位置可能是大于int类型的数值
* @param buffer
* @param start
* @return
*/
public static byte[] getLenBytes(byte[] buffer, VlInt start,VlInt len){
int startInt=start.getVlIntValue2Int();
int lenInt=len.getVlIntValue2Int();
byte[] lenbytes=new byte[1];
System.arraycopy(buffer,startInt,lenbytes,0,lenInt);
return lenbytes;
}
public static byte[] getLenBytes(byte[] buffer, VlInt start,int len){
int startInt=start.getVlIntValue2Int();
byte[] lenbytes=new byte[1];
System.arraycopy(buffer,startInt,lenbytes,0,len);
return lenbytes;
}
/**
* 无符号字节转int
* @param b
* @return
*/
public static int unsignedbyte2int(byte b){
return (int) b;
}
}