增加了face中使用udp连接的收发包测试

This commit is contained in:
free will
2021-05-12 21:38:39 +08:00
parent 2a3288c076
commit 6052e73ef5
+79 -26
View File
@@ -82,6 +82,30 @@ public class LogicFaceTest {
}
}
@Test
public void testReceiveInterestByUdp() throws ComponentException, LogicFaceException, EncoderException, BlockException, MgmtException, PacketException {
Interest interest=new Interest();
interest.ttl.setTtl(Long.MAX_VALUE);
byte[] value={(byte)132,(byte)221,(byte)223,(byte)25};
interest.payload.setValue(value);
interest.congestionMark.setCongestionLevel(Long.MAX_VALUE);
Identifier name=new Identifier("/wefree");
interest.setName(name);
LogicFace face=new LogicFace();
face.initWithUdp("127.0.0.1",50000);
face.sendInterest(interest);
// 等待两秒钟,接收兴趣包
try {
Interest newInterest = face.receiveInterest(2000);
System.out.println(Arrays.toString(
new SelfEncodingBase().selfWireEncode(newInterest).getRaw()));
}catch (Exception e){
e.printStackTrace();
}
}
@Test
public void testReceiveDataByTcp() throws ComponentException, LogicFaceException, EncoderException, BlockException, MgmtException, PacketException {
Data data=new Data();
@@ -105,6 +129,29 @@ public class LogicFaceTest {
}
}
@Test
public void testReceiveDataByUdp() 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.initWithUdp("127.0.0.1",50000);
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();
@@ -130,23 +177,27 @@ public class LogicFaceTest {
}
@Test
public void testReceiveMinPacketByTcp() throws ComponentException, LogicFaceException, EncoderException, BlockException, MgmtException, PacketException {
// MINPacket minPacket=new MINPacket();
// minPacket.packetType=new VlInt(TLV.TlvPacketMINCommon);
//// minPacket
//
// LogicFace face=new LogicFace();
// face.initWithTcp("127.0.0.1",60000);
// face.sendInterest(interest);
//
// // 等待两秒钟,接收兴趣包
// try {
// Interest newInterest = face.receiveInterest(-1);
// System.out.println(Arrays.toString(
// new SelfEncodingBase().selfWireEncode(newInterest).getRaw()));
// }catch (Exception e){
// System.out.println("超时未收到数据:"+e.getMessage());
// }
public void testReceiveCPacketByUdp() 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.initWithUdp("127.0.0.1",50000);
face.sendCPacket(cPacket);
// 等待两秒钟,接收推式包
try {
CPacket cPacket1 = face.receiveCPacket(2000);
System.out.println(Arrays.toString(
new SelfEncodingBase().selfWireEncode(cPacket1).getRaw()));
}catch (Exception e){
e.printStackTrace();
}
}
/**
@@ -193,15 +244,15 @@ public class LogicFaceTest {
* 测试结果:
* [-15, 0, -6, 25, -52, 4, 4, 5, 6, 7, -15, 0, -5, 15, -15, 0, -4, 1, 1, -15, 0, -3, 1, 1, -15, 0, -2, 1, 0]
*/
// @Test
@Test
public void startUDPServer() throws IOException {
//1.创建服务端+端口
DatagramSocket server = new DatagramSocket(50000);
//2.准备接受容器
byte[] container = new byte[1024];
//3.封装成包
DatagramPacket packet = new DatagramPacket(container, container.length);
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.分析数据
@@ -209,12 +260,14 @@ public class LogicFaceTest {
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());
// 做数据应答
server.connect(remoteHost);
server.send(packet);
//6.释放
server.close();
}
//6.释放
// server.close();
}
}