mostly finished mgmt

This commit is contained in:
free will
2021-04-29 11:36:59 +08:00
parent b6d97e2eb1
commit e6aa89cc3b
6 changed files with 217 additions and 24 deletions
+79 -20
View File
@@ -1,13 +1,23 @@
package mgmt;
import common.LoggerHelper;
import component.ComponentException;
import component.Identifier;
import component.NackHeader;
import encoding.Encoder;
import encoding.TLV;
import encoding.VlInt;
import jnr.ffi.annotations.In;
import logicface.LogicFace;
import logicface.LogicFaceException;
import packet.*;
import security.KeyChain;
import util.BytesBuffer;
import util.JSONHelper;
import util.JSONHelperException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import static mgmt.ControlResponse.ControlResponseCodeSuccess;
@@ -152,29 +162,78 @@ public class CommandExecutor {
* @param data
* @return
*/
private ControlResponse onReceiveFirstMetaData(Data data){
ControlResponse controlResponse=new ControlResponse();
// todo : json解析,将byte[]转为对象
// if err := json.Unmarshal(data.GetValue(), controlResponse); err != nil {
// common.LogError("parse mgmt_data fail!the err is:", err)
// return nil, err
// }
private ControlResponse onReceiveFirstMetaData(Data data) throws MgmtException {
try {
ControlResponse controlResponse = new ControlResponse();
// json解析,将byte[]转为对象
try {
controlResponse = (ControlResponse) JSONHelper.parseJsonToClass(data.payload.getValue(), controlResponse);
} catch (JSONHelperException e) {
LoggerHelper.logger.error("parse mgmt_data fail!the err is: " + e.getMessage());
return null;
}
// 判断状态码
switch (controlResponse.code){
case ControlResponse.ControlResponseCodeSuccess:
// 请求成功,直接返回结果
return controlResponse;
case ControlResponse.ControlResponseCodeCommonError:
// 请求失败,直接返回结果,结果中已经包含错误信息
return controlResponse;
case ControlResponse.ControlResponseCodeContinue:
// 请求到元数据,继续请求所有的分片
// todo: blabla
// 判断状态码
switch (controlResponse.code) {
case ControlResponse.ControlResponseCodeSuccess:
// 请求成功,直接返回结果
return controlResponse;
case ControlResponse.ControlResponseCodeCommonError:
// 请求失败,直接返回结果,结果中已经包含错误信息
return controlResponse;
case ControlResponse.ControlResponseCodeContinue:
// 请求到元数据,继续请求所有的分片
// todo: 分片请求数据
BytesBuffer bytesBuilder = new BytesBuffer();
ControlResponseMeta metaData = controlResponse.getMeta();
int length = (int) metaData.SliceNum;
// 逐一串行的请求所有的分片,并整合返回的数据
for (int i = 0; i < length; i++) {
Interest tempInterest = this.newCommandInterest();
Identifier tempIdentifier=tempInterest.getName();
tempIdentifier.appendVersionNumber(metaData.Version);
tempIdentifier.appendFragmentNumber(i);
tempInterest.ttl.setTtl(this.ttl);
tempInterest.interestLifeTime.setInterestLifeTime(this.interestLifeTime);
// 将兴趣包发出
if(!this.logicFace.sendInterest(tempInterest)){
controlResponse.code=ControlResponse.ControlResponseCodeCommonError;
controlResponse.msg="Send Interest failed!";
return controlResponse;
}
// 等待接收分片
MINPacket newPacket=this.logicFace.receivePacket(this.interestLifeTime);
if(newPacket==null){
controlResponse.code=ControlResponse.ControlResponseCodeCommonError;
controlResponse.msg="Request timeout!";
return controlResponse;
}else {
if(PacketTypeHelper.isData(newPacket)){
bytesBuilder.put(newPacket.readOnlyField.
getBlockByType(new VlInt(TLV.TlvPayload)).getValue());
}else{
// 收到的不是Data包,未预期行为
controlResponse.code=ControlResponse.ControlResponseCodeCommonError;
controlResponse.msg="Receive other packetnot Data),Unexpected behavior";
return controlResponse;
}
}
}
// 得到返回数据
controlResponse.code= ControlResponseCodeSuccess;
controlResponse.msg="";
controlResponse.setBytes(bytesBuilder.byteArray());
// 关闭逻辑接口
}
return controlResponse;
} catch (MgmtException | LogicFaceException | ComponentException e) {
throw new MgmtException("CommandExecutor.onReceiveFirstMetaData: "+e.getMessage());
}
return controlResponse;
}
/**
+31 -3
View File
@@ -1,7 +1,14 @@
package mgmt;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.org.apache.regexp.internal.RE;
import org.checkerframework.checker.units.qual.C;
import util.JSONHelper;
import util.JSONHelperException;
import util.StringHelper;
import java.io.IOException;
/*
* @Author: Wang Feng
* @Description:
@@ -38,6 +45,16 @@ public class ControlResponse {
}
public ControlResponseData data=new ControlResponseData();
public ControlResponse(){
}
public ControlResponse(ControlResponse response) {
this.type=response.type;
this.code=response.code;
this.msg=response.msg;
this.data.value=response.data.value;
}
/**
* GetString 按字符串类型访问 Data
* @return
@@ -91,11 +108,22 @@ public class ControlResponse {
/**
* UnmarshalJSON 自定义 JSON 反序列化逻辑,可以根据不同类型,Data 反序列化为对应的子类型
* todo: 本方法的实现可能存在一定问题,后期使用时需要进行进一步的测试和修改。暂时用不到。
* @param data
* @return
*/
public boolean UnmarshalJSON(byte[] data){
// todo: here!!!
return true;
public boolean UnmarshalJSON(byte[] data) throws MgmtException {
try {
// 使用jackson将json格式数据解析为Java对象
ControlResponse response=new ControlResponse();
response= (ControlResponse) JSONHelper.parseJsonToClass(data,response);
this.type=response.type;
this.code=response.code;
this.msg=response.msg;
this.data=response.data;
return true;
} catch (JSONHelperException e) {
throw new MgmtException("ControlResponse.UnmarshalJSON: "+e.getMessage());
}
}
}
+57
View File
@@ -0,0 +1,57 @@
package util;
/*
* @Author: Wang Feng
* @Description: 自定义无限变长byte数组
* @Version: 1.0.0
* @Date: 11:13 2021/4/29
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class BytesBuffer {
private int MaxNum = 9000; // byte初始化长度
private byte[] bytes;
private int len; //实际长度
public BytesBuffer() {
bytes = new byte[MaxNum];
len = 0;
}
public BytesBuffer(int maxNum) {
MaxNum = maxNum;
bytes = new byte[MaxNum];
len = 0;
}
/**
* put进一个byte
*
* @param b
*/
public void put(byte b) {
while (len >= MaxNum) {
MaxNum *= 2;
byte[] newBytes = new byte[MaxNum];
System.arraycopy(bytes, 0, newBytes, 0, len);
bytes = newBytes;
}
bytes[len] = b;
len++;
}
public void put(byte[] bytes){
//todo:
}
/**
* 返回一个byte[]
*
* @return
*/
public byte[] byteArray() {
byte[] res = new byte[len];
System.arraycopy(bytes, 0, res, 0, len);
return res;
}
}
+31
View File
@@ -0,0 +1,31 @@
package util;
import com.fasterxml.jackson.databind.ObjectMapper;
import mgmt.ControlResponse;
import java.io.IOException;
/*
* @Author: Wang Feng
* @Description: 为解析json数据提供统一的函数接口
* @Version: 1.0.0
* @Date: 10:18 2021/4/29
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class JSONHelper {
/**
* 将json格式数据解析为Java对象
* @param data 要解析的原始数据
* @param ob 要解析成的对象(蕴含对象所属类的信息)
* @return
*/
public static Object parseJsonToClass(byte[] data,Object ob) throws JSONHelperException {
try {
// 使用jackson将json格式数据解析为指定对象类型的Java对象
ObjectMapper mapper=new ObjectMapper();
return mapper.readValue(data, ob.getClass());
} catch (IOException e) {
throw new JSONHelperException(e.getMessage());
}
}
}
@@ -0,0 +1,14 @@
package util;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 17:38 2021/04/29
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class JSONHelperException extends Exception{
public JSONHelperException(String msg){
super(msg);
}
}