mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
finished first component: TTL
This commit is contained in:
1 parent
879e09e879
commit
28d7bb2436
9 files changed
+279
-1
No files matched your search
@@ -0,0 +1,19 @@
|
||||
package component;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 指示当前组件是否有初始化,如果没有初始化,且组件是可选组件,则无需编码
|
||||
* @Version: 1.0.0
|
||||
* @Date: 10:15 2021/3/9
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public interface InitialAble {
|
||||
// 指示当前组件是否有初始化,如果没有初始化,且组件是可选组件,则无需编码
|
||||
boolean initial = false;
|
||||
|
||||
// 将组件标记为已初始化
|
||||
public void doInitial();
|
||||
|
||||
// 判断当前组件是否已经初始化
|
||||
public boolean isInitial();
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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 0;
|
||||
}
|
||||
if((start<0)||(end>buffer.length)){
|
||||
return 0;
|
||||
}
|
||||
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 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查某个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