From eed9a7bc2cda5170cc1479da4e1efc0ba393e208 Mon Sep 17 00:00:00 2001
From: Guoqs <123@jsowell.com>
Date: Wed, 31 Jul 2024 16:48:29 +0800
Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E8=8E=B7=E5=8F=96=E5=93=8D?=
=?UTF-8?q?=E5=BA=94=E6=95=B0=E6=8D=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
jsowell-admin/pom.xml | 21 +++++
.../test/java/SpringBootTestController.java | 2 -
.../test/java/demo/ChargingPileClient.java | 80 -------------------
.../java/demo/ChargingPileController.java | 34 --------
.../test/java/demo/ChargingPileServer.java | 50 ------------
.../java/demo/ChargingPileServerHandler.java | 31 -------
.../src/test/java/demo/ProtocolUtil.java | 77 ------------------
.../java/rpc/ClientChannelInitializer.java | 19 +++++
jsowell-admin/src/test/java/rpc/Message.java | 11 +++
.../src/test/java/rpc/MessageConstant.java | 22 +++++
.../src/test/java/rpc/MessageDecode.java | 44 ++++++++++
.../src/test/java/rpc/MessageEncode.java | 22 +++++
.../src/test/java/rpc/RpcClient.java | 52 ++++++++++++
.../src/test/java/rpc/RpcRequest.java | 21 +++++
.../src/test/java/rpc/RpcRequestHandler.java | 36 +++++++++
.../src/test/java/rpc/RpcResponse.java | 18 +++++
.../src/test/java/rpc/RpcResponseHandler.java | 23 ++++++
.../src/test/java/rpc/RpcServer.java | 55 +++++++++++++
jsowell-admin/src/test/java/rpc/RpcUtil.java | 63 +++++++++++++++
.../src/test/java/rpc/SerializationUtil.java | 53 ++++++++++++
.../java/rpc/ServerChannelInitializer.java | 19 +++++
.../src/test/java/rpc/SyncPromise.java | 49 ++++++++++++
.../src/test/java/rpc/TestRpcClient.java | 48 +++++++++++
.../src/test/java/rpc/TestRpcServer.java | 7 ++
.../yunkuaichong/NettyServerHandler.java | 9 ++-
25 files changed, 590 insertions(+), 276 deletions(-)
delete mode 100644 jsowell-admin/src/test/java/demo/ChargingPileClient.java
delete mode 100644 jsowell-admin/src/test/java/demo/ChargingPileController.java
delete mode 100644 jsowell-admin/src/test/java/demo/ChargingPileServer.java
delete mode 100644 jsowell-admin/src/test/java/demo/ChargingPileServerHandler.java
delete mode 100644 jsowell-admin/src/test/java/demo/ProtocolUtil.java
create mode 100644 jsowell-admin/src/test/java/rpc/ClientChannelInitializer.java
create mode 100644 jsowell-admin/src/test/java/rpc/Message.java
create mode 100644 jsowell-admin/src/test/java/rpc/MessageConstant.java
create mode 100644 jsowell-admin/src/test/java/rpc/MessageDecode.java
create mode 100644 jsowell-admin/src/test/java/rpc/MessageEncode.java
create mode 100644 jsowell-admin/src/test/java/rpc/RpcClient.java
create mode 100644 jsowell-admin/src/test/java/rpc/RpcRequest.java
create mode 100644 jsowell-admin/src/test/java/rpc/RpcRequestHandler.java
create mode 100644 jsowell-admin/src/test/java/rpc/RpcResponse.java
create mode 100644 jsowell-admin/src/test/java/rpc/RpcResponseHandler.java
create mode 100644 jsowell-admin/src/test/java/rpc/RpcServer.java
create mode 100644 jsowell-admin/src/test/java/rpc/RpcUtil.java
create mode 100644 jsowell-admin/src/test/java/rpc/SerializationUtil.java
create mode 100644 jsowell-admin/src/test/java/rpc/ServerChannelInitializer.java
create mode 100644 jsowell-admin/src/test/java/rpc/SyncPromise.java
create mode 100644 jsowell-admin/src/test/java/rpc/TestRpcClient.java
create mode 100644 jsowell-admin/src/test/java/rpc/TestRpcServer.java
diff --git a/jsowell-admin/pom.xml b/jsowell-admin/pom.xml
index 8cf2bb63c..858c2d94b 100644
--- a/jsowell-admin/pom.xml
+++ b/jsowell-admin/pom.xml
@@ -107,6 +107,27 @@
jsowell-thirdparty
+
+ org.projectlombok
+ lombok
+ test
+
+
+
+ io.protostuff
+ protostuff-core
+ 1.8.0
+ test
+
+
+
+ io.protostuff
+ protostuff-runtime
+ 1.8.0
+ test
+
+
+
diff --git a/jsowell-admin/src/test/java/SpringBootTestController.java b/jsowell-admin/src/test/java/SpringBootTestController.java
index f00bcc8e1..cad30e446 100644
--- a/jsowell-admin/src/test/java/SpringBootTestController.java
+++ b/jsowell-admin/src/test/java/SpringBootTestController.java
@@ -85,8 +85,6 @@ import com.jsowell.wxpay.common.WeChatPayParameter;
import com.jsowell.wxpay.dto.AppletTemplateMessageSendDTO;
import com.jsowell.wxpay.response.WechatPayRefundRequest;
import com.jsowell.wxpay.service.WxAppletRemoteService;
-import demo.ChargingPileServerHandler;
-import demo.ProtocolUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
diff --git a/jsowell-admin/src/test/java/demo/ChargingPileClient.java b/jsowell-admin/src/test/java/demo/ChargingPileClient.java
deleted file mode 100644
index 29f2ee4c2..000000000
--- a/jsowell-admin/src/test/java/demo/ChargingPileClient.java
+++ /dev/null
@@ -1,80 +0,0 @@
-package demo;
-
-import io.netty.bootstrap.Bootstrap;
-import io.netty.channel.*;
-import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.SocketChannel;
-import io.netty.channel.socket.nio.NioSocketChannel;
-import io.netty.handler.codec.bytes.ByteArrayDecoder;
-import io.netty.handler.codec.bytes.ByteArrayEncoder;
-
-public class ChargingPileClient {
- private final String host;
- private final int port;
-
- public ChargingPileClient(String host, int port) {
- this.host = host;
- this.port = port;
- }
-
- public void start() throws Exception {
- EventLoopGroup group = new NioEventLoopGroup();
- try {
- Bootstrap b = new Bootstrap();
- b.group(group)
- .channel(NioSocketChannel.class)
- .handler(new ChannelInitializer() {
- @Override
- public void initChannel(SocketChannel ch) throws Exception {
- ChannelPipeline pipeline = ch.pipeline();
- pipeline.addLast(new ByteArrayDecoder());
- pipeline.addLast(new ByteArrayEncoder());
- pipeline.addLast(new ChargingPileClientHandler());
- }
- });
-
- ChannelFuture f = b.connect(host, port).sync();
- System.out.println("充电桩客户端已连接到服务器");
- f.channel().closeFuture().sync();
- } finally {
- group.shutdownGracefully();
- }
- }
-
- public static void main(String[] args) throws Exception {
- String host = "localhost";
- int port = 9011;
- new ChargingPileClient(host, port).start();
- }
-}
-
-class ChargingPileClientHandler extends ChannelInboundHandlerAdapter {
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) {
- byte[] command = (byte[]) msg;
- System.out.println("收到服务器指令: " + ProtocolUtil.bytesToHex(command));
-
- // 处理工作参数设置指令
- if (command[5] == 0x52) {
- String pileId = ProtocolUtil.bytesToHex(command).substring(12, 26);
- boolean allowWork = (command[19] == 0x00);
- int maxPower = command[20] & 0xFF;
-
- System.out.println("收到工作参数设置:");
- System.out.println("桩编号: " + pileId);
- System.out.println("是否允许工作: " + (allowWork ? "允许" : "不允许"));
- System.out.println("最大允许输出功率: " + maxPower + "%");
-
- // 模拟设置成功
- byte[] response = ProtocolUtil.createSetWorkParamsResponseFrame(pileId, true);
- ctx.writeAndFlush(response);
- System.out.println("发送响应: " + ProtocolUtil.bytesToHex(response));
- }
- }
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
- cause.printStackTrace();
- ctx.close();
- }
-}
\ No newline at end of file
diff --git a/jsowell-admin/src/test/java/demo/ChargingPileController.java b/jsowell-admin/src/test/java/demo/ChargingPileController.java
deleted file mode 100644
index f29477b15..000000000
--- a/jsowell-admin/src/test/java/demo/ChargingPileController.java
+++ /dev/null
@@ -1,34 +0,0 @@
-package demo;
-
-import org.springframework.web.bind.annotation.*;
-import io.netty.channel.ChannelHandlerContext;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.TimeUnit;
-
-@RestController
-@RequestMapping("/api/charging")
-public class ChargingPileController {
-
- private final ChannelHandlerContext chargingPileChannel;
-
- public ChargingPileController(ChannelHandlerContext chargingPileChannel) {
- this.chargingPileChannel = chargingPileChannel;
- }
-
- @PostMapping("/setWorkParams")
- public String setWorkParams(@RequestParam String pileId,
- @RequestParam boolean allowWork,
- @RequestParam int maxPower) {
- try {
- byte[] command = ProtocolUtil.createSetWorkParamsFrame(pileId, allowWork, maxPower);
- CompletableFuture future = ChargingPileServerHandler.sendCommand(chargingPileChannel, command);
- byte[] response = future.get(5, TimeUnit.SECONDS);
-
- // 解析响应
- boolean success = (response[18] == 0x01);
- return "设置" + (success ? "成功" : "失败");
- } catch (Exception e) {
- return "发送指令失败: " + e.getMessage();
- }
- }
-}
\ No newline at end of file
diff --git a/jsowell-admin/src/test/java/demo/ChargingPileServer.java b/jsowell-admin/src/test/java/demo/ChargingPileServer.java
deleted file mode 100644
index 0ddc9d1b1..000000000
--- a/jsowell-admin/src/test/java/demo/ChargingPileServer.java
+++ /dev/null
@@ -1,50 +0,0 @@
-package demo;
-
-import io.netty.bootstrap.ServerBootstrap;
-import io.netty.channel.*;
-import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.SocketChannel;
-import io.netty.channel.socket.nio.NioServerSocketChannel;
-import io.netty.handler.codec.bytes.ByteArrayDecoder;
-import io.netty.handler.codec.bytes.ByteArrayEncoder;
-
-public class ChargingPileServer {
- private final int port;
-
- public ChargingPileServer(int port) {
- this.port = port;
- }
-
- public void start() throws Exception {
- EventLoopGroup bossGroup = new NioEventLoopGroup(1);
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try {
- ServerBootstrap b = new ServerBootstrap();
- b.group(bossGroup, workerGroup)
- .channel(NioServerSocketChannel.class)
- .childHandler(new ChannelInitializer() {
- @Override
- public void initChannel(SocketChannel ch) throws Exception {
- ChannelPipeline pipeline = ch.pipeline();
- pipeline.addLast(new ByteArrayDecoder());
- pipeline.addLast(new ByteArrayEncoder());
- pipeline.addLast(new ChargingPileServerHandler());
- }
- })
- .option(ChannelOption.SO_BACKLOG, 128)
- .childOption(ChannelOption.SO_KEEPALIVE, true);
-
- ChannelFuture f = b.bind(port).sync();
- System.out.println("充电桩服务器启动,监听端口: " + port);
- f.channel().closeFuture().sync();
- } finally {
- workerGroup.shutdownGracefully();
- bossGroup.shutdownGracefully();
- }
- }
-
- public static void main(String[] args) throws Exception {
- int port = 9011;
- new ChargingPileServer(port).start();
- }
-}
\ No newline at end of file
diff --git a/jsowell-admin/src/test/java/demo/ChargingPileServerHandler.java b/jsowell-admin/src/test/java/demo/ChargingPileServerHandler.java
deleted file mode 100644
index 4981ca69d..000000000
--- a/jsowell-admin/src/test/java/demo/ChargingPileServerHandler.java
+++ /dev/null
@@ -1,31 +0,0 @@
-package demo;
-
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.channel.ChannelInboundHandlerAdapter;
-import java.util.concurrent.CompletableFuture;
-
-public class ChargingPileServerHandler extends ChannelInboundHandlerAdapter {
- private static CompletableFuture responseFuture;
-
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) {
- byte[] response = (byte[]) msg;
- System.out.println("接收到充电桩响应: " + ProtocolUtil.bytesToHex(response));
- if (responseFuture != null) {
- responseFuture.complete(response);
- }
- }
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
- cause.printStackTrace();
- ctx.close();
- }
-
- public static CompletableFuture sendCommand(ChannelHandlerContext ctx, byte[] command) {
- responseFuture = new CompletableFuture<>();
- ctx.writeAndFlush(command);
- System.out.println("发送指令到充电桩: " + ProtocolUtil.bytesToHex(command));
- return responseFuture;
- }
-}
\ No newline at end of file
diff --git a/jsowell-admin/src/test/java/demo/ProtocolUtil.java b/jsowell-admin/src/test/java/demo/ProtocolUtil.java
deleted file mode 100644
index ba1774c8c..000000000
--- a/jsowell-admin/src/test/java/demo/ProtocolUtil.java
+++ /dev/null
@@ -1,77 +0,0 @@
-package demo;
-
-import java.nio.ByteBuffer;
-
-public class ProtocolUtil {
-
- public static byte[] createSetWorkParamsFrame(String pileId, boolean allowWork, int maxPower) {
- ByteBuffer buffer = ByteBuffer.allocate(21);
- buffer.put((byte) 0x68); // 起始标志
- buffer.put((byte) 0x0D); // 数据长度
- buffer.putShort((short) 0x0008); // 序列号域
- buffer.put((byte) 0x00); // 加密标志
- buffer.put((byte) 0x52); // 帧类型码
-
- // 桩编码
- buffer.put(hexStringToByteArray(pileId));
-
- // 是否允许工作
- buffer.put((byte) (allowWork ? 0x00 : 0x01));
-
- // 充电桩最大允许输出功率
- buffer.put((byte) maxPower);
-
- // 计算校验和
- short checksum = calculateChecksum(buffer.array(), 1, 19);
- buffer.putShort(checksum);
-
- return buffer.array();
- }
-
- public static byte[] createSetWorkParamsResponseFrame(String pileId, boolean success) {
- ByteBuffer buffer = ByteBuffer.allocate(20);
- buffer.put((byte) 0x68); // 起始标志
- buffer.put((byte) 0x0C); // 数据长度
- buffer.putShort((short) 0x0008); // 序列号域
- buffer.put((byte) 0x00); // 加密标志
- buffer.put((byte) 0x51); // 帧类型码
-
- // 桩编码
- buffer.put(hexStringToByteArray(pileId));
-
- // 设置结果
- buffer.put((byte) (success ? 0x01 : 0x00));
-
- // 计算校验和
- short checksum = calculateChecksum(buffer.array(), 1, 18);
- buffer.putShort(checksum);
-
- return buffer.array();
- }
-
- public static short calculateChecksum(byte[] data, int offset, int length) {
- int sum = 0;
- for (int i = offset; i < offset + length; i++) {
- sum += (data[i] & 0xFF);
- }
- return (short) (sum & 0xFFFF);
- }
-
- public static byte[] hexStringToByteArray(String s) {
- int len = s.length();
- byte[] data = new byte[len / 2];
- for (int i = 0; i < len; i += 2) {
- data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
- + Character.digit(s.charAt(i+1), 16));
- }
- return data;
- }
-
- public static String bytesToHex(byte[] bytes) {
- StringBuilder sb = new StringBuilder();
- for (byte b : bytes) {
- sb.append(String.format("%02X ", b));
- }
- return sb.toString().trim();
- }
-}
\ No newline at end of file
diff --git a/jsowell-admin/src/test/java/rpc/ClientChannelInitializer.java b/jsowell-admin/src/test/java/rpc/ClientChannelInitializer.java
new file mode 100644
index 000000000..d086c5383
--- /dev/null
+++ b/jsowell-admin/src/test/java/rpc/ClientChannelInitializer.java
@@ -0,0 +1,19 @@
+package rpc;
+
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.socket.SocketChannel;
+
+public class ClientChannelInitializer extends ChannelInitializer {
+
+ @Override
+ protected void initChannel(SocketChannel socketChannel) throws Exception {
+ ChannelPipeline pipeline = socketChannel.pipeline();
+
+ pipeline.addLast(new MessageEncode());
+ pipeline.addLast(new MessageDecode());
+
+ pipeline.addLast(new RpcResponseHandler());
+ }
+
+}
\ No newline at end of file
diff --git a/jsowell-admin/src/test/java/rpc/Message.java b/jsowell-admin/src/test/java/rpc/Message.java
new file mode 100644
index 000000000..3323b79ea
--- /dev/null
+++ b/jsowell-admin/src/test/java/rpc/Message.java
@@ -0,0 +1,11 @@
+package rpc;
+
+import lombok.Data;
+
+@Data
+public abstract class Message {
+
+ protected Byte messageType;
+
+
+}
\ No newline at end of file
diff --git a/jsowell-admin/src/test/java/rpc/MessageConstant.java b/jsowell-admin/src/test/java/rpc/MessageConstant.java
new file mode 100644
index 000000000..f3f78453b
--- /dev/null
+++ b/jsowell-admin/src/test/java/rpc/MessageConstant.java
@@ -0,0 +1,22 @@
+package rpc;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class MessageConstant {
+
+ public final static Byte rpcRequest = 1;
+ public final static Byte rpcResponse = 2;
+
+ public static Map> messageTypeMap = new ConcurrentHashMap<>();
+
+ static {
+ messageTypeMap.put(rpcRequest, RpcRequest.class);
+ messageTypeMap.put(rpcResponse, RpcResponse.class);
+ }
+
+ public static Class extends Message> getMessageClass(Byte messageType){
+ return messageTypeMap.get(messageType);
+ }
+
+}
\ No newline at end of file
diff --git a/jsowell-admin/src/test/java/rpc/MessageDecode.java b/jsowell-admin/src/test/java/rpc/MessageDecode.java
new file mode 100644
index 000000000..029a74178
--- /dev/null
+++ b/jsowell-admin/src/test/java/rpc/MessageDecode.java
@@ -0,0 +1,44 @@
+package rpc;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+
+import java.util.List;
+
+public class MessageDecode extends ByteToMessageDecoder {
+
+
+ @Override
+ protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List