mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-06-15 17:34:47 +08:00
writing test: face&linkservice
This commit is contained in:
@@ -1,9 +1,6 @@
|
|||||||
package logicface;
|
package logicface;
|
||||||
|
|
||||||
import component.ComponentException;
|
import component.*;
|
||||||
import component.LpPacketFragmentId;
|
|
||||||
import component.LpPacketFragmentNum;
|
|
||||||
import component.LpPacketFragmentSeq;
|
|
||||||
import encoding.*;
|
import encoding.*;
|
||||||
import mgmt.MgmtException;
|
import mgmt.MgmtException;
|
||||||
import packet.*;
|
import packet.*;
|
||||||
@@ -17,6 +14,9 @@ import util.ByteHelper;
|
|||||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||||
*/
|
*/
|
||||||
public class LinkService {
|
public class LinkService {
|
||||||
|
// 常量
|
||||||
|
private final int LpPacketHeaderMaxSize = 1000; // 默认最长头部长度
|
||||||
|
|
||||||
public ITransport transport; // 传输通道
|
public ITransport transport; // 传输通道
|
||||||
public LogicFace logicFace=new LogicFace();// LinkService关联的logicFace
|
public LogicFace logicFace=new LogicFace();// LinkService关联的logicFace
|
||||||
public int mtu; // MTU大小
|
public int mtu; // MTU大小
|
||||||
@@ -36,7 +36,7 @@ public class LinkService {
|
|||||||
byte[] buf=new byte[Encoder.MaxPacketSize];
|
byte[] buf=new byte[Encoder.MaxPacketSize];
|
||||||
lpPacket.setValue(buf);
|
lpPacket.setValue(buf);
|
||||||
Encoder encoder=new Encoder();
|
Encoder encoder=new Encoder();
|
||||||
if(!encoder.encoderReset(new SizeT(Encoder.MaxPacketSize+1000),new SizeT(0))){
|
if(!encoder.encoderReset(new SizeT(Encoder.MaxPacketSize+this.LpPacketHeaderMaxSize),new SizeT(0))){
|
||||||
throw new LogicFaceException("LinkService.calculateLpPacketHeadSize: encoderReset error");
|
throw new LogicFaceException("LinkService.calculateLpPacketHeadSize: encoderReset error");
|
||||||
}
|
}
|
||||||
this.lpPacketHeadSize = lpPacket.wireEncode(encoder);
|
this.lpPacketHeadSize = lpPacket.wireEncode(encoder);
|
||||||
@@ -222,4 +222,13 @@ public class LinkService {
|
|||||||
throw new LogicFaceException("LinkService.sendMINPacket: "+e.getMessage());
|
throw new LogicFaceException("LinkService.sendMINPacket: "+e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印自己的状态,测试使用。
|
||||||
|
*/
|
||||||
|
public void printSelf(){
|
||||||
|
System.out.println("lpPacketHeadSize: "+this.lpPacketHeadSize);
|
||||||
|
System.out.println("mtu: "+this.mtu);
|
||||||
|
System.out.println("lpPacketId: "+this.lpPacketId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package logicface;
|
package logicface;
|
||||||
|
|
||||||
import common.LoggerHelper;
|
|
||||||
import component.ComponentException;
|
|
||||||
import component.Identifier;
|
import component.Identifier;
|
||||||
import component.IdentifierWrapper;
|
|
||||||
import encoding.TLV;
|
import encoding.TLV;
|
||||||
import encoding.VlIntException;
|
import encoding.VlIntException;
|
||||||
import jnr.unixsocket.UnixSocketAddress;
|
import jnr.unixsocket.UnixSocketAddress;
|
||||||
@@ -25,6 +22,9 @@ import java.nio.channels.SocketChannel;
|
|||||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||||
*/
|
*/
|
||||||
public class LogicFace {
|
public class LogicFace {
|
||||||
|
// 定量区
|
||||||
|
public final int DefaultMtuSize = 9000; // 缺省MTU为 9000 字节, 初始化face时会用到
|
||||||
|
|
||||||
// LogicFaceType区:LogicFace的类型
|
// LogicFaceType区:LogicFace的类型
|
||||||
public static final int LogicFaceTypeTCP = 0;
|
public static final int LogicFaceTypeTCP = 0;
|
||||||
public static final int LogicFaceTypeUDP = 1;
|
public static final int LogicFaceTypeUDP = 1;
|
||||||
@@ -47,18 +47,24 @@ public class LogicFace {
|
|||||||
* @param port MIR的TCP端口号,如 13899
|
* @param port MIR的TCP端口号,如 13899
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean initWithTcp(String ip,String port) throws LogicFaceException {
|
public boolean initWithTcp(String ip,Integer port) throws LogicFaceException {
|
||||||
try {
|
try {
|
||||||
|
// Transport
|
||||||
SocketChannel channel=SocketChannel.open();
|
SocketChannel channel=SocketChannel.open();
|
||||||
channel.connect(new InetSocketAddress(ip, Integer.parseInt(port)));
|
channel.connect(new InetSocketAddress(ip, port));
|
||||||
this.linkService=new LinkService();
|
|
||||||
TcpTransport tcpTransport=new TcpTransport();
|
TcpTransport tcpTransport=new TcpTransport();
|
||||||
tcpTransport.init(channel);
|
tcpTransport.init(channel);
|
||||||
this.linkService.init(9000); // 设置MTU为 9000 字节
|
|
||||||
|
// LinkService
|
||||||
|
this.linkService=new LinkService();
|
||||||
|
this.linkService.init(DefaultMtuSize);
|
||||||
this.linkService.logicFace=this;
|
this.linkService.logicFace=this;
|
||||||
this.linkService.transport=tcpTransport;
|
this.linkService.transport=tcpTransport;
|
||||||
|
|
||||||
tcpTransport.linkService=this.linkService;
|
tcpTransport.linkService=this.linkService;
|
||||||
this.transport=tcpTransport;
|
this.transport=tcpTransport;
|
||||||
|
|
||||||
|
// Others
|
||||||
this.logicFaceType=LogicFaceTypeTCP;
|
this.logicFaceType=LogicFaceTypeTCP;
|
||||||
this.keyChain=null;
|
this.keyChain=null;
|
||||||
this.state=true;
|
this.state=true;
|
||||||
@@ -74,18 +80,24 @@ public class LogicFace {
|
|||||||
* @param port MIR的TCP端口号,如 13899
|
* @param port MIR的TCP端口号,如 13899
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean initWithUdp(String ip,String port) throws LogicFaceException {
|
public boolean initWithUdp(String ip,Integer port) throws LogicFaceException {
|
||||||
try {
|
try {
|
||||||
|
// Transport
|
||||||
DatagramChannel channel=DatagramChannel.open();
|
DatagramChannel channel=DatagramChannel.open();
|
||||||
channel.connect(new InetSocketAddress(ip, Integer.parseInt(port)));
|
channel.connect(new InetSocketAddress(ip, port));
|
||||||
this.linkService=new LinkService();
|
|
||||||
UdpTransport udpTransport=new UdpTransport();
|
UdpTransport udpTransport=new UdpTransport();
|
||||||
udpTransport.init(channel,new InetSocketAddress(ip, Integer.parseInt(port)));
|
udpTransport.init(channel,new InetSocketAddress(ip, port));
|
||||||
this.linkService.init(9000); // 设置MTU为 9000 字节
|
|
||||||
|
// LinkService
|
||||||
|
this.linkService=new LinkService();
|
||||||
|
this.linkService.init(DefaultMtuSize);
|
||||||
this.linkService.logicFace=this;
|
this.linkService.logicFace=this;
|
||||||
this.linkService.transport=udpTransport;
|
this.linkService.transport=udpTransport;
|
||||||
|
|
||||||
udpTransport.linkService=this.linkService;
|
udpTransport.linkService=this.linkService;
|
||||||
this.transport=udpTransport;
|
this.transport=udpTransport;
|
||||||
|
|
||||||
|
// Others
|
||||||
this.keyChain=null;
|
this.keyChain=null;
|
||||||
this.logicFaceType=LogicFaceTypeUDP;
|
this.logicFaceType=LogicFaceTypeUDP;
|
||||||
this.state=true;
|
this.state=true;
|
||||||
@@ -97,23 +109,27 @@ public class LogicFace {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过unixSocket连接MIR
|
* 通过unixSocket连接MIR
|
||||||
|
* 暂时不测
|
||||||
* @param path MIR的unix socket地址,默认是 "/tmp/mir-sock"
|
* @param path MIR的unix socket地址,默认是 "/tmp/mir-sock"
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean initWithUnixSocket(String path) throws LogicFaceException {
|
private boolean initWithUnixSocket(String path) throws LogicFaceException {
|
||||||
try {
|
try {
|
||||||
// 建立 Unix Socket 连接
|
// 建立 Unix Socket 连接
|
||||||
File unixFile = new File(path);
|
File unixFile = new File(path);
|
||||||
UnixSocketChannel channel=UnixSocketChannel.open();
|
UnixSocketChannel channel=UnixSocketChannel.open();
|
||||||
channel.connect(new UnixSocketAddress(unixFile));
|
channel.connect(new UnixSocketAddress(unixFile));
|
||||||
this.linkService=new LinkService();
|
|
||||||
UnixStreamTransport unixStreamTransport=new UnixStreamTransport();
|
UnixStreamTransport unixStreamTransport=new UnixStreamTransport();
|
||||||
unixStreamTransport.init(channel);
|
unixStreamTransport.init(channel);
|
||||||
this.linkService.init(9000); // 设置MTU为 9000 字节
|
|
||||||
|
this.linkService=new LinkService();
|
||||||
|
this.linkService.init(DefaultMtuSize);
|
||||||
this.linkService.logicFace=this;
|
this.linkService.logicFace=this;
|
||||||
this.linkService.transport=unixStreamTransport;
|
this.linkService.transport=unixStreamTransport;
|
||||||
|
|
||||||
unixStreamTransport.linkService=this.linkService;
|
unixStreamTransport.linkService=this.linkService;
|
||||||
this.transport=unixStreamTransport;
|
this.transport=unixStreamTransport;
|
||||||
|
|
||||||
this.keyChain=null;
|
this.keyChain=null;
|
||||||
this.logicFaceType=LogicFaceTypeUnix;
|
this.logicFaceType=LogicFaceTypeUnix;
|
||||||
this.state=true;
|
this.state=true;
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package logicface;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Author: Wang Feng
|
||||||
|
* @Description:
|
||||||
|
* @Version: 1.0.0
|
||||||
|
* @Date: 20:34 2021/5/10
|
||||||
|
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||||
|
*/
|
||||||
|
public class LinkServiceTest {
|
||||||
|
@Test
|
||||||
|
public void testIni() throws LogicFaceException {
|
||||||
|
LinkService linkService=new LinkService();
|
||||||
|
linkService.init(9000);
|
||||||
|
linkService.printSelf();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
package logicface;
|
||||||
|
|
||||||
|
import component.ComponentException;
|
||||||
|
import component.Identifier;
|
||||||
|
import org.junit.Test;
|
||||||
|
import packet.Interest;
|
||||||
|
import packet.MINPacket;
|
||||||
|
import util.ByteHelper;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.*;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @Author: Wang Feng
|
||||||
|
* @Description:
|
||||||
|
* @Version: 1.0.0
|
||||||
|
* @Date: 16:26 2021/5/10
|
||||||
|
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||||
|
*/
|
||||||
|
public class LogicFaceTest {
|
||||||
|
/**
|
||||||
|
* 测试初始化&UDP发包
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testIniUdp() throws LogicFaceException, 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("/pkusz/wefree");
|
||||||
|
interest.setName(name);
|
||||||
|
|
||||||
|
LogicFace face=new LogicFace();
|
||||||
|
face.initWithUdp("127.0.0.1",50000);
|
||||||
|
face.sendInterest(interest);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试初始化&TCP发包
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testIniTcp() throws LogicFaceException, 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("/pkusz/wefree");
|
||||||
|
interest.setName(name);
|
||||||
|
|
||||||
|
LogicFace face=new LogicFace();
|
||||||
|
face.initWithTcp("127.0.0.1",60000);
|
||||||
|
face.sendInterest(interest);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开启一个tcp服务器,端口号为60000
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void startTCPServer() {
|
||||||
|
ServerSocket socket = null;
|
||||||
|
OutputStream outputStream = null;
|
||||||
|
try {
|
||||||
|
//建立基站
|
||||||
|
socket = new ServerSocket(60000);
|
||||||
|
//开始开启接收模式,接到后返回客户端的socket对象
|
||||||
|
Socket client = socket.accept();
|
||||||
|
|
||||||
|
byte[] bytes = new byte[1500];
|
||||||
|
|
||||||
|
// 循环接收数据,并打印
|
||||||
|
InputStream inputStream = client.getInputStream();
|
||||||
|
int len = inputStream.read(bytes);
|
||||||
|
// inputStream.close();
|
||||||
|
System.out.println(Arrays.toString(ByteHelper.getLenBytes(bytes, 0, len)));
|
||||||
|
|
||||||
|
//获取向客户端发送消息的对象流
|
||||||
|
outputStream = client.getOutputStream();
|
||||||
|
//向客户端写数据
|
||||||
|
outputStream.write(ByteHelper.getLenBytes(bytes, 0, len));
|
||||||
|
|
||||||
|
inputStream.close();
|
||||||
|
outputStream.close();
|
||||||
|
socket.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个UDPServer,从而打印出来udp face发送的数据。端口号为50000
|
||||||
|
* 测试结果:
|
||||||
|
* [-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
|
||||||
|
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){
|
||||||
|
//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());
|
||||||
|
server.connect(remoteHost);
|
||||||
|
server.send(packet);
|
||||||
|
}
|
||||||
|
//6.释放
|
||||||
|
// server.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user