mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add test: mgmt/CommandExecutor.java
This commit is contained in:
1 parent
490d115ff5
commit
5725452843
6 files changed
+121
-5
No files matched your search
@@ -261,6 +261,7 @@ public class CommandExecutor {
|
||||
|
||||
// 等待兴趣包应答
|
||||
MINPacket minPacket=this.logicFace.receivePacket(commandInterest.interestLifeTime.getInterestLifeTime());
|
||||
System.out.println();
|
||||
|
||||
// 判断收到的是什么包
|
||||
if(minPacket==null){
|
||||
|
||||
@@ -108,7 +108,6 @@ public class ControlResponse {
|
||||
|
||||
/**
|
||||
* UnmarshalJSON 自定义 JSON 反序列化逻辑,可以根据不同类型,Data 反序列化为对应的子类型
|
||||
* todo: 本方法的实现可能存在一定问题,后期使用时需要进行进一步的测试和修改。暂时用不到。
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package util;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import mgmt.ControlResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ public class CommandBaseTest {
|
||||
Interest interest = CommandBase.createCommandIdentifierInterest(parameters, "/cp1/test");
|
||||
|
||||
ControlParameters cp = CommandBase.parseControlParameters(interest);
|
||||
System.out.println(cp);
|
||||
System.out.println(cp.controlParameterPrefix.getPrefix().toUri());
|
||||
}catch (Exception ex){
|
||||
LoggerHelper.info(ex.getMessage());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,27 @@
|
||||
package mgmt;
|
||||
|
||||
import component.ComponentException;
|
||||
import component.ControlParameterPrefix;
|
||||
import component.Identifier;
|
||||
import encoding.BlockException;
|
||||
import encoding.EncoderException;
|
||||
import encoding.SelfEncodingBase;
|
||||
import logicface.LogicFace;
|
||||
import logicface.LogicFaceException;
|
||||
import mgmt.FibCommands.FibAddCommand;
|
||||
import org.junit.Test;
|
||||
import packet.Data;
|
||||
import packet.LpPacket;
|
||||
import packet.PacketException;
|
||||
import util.ByteHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
@@ -8,5 +30,101 @@ package mgmt;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class CommandExecutorTest {
|
||||
/**
|
||||
* 测试结果:
|
||||
* server receive:[-15, 0, -6, -127, -52, 108,
|
||||
* 5, 106,
|
||||
* 50, 76, 103, 74, 101, 72, 100, 7, 0, 119, 101, 102, 114, 101, 101, 100, 9, 0, 102, 105, 98, 45, 109, 103, 109, 116, 100, 4, 0, 97, 100, 100, 100, 44, 2, -36, 41, -30, 1, 1, -31, 1, 1, -32, 1, 1, -26, 6, 47, 108, 111, 99, 97, 108, -34, 7, 47, 112, 107, 117, 47, 115, 122, -27, 1, 1, -28, 1, 1, -29, 1, 1, -33, 1, 1, -35, 1, 1,
|
||||
* 52, 14, -49, 2, 23, 112, -48, 8, 44, -117, 107, 18, -96, -46, -31, 34,
|
||||
* 53, 10, 54, 3, -45, 1, 0, 55, 3, -44, 1, 1,
|
||||
* -15, 0, -5, 15, -15, 0, -4, 1, 0, -15, 0, -3, 1, 1, -15, 0, -2, 1, 0]
|
||||
* @throws LogicFaceException
|
||||
* @throws MgmtException
|
||||
*/
|
||||
@Test
|
||||
public void testFibAddCommand() throws LogicFaceException, MgmtException {
|
||||
// init
|
||||
CommandExecutor commandExecutor = new CommandExecutor();
|
||||
ControlParameters parameters=new ControlParameters();
|
||||
ControlParameterPrefix controlParameterPrefix = new ControlParameterPrefix();
|
||||
parameters.controlParameterPrefix = controlParameterPrefix;
|
||||
parameters.controlParameterLogicFaceId.setLogicFaceId(1);
|
||||
parameters.controlParameterCost.setCost(1);
|
||||
parameters.controlParameterCapacity.setCapacity(1);
|
||||
parameters.controlParameterCount.setCount(1);
|
||||
parameters.controlParameterExpireTime.setExpireTime(1);
|
||||
parameters.controlParameterUri.setUri("/pku/sz");
|
||||
parameters.controlParameterLocalUri.setLocalUri("/local");
|
||||
parameters.controlParameterLogicFacePersistency.setPersistency(1);
|
||||
parameters.controlParameterUriScheme.setUriScheme(1);
|
||||
parameters.controlParameterMtu.setMtu(1);
|
||||
FibAddCommand fibAddCommand = FibAddCommand.createFibAddCommand("/wefree",
|
||||
parameters);
|
||||
LogicFace face=new LogicFace();
|
||||
face.initWithUdp("127.0.0.1",50000);
|
||||
commandExecutor.init(fibAddCommand,face);
|
||||
commandExecutor.interestLifeTime=6000;
|
||||
|
||||
// start
|
||||
ControlResponse response=commandExecutor.start();
|
||||
System.out.println(response.type);
|
||||
System.out.println(response.msg);
|
||||
System.out.println(response.code);
|
||||
System.out.println(response.data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个UDPServer,从而打印出来udp face发送的数据。端口号为50000
|
||||
* 模拟MIR应答:伪造一个包含ControlResponse类型的Data包,然后把这个data包放入LpPacket包里面。
|
||||
*/
|
||||
@Test
|
||||
public void startUDPServer() throws IOException, ComponentException, EncoderException, BlockException, MgmtException, PacketException {
|
||||
while(true){
|
||||
//1.创建服务端+端口
|
||||
DatagramSocket server = new DatagramSocket(50000);
|
||||
//2.准备接受容器
|
||||
byte[] container = new byte[1024];
|
||||
//3.封装成包
|
||||
DatagramPacket packet = new DatagramPacket(container, container.length);
|
||||
//4.接受数据
|
||||
server.receive(packet);
|
||||
//5.分析数据
|
||||
byte[] data = packet.getData();
|
||||
int len = packet.getLength();
|
||||
System.out.println("server receive:" +
|
||||
Arrays.toString(ByteHelper.getLenBytes(data, 0, len)));
|
||||
|
||||
SocketAddress remoteHost=packet.getSocketAddress();
|
||||
System.out.println("remote host: "+packet.getSocketAddress());
|
||||
// 睡眠5s
|
||||
try {
|
||||
System.out.println("start 5s sleeping ... ");
|
||||
Thread.sleep(5000);
|
||||
System.out.println("After 5s, wake up now!");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 做数据应答:伪造数据包
|
||||
server.connect(remoteHost);
|
||||
// 1. 最内层的应答包
|
||||
String responseString = "{\"Code\": 1, \"Type\": \"int\", \"Msg\": \"bbb\", \"Data\": \"abc\"}";
|
||||
// 2. 次之的数据包
|
||||
Data newData=new Data();
|
||||
Identifier identifier=new Identifier("/response");
|
||||
newData.setName(identifier);
|
||||
newData.payload.setValue(responseString.getBytes(StandardCharsets.UTF_8));
|
||||
byte[] dataBytes=new SelfEncodingBase().selfWireEncode(newData).getRaw();
|
||||
// 3. 最外层的LpPacket
|
||||
LpPacket lpPacket=new LpPacket();
|
||||
lpPacket.setValue(dataBytes);
|
||||
lpPacket.setFragmentNum(1);
|
||||
lpPacket.setId(0);
|
||||
lpPacket.setFragmentSeq(0);
|
||||
byte[] lpBytes=new SelfEncodingBase().selfWireEncode(lpPacket).getRaw();
|
||||
DatagramPacket newPacket=new DatagramPacket(lpBytes,lpBytes.length);
|
||||
server.send(newPacket);
|
||||
//6.释放
|
||||
server.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ public class ControlResponseTest {
|
||||
public void testUnmarshal(){
|
||||
try{
|
||||
ControlResponse response = new ControlResponse();
|
||||
String jsonString = "{\"Code\": 1, \"Type\": \"string\", \"Msg\": \"bbb\", \"Data\": \"abcd\"}";
|
||||
String jsonString = "{\"Code\": 1, \"Type\": \"int\", \"Msg\": \"bbb\", \"Data\": 1}";
|
||||
// Data无法解析
|
||||
System.out.println(response.UnmarshalJSON(jsonString.getBytes()));
|
||||
System.out.println(response.type);
|
||||
@@ -21,6 +21,5 @@ public class ControlResponseTest {
|
||||
}catch (Exception ex){
|
||||
LoggerHelper.info(ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user