mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
writing VlInt ...
This commit is contained in:
1 parent
e9321ab49a
commit
0f6863efe2
7 files changed
+255
No files matched your search
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,5 @@
|
||||
package encoding;
|
||||
|
||||
public class Block {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package encoding;
|
||||
|
||||
/*
|
||||
* @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;
|
||||
|
||||
// public void ReadVarNumber
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 传入一个int,将它解析成一个VlInt
|
||||
|
||||
// 传入一个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 vlint){
|
||||
if(VlIntValue.compareTo(new BigInteger(String.valueOf(vlint)))==0){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 判断vlint是否与某个long值相等
|
||||
|
||||
// 将BigInteger转为byte[按照变长无符号整数方式转]
|
||||
private byte[] bigInteger2vlintBytes(BigInteger bigInteger){
|
||||
byte[] vlintbytes;
|
||||
if(SizeOfVarNumber(bigInteger)==1){
|
||||
vlintbytes=new byte[1];
|
||||
//...
|
||||
}else if(SizeOfVarNumber(bigInteger)==3){
|
||||
vlintbytes=new byte[3];
|
||||
//...
|
||||
}else if(SizeOfVarNumber(bigInteger)==5){
|
||||
vlintbytes=new byte[5];
|
||||
//...
|
||||
}else{
|
||||
vlintbytes=new byte[9];
|
||||
//...
|
||||
}
|
||||
return vlintbytes;
|
||||
}
|
||||
|
||||
// 将变长无符号整数数组转为BigInteger
|
||||
private BigInteger vlintBytes2bigInteger(byte[] vlIntBytes){
|
||||
BigInteger vlintValue=null;
|
||||
if(vlIntBytes.length==1){
|
||||
|
||||
}else if(vlIntBytes.length==3){
|
||||
|
||||
}else if(vlIntBytes.length==5){
|
||||
|
||||
}else if(vlIntBytes.length==9){
|
||||
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
return vlintValue;
|
||||
}
|
||||
|
||||
// 测试
|
||||
public static void main(String[] args){
|
||||
BigInteger bigInteger=new BigInteger("3");
|
||||
|
||||
System.out.println(bigInteger);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user