mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
Merge branch 'master' of gitee.com:willfree/min-dev-java
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示内容兴趣包中的组件 CanBePrefix
|
||||
* @Version: 1.0.0
|
||||
* @Date: 11:33 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class CanBePrefix implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 成员变量
|
||||
private boolean canBePrefix;
|
||||
|
||||
// 函数
|
||||
public void setCanBePrefix(boolean canBePrefix){
|
||||
this.canBePrefix=canBePrefix;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
public boolean getCanBePrefix(){
|
||||
return this.canBePrefix;
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 CanBePrefix 线速编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
// 这是一个存在性组件,所以如果值为false,则无需编码
|
||||
if(!this.canBePrefix){
|
||||
return 0;
|
||||
}
|
||||
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvCanBePrefix));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码一个 CanBePrefix
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvCanBePrefix))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 这是一个存在性组件,所以存在则为 true
|
||||
this.canBePrefix=true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:表示一个拥塞标记 TLV 组件
|
||||
* @Version: 1.0.0
|
||||
* @Date: 15:54 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 成员变量 todo: 越界问题
|
||||
private long congestionMark;
|
||||
|
||||
// 函数
|
||||
public long getCongestionLevel(){
|
||||
return this.congestionMark;
|
||||
}
|
||||
|
||||
public void setCongestionLevel(long congestionMark){
|
||||
this.congestionMark=congestionMark;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 CongestionMark 线速编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen=encoder.prependNonNegativeInteger(this.congestionMark);
|
||||
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(TLV.TlvCongestionMark));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 CongestionMark
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvCongestionMark))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读取 CongestionMark
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
this.setCongestionLevel(value);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示一个内容的新鲜期 TLV 组件
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:10 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 成员变量
|
||||
private long freshnessPeriod;
|
||||
|
||||
// 函数
|
||||
public long getFreshnessPeriod() {
|
||||
return this.freshnessPeriod;
|
||||
}
|
||||
|
||||
public void setFreshnessPeriod(long freshnessPeriod) {
|
||||
this.freshnessPeriod = freshnessPeriod;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 FreshnessPeriod 线速编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen=encoder.prependNonNegativeInteger(this.freshnessPeriod);
|
||||
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(TLV.TlvFreshnessPeriod));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 FreshnessPeriod
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvFreshnessPeriod))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读取 value
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
this.setFreshnessPeriod(value);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:38 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 成员变量
|
||||
private long hopLimit;
|
||||
|
||||
// 成员函数
|
||||
public long getHopLimit() {
|
||||
return hopLimit;
|
||||
}
|
||||
|
||||
public void setHopLimit(long hopLimit) {
|
||||
this.hopLimit = hopLimit;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen=encoder.prependNonNegativeInteger(this.hopLimit);
|
||||
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(TLV.TlvHopLimit));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvHopLimit))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读取 HopLimit
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
this.setHopLimit(value);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package component;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 名称组件
|
||||
* @Version: 1.0.0
|
||||
* @Date: 21:26 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
|
||||
import encoding.Block;
|
||||
import encoding.Encoder;
|
||||
import encoding.IEncodingAble;
|
||||
|
||||
// @Description:
|
||||
// 1. 参考 => https://named-data.net/doc/tech-memos/naming-conventions.pdf ,借鉴但不是按这个实现的
|
||||
// 2. 格式如下:
|
||||
// IdentifierComponent = 100 TLV-LENGTH
|
||||
// <Marker>
|
||||
// *OCTET
|
||||
// Marker = OCTET
|
||||
// 3. Marker 用来标记不同类型的 IdentifierComponent,目前分配如下
|
||||
// Marker value Description
|
||||
// 0x00 普通的UTF-8编码的字符串
|
||||
// 0x01 非负整数(可以用1、2、4、8个字节表示)
|
||||
// 0x02 任意字节数组
|
||||
public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
|
||||
private Block block;
|
||||
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;
|
||||
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package component;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 指示当前组件是否有初始化,如果没有初始化,且组件是可选组件,则无需编码
|
||||
* @Version: 1.0.0
|
||||
* @Date: 10:15 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public interface InitialAble {
|
||||
// 指示当前组件是否有初始化,如果没有初始化,且组件是可选组件,则无需编码.
|
||||
// 接口中的变量都是final类型的(常量)。因此,如果需要一个变量,
|
||||
// 需要在实现该接口的类中定义该变量。
|
||||
// boolean initial = false;
|
||||
|
||||
// 将组件标记为已初始化
|
||||
public void doInitial();
|
||||
|
||||
// 判断当前组件是否已经初始化
|
||||
public boolean isInitial();
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:46 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 变量
|
||||
private long interestLifeTime;
|
||||
|
||||
// 方法
|
||||
public long getInterestLifeTime() {
|
||||
return interestLifeTime;
|
||||
}
|
||||
|
||||
public void setInterestLifeTime(long interestLifeTime) {
|
||||
this.interestLifeTime = interestLifeTime;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 InterestLifeTime 线速编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen=encoder.prependNonNegativeInteger(this.interestLifeTime);
|
||||
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(TLV.TlvInterestLifeTime));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 InterestLifeTime
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvInterestLifeTime))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读取 value
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
this.setInterestLifeTime(value);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示内容兴趣包中的组件 MustBeRefresh
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:30 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class MustBeRefresh implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 成员变量
|
||||
private boolean mustBeRefresh;
|
||||
|
||||
// 函数
|
||||
public boolean getMustBeRefresh() {
|
||||
return mustBeRefresh;
|
||||
}
|
||||
|
||||
public void setMustBeRefresh(boolean mustBeRefresh) {
|
||||
this.mustBeRefresh = mustBeRefresh;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 MustBeRefresh 线速编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
// 这是一个存在性组件,所以如果值为false,则无需编码
|
||||
if(!this.mustBeRefresh){
|
||||
return 0;
|
||||
}
|
||||
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
int tmplen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMustBeRefresh));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 MustBeRefresh
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvMustBeRefresh))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 这是一个存在性组件,所以存在则为 true
|
||||
this.mustBeRefresh=true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示一个MIN包格式中的可变非受保护区
|
||||
* @Version: 1.0.0
|
||||
* @Date: 20:34 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class MutableDangerousField 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示一个MIN包格式中的可变区
|
||||
* @Version: 1.0.0
|
||||
* @Date: 20:48 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class MutableField implements TlvComponentBase,IEncodingAble {
|
||||
private MutableProtectField mutableProtectField;
|
||||
private MutableDangerousField mutableDangerousField;
|
||||
|
||||
public MutableField(){
|
||||
mutableProtectField=new MutableProtectField();
|
||||
mutableDangerousField=new MutableDangerousField();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将可变区线速编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV—VALUE
|
||||
int tmplen=this.mutableDangerousField.wireEncode(encoder);
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
tmplen=this.mutableProtectField.wireEncode(encoder);
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 如果可变保护区和可变非受保护区均为空,则可以去掉可变区
|
||||
if(totalLength==0){
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMutableField));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 MutableField
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvMutableField))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析子 TLV
|
||||
if(!block.parseSubElements()){
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < block.getSubElements().length(); i++) {
|
||||
Block subBlock=block.getSubElements().getBlock(i);
|
||||
if(subBlock.getTlvType().isEqual(TLV.TlvMutableProtectField)){
|
||||
if(!this.mutableProtectField.wireDecode(subBlock)){
|
||||
return false;
|
||||
}
|
||||
}else if(subBlock.getTlvType().isEqual(TLV.TlvMutableDangerousField)){
|
||||
if(!this.mutableDangerousField.wireDecode(subBlock)){
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示一个MIN包格式中的可变保护区
|
||||
* @Version: 1.0.0
|
||||
* @Date: 20:45 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class MutableProtectField 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,则可以去掉它
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:52 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 变量
|
||||
private long nackReason;
|
||||
|
||||
// 函数
|
||||
public long getNackReason() {
|
||||
return nackReason;
|
||||
}
|
||||
|
||||
public void setNackReason(long nackReason) {
|
||||
this.nackReason = nackReason;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 NackHeader 编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen=encoder.prependNonNegativeInteger(this.nackReason);
|
||||
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(TLV.TlvNackHeader));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 Block TLV 中解码出一个 NackHeader
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvNackHeader))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读取 value
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
this.setNackReason(value);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示一个随机数 TLV 组件
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:13 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 成员变量
|
||||
private long nonce;
|
||||
|
||||
// 成员函数
|
||||
public long getNonce() {
|
||||
return this.nonce;
|
||||
}
|
||||
|
||||
public void setNonce(long nonce) {
|
||||
this.nonce = nonce;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新Nonce,随机生成一个数值传递给Nonce
|
||||
*/
|
||||
public void refreshNonce(){
|
||||
long randomLong=Math.abs(new Random().nextLong());
|
||||
this.setNonce(randomLong);
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Nonce 线速编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen=encoder.prependNonNegativeInteger(this.nonce);
|
||||
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(TLV.TlvNonce));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 Nonce
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvNonce))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读取 value
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
this.setNonce(value);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 表示网络包负载
|
||||
// 1. 格式如下:
|
||||
// Payload = PAYLOAD-TYPE TLV-LENGTH *OCTET
|
||||
// 2. 详情参见:http://gitea.qjm253.cn/PKUSZ-future-network-lab/minlib/src/branch/master/docs/PacketFormat.md#4-signature
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:26 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class Payload implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 成员变量
|
||||
private byte[] value;
|
||||
|
||||
// 函数
|
||||
/**
|
||||
* 根据 TLV Block 初始化一个 Payload
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
public boolean buildPayloadByBlock(Block block){
|
||||
return this.wireDecode(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到Payload TLV中实际包含的数据
|
||||
* @return
|
||||
*/
|
||||
public byte[] getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 Payload TLV 中的Value
|
||||
* @param value
|
||||
*/
|
||||
public void setValue(byte[] value) {
|
||||
this.value = value;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Payload 线速编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen=encoder.prependByteArray(this.value,new SizeT(this.value.length));
|
||||
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(TLV.TlvPayload));
|
||||
if(tmplen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查 Type 是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvPayload))){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 存储Value
|
||||
// int len=block.getLength().getVlIntValue2Int();
|
||||
// byte[] bytes=new byte[len];
|
||||
// System.arraycopy(block.getValue(),0,bytes,0,len);
|
||||
this.setValue(block.getValue());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 10:20 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TTL implements IEncodingAble {
|
||||
/**
|
||||
* 表示一个 TTL Tlv
|
||||
* todo: 后台使用的uint64,故这里用long表示,则理论上可能越界
|
||||
* // @Description:
|
||||
* 1. 格式如下:
|
||||
* TTL = TTL-TYPE TLV-LENGTH NonNegativeInteger
|
||||
*/
|
||||
private long ttl;
|
||||
|
||||
public TTL(Block block){
|
||||
buildTTLByBlock(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 TLV Block 初始化一个 TTL
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
public boolean buildTTLByBlock(Block block){
|
||||
return this.wireDecode(block);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 TTL 值
|
||||
* @return
|
||||
*/
|
||||
public long ttl(){
|
||||
return this.ttl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 TTL 值
|
||||
* @param ttl
|
||||
*/
|
||||
public void setTtl(long ttl){
|
||||
this.ttl=ttl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 TTL 线速编码为一个 TLV
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
int totalLength=0;
|
||||
// 编码 TLV-VALUE
|
||||
int tmpLen=encoder.prependNonNegativeInteger(this.ttl);
|
||||
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(TLV.TlvTTL));
|
||||
if(tmpLen==0){
|
||||
return 0;
|
||||
}
|
||||
totalLength+=tmpLen;
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 TTL
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
// 检查Type是否正确
|
||||
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvTTL))){
|
||||
return false;
|
||||
}
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
this.ttl=value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package component;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 实现所有 TLV 组件需要的一些通用功能
|
||||
* @Version: 1.0.0
|
||||
* @Date: 21:49 2021/3/6
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public interface TlvComponentBase {
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -122,6 +122,46 @@ public class Encoder {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 编码一个非负整数到一个数组当中
|
||||
* todo: 越界问题
|
||||
* @param uint64
|
||||
* @return
|
||||
*/
|
||||
public byte[] buildNonNegativeIntegerArr(long uint64){
|
||||
VlInt vlInt=new VlInt(uint64);
|
||||
int len=vlInt.getVlIntBytes().length;
|
||||
if(len==1){
|
||||
return vlInt.getVlIntBytes();
|
||||
}else {
|
||||
byte[] res = new byte[len - 1];
|
||||
System.arraycopy(vlInt.getVlIntBytes(),1,res,0,len-1);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在前向插入一个非负整数,最大为 uint64::Max
|
||||
* todo: 越界问题
|
||||
* @param uint64_value
|
||||
* @return
|
||||
*/
|
||||
public int prependNonNegativeInteger(long uint64_value){
|
||||
byte[] bytes=buildNonNegativeIntegerArr(uint64_value);
|
||||
return this.prependByteArray(bytes,new SizeT(bytes.length));
|
||||
}
|
||||
|
||||
/**
|
||||
* 在后向插入一个非负整数,最大为 uint64::Max
|
||||
* todo: 越界问题
|
||||
* @param uint64_value
|
||||
* @return
|
||||
*/
|
||||
public int appendNonNegativeInteger(long uint64_value){
|
||||
byte[] bytes=buildNonNegativeIntegerArr(uint64_value);
|
||||
return this.appendByteArray(bytes,new SizeT(bytes.length));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array
|
||||
* @param size
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package encoding;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 21:06 2021/3/7
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public interface IEncodingAble {
|
||||
/**
|
||||
* 线速编码,将一个包编码成网络可传输的格式
|
||||
* @Description:
|
||||
* 1. 可以使用 Encoder.getBuffer() 获取编码的结果
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
public int wireEncode(Encoder encoder);
|
||||
|
||||
/**
|
||||
* 线速解码,将一个TLV解码成一个网络包
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
public boolean wireDecode(Block block);
|
||||
|
||||
}
|
||||
@@ -117,7 +117,32 @@ public class TLV {
|
||||
return tlvType;
|
||||
}
|
||||
|
||||
// todo:从字节数组的指定位置中读取一个非负整型
|
||||
/**
|
||||
* 从字节数组的指定位置中读取一个非负整型
|
||||
* todo: 越界问题
|
||||
*/
|
||||
public static long readNonNegativeInteger(byte[] buffer,int start,int end){
|
||||
if(start==end){
|
||||
return -1;
|
||||
}
|
||||
if((start<0)||(end>buffer.length)){
|
||||
return -1;
|
||||
}
|
||||
int len=end-start;
|
||||
byte[] bytes=new byte[len];
|
||||
System.arraycopy(buffer,start,bytes,0,len);
|
||||
if(len==1){
|
||||
return ByteHelper.uint8ToLong(buffer[start]);
|
||||
}else if(len==2){
|
||||
return ByteHelper.uint16ToLong(bytes);
|
||||
}else if(len==4){
|
||||
return ByteHelper.uint32ToLong(bytes);
|
||||
}else if(len==8){
|
||||
return ByteHelper.uint64ToLong(bytes);
|
||||
}else{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查某个type值是否是指定的 TLV-TYPE
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package others;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 9:15 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
interface Animal {
|
||||
// private String name="灵长类动物";
|
||||
|
||||
|
||||
public void sayHi();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package others;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 9:15 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
interface God {
|
||||
|
||||
public void makeWorld();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package others;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 10:10 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class HumanImpl implements God,Animal {
|
||||
@Override
|
||||
public void sayHi() {
|
||||
System.out.println("hi");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeWorld() {
|
||||
System.out.println("world");
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
HumanImpl h=new HumanImpl();
|
||||
h.sayHi();
|
||||
h.makeWorld();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user