mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add test: udptransporttest
This commit is contained in:
1 parent
b1535aa824
commit
6393c7985e
4 files changed
+166
-4
No files matched your search
@@ -2,6 +2,7 @@ package logicface;
|
||||
|
||||
import common.LoggerHelper;
|
||||
import packet.LpPacket;
|
||||
import util.ByteHelper;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
@@ -9,7 +10,9 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channel;
|
||||
import java.nio.channels.DatagramChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.sql.Array;
|
||||
import java.sql.Time;
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
@@ -33,6 +36,11 @@ public class UdpTransport extends Transport implements ITransport{
|
||||
*/
|
||||
public void init(DatagramChannel channel,SocketAddress remoteUdpAddr) throws LogicFaceException {
|
||||
try {
|
||||
// 加一个预处理
|
||||
if(channel.getLocalAddress()==null){
|
||||
channel.socket().connect(remoteUdpAddr);
|
||||
}
|
||||
|
||||
this.channel=channel;
|
||||
this.localAddr=channel.getLocalAddress().toString();
|
||||
this.localUri="udp://"+this.localAddr;
|
||||
@@ -71,7 +79,6 @@ public class UdpTransport extends Transport implements ITransport{
|
||||
return false;
|
||||
}
|
||||
LoggerHelper.logger.debug("start write to udp : "+this.remoteUdpAddr.toString());
|
||||
this.channel.socket().connect(remoteUdpAddr);
|
||||
int res=this.channel.write(ByteBuffer.wrap(encodeBuf));
|
||||
if(res<0){
|
||||
return false;
|
||||
@@ -88,12 +95,14 @@ public class UdpTransport extends Transport implements ITransport{
|
||||
*/
|
||||
private LpPacket doReceive() throws LogicFaceException {
|
||||
try {
|
||||
SocketAddress remoteUdpAddr=this.channel.receive(ByteBuffer.wrap(this.recvBuf));
|
||||
if(remoteUdpAddr==null){
|
||||
ByteBuffer bf = ByteBuffer.wrap(this.recvBuf);
|
||||
int readLen=this.channel.read(bf);
|
||||
if(readLen<=0){
|
||||
return null;
|
||||
}
|
||||
byte[] readBytes= ByteHelper.getLenBytes(bf.array(),0,readLen);
|
||||
LoggerHelper.logger.debug("recv from udp: "+this.remoteAddr);
|
||||
LpPacket lpPacket=this.parseByteArray2LpPacket(this.recvBuf);
|
||||
LpPacket lpPacket=this.parseByteArray2LpPacket(readBytes);
|
||||
if(lpPacket==null){
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
package logicface;
|
||||
|
||||
import component.ComponentException;
|
||||
import encoding.BlockException;
|
||||
import encoding.EncoderException;
|
||||
import encoding.SelfEncodingBase;
|
||||
import mgmt.MgmtException;
|
||||
import org.junit.Test;
|
||||
import packet.LpPacket;
|
||||
import packet.PacketException;
|
||||
import util.ByteHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.nio.channels.DatagramChannel;
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 21:17 2021/5/6
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class UdpTransportTest {
|
||||
/**
|
||||
* 测试结果:
|
||||
* send: [-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]
|
||||
* server receive:[-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]
|
||||
* @throws IOException
|
||||
* @throws LogicFaceException
|
||||
* @throws EncoderException
|
||||
* @throws BlockException
|
||||
* @throws MgmtException
|
||||
* @throws ComponentException
|
||||
* @throws PacketException
|
||||
*/
|
||||
@Test
|
||||
public void testSend() throws IOException, LogicFaceException, EncoderException, BlockException, MgmtException, ComponentException, PacketException {
|
||||
// 初始化一个udp连接
|
||||
UdpTransport udpTransport=new UdpTransport();
|
||||
SocketAddress socketAddress=new InetSocketAddress("localhost",50000);
|
||||
DatagramChannel datagramChannel=DatagramChannel.open();
|
||||
udpTransport.init(datagramChannel,socketAddress);
|
||||
|
||||
// 发送数据包
|
||||
LpPacket lpPacket=new LpPacket();
|
||||
lpPacket.setId(1);
|
||||
lpPacket.setFragmentNum(1);
|
||||
lpPacket.setFragmentSeq(0);
|
||||
lpPacket.setValue(new byte[]{4,5,6,7});
|
||||
udpTransport.send(lpPacket);
|
||||
System.out.println("send: "+
|
||||
Arrays.toString(new SelfEncodingBase().selfWireEncode(lpPacket).getRaw()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试结果:
|
||||
* 2021-05-07 10:35:32.625 DEBUG [min-dev-java] - start write to udp : localhost/127.0.0.1:50000
|
||||
* send: [-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]
|
||||
* 2021-05-07 10:35:32.641 DEBUG [min-dev-java] - recv from udp: localhost/127.0.0.1:50000
|
||||
* receive: [-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]
|
||||
* @throws IOException
|
||||
* @throws LogicFaceException
|
||||
* @throws EncoderException
|
||||
* @throws BlockException
|
||||
* @throws MgmtException
|
||||
* @throws ComponentException
|
||||
* @throws PacketException
|
||||
*/
|
||||
@Test
|
||||
public void testReceive() throws IOException, LogicFaceException, EncoderException, BlockException, MgmtException, ComponentException, PacketException {
|
||||
// 初始化一个udp连接
|
||||
UdpTransport udpTransport=new UdpTransport();
|
||||
SocketAddress socketAddress=new InetSocketAddress("localhost",50000);
|
||||
DatagramChannel datagramChannel=DatagramChannel.open();
|
||||
udpTransport.init(datagramChannel,socketAddress);
|
||||
|
||||
// 发送数据包
|
||||
LpPacket lpPacket=new LpPacket();
|
||||
lpPacket.setId(1);
|
||||
lpPacket.setFragmentNum(1);
|
||||
lpPacket.setFragmentSeq(0);
|
||||
lpPacket.setValue(new byte[]{4,5,6,7});
|
||||
udpTransport.send(lpPacket);
|
||||
System.out.println("send: "+
|
||||
Arrays.toString(new SelfEncodingBase().selfWireEncode(lpPacket).getRaw()));
|
||||
|
||||
// 接收数据包
|
||||
LpPacket newLp=udpTransport.receive();
|
||||
System.out.println("receive: "+
|
||||
Arrays.toString(new SelfEncodingBase().selfWireEncode(newLp).getRaw()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package mgmt;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 21:14 2021/5/6
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class CommandExecutorTest {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package mgmt;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 21:13 2021/5/6
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class RegisterPrefixHelperTest {
|
||||
@Test
|
||||
public void registerPrefixTest(){
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user