mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add mgmt: ControlParameters
This commit is contained in:
1 parent
442b4e3544
commit
0166bf38ac
6 files changed
+350
-107
No files matched your search
@@ -17,89 +17,6 @@ import java.util.List;
|
||||
* @Date: 17:34 2021/3/10
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
class IdentifierComponentContainer {
|
||||
private List<IdentifierComponent> identifierComponents=new LinkedList<>();
|
||||
|
||||
public IdentifierComponentContainer() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有名称组件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<IdentifierComponent> getElements() {
|
||||
return identifierComponents;
|
||||
}
|
||||
|
||||
/**
|
||||
* 组件个数
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int length() {
|
||||
return identifierComponents.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空组件
|
||||
*/
|
||||
public void clear() {
|
||||
identifierComponents.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个组件
|
||||
*
|
||||
* @param identifierComponent
|
||||
*/
|
||||
public void addElement(IdentifierComponent identifierComponent) {
|
||||
identifierComponents.add(identifierComponent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定索引位置的组件
|
||||
*
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponent getElement(int index) throws ComponentException {
|
||||
if (index >= this.length()) {
|
||||
throw new ComponentException("IdentifierComponents.getElement: index is out of bound.");
|
||||
}
|
||||
return this.identifierComponents.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前 num 个名称组件构成的标识
|
||||
*
|
||||
* @param num
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponentContainer getPrefix(int num) throws ComponentException {
|
||||
if (num > this.length()) {
|
||||
return null;
|
||||
}
|
||||
IdentifierComponentContainer identifierComponentContainer = new IdentifierComponentContainer();
|
||||
for (int i = 0; i < num; i++) {
|
||||
identifierComponentContainer.addElement(this.getElement(i));
|
||||
}
|
||||
return identifierComponentContainer;
|
||||
}
|
||||
|
||||
public IdentifierComponentContainer getSubIdentifierContainer(int startIndex, int num) throws ComponentException {
|
||||
if ((startIndex + num) > this.length()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
IdentifierComponentContainer identifierComponentContainer = new IdentifierComponentContainer();
|
||||
for (int i = startIndex; i < startIndex + num; i++) {
|
||||
identifierComponentContainer.addElement(this.getElement(i));
|
||||
}
|
||||
return identifierComponentContainer;
|
||||
}
|
||||
}
|
||||
|
||||
public class Identifier implements TlvComponentBase, IEncodingAble {
|
||||
private IdentifierComponentContainer components=new IdentifierComponentContainer();
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package component;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 16:55 2021/4/13
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class IdentifierComponentContainer {
|
||||
private List<IdentifierComponent> identifierComponents = new LinkedList<>();
|
||||
|
||||
public IdentifierComponentContainer() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有名称组件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<IdentifierComponent> getElements() {
|
||||
return identifierComponents;
|
||||
}
|
||||
|
||||
/**
|
||||
* 组件个数
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int length() {
|
||||
return identifierComponents.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空组件
|
||||
*/
|
||||
public void clear() {
|
||||
identifierComponents.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个组件
|
||||
*
|
||||
* @param identifierComponent
|
||||
*/
|
||||
public void addElement(IdentifierComponent identifierComponent) {
|
||||
identifierComponents.add(identifierComponent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定索引位置的组件
|
||||
*
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public IdentifierComponent getElement(int index) throws ComponentException {
|
||||
if (index >= this.length()) {
|
||||
throw new ComponentException("IdentifierComponents.getElement: index is out of bound.");
|
||||
}
|
||||
return this.identifierComponents.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前 num 个名称组件构成的标识
|
||||
*
|
||||
* @param num
|
||||
* @return
|
||||
*/
|
||||
public component.IdentifierComponentContainer getPrefix(int num) throws ComponentException {
|
||||
if (num > this.length()) {
|
||||
return null;
|
||||
}
|
||||
component.IdentifierComponentContainer identifierComponentContainer = new component.IdentifierComponentContainer();
|
||||
for (int i = 0; i < num; i++) {
|
||||
identifierComponentContainer.addElement(this.getElement(i));
|
||||
}
|
||||
return identifierComponentContainer;
|
||||
}
|
||||
|
||||
public component.IdentifierComponentContainer getSubIdentifierContainer(int startIndex, int num) throws ComponentException {
|
||||
if ((startIndex + num) > this.length()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
component.IdentifierComponentContainer identifierComponentContainer = new component.IdentifierComponentContainer();
|
||||
for (int i = startIndex; i < startIndex + num; i++) {
|
||||
identifierComponentContainer.addElement(this.getElement(i));
|
||||
}
|
||||
return identifierComponentContainer;
|
||||
}
|
||||
}
|
||||
@@ -189,8 +189,7 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
|
||||
// 解析每一个签名
|
||||
for (int i = 0; i < subElementNum; i += 2) {
|
||||
// 第奇数个子 TLV 解析为 SignatureInfo
|
||||
SignatureInfo signatureInfo = null;
|
||||
signatureInfo = new SignatureInfo(subElements.getBlock(i));
|
||||
SignatureInfo signatureInfo = new SignatureInfo(subElements.getBlock(i));
|
||||
// 第偶数个子 TLV 解析为 SignatureValue
|
||||
SignatureValue signatureValue = new SignatureValue(subElements.getBlock(i + 1));
|
||||
// 构造Signature
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package encoding;
|
||||
|
||||
import component.ComponentException;
|
||||
import mgmt.MgmtException;
|
||||
import packet.PacketException;
|
||||
|
||||
/*
|
||||
@@ -18,13 +19,13 @@ public interface IEncodingAble {
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
public int wireEncode(Encoder encoder) throws ComponentException, PacketException;
|
||||
public int wireEncode(Encoder encoder) throws ComponentException, PacketException, MgmtException;
|
||||
|
||||
/**
|
||||
* 线速解码,将一个TLV解码成一个网络包
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
public boolean wireDecode(Block block) throws ComponentException, PacketException;
|
||||
public boolean wireDecode(Block block) throws ComponentException, PacketException, MgmtException;
|
||||
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
package mgmt;
|
||||
|
||||
import component.*;
|
||||
import encoding.Block;
|
||||
import encoding.Encoder;
|
||||
import encoding.IEncodingAble;
|
||||
import packet.PacketException;
|
||||
import encoding.*;
|
||||
import packet.Interest;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
@@ -15,26 +13,246 @@ import packet.PacketException;
|
||||
*/
|
||||
public class ControlParameters implements IEncodingAble {
|
||||
// fib add 必须字段
|
||||
public ControlParameterPrefix controlParameterPrefix =new ControlParameterPrefix(); // 前缀标识
|
||||
public ControlParameterLogicFaceId controlParameterLogicFaceId=new ControlParameterLogicFaceId(); // 逻辑faceId
|
||||
public ControlParameterCost controlParameterCost=new ControlParameterCost(); // 链路代价
|
||||
public ControlParameterCapacity controlParameterCapacity =new ControlParameterCapacity(); // CS 容量
|
||||
public ControlParameterCount controlParameterCount=new ControlParameterCount(); // 需要删除的cs表项数
|
||||
public ControlParameterExpireTime controlParameterExpireTime=new ControlParameterExpireTime(); // 超时时间
|
||||
public ControlParameterUri controlParameterUri=new ControlParameterUri(); // 远端地址
|
||||
public ControlParameterLocalUri controlParameterLocalUri=new ControlParameterLocalUri(); // 本地地址
|
||||
public ControlParameterLogicFacePersistency controlParameterLogicFacePersistency=new ControlParameterLogicFacePersistency(); // 逻辑接口持久性
|
||||
public ControlParameterUriScheme controlParameterUriScheme=new ControlParameterUriScheme(); // 逻辑地址模式
|
||||
public ControlParameterMtu controlParameterMtu=new ControlParameterMtu(); // MTU
|
||||
|
||||
public ControlParameterPrefix controlParameterPrefix = new ControlParameterPrefix(); // 前缀标识
|
||||
public ControlParameterLogicFaceId controlParameterLogicFaceId = new ControlParameterLogicFaceId(); // 逻辑faceId
|
||||
public ControlParameterCost controlParameterCost = new ControlParameterCost(); // 链路代价
|
||||
public ControlParameterCapacity controlParameterCapacity = new ControlParameterCapacity(); // CS 容量
|
||||
public ControlParameterCount controlParameterCount = new ControlParameterCount(); // 需要删除的cs表项数
|
||||
public ControlParameterExpireTime controlParameterExpireTime = new ControlParameterExpireTime(); // 超时时间
|
||||
public ControlParameterUri controlParameterUri = new ControlParameterUri(); // 远端地址
|
||||
public ControlParameterLocalUri controlParameterLocalUri = new ControlParameterLocalUri(); // 本地地址
|
||||
public ControlParameterLogicFacePersistency controlParameterLogicFacePersistency = new ControlParameterLogicFacePersistency(); // 逻辑接口持久性
|
||||
public ControlParameterUriScheme controlParameterUriScheme = new ControlParameterUriScheme(); // 逻辑地址模式
|
||||
public ControlParameterMtu controlParameterMtu = new ControlParameterMtu(); // MTU
|
||||
|
||||
/**
|
||||
* 将 ControlParameters 编码成一个 TLV
|
||||
*
|
||||
* @param encoder
|
||||
* @return
|
||||
* @throws MgmtException
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws ComponentException, PacketException {
|
||||
return 0;
|
||||
public int wireEncode(Encoder encoder) throws MgmtException {
|
||||
try {
|
||||
int totalLength = 0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
if (this.controlParameterPrefix.isInitial()) {
|
||||
int tmpLen = this.controlParameterPrefix.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
if (this.controlParameterLogicFaceId.isInitial()) {
|
||||
int tmpLen = this.controlParameterLogicFaceId.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
if (this.controlParameterCost.isInitial()) {
|
||||
int tmpLen = this.controlParameterCost.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
if (this.controlParameterCapacity.isInitial()) {
|
||||
int tmpLen = this.controlParameterCapacity.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
if (this.controlParameterCount.isInitial()) {
|
||||
int tmpLen = this.controlParameterCount.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
if (this.controlParameterExpireTime.isInitial()) {
|
||||
int tmpLen = this.controlParameterExpireTime.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
if (this.controlParameterUri.isInitial()) {
|
||||
int tmpLen = this.controlParameterUri.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
if (this.controlParameterLocalUri.isInitial()) {
|
||||
int tmpLen = this.controlParameterLocalUri.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
if (this.controlParameterLogicFacePersistency.isInitial()) {
|
||||
int tmpLen = this.controlParameterLogicFacePersistency.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
if (this.controlParameterUriScheme.isInitial()) {
|
||||
int tmpLen = this.controlParameterUriScheme.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
if (this.controlParameterMtu.isInitial()) {
|
||||
int tmpLen = this.controlParameterMtu.wireEncode(encoder);
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
}
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
int tmpLen = encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
|
||||
// 编码 TLV-TYPE
|
||||
tmpLen = encoder.prependVarNumber(new VlInt(TLV.TlvManagementControlParameters));
|
||||
if (tmpLen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmpLen;
|
||||
|
||||
return totalLength;
|
||||
} catch (ComponentException | EncoderException e) {
|
||||
throw new MgmtException("ControlParameters.wireEncode" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 ControlParameters
|
||||
*
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) throws ComponentException, PacketException {
|
||||
return false;
|
||||
public boolean wireDecode(Block block) throws MgmtException {
|
||||
try {
|
||||
// 检查 Type 是否正确
|
||||
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvManagementControlParameters))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 首先解析子 Block
|
||||
if (!block.parseSubElements()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取参数
|
||||
ElementContainer elementContainer = block.getSubElements();
|
||||
int len = elementContainer.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Block newBlock = elementContainer.getBlock(i);
|
||||
int type = newBlock.getType().getVlIntValue2Int();
|
||||
switch (type) {
|
||||
case TLV.TlvManagementPrefix:
|
||||
if (!this.controlParameterPrefix.wireDecode(newBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvManagementCost:
|
||||
if (!this.controlParameterCost.wireDecode(newBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvManagementCapacity:
|
||||
if (!this.controlParameterCapacity.wireDecode(newBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvManagementCount:
|
||||
if (!this.controlParameterCount.wireDecode(newBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvManagementExpireTime:
|
||||
if (!this.controlParameterExpireTime.wireDecode(newBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvManagementUri:
|
||||
if (!this.controlParameterUri.wireDecode(newBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvManagementLocalUri:
|
||||
if (!this.controlParameterLocalUri.wireDecode(newBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvManagementLogicFacePersistency:
|
||||
if (!this.controlParameterLogicFacePersistency.wireDecode(newBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvManagementUriScheme:
|
||||
if (!this.controlParameterUriScheme.wireDecode(newBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case TLV.TlvManagementMtu:
|
||||
if (!this.controlParameterMtu.wireDecode(newBlock)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (BlockException | VlIntException | ComponentException e) {
|
||||
throw new MgmtException("ControlParameters.wireDecode: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean parse(Interest interest) throws MgmtException {
|
||||
try {
|
||||
if (interest == null) {
|
||||
return false;
|
||||
}
|
||||
Identifier identifier = interest.getName();
|
||||
IdentifierComponentContainer components = identifier.getComponents();
|
||||
int len = components.length();
|
||||
if (len < 3) {
|
||||
return false;
|
||||
}
|
||||
// 没有分片 最后一个就是控制参数
|
||||
if (components.getElement(len - 1).isByteArray()){
|
||||
Block block=new Block(components.getElement(len-1).getByteArray(),true);
|
||||
if(!this.wireDecode(block)){
|
||||
return false;
|
||||
}
|
||||
}else if (components.getElement(len - 1).isFragmentNumber()
|
||||
&& components.getElement(len - 2).isVersionNumber()
|
||||
&& components.getElement(len - 3).isByteArray()){
|
||||
//如果最后一位是分片号 倒数第二位是版本号 倒数第三位是控制参数
|
||||
Block block=new Block(components.getElement(len-3).getByteArray(),true);
|
||||
if(!this.wireDecode(block)){
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (ComponentException | BlockException e) {
|
||||
throw new MgmtException("ControlParameters.parse: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package mgmt;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:38 2021/03/15
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class MgmtException extends Exception{
|
||||
public MgmtException(String msg){
|
||||
super("Mgmt."+msg);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user