add mgmt: CommandBase

This commit is contained in:
free will
2021-04-23 21:36:29 +08:00
parent 0da8ecd362
commit a814a13143
2 changed files with 153 additions and 28 deletions
-28
View File
@@ -16,33 +16,5 @@ import packet.Interest;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class Command {
/**
* 根据传入的控制参数和前缀创建一个命令兴趣包
*
* @param parameters 控制参数
* @param prefix 前缀 /min-mir/mgmt/localhost
* @return
*/
public static Interest createCommandIdentifierInterest(ControlParameters parameters, String prefix) throws MgmtException {
try {
// /min-mir/mgmt/localhop/<模块名称>/<命令>/<参数>/[版本号]/[分片号]
Interest interest = new Interest();
Identifier commandIdentifier = new Identifier(prefix);
Encoder encoder=new Encoder();
if(!encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0))){
throw new MgmtException("Command.createCommandIdentifierInterest: encoder reset error.");
}
if(parameters.wireEncode(encoder)<0){
throw new MgmtException("Command.createCommandIdentifierInterest: wireEncode error.");
}
byte[] controlParametersbuf=encoder.getBuffer();
commandIdentifier.append(new IdentifierComponent(controlParametersbuf));
interest.setName(commandIdentifier);
return interest;
} catch (ComponentException | EncoderException e) {
throw new MgmtException("Command.createCommandIdentifierInterest: " + e.getMessage());
}
}
}
+153
View File
@@ -0,0 +1,153 @@
package mgmt;
import component.*;
import encoding.*;
import packet.Interest;
/*
* @Author: Wang Feng
* @Description: CommandBase 表示一个控制管理的命令的通用结构
* // eg. /min-mir/mgmt/localhost/face-mgmt/list
// - topPrefix => /min-mir/mgmt/localhost
// - moduleName => face-mgmt
// - action => list
* @Version: 1.0.0
* @Date: 21:13 2021/4/23
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class CommandBase {
public String topPrefix; // 顶级前缀
public String moduleName; // 模块名称
public String action; // 命令动作
public ControlParameters parameters; //管理命令参数
/**
* GetTopPrefix 获取顶级前缀
* @return
*/
public String getTopPrefix(){
return this.topPrefix;
}
/**
* SetTopPrefix 设置顶级前缀
* @param topPrefix
*/
public void setTopPrefix(String topPrefix){
this.topPrefix=topPrefix;
}
/**
* GetModuleName 获取模块名称
* @return
*/
public String getModuleName() {
return moduleName;
}
/**
* SetModuleName 设置模块名称
* @param moduleName
*/
public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}
/**
* GetAction 获取命令动作
* @return
*/
public String getAction() {
return action;
}
/**
* SetAction 设置命令动作
* @param action
*/
public void setAction(String action) {
this.action = action;
}
/**
* GetParameters 获取管理命令前缀
* @return
*/
public ControlParameters getParameters() {
return parameters;
}
/**
* SetParameters 设置管理命令前缀
* @param parameters
*/
public void setParameters(ControlParameters parameters) {
this.parameters = parameters;
}
/**
* 根据传入的控制参数和前缀创建一个命令兴趣包
* @param parameters 控制参数
* @param prefix 前缀 /min-mir/mgmt/localhost
* @return
*/
public static Interest createCommandIdentifierInterest(ControlParameters parameters, String prefix) throws MgmtException {
try {
// /min-mir/mgmt/localhop/<模块名称>/<命令>/<参数>/[版本号]/[分片号]
Interest interest = new Interest();
Identifier commandIdentifier = new Identifier(prefix);
Encoder encoder=new Encoder();
if(!encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0))){
throw new MgmtException("Command.createCommandIdentifierInterest: encoder reset error.");
}
if(parameters.wireEncode(encoder)<0){
throw new MgmtException("Command.createCommandIdentifierInterest: wireEncode error.");
}
byte[] controlParametersbuf=encoder.getBuffer();
commandIdentifier.append(new IdentifierComponent(controlParametersbuf));
interest.setName(commandIdentifier);
return interest;
} catch (ComponentException | EncoderException e) {
throw new MgmtException("Command.createCommandIdentifierInterest: " + e.getMessage());
}
}
public static ControlParameters parseControlParameters(Interest interest) throws MgmtException {
try {
ControlParameters controlParameters = new ControlParameters();
if (interest == null) {
throw new MgmtException("CommandBase.parseControlParameters: interest null");
}
Identifier identifier = interest.getName();
IdentifierComponentContainer components = identifier.getComponents();
int length = components.length();
if (length < 3) {
throw new MgmtException("CommandBase.parseControlParameters: identifier components not enough");
}
// 没有分片 最后一个就是控制参数
if (components.getElement(length - 1).isByteArray()) {
Block block =new Block(components.getElement(length-1).getByteArray(),true);
if(!controlParameters.wireDecode(block)){
return null;
}
}else if(components.getElement(length - 1).isFragmentNumber()&&
components.getElement(length - 2).isVersionNumber()&&
components.getElement(length - 3).isByteArray()){
//如果最后一位是分片号 倒数第二位是版本号 倒数第三位是控制参数
//解析倒数第三位的控制参数
Block block=new Block(components.getElement(length-3).getByteArray(),true);
if(!controlParameters.wireDecode(block)){
return null;
}
}else{
// 否则没有对应参数报错
throw new MgmtException("CommandBase.parseControlParameters: not find the parameters field");
}
return controlParameters;
} catch (ComponentException | BlockException e) {
throw new MgmtException("CommandBase.parseControlParameters: "+e.getMessage());
}
}
}