mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
finish encoding: SelfEncodingBase
This commit is contained in:
1 parent
0a945ada8e
commit
8bd3547a70
6 files changed
+209
-8
No files matched your search
@@ -10,8 +10,8 @@ import encoding.*;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class MutableField implements TlvComponentBase,IEncodingAble {
|
||||
private MutableProtectField mutableProtectField;
|
||||
private MutableDangerousField mutableDangerousField;
|
||||
public MutableProtectField mutableProtectField;
|
||||
public MutableDangerousField mutableDangerousField;
|
||||
|
||||
public MutableField(){
|
||||
mutableProtectField=new MutableProtectField();
|
||||
|
||||
@@ -19,6 +19,9 @@ public class TTL implements IEncodingAble {
|
||||
*/
|
||||
private long ttl;
|
||||
|
||||
public TTL(){
|
||||
}
|
||||
|
||||
public TTL(Block block){
|
||||
buildTTLByBlock(block);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package encoding;
|
||||
|
||||
import component.CanBePrefix;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description: 利用对接口的函数回调,实现对任意实现了 IEncodingAble 接口的对象进行自编码
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:17 2021/3/15
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class SelfEncodingBase {
|
||||
|
||||
// 用来实现自编码的辅助结构
|
||||
private Block rawBlock;
|
||||
// 存放回调的函数
|
||||
// private IEncodingAble iEncodingAble;
|
||||
|
||||
public SelfEncodingBase(){
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取编码后的Block
|
||||
* @return
|
||||
*/
|
||||
public Block getRawBlock(){
|
||||
return rawBlock;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置回调函数(接口的函数)
|
||||
* @param iEncodingAble
|
||||
*/
|
||||
// public void setiEncodingAble(IEncodingAble iEncodingAble) {
|
||||
// this.iEncodingAble = iEncodingAble;
|
||||
// }
|
||||
|
||||
/**
|
||||
* 对一个实现了 IEncodingAble 接口的对象进行自编码
|
||||
* // @Description:
|
||||
* // 1. 用 rawBlock缓存编码的结果;
|
||||
* // 2. 如果 rawBlock不为空,且指向一个有效的 Block,则直接返回raw
|
||||
* // 3. 如果 rawBlock为空,或者指向的不是一个有效的 Block,则将对象编码为 Block 之后返回,同时用 raw指针缓存编码的结果
|
||||
* @param iEncodingAble
|
||||
* @return
|
||||
*/
|
||||
public Block selfWireEncode(IEncodingAble iEncodingAble){
|
||||
// this.setiEncodingAble(iEncodingAble);
|
||||
if((this.rawBlock!=null)&&this.rawBlock.isValid()){
|
||||
return this.rawBlock;
|
||||
}
|
||||
|
||||
Block block=easyEncoder(iEncodingAble);
|
||||
if(block==null){
|
||||
return null;
|
||||
}
|
||||
this.rawBlock=block;
|
||||
return block;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自编码辅助函数
|
||||
* 利用对接口的函数回调,实现对任意实现了 IEncodingAble 接口的对象进行自编码
|
||||
* @param iEncodingAble
|
||||
* @return
|
||||
*/
|
||||
private Block easyEncoder(IEncodingAble iEncodingAble){
|
||||
Encoder encoder=new Encoder();
|
||||
|
||||
// 首先测量目标 TLV 的大小
|
||||
encoder.estimatorReset();
|
||||
int targetLen=iEncodingAble.wireEncode(encoder);
|
||||
if(targetLen<=0){
|
||||
return null;
|
||||
}
|
||||
|
||||
// 编码初始化
|
||||
if(!encoder.encoderReset(new SizeT(targetLen),new SizeT(0))){
|
||||
return null;
|
||||
}
|
||||
|
||||
// 实际执行编码
|
||||
targetLen=iEncodingAble.wireEncode(encoder);
|
||||
if(targetLen<=0){
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] buffer=encoder.getBuffer();
|
||||
if((buffer==null)||buffer.length==0){
|
||||
return null;
|
||||
}
|
||||
|
||||
// 返回编码的结果
|
||||
return Block.createBlockByBuffer(buffer,true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,10 @@
|
||||
package packet;
|
||||
|
||||
import component.IdentifierWrapper;
|
||||
import component.Payload;
|
||||
import component.TTL;
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
@@ -27,6 +32,79 @@ package packet;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
public class CPacket {
|
||||
public class CPacket implements InteractWithField, IEncodingAble {
|
||||
private MINPacket minPacket;
|
||||
private TTL ttl;
|
||||
private Payload payload;
|
||||
private String flatIdentifier; // 推式包拥有一个扁平的标识
|
||||
|
||||
public CPacket(){
|
||||
minPacket=new MINPacket();
|
||||
ttl=new TTL();
|
||||
payload=new Payload();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 CPacket 的各项属性填充到 MINPacket 中定义的对应分区当中
|
||||
* @Description:
|
||||
* // 1. 主要填充标识区、只读区和可变区
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean fillDataToFields() {
|
||||
// 可变区 => 受保护区(无)
|
||||
|
||||
// 填充可变区 => 非受保护区
|
||||
// 首先清除所有的Block
|
||||
this.minPacket.mutableField.mutableDangerousField.clearBlocks();
|
||||
// Block block
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 MINPacket 的分区当中提取出 CPacket 的各项属性
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean extraDataFromFields() {
|
||||
// 提取 Payload
|
||||
Block payloadBlock=this.minPacket.readOnlyField.getBlockByType(new VlInt(TLV.TlvPayload));
|
||||
if(payloadBlock==null){
|
||||
return false;
|
||||
}
|
||||
if(this.payload.wireDecode(payloadBlock)){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取 TTL
|
||||
Block ttlBlock=this.minPacket.mutableField.mutableDangerousField.getBlockByType(new VlInt(TLV.TlvTTL));
|
||||
if(ttlBlock==null){
|
||||
return false;
|
||||
}
|
||||
if(this.ttl.wireDecode(ttlBlock)){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取标识
|
||||
IdentifierWrapper commonIdentifier=this.minPacket.identifierField.getIdentifierByType(new VlInt(TLV.TlvIdentifierCommon));
|
||||
if(commonIdentifier==null){
|
||||
return false;
|
||||
}
|
||||
String identifierString=commonIdentifier.toUri();
|
||||
if((identifierString.length()>0)&&(identifierString.startsWith("/"))){
|
||||
this.flatIdentifier=identifierString.substring(1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wireDecode(Block block) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import component.ReadOnlyField;
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Author: Wang Fengs
|
||||
* @Description: 表示一个MIN网络层包的通用结构
|
||||
* todo: 需要在signature完成之后取消signatureField的注释
|
||||
* @Version: 1.0.0
|
||||
@@ -14,11 +14,11 @@ import encoding.*;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class MINPacket implements IEncodingAble {
|
||||
private IdentifierField identifierField; // 标识区
|
||||
public IdentifierField identifierField; // 标识区
|
||||
// private SignatureField signatureField; // 签名区
|
||||
private ReadOnlyField readOnlyField; // 只读区
|
||||
private MutableField mutableField; // 可变区
|
||||
private VlInt packetType; // 包类型
|
||||
public ReadOnlyField readOnlyField; // 只读区
|
||||
public MutableField mutableField; // 可变区
|
||||
public VlInt packetType; // 包类型
|
||||
|
||||
/**
|
||||
* 判断 MIN 包是否是一个有效的包
|
||||
@@ -90,6 +90,10 @@ public class MINPacket implements IEncodingAble {
|
||||
|
||||
/**
|
||||
* 线速解码,将 MIN包 TLV 中的四个区域分别解析出来
|
||||
* // @Description:
|
||||
* // 1. 解析出四个区的TLV;
|
||||
* // 2. 检查必要的TLV是否存在,不存在则抛出异常,以下为必须条件:
|
||||
* // a. 必须包含标识区,且标识区至少包含一个标识
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package others;
|
||||
|
||||
import component.CanBePrefix;
|
||||
import encoding.Block;
|
||||
import encoding.SelfEncodingBase;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:36 2021/3/15
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestCallBack {
|
||||
public static void main(String[] args){
|
||||
CanBePrefix canBePrefix=new CanBePrefix();
|
||||
Block block=new SelfEncodingBase().selfWireEncode(canBePrefix);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user