mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-18 04:50:25 +08:00
code update to follow go-language & test register identifier
This commit is contained in:
@@ -83,17 +83,28 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public boolean buildIdentifierByString(String identifierString) {
|
||||
if ((identifierString.length() <= 0) || (!identifierString.startsWith("/"))) {
|
||||
return false;
|
||||
}
|
||||
public boolean buildIdentifierByString(String identifierString) throws ComponentException {
|
||||
try {
|
||||
if ((identifierString.length() <= 0) || (!identifierString.startsWith("/"))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] componentStrings = identifierString.split("/");
|
||||
this.components.clear();
|
||||
for (int i = 1; i < componentStrings.length; i++) {
|
||||
this.components.addElement(new IdentifierComponent(componentStrings[i]));
|
||||
if (identifierString.equals("/")) {
|
||||
IdentifierComponentContainer components=new IdentifierComponentContainer();
|
||||
components.addElement(new IdentifierComponent("/"));
|
||||
return this.buildIdentifierByComponents(components);
|
||||
}
|
||||
|
||||
String[] componentStrings = identifierString.split("/");
|
||||
IdentifierComponentContainer components=new IdentifierComponentContainer();
|
||||
components.addElement(new IdentifierComponent("/"));
|
||||
for (int i = 1; i < componentStrings.length; i++) {
|
||||
components.addElement(new IdentifierComponent(componentStrings[i]));
|
||||
}
|
||||
return this.buildIdentifierByComponents(components);
|
||||
} catch (ComponentException e) {
|
||||
throw new ComponentException("Identifier.buildIdentifierByString: "+e.getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -117,11 +117,15 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper createCommonIdentifierByString(String identifierString) {
|
||||
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
|
||||
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierCommon);
|
||||
identifierWrapper.identifier.buildIdentifierByString(identifierString);
|
||||
return identifierWrapper;
|
||||
public IdentifierWrapper createCommonIdentifierByString(String identifierString) throws ComponentException {
|
||||
try {
|
||||
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
|
||||
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierCommon);
|
||||
identifierWrapper.identifier.buildIdentifierByString(identifierString);
|
||||
return identifierWrapper;
|
||||
} catch (ComponentException e) {
|
||||
throw new ComponentException("IdentifierWrapper.createCommonIdentifierByString: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,11 +174,15 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper createContentInterestIdentifierByString(String identifierString) {
|
||||
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
|
||||
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentInterest);
|
||||
identifierWrapper.identifier.buildIdentifierByString(identifierString);
|
||||
return identifierWrapper;
|
||||
public IdentifierWrapper createContentInterestIdentifierByString(String identifierString) throws ComponentException {
|
||||
try {
|
||||
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
|
||||
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentInterest);
|
||||
identifierWrapper.identifier.buildIdentifierByString(identifierString);
|
||||
return identifierWrapper;
|
||||
} catch (ComponentException e) {
|
||||
throw new ComponentException("IdentifierWrapper.createContentInterestIdentifierByString: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -224,11 +232,15 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public IdentifierWrapper createContentDataIdentifierByString(String identifierString) {
|
||||
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
|
||||
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentData);
|
||||
identifierWrapper.identifier.buildIdentifierByString(identifierString);
|
||||
return identifierWrapper;
|
||||
public IdentifierWrapper createContentDataIdentifierByString(String identifierString) throws ComponentException {
|
||||
try {
|
||||
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
|
||||
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentData);
|
||||
identifierWrapper.identifier.buildIdentifierByString(identifierString);
|
||||
return identifierWrapper;
|
||||
} catch (ComponentException e) {
|
||||
throw new ComponentException("IdentifierWrapper.createContentDataIdentifierByString: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
package component;
|
||||
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 15:54 2021/5/20
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class IncomingLogicFaceId implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
// 成员变量
|
||||
private long incomingLogicFaceId;
|
||||
|
||||
public IncomingLogicFaceId(){
|
||||
}
|
||||
|
||||
public IncomingLogicFaceId(long incomingLogicFaceId){
|
||||
this.setIncomingLogicFaceId(incomingLogicFaceId);
|
||||
}
|
||||
|
||||
// 成员函数
|
||||
public long getIncomingLogicFaceId() {
|
||||
return incomingLogicFaceId;
|
||||
}
|
||||
|
||||
public void setIncomingLogicFaceId(long incomingLogicFaceId) {
|
||||
this.incomingLogicFaceId = incomingLogicFaceId;
|
||||
this.doInitial();
|
||||
}
|
||||
|
||||
// 接口实现-变量区
|
||||
private boolean initial=false;
|
||||
|
||||
// 接口实现-方法区
|
||||
@Override
|
||||
public void doInitial() {
|
||||
initial=true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInitial() {
|
||||
return initial;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws ComponentException {
|
||||
try {
|
||||
int totalLength = 0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen = encoder.prependNonNegativeInteger(this.incomingLogicFaceId);
|
||||
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(new VlInt(TLV.TlvIncomingLogicFaceId));
|
||||
if (tmplen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
return totalLength;
|
||||
} catch (EncoderException e) {
|
||||
throw new ComponentException("IncomingLogicFaceId.wireEncode: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wireDecode(Block block) throws ComponentException {
|
||||
try {
|
||||
// 检查 Type 是否正确
|
||||
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvIncomingLogicFaceId))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 读取 HopLimit
|
||||
long value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int());
|
||||
if (value < 0) {
|
||||
return false;
|
||||
}
|
||||
this.setIncomingLogicFaceId(value);
|
||||
|
||||
return true;
|
||||
} catch (TLVException | VlIntException e) {
|
||||
throw new ComponentException("IncomingLogicFaceId.wireDecode: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,8 +68,12 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
|
||||
* @param identifierString
|
||||
* @return
|
||||
*/
|
||||
public boolean buildKeyLocatorByString(String identifierString){
|
||||
return this.identifier.buildIdentifierByString(identifierString);
|
||||
public boolean buildKeyLocatorByString(String identifierString) throws ComponentException {
|
||||
try {
|
||||
return this.identifier.buildIdentifierByString(identifierString);
|
||||
} catch (ComponentException e) {
|
||||
throw new ComponentException("KeyLocator.buildKeyLocatorByString: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -82,14 +82,16 @@ public class Payload implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
int totalLength = 0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
int tmplen = encoder.prependByteArray(this.value, new SizeT(this.value.length));
|
||||
if (tmplen < 0) {
|
||||
return -1;
|
||||
if(this.value!=null){
|
||||
int tmplen = encoder.prependByteArray(this.value, new SizeT(this.value.length));
|
||||
if (tmplen < 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
// 编码 TLV-LENGTH
|
||||
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
|
||||
int tmplen = encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if (tmplen < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,14 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
|
||||
return signatures;
|
||||
}
|
||||
|
||||
/**
|
||||
* SetSignatures 覆盖签名数组
|
||||
* @param signatures
|
||||
*/
|
||||
public void setSignatures(SignatureContainer signatures){
|
||||
this.signatures=signatures;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将签名区线速编码为一个 TLV
|
||||
*
|
||||
|
||||
@@ -52,6 +52,8 @@ public class TLV {
|
||||
public static final int TlvCongestionMark = 211; // 拥塞标记
|
||||
public static final int TlvTTL = 212; // Time to live
|
||||
public static final int TlvNackHeader = 213; // Nack header
|
||||
public static final int TlvNoCache = 214; // No Cache
|
||||
public static final int TlvIncomingLogicFaceId = 215; // 入口逻辑接口地址
|
||||
|
||||
// 管理通信协议(old version)
|
||||
// public static final int TlvSliceNumber = 211; // 分片数量
|
||||
|
||||
@@ -25,10 +25,10 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class LogicFaceICN extends LogicFace {
|
||||
public ReschedulableTimer timer; // 定时器,在processEvent函数中就被设置成很大的值
|
||||
public long recentExpireTime; // 最近将要超时的时间戳
|
||||
private ReschedulableTimer timer; // 定时器,在processEvent函数中就被设置成很大的值
|
||||
private long recentExpireTime; // 最近将要超时的时间戳
|
||||
public Map<String, PITEntry> mPit; // PIT表。不初始化,可以根据它来判断本face是否初始化
|
||||
public Lock timeoutEventHeapLock; // 锁, mpit 、timeoutEventHeap、recentExpireTime
|
||||
private Lock timeoutEventHeapLock; // 锁, mpit 、timeoutEventHeap、recentExpireTime
|
||||
// 超时事件堆, 以超时时间排序的最小堆,初始化大小为1000
|
||||
public PriorityBlockingQueue<TimeoutEvent> timeoutEventHeap;
|
||||
public Map<String, OnInterestInterface> mFib;
|
||||
@@ -72,7 +72,7 @@ public class LogicFaceICN extends LogicFace {
|
||||
* 接收到 MINPacket 后调用 本函数,将 MINPacket 转为 Interest
|
||||
* @param minPacket
|
||||
*/
|
||||
public void onReceiveInterest(MINPacket minPacket) throws LogicFaceException {
|
||||
private void onReceiveInterest(MINPacket minPacket) throws LogicFaceException {
|
||||
try {
|
||||
Interest interest=new Interest().createInterestByMINPacket(minPacket);
|
||||
if(interest==null){
|
||||
@@ -119,7 +119,7 @@ public class LogicFaceICN extends LogicFace {
|
||||
* 接收到 MINPacket 后调用 本函数,将 MINPacket 转为 Data, 查询PIT表,调用相应的onData回调函数来处理数据包
|
||||
* @param minPacket
|
||||
*/
|
||||
public void onReceiveData(MINPacket minPacket) throws LogicFaceException {
|
||||
private void onReceiveData(MINPacket minPacket) throws LogicFaceException {
|
||||
try {
|
||||
Data data=new Data().createDataByMINPacket(minPacket);
|
||||
if(data==null){
|
||||
@@ -142,7 +142,7 @@ public class LogicFaceICN extends LogicFace {
|
||||
* // 用函数查找并删除PIT表项,并使用表项中记录的相应的onNack函数处理NACK包
|
||||
* @param interest
|
||||
*/
|
||||
public void onReceiveNack(Interest interest) throws LogicFaceException {
|
||||
private void onReceiveNack(Interest interest) throws LogicFaceException {
|
||||
try {
|
||||
Nack nack=new Nack().createNackByInterest(interest);
|
||||
if(nack==null){
|
||||
@@ -275,7 +275,7 @@ public class LogicFaceICN extends LogicFace {
|
||||
* // 则将 timer 超时时间重新设置为 timeoutEventHeap 数组头的超时事件的超时时间 ,
|
||||
* // recentExpireTime 也设置为timeoutEventHeap 数组头的超时事件的超时时间。
|
||||
*/
|
||||
public void tryResetTimer(){
|
||||
private void tryResetTimer(){
|
||||
this.timeoutEventHeapLock.lock();
|
||||
if(this.timeoutEventHeap.size()<=0){
|
||||
this.timeoutEventHeapLock.unlock();
|
||||
@@ -295,7 +295,7 @@ public class LogicFaceICN extends LogicFace {
|
||||
* @param interestNameStr
|
||||
* @return
|
||||
*/
|
||||
public boolean insert2PIT(PITEntry entry,String interestNameStr){
|
||||
private boolean insert2PIT(PITEntry entry,String interestNameStr){
|
||||
long interestExpireTime=TimeHelper.getTimestampMS()+
|
||||
entry.interest.interestLifeTime.getInterestLifeTime();
|
||||
this.timeoutEventHeapLock.lock();
|
||||
|
||||
@@ -4,8 +4,7 @@ import common.LoggerHelper;
|
||||
import component.ComponentException;
|
||||
import component.Identifier;
|
||||
import component.NackHeader;
|
||||
import encoding.TLV;
|
||||
import encoding.VlInt;
|
||||
import encoding.*;
|
||||
import logicface.LogicFace;
|
||||
import logicface.LogicFaceException;
|
||||
import packet.*;
|
||||
@@ -14,6 +13,8 @@ import util.BytesBuffer;
|
||||
import util.JSONHelper;
|
||||
import util.JSONHelperException;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static mgmt.ControlResponse.ControlResponseCodeSuccess;
|
||||
|
||||
/*
|
||||
@@ -254,6 +255,16 @@ public class CommandExecutor {
|
||||
|
||||
ControlResponse controlResponse=new ControlResponse();
|
||||
|
||||
// 打印输出
|
||||
try {
|
||||
System.out.println(Arrays.toString(new SelfEncodingBase()
|
||||
.selfWireEncode(commandInterest).getRaw()));
|
||||
} catch (EncoderException e) {
|
||||
e.printStackTrace();
|
||||
} catch (BlockException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// 发送第一个命令兴趣包
|
||||
if(!this.logicFace.sendInterest(commandInterest)){
|
||||
return null;
|
||||
|
||||
@@ -49,18 +49,20 @@ public class Interest implements InteractWithField, IEncodingAble {
|
||||
public CongestionMark congestionMark=new CongestionMark();
|
||||
public TTL ttl=new TTL();
|
||||
public NackHeader nackHeader=new NackHeader();
|
||||
public IncomingLogicFaceId incomingLogicFaceId=new IncomingLogicFaceId();
|
||||
|
||||
public Identifier name=new Identifier();
|
||||
|
||||
// 标识是否是命令包
|
||||
public boolean isCommandInterest;
|
||||
public boolean isCommandInterest=false;
|
||||
|
||||
public Interest(){
|
||||
}
|
||||
|
||||
public Interest(Identifier name,Payload payload,InterestLifeTime interestLifeTime,
|
||||
CanBePrefix canBePrefix,MustBeRefresh mustBeRefresh,Nonce nonce,
|
||||
HopLimit hopLimit,NackHeader nackHeader,CongestionMark congestionMark,TTL ttl){
|
||||
HopLimit hopLimit,NackHeader nackHeader,CongestionMark congestionMark,TTL ttl,
|
||||
IncomingLogicFaceId incomingLogicFaceId){
|
||||
this.name=name;
|
||||
this.payload=payload;
|
||||
this.interestLifeTime=interestLifeTime;
|
||||
@@ -71,6 +73,7 @@ public class Interest implements InteractWithField, IEncodingAble {
|
||||
this.nackHeader=nackHeader;
|
||||
this.congestionMark=congestionMark;
|
||||
this.ttl=ttl;
|
||||
this.incomingLogicFaceId=incomingLogicFaceId;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,6 +84,10 @@ public class Interest implements InteractWithField, IEncodingAble {
|
||||
public Interest createInterestByMINPacket(MINPacket minPacket) throws PacketException {
|
||||
try {
|
||||
Interest interest = new Interest();
|
||||
if(minPacket.packetType.isEqual(TLV.TlvPacketMINManagement)){
|
||||
interest.isCommandInterest=true;
|
||||
}
|
||||
interest.minPacket.signatureField.setSignatures(minPacket.signatureField.getSignatures());
|
||||
if (!interest.doExtraDataFromFields(minPacket)) {
|
||||
return null;
|
||||
}
|
||||
@@ -207,6 +214,14 @@ public class Interest implements InteractWithField, IEncodingAble {
|
||||
return false;
|
||||
}
|
||||
minPacket.mutableField.mutableDangerousField.addBlock(block);
|
||||
// IncomingLogicFaceId
|
||||
if(this.incomingLogicFaceId.isInitial()){
|
||||
block = new SelfEncodingBase().selfWireEncode(this.incomingLogicFaceId);
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
minPacket.mutableField.mutableDangerousField.addBlock(block);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 填充只读区
|
||||
@@ -272,13 +287,13 @@ public class Interest implements InteractWithField, IEncodingAble {
|
||||
}
|
||||
|
||||
// Payload
|
||||
if (this.payload.getValue() != null){
|
||||
// if (this.payload.getValue() != null){
|
||||
block = new SelfEncodingBase().selfWireEncode(this.payload);
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
minPacket.readOnlyField.addBlock(block);
|
||||
}
|
||||
// }
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 填充标识区
|
||||
@@ -322,6 +337,7 @@ public class Interest implements InteractWithField, IEncodingAble {
|
||||
// }
|
||||
// { => 非受保护区
|
||||
// [TTL]
|
||||
// [IncomingLogicFaceId]
|
||||
// }
|
||||
// }
|
||||
/////////////////////////////////////////////////////////////
|
||||
@@ -337,6 +353,12 @@ public class Interest implements InteractWithField, IEncodingAble {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
block=minPacket.mutableField.mutableDangerousField.getBlockByType(new VlInt(TLV.TlvIncomingLogicFaceId));
|
||||
if (block != null) {
|
||||
if (!this.incomingLogicFaceId.wireDecode(block)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 解析只读区
|
||||
@@ -439,7 +461,11 @@ public class Interest implements InteractWithField, IEncodingAble {
|
||||
}
|
||||
|
||||
// 指定包类型
|
||||
this.minPacket.packetType = new VlInt(TLV.TlvPacketMINCommon);
|
||||
if(this.isCommandInterest){
|
||||
this.minPacket.packetType=new VlInt(TLV.TlvPacketMINManagement);
|
||||
}else {
|
||||
this.minPacket.packetType = new VlInt(TLV.TlvPacketMINCommon);
|
||||
}
|
||||
|
||||
return this.minPacket.wireEncode(encoder);
|
||||
}
|
||||
@@ -455,6 +481,9 @@ public class Interest implements InteractWithField, IEncodingAble {
|
||||
if (!this.minPacket.wireDecode(block)) {
|
||||
return false;
|
||||
}
|
||||
if(this.minPacket.packetType.isEqual(TLV.TlvPacketMINManagement)){
|
||||
this.isCommandInterest=true;
|
||||
}
|
||||
return this.extraDataFromFields();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package logicface;
|
||||
|
||||
import component.ComponentException;
|
||||
import component.Identifier;
|
||||
import encoding.BlockException;
|
||||
import encoding.EncoderException;
|
||||
import encoding.SelfEncodingBase;
|
||||
import mgmt.MgmtException;
|
||||
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:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:18 2021/5/18
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class LogicFaceICNTest {
|
||||
// 肢解式测试ICN:查表操作
|
||||
@Test
|
||||
public void testLookUpFib() throws LogicFaceException, ComponentException {
|
||||
LogicFaceICN logicFaceICN=new LogicFaceICN();
|
||||
logicFaceICN.initBeforeProcessEvent();
|
||||
logicFaceICN.initWithUdp("127.0.0.1",50000);
|
||||
Identifier identifier=new Identifier("/wefree");
|
||||
logicFaceICN.lookUpFib(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import component.ComponentException;
|
||||
import component.Identifier;
|
||||
import encoding.*;
|
||||
import mgmt.MgmtException;
|
||||
import mgmt.RegisterPrefixHelper;
|
||||
import org.junit.Test;
|
||||
import packet.*;
|
||||
import util.ByteHelper;
|
||||
@@ -142,7 +143,7 @@ public class LogicFaceTest {
|
||||
data.setName(name);
|
||||
|
||||
LogicFace face=new LogicFace();
|
||||
face.initWithUdp("192.168.3.9",50000);
|
||||
face.initWithUdp("192.168.43.164",13899);
|
||||
face.sendData(data);
|
||||
|
||||
// 等待两秒钟,接收数据包
|
||||
@@ -202,6 +203,43 @@ public class LogicFaceTest {
|
||||
}
|
||||
}
|
||||
|
||||
// 在这里测试是否可以在MIR注册前缀,并通过注册前缀,接收MIR的应答
|
||||
@Test
|
||||
public void testReceiveDataByUdp_ToGoLanguage() {
|
||||
try {
|
||||
// 初始化udp face
|
||||
LogicFace face=new LogicFace();
|
||||
face.initWithUdp("192.168.43.164",13899);
|
||||
|
||||
// 注册前缀
|
||||
Identifier myHostIdentifier=new Identifier("/pkuszwefree");
|
||||
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()));
|
||||
|
||||
// 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()));
|
||||
}catch (LogicFaceException | ComponentException | EncoderException | BlockException | PacketException | MgmtException e){
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启一个tcp服务器,端口号为60000
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user