恢复被错误覆盖掉的Packet测试代码

This commit is contained in:
free will
2021-05-12 19:34:05 +08:00
parent 7529b3e6ac
commit 18dbeb894e
11 changed files with 430 additions and 7 deletions
+8 -3
View File
@@ -1,8 +1,8 @@
package logicface;
import component.ComponentException;
import component.Identifier;
import encoding.TLV;
import encoding.VlIntException;
import encoding.*;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
import mgmt.MgmtException;
@@ -248,14 +248,19 @@ 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();
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 e) {
} catch (VlIntException | PacketException | EncoderException | BlockException | ComponentException | MgmtException e) {
throw new LogicFaceException("LogicFace.receiveInterest: "+e.getMessage());
}
}
+1 -1
View File
@@ -176,7 +176,7 @@ public class StreamTransport extends Transport implements ITransport{
public boolean setReadTimeout(long duration) throws LogicFaceException {
try {
if (duration <= 0) {
this.channel.socket().setSoTimeout(0);
// this.channel.socket().setSoTimeout(0);
return true;
}
this.channel.socket().setSoTimeout((int)(duration));
+6 -1
View File
@@ -112,11 +112,16 @@ public class UdpTransport extends Transport implements ITransport{
return this.doReceive();
}
/**
* @param duration 超时时间 , 以 毫秒 为单位, 小于等于0,表示永不超时
* @return
* @throws LogicFaceException
*/
@Override
public boolean setReadTimeout(long duration) throws LogicFaceException {
try {
if (duration <= 0) {
this.channel.socket().setSoTimeout(0);
// this.channel.socket().setSoTimeout(0);
return true;
}
this.channel.socket().setSoTimeout((int)(duration));
+50 -1
View File
@@ -2,9 +2,12 @@ package logicface;
import component.ComponentException;
import component.Identifier;
import encoding.*;
import mgmt.MgmtException;
import org.junit.Test;
import packet.Interest;
import packet.MINPacket;
import packet.PacketException;
import util.ByteHelper;
import java.io.IOException;
@@ -57,10 +60,54 @@ public class LogicFaceTest {
face.sendInterest(interest);
}
@Test
public void testReceiveInterestByTcp() 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("/pkusz/wefree");
interest.setName(name);
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.getLocalizedMessage());
}
}
@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());
// }
}
/**
* 开启一个tcp服务器,端口号为60000
*/
// @Test
@Test
public void startTCPServer() {
ServerSocket socket = null;
OutputStream outputStream = null;
@@ -82,7 +129,9 @@ public class LogicFaceTest {
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);
inputStream.close();
outputStream.close();
socket.close();
@@ -91,7 +91,7 @@ public class TcpTransportTest {
/**
* 开启一个tcp服务器,端口号为60000
*/
// @Test
@Test
public void startTCPServer() {
ServerSocket socket = null;
OutputStream outputStream = null;
+89
View File
@@ -0,0 +1,89 @@
package packet;
import component.ComponentException;
import component.Identifier;
import component.IdentifierWrapper;
import component.TTL;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 14:08 2021/4/7
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class CPacketTest_WF {
@Test
public void TestCPacket_GetIdentifier() throws ComponentException {
MINPacket packet=new MINPacket();
Identifier wrapper=new Identifier("/min/pku/sz");
Identifier wrapper1=new Identifier("/net/王");
packet.packetType=new VlInt(3);
CPacket cPacket=new CPacket();
cPacket.setSrcIdentifier(wrapper);
cPacket.setDstIdentifier(wrapper1);
cPacket.setTtl(new TTL(Long.MAX_VALUE));
byte[] bytes={(byte)1,(byte)2,(byte)3,(byte)4};
cPacket.payload.setValue(bytes);
System.out.println("src: "+cPacket.getSrcIdentifier().toUri());
System.out.println("dst: "+cPacket.getDstIdentifier().toUri());
System.out.println("ttl: "+cPacket.getTtl().ttl());
System.out.println("value: "+ Arrays.toString(cPacket.getValue()));
}
/**
* CPacket wireEncode:
* [5, 61,
* 50, 37,
* 102, 19, 101, 17, 100, 4, 0, 109, 105, 110, 100, 4, 0, 112, 107, 117, 100, 3, 0, 115, 122,
* 102, 14, 101, 12, 100, 4, 0, 110, 101, 116, 100, 4, 0, -25, -114, -117,
* 52, 6, -52, 4, 1, 2, 3, 4,
* 53, 12, 55, 10, -44, 8, 127, -1, -1, -1, -1, -1, -1, -1]
* @throws EncoderException
* @throws ComponentException
* @throws PacketException
* @throws BlockException
*/
@Test
public void TestCPacket_WireEncode() throws EncoderException, ComponentException, PacketException, BlockException {
MINPacket packet=new MINPacket();
Identifier wrapper=new Identifier("/min/pku/sz");
Identifier wrapper1=new Identifier("/net/王");
packet.packetType=new VlInt(3);
CPacket cPacket=new CPacket();
cPacket.setSrcIdentifier(wrapper);
cPacket.setDstIdentifier(wrapper1);
cPacket.setTtl(new TTL(Long.MAX_VALUE));
byte[] bytes={(byte)1,(byte)2,(byte)3,(byte)4};
cPacket.payload.setValue(bytes);
System.out.println("src: "+cPacket.getSrcIdentifier().toUri());
System.out.println("dst: "+cPacket.getDstIdentifier().toUri());
System.out.println("ttl: "+cPacket.getTtl().ttl());
System.out.println("value: "+ Arrays.toString(cPacket.getValue()));
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("encode res: "+cPacket.wireEncode(encoder));
byte[] buf=encoder.getBuffer();
System.out.println("buf: "+Arrays.toString(buf));
Block block=new Block(buf,false);
System.out.println("block len: "+block.getLength());
CPacket newPacket=new CPacket();
System.out.println("decode res: "+newPacket.wireDecode(block));
System.out.println("src: "+newPacket.getSrcIdentifier().toUri());
System.out.println("dst: "+newPacket.getDstIdentifier().toUri());
System.out.println("ttl: "+newPacket.getTtl().ttl());
System.out.println("value: "+ Arrays.toString(newPacket.getValue()));
}
}
+69
View File
@@ -0,0 +1,69 @@
package packet;
import component.ComponentException;
import component.Identifier;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 21:19 2021/4/7
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class DataTest_WF {
@Test
public void TestData_ToUri() throws ComponentException {
Data data=new Data();
Identifier name=new Identifier("/wefree/王");
data.setName(name);
System.out.println(data.getName().toUri());
}
/**
* Data wireEncode:
* [5, 53,
* 50, 19,
* 103, 17, 101, 15,
* 100, 7, 0, 119, 101, 102, 114, 101, 101,
* 100, 4, 0, -25, -114, -117,
* 52, 16,
* -46, 8, 127, -1, -1, -1, -1, -1, -1, -1,
* -52, 4, -124, -35, -33, 25,
* 53, 12, 54, 10, -45, 8, 127, -1, -1, -1, -1, -1, -1, -1]
* @throws ComponentException
* @throws EncoderException
* @throws PacketException
* @throws BlockException
*/
@Test
public void TestData_WireEncode() throws ComponentException, EncoderException, PacketException, BlockException {
Data data=new Data();
data.freshnessPeriod.setFreshnessPeriod(Long.MAX_VALUE);
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);
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("encode res: "+data.wireEncode(encoder));
byte[] buf=encoder.getBuffer();
System.out.println("buf: "+ Arrays.toString(buf));
Block block=new Block(buf,false);
System.out.println("block buf: "+Arrays.toString(block.getValue()));
Data newData=new Data();
System.out.println("decode res: "+newData.wireDecode(block));
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("encode res: "+newData.wireEncode(encoder));
byte[] newbuf=encoder.getBuffer();
System.out.println("newbuf: "+ Arrays.toString(newbuf));
System.out.println("new uri: "+newData.toUri()
+" , freshness "+newData.freshnessPeriod.getFreshnessPeriod());
}
}
+71
View File
@@ -0,0 +1,71 @@
package packet;
import component.ComponentException;
import component.Identifier;
import component.TTL;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 10:32 2021/4/8
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class InterestTest_WF {
@Test
public void TestInterest_ToUri() throws ComponentException {
Interest interest=new Interest();
Identifier name=new Identifier("/wefree/王");
interest.setName(name);
System.out.println(interest.getName().toUri());
}
/**
* Interest wireEncode:
* [5, 69,
* 50, 19,
* 103, 17, 101, 15,
* 100, 7, 0, 119, 101, 102, 114, 101, 101,
* 100, 4, 0, -25, -114, -117,
* 52, 20,
* -49, 2, 15, -96,
* -48, 8, 45, -7, 121, 78, -63, 21, -121, -66,
* -52, 4, -124, -35, -33, 25,
* 53, 24, 54, 10, -45, 8, 127, -1, -1, -1, -1, -1, -1, -1, 55, 10, -44, 8, 127, -1, -1, -1, -1, -1, -1, -1]
* @throws ComponentException
* @throws EncoderException
* @throws PacketException
* @throws BlockException
*/
@Test
public void TestInterest_WireEncode() throws ComponentException, EncoderException, PacketException, BlockException {
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);
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("encode res: "+interest.wireEncode(encoder));
byte[] buf=encoder.getBuffer();
System.out.println("buf: "+ Arrays.toString(buf));
Block block=new Block(buf,false);
System.out.println("block buf: "+Arrays.toString(block.getValue()));
Interest newInterest=new Interest();
System.out.println("decode res: "+newInterest.wireDecode(block));
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("encode res: "+newInterest.wireEncode(encoder));
byte[] newbuf=encoder.getBuffer();
System.out.println("newbuf: "+ Arrays.toString(newbuf));
System.out.println("new uri: "+newInterest.toUri()
+" , ttl "+newInterest.ttl.ttl());
}
}
+59
View File
@@ -0,0 +1,59 @@
package packet;
import component.*;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 10:42 2021/4/8
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class LpPacketTest_WF {
/**
* LpPacket wireEncode:
* [-15, 0, -6, 34,
* -52, 4, 1, 2, 3, 4,
* -15, 0, -5, 15,
* -15, 0, -4, 1, 1,
* -15, 0, -3, 1, 2,
* -15, 0,-2, 1, 3]
* @throws EncoderException
* @throws PacketException
* @throws BlockException
*/
@Test
public void TestLpPacket_wireEncode() throws EncoderException, PacketException, BlockException {
LpPacketHeader header=new LpPacketHeader(new LpPacketFragmentId(1),
new LpPacketFragmentNum(2),new LpPacketFragmentSeq(3));
byte[] value={(byte)1,(byte)2,(byte)3,(byte)4};
LpPacket lpPacket=new LpPacket(header,new Payload(value));
System.out.println(lpPacket.lpPacketHeader.getLpPacketFragmentId().getId()+", "
+lpPacket.lpPacketHeader.getLpPacketFragmentNum().getFragmentNum()
+", "+lpPacket.lpPacketHeader.getLpPacketFragmentSeq().getFragmentSeq()
+", "+ Arrays.toString(lpPacket.payload.getValue()));
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("encode res: "+lpPacket.wireEncode(encoder));
byte[] buf=encoder.getBuffer();
System.out.println("buf: "+ Arrays.toString(buf));
Block block=new Block(buf,false);
System.out.println("block buf: "+Arrays.toString(block.getValue()));
LpPacket newLpPacket=new LpPacket();
System.out.println("decode res: "+newLpPacket.wireDecode(block));
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("encode res: "+newLpPacket.wireEncode(encoder));
byte[] newbuf=encoder.getBuffer();
System.out.println("newbuf: "+ Arrays.toString(newbuf));
System.out.println(newLpPacket.lpPacketHeader.getLpPacketFragmentId().getId()+". "
+newLpPacket.lpPacketHeader.getLpPacketFragmentNum().getFragmentNum()
+", "+newLpPacket.lpPacketHeader.getLpPacketFragmentSeq().getFragmentSeq()
+", "+ Arrays.toString(newLpPacket.payload.getValue()));
}
}
@@ -0,0 +1,12 @@
package packet;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 11:17 2021/4/2
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class MINPacketTest_WF {
}
+64
View File
@@ -0,0 +1,64 @@
package packet;
import component.*;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 11:23 2021/4/8
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class NackTest_WF {
/**
* Nack wireEncode:
* [5, 73,
* 50, 19,
* 103, 17, 101, 15,
* 100, 7, 0, 119, 101, 102, 114, 101, 101,
* 100, 4, 0, -25, -114, -117,
* 52, 24,
* -49, 2, 15, -96,
* -48, 8, 28, -40, -82, -73, -63, -103, -15, -43,
* -43, 2, 9, 29,
* -52, 4, -124, -35, -33, 25,
* 53, 24, 54, 10, -45, 8, 127, -1, -1, -1, -1, -1, -1, -1, 55, 10, -44, 8, 127, -1, -1, -1, -1, -1, -1, -1]
* @throws EncoderException
* @throws PacketException
* @throws BlockException
* @throws ComponentException
*/
@Test
public void TestNack_wireEncode() throws EncoderException, PacketException, BlockException, ComponentException {
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);
Nack nack=new Nack(interest,2333);
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("encode res: "+nack.wireEncode(encoder));
byte[] buf=encoder.getBuffer();
System.out.println("buf: "+ Arrays.toString(buf));
Block block=new Block(buf,false);
System.out.println("block buf: "+Arrays.toString(block.getValue()));
Nack newNack=new Nack();
System.out.println("decode res: "+newNack.wireDecode(block));
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("encode res: "+newNack.wireEncode(encoder));
byte[] newbuf=encoder.getBuffer();
System.out.println("newbuf: "+ Arrays.toString(newbuf));
System.out.println("new uri: "+newNack.interest.toUri()
+" , ttl "+newNack.interest.ttl.ttl()
+" ,reason: "+newNack.getNackReason());
}
}