fixed bug: LogicFace.receiveInterest&Data

This commit is contained in:
free will
2021-05-12 21:14:36 +08:00
parent 18dbeb894e
commit 2a3288c076
5 changed files with 80 additions and 18 deletions
+3
View File
@@ -2,9 +2,12 @@ package logicface;
import component.*;
import encoding.*;
import mgmt.MgmtException;
import packet.*;
import util.ByteHelper;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
+5 -9
View File
@@ -14,6 +14,7 @@ import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
/*
* @Author: Wang Feng
@@ -228,7 +229,7 @@ public class LogicFace {
try {
while (true){
MINPacket minPacket=this.receivePacket(timeout);
int packetType=minPacket.packetType.getVlIntValue2Int();
int packetType=minPacket.getPacketType().getVlIntValue2Int();
if(packetType == TLV.TlvIdentifierCommon){
CPacket cPacket=new CPacket().createCPacketByMINPacket(minPacket);
return cPacket;
@@ -248,19 +249,14 @@ public class LogicFace {
public Interest receiveInterest(long timeout) throws LogicFaceException {
try {
while (true){
System.out.println("receiveInterest ing...");
MINPacket minPacket=this.receivePacket(timeout);
System.out.println("minpacket: "+new SelfEncodingBase().selfWireEncode(minPacket).getRaw());
int packetType=minPacket.packetType.getVlIntValue2Int();
int packetType=minPacket.getPacketType().getVlIntValue2Int();
if(packetType == TLV.TlvIdentifierContentInterest){
Interest interest=new Interest().createInterestByMINPacket(minPacket);
return interest;
}else{
// System.out.println(new SelfEncodingBase().selfWireEncode(minPacket).getRaw());
}
System.out.println(new SelfEncodingBase().selfWireEncode(minPacket).getRaw());
}
} catch (VlIntException | PacketException | EncoderException | BlockException | ComponentException | MgmtException e) {
} catch (VlIntException | PacketException e) {
throw new LogicFaceException("LogicFace.receiveInterest: "+e.getMessage());
}
}
@@ -274,7 +270,7 @@ public class LogicFace {
try {
while (true){
MINPacket minPacket=this.receivePacket(timeout);
int packetType=minPacket.packetType.getVlIntValue2Int();
int packetType=minPacket.getPacketType().getVlIntValue2Int();
if(packetType == TLV.TlvIdentifierContentData){
Data data=new Data().createDataByMINPacket(minPacket);
return data;
+2 -2
View File
@@ -168,7 +168,7 @@ public class Data implements IEncodingAble, InteractWithField {
/////////////////////////////////////////////////////////////
minPacket.identifierField.clearIdentifiers();
IdentifierWrapper identifierWrapper =
new IdentifierWrapper().createContentInterestIdentifierByComponents(this.name.getComponents());
new IdentifierWrapper().createContentDataIdentifierByComponents(this.name.getComponents());
if (identifierWrapper == null) {
return false;
}
@@ -247,7 +247,7 @@ public class Data implements IEncodingAble, InteractWithField {
//// 解析标识区
/////////////////////////////////////////////////////////////
IdentifierWrapper interestIdentifierWrapper =
minPacket.identifierField.getIdentifierByType(new VlInt(TLV.TlvIdentifierContentInterest));
minPacket.identifierField.getIdentifierByType(new VlInt(TLV.TlvIdentifierContentData));
if (interestIdentifierWrapper == null) {
return false;
}
+15
View File
@@ -26,6 +26,18 @@ public class MINPacket implements IEncodingAble,IMINPacket {
return TLV.isValidPacketType(packetType);
}
/**
* 获得MINPacket中,第一个标识的类型
* @return encoding.VlInt MINPacket中,第一个标识的类型
*/
public VlInt getPacketType() throws PacketException {
try {
IdentifierWrapper identifierWrapper=this.identifierField.getIdentifier(0);
return identifierWrapper.getTlvType();
} catch (ComponentException e) {
throw new PacketException("MINPacket.getPacketType: " + e.getMessage());
}
}
/**
* 将 Packet 线速编码为 TLV
@@ -113,6 +125,9 @@ public class MINPacket implements IEncodingAble,IMINPacket {
return false;
}
// 解析包类型
this.packetType=block.getType();
// 标记,用来记录是否满足必要条件
boolean existIdentifier = false;
+55 -7
View File
@@ -5,9 +5,7 @@ import component.Identifier;
import encoding.*;
import mgmt.MgmtException;
import org.junit.Test;
import packet.Interest;
import packet.MINPacket;
import packet.PacketException;
import packet.*;
import util.ByteHelper;
import java.io.IOException;
@@ -67,7 +65,7 @@ public class LogicFaceTest {
byte[] value={(byte)132,(byte)221,(byte)223,(byte)25};
interest.payload.setValue(value);
interest.congestionMark.setCongestionLevel(Long.MAX_VALUE);
Identifier name=new Identifier("/pkusz/wefree");
Identifier name=new Identifier("/wefree");
interest.setName(name);
LogicFace face=new LogicFace();
@@ -76,11 +74,58 @@ public class LogicFaceTest {
// 等待两秒钟,接收兴趣包
try {
Interest newInterest = face.receiveInterest(-1);
Interest newInterest = face.receiveInterest(2000);
System.out.println(Arrays.toString(
new SelfEncodingBase().selfWireEncode(newInterest).getRaw()));
}catch (Exception e){
System.out.println("超时未收到数据:"+e.getLocalizedMessage());
e.printStackTrace();
}
}
@Test
public void testReceiveDataByTcp() throws ComponentException, LogicFaceException, EncoderException, BlockException, MgmtException, PacketException {
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);
LogicFace face=new LogicFace();
face.initWithTcp("127.0.0.1",60000);
face.sendData(data);
// 等待两秒钟,接收数据包
try {
Data data1 = face.receiveData(2000);
System.out.println(Arrays.toString(
new SelfEncodingBase().selfWireEncode(data1).getRaw()));
}catch (Exception e){
e.printStackTrace();
}
}
@Test
public void testReceiveCPacketByTcp() throws ComponentException, LogicFaceException, EncoderException, BlockException, MgmtException, PacketException {
CPacket cPacket=new CPacket();
byte[] value={(byte)132,(byte)221,(byte)223,(byte)25};
cPacket.payload.setValue(value);
Identifier name=new Identifier("/wefree");
Identifier nameTo=new Identifier("/wf");
cPacket.setSrcIdentifier(name);
cPacket.setDstIdentifier(nameTo);
LogicFace face=new LogicFace();
face.initWithTcp("127.0.0.1",60000);
face.sendCPacket(cPacket);
// 等待两秒钟,接收推式包
try {
CPacket cPacket1 = face.receiveCPacket(2000);
System.out.println(Arrays.toString(
new SelfEncodingBase().selfWireEncode(cPacket1).getRaw()));
}catch (Exception e){
e.printStackTrace();
}
}
@@ -125,13 +170,16 @@ public class LogicFaceTest {
// inputStream.close();
System.out.println(Arrays.toString(ByteHelper.getLenBytes(bytes, 0, len)));
// 等待三秒,再发送应答
Thread.sleep(5000);
//获取向客户端发送消息的对象流
outputStream = client.getOutputStream();
//向客户端写数据
outputStream.write(ByteHelper.getLenBytes(bytes, 0, len));
System.out.println("writed to client: "+Arrays.toString(ByteHelper.getLenBytes(bytes, 0, len)));
Thread.sleep(20000);
Thread.sleep(200000);
inputStream.close();
outputStream.close();
socket.close();