add component: ControlParameterCommonString

This commit is contained in:
free will committed 2021-05-25 15:15:57 +08:00
1 parent 3cef9d50f5
commit 65ad6243d6
7 files changed
+219 -20

No files matched your search

+110
View File
@@ -0,0 +1,110 @@
package component;
import encoding.*;
import mgmt.MgmtException;
import packet.PacketException;
import java.nio.charset.StandardCharsets;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 14:47 2021/5/25
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class CommonString implements TlvComponentBase,InitialAble, IEncodingAble {
private VlInt tlvType;
private String value;
public CommonString(){
}
public CommonString(VlInt tlvType,String str){
this.setTlvType(tlvType);
this.setValue(str);
}
public VlInt getTlvType() {
return tlvType;
}
public void setTlvType(VlInt tlvType) {
this.tlvType = tlvType;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
this.doInitial();
}
// 接口实现-变量区
private boolean initial=false;
// 接口实现-方法区
@Override
public void doInitial() {
initial=true;
}
@Override
public boolean isInitial() {
return initial;
}
/**
* WireEncode 将 CommonString 线速编码为一个TLV
* @param encoder
* @return
* @throws ComponentException
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException{
try {
int totalLength = 0;
// 编码 TLV-VALUE
byte[] valueBytes=this.value.getBytes(StandardCharsets.UTF_8);
int tmplen = encoder.prependByteArray(valueBytes, new SizeT(valueBytes.length));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(this.tlvType);
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("String.wireEncode: " + e.getMessage());
}
}
/**
* WireDecode 从 TLV Block 中解码出一个 CommonString
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) {
this.tlvType=block.getType();
this.setValue(new String(block.getValue()));
return true;
}
}
@@ -0,0 +1,55 @@
package component;
import encoding.*;
import mgmt.MgmtException;
import packet.PacketException;
/*
* @Author: Wang Feng
* @Description: ControlParameterCommonString 表示一个通用的字符串参数,在不同的命令中可能表示不同的含义
* @Version: 1.0.0
* @Date: 14:45 2021/5/25
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class ControlParameterCommonString implements IEncodingAble {
public CommonString commonString=new CommonString();
public ControlParameterCommonString(){
this.commonString.setTlvType(new VlInt(TLV.TlvManagementCommonString));
}
public ControlParameterCommonString(String str){
this.commonString=new CommonString(new VlInt(TLV.TlvManagementCommonString),str);
}
/**
* WireEncode 将 ControlParameterCommonString 线速编码为一个TLV
* @param encoder
* @return
* @throws ComponentException
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
try {
this.commonString.setTlvType(new VlInt(TLV.TlvManagementCommonString));
return this.commonString.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("ControlParameterCommonString.wireEncode: "+e.getMessage());
}
}
/**
* WireDecode 从 TLV Block 中解码出一个 ControlParameterCommonString
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block){
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvManagementCommonString))) {
return false;
}
return this.commonString.wireDecode(block);
}
}
+2
View File
@@ -82,6 +82,8 @@ public class TLV {
public static final int TlvManagementExpireTime = 229; // 超时时间
public static final int TlvManagementLocalUri = 230; // 逻辑接口地址,例如: tcp://192.168.1.1:13899,存在localUri时,也可表示remoteUri
public static final int TlvManagementPrefix = 240; // 前缀
public static final int TlvManagementPasswd = 241; // 密码
public static final int TlvManagementCommonString = 242; // 通用字符串参数
// LpPacket
public static final int TlvLpPacket = 250; // LpPacket
+2 -1
View File
@@ -90,6 +90,7 @@ public class UdpTransport extends Transport implements ITransport{
*/
private LpPacket doReceive() throws LogicFaceException {
try {
this.channel.disconnect();
// 支持触发超时机制的数据读取。这是第二种写法,可以正常触发超时机制
DatagramPacket receivePacket=new DatagramPacket(this.recvBuf,this.recvBuf.length);
this.channel.socket().receive(receivePacket);
@@ -98,7 +99,7 @@ public class UdpTransport extends Transport implements ITransport{
return null;
}
byte[] readBytes=ByteHelper.getLenBytes(this.recvBuf,0,readLen);
// // channel直接读取是无法触发超时机制的,所以注释掉这第一种写法
// // channel直接读取是无法触发超时机制的,所以注释掉这第一种写法
// ByteBuffer bf = ByteBuffer.wrap(this.recvBuf);
// int readLen=this.channel.read(bf);
// if(readLen<=0){
+8 -1
View File
@@ -272,7 +272,14 @@ public class CommandExecutor {
// 等待兴趣包应答
MINPacket minPacket=this.logicFace.receivePacket(commandInterest.interestLifeTime.getInterestLifeTime());
System.out.println();
try {
System.out.println(Arrays.toString(new SelfEncodingBase()
.selfWireEncode(minPacket).getRaw()));
} catch (EncoderException e) {
e.printStackTrace();
} catch (BlockException e) {
e.printStackTrace();
}
// 判断收到的是什么包
if(minPacket==null){
@@ -45,6 +45,12 @@ public class RegisterPrefixHelper implements IRegisterPrefixHelper {
return false;
}
// 测试
System.out.println(response.type);
System.out.println(response.msg);
System.out.println(response.code);
System.out.println(response.data);
if(response.code==ControlResponse.ControlResponseCodeSuccess){
return true;
}else{
+36 -18
View File
@@ -205,14 +205,41 @@ public class LogicFaceTest {
// 在这里测试是否可以在MIR注册前缀,并通过注册前缀,接收MIR的应答
@Test
public void testReceiveDataByUdp_ToGoLanguage() {
public void testReceiveDataByUDP_ToGoLanguage() {
try {
// 初始化udp face
LogicFace face=new LogicFace();
face.initWithUdp("192.168.43.164",13899);
face.initWithUdp("192.168.3.40",13899);
// face.initWithUdp("192.168.43.164",15899);
// face.initWithUdp("localhost",15899);
// 注册前缀
Identifier myHostIdentifier=new Identifier("/pkuszwefree");
Identifier myHostIdentifier=new Identifier("/hhhw");
if(myHostIdentifier==null){
return;
}
System.out.println("identifier: "+ Arrays.toString(new SelfEncodingBase().
selfWireEncode(myHostIdentifier).getRaw()));
System.out.println("register face res: "+
face.registerIdentifier(myHostIdentifier,-1,new RegisterPrefixHelper()));
}catch (LogicFaceException | ComponentException | EncoderException | BlockException | PacketException | MgmtException e){
System.out.println(e.getMessage());
}
}
// 在这里测试是否可以在MIR注册前缀,并通过注册前缀,接收MIR的应答
@Test
public void testReceiveDataByTCP_ToGoLanguage() {
try {
// 初始化udp face
LogicFace face=new LogicFace();
face.initWithTcp("192.168.3.40",13899);
// face.initWithUdp("192.168.43.164",15899);
// face.initWithUdp("localhost",15899);
// 注册前缀
Identifier myHostIdentifier=new Identifier("/hhhwsss");
if(myHostIdentifier==null){
return;
}
@@ -222,19 +249,10 @@ public class LogicFaceTest {
System.out.println("register face res: "+
face.registerIdentifier(myHostIdentifier,-1,new RegisterPrefixHelper()));
// Data data=new Data();
// byte[] value={(byte)132,(byte)221,(byte)223,(byte)25};
// data.payload.setValue(value);
// data.congestionMark.setCongestionLevel(Long.MAX_VALUE);
// Identifier name=new Identifier("/wefree");
// data.setName(name);
//
// face.sendData(data);
// 等待两秒钟,接收
// MINPacket minPacket = face.receivePacket(9000);
// System.out.println(Arrays.toString(
// new SelfEncodingBase().selfWireEncode(minPacket).getRaw()));
// 等待100秒钟,这个时间可以保持tcp连接,从而查看后台是否成功添加了fib条目
MINPacket minPacket = face.receivePacket(100000);
System.out.println(Arrays.toString(
new SelfEncodingBase().selfWireEncode(minPacket).getRaw()));
}catch (LogicFaceException | ComponentException | EncoderException | BlockException | PacketException | MgmtException e){
System.out.println(e.getMessage());
}
@@ -290,7 +308,7 @@ public class LogicFaceTest {
public void startUDPServer() throws IOException {
while(true){
//1.创建服务端+端口
DatagramSocket server = new DatagramSocket(50000);
DatagramSocket server = new DatagramSocket(15899);
//2.准备接受容器
byte[] container = new byte[1024];
//3.封装成包
@@ -308,7 +326,7 @@ public class LogicFaceTest {
// 睡眠5s
try {
System.out.println("start 5s sleeping ... ");
Thread.sleep(5000);
Thread.sleep(1000);
System.out.println("After 5s, wake up now!");
} catch (InterruptedException e) {
e.printStackTrace();