From 0fd091c878d2f75c871a83ce1d6dc74a48f4009e Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 18 Sep 2023 18:27:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dtcp=20mock=E8=AF=B7=E6=B1=82=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E8=B6=85=E8=BF=871024=E4=B8=AA=E5=AD=97=E8=8A=82?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.tapd.cn/55049933/bugtrace/bugs/view?bug_id=1155049933001030652&from=wxnotification&corpid=ww918354e3468dc0cc&agentid=1000014&jump_count=1 Signed-off-by: fit2-zhao --- .../java/io/metersphere/api/tcp/TCPPool.java | 44 ++--------- .../metersphere/api/tcp/server/TCPServer.java | 25 +++---- .../api/tcp/server/TCPService.java | 73 ++++++++++--------- .../service/MockConfigService.java | 2 +- 4 files changed, 55 insertions(+), 89 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java b/api-test/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java index 9598aba619..8f2b30d1e7 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java +++ b/api-test/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java @@ -3,10 +3,8 @@ package io.metersphere.api.tcp; import io.metersphere.api.tcp.server.TCPServer; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.LogUtil; -import org.apache.commons.lang3.StringUtils; import java.util.HashMap; -import java.util.Map; /** * @author song.tianyang @@ -14,15 +12,14 @@ import java.util.Map; */ public class TCPPool { - private static HashMap serverSockedMap = new HashMap<>(); + private static final HashMap serverSockedMap = new HashMap<>(); private TCPPool() { } - public static String createTcp(int port) { - String returnString = StringUtils.EMPTY; + public static void createTcp(int port) { if (port > 0) { - TCPServer tcpServer = null; + TCPServer tcpServer; if (serverSockedMap.containsKey(port)) { tcpServer = serverSockedMap.get(port); } else { @@ -34,15 +31,12 @@ public class TCPPool { Thread t = new Thread(tcpServer); t.start(); } - returnString = "OK"; } catch (Exception e) { - returnString = e.getMessage(); LogUtil.error(e); MSException.throwException(e.getMessage()); } } - return returnString; } public static boolean isTcpOpen(int port) { @@ -53,40 +47,14 @@ public class TCPPool { return false; } - public static String getTcpStatus() { - if (serverSockedMap.isEmpty()) { - return "null"; - } else { - StringBuffer stringBuffer = new StringBuffer(); - for (Map.Entry entry : serverSockedMap.entrySet()) { - int port = entry.getKey(); - TCPServer tcpServer = entry.getValue(); - if (tcpServer == null) { - stringBuffer.append("Port is " + port + ";"); - stringBuffer.append("Server is null;"); - } else { - stringBuffer.append("Port is " + port + ";"); - stringBuffer.append("Server is open: " + tcpServer.isSocketOpen() + ";"); - } - } - return stringBuffer.toString(); - } - } - - public static String closeTcp(int portNum) { + public static void closeTcp(int portNum) { TCPServer server = serverSockedMap.get(portNum); - if (server == null) { - return "Tcp Is not create!"; - } else { - String returnMsg = null; + if (server != null) { try { server.closeSocket(); - returnMsg = "OK"; } catch (Exception e) { - returnMsg = e.getMessage(); - LogUtil.error(e); + throw new RuntimeException(e); } - return returnMsg; } } } diff --git a/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPServer.java b/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPServer.java index decb8b534a..a5687c000c 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPServer.java +++ b/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPServer.java @@ -10,10 +10,10 @@ import java.net.Socket; * @Date 2021/8/11 10:35 上午 */ public class TCPServer implements Runnable { - private int port; + private final int port; private ServerSocket serverSocket; - private TCPService servicer; + private TCPService server; public TCPServer(int port) { this.port = port; @@ -22,30 +22,23 @@ public class TCPServer implements Runnable { public void openSocket() throws Exception { this.serverSocket = new ServerSocket(this.port); - while (true) { + do { if (!this.serverSocket.isClosed()) { Socket socket = this.serverSocket.accept(); - servicer = new TCPService(socket, port); - servicer.run(); + server = new TCPService(socket, port); + server.run(); } - if (this.serverSocket.isClosed()) { - break; - } - } + } while (!this.serverSocket.isClosed()); } public boolean isSocketOpen() { - if (this.serverSocket != null && !this.serverSocket.isClosed()) { - return true; - } else { - return false; - } + return this.serverSocket != null && !this.serverSocket.isClosed(); } public void closeSocket() throws Exception { if (this.serverSocket != null && !this.serverSocket.isClosed()) { - if (servicer != null) { - servicer.close(); + if (server != null) { + server.close(); } this.serverSocket.close(); } diff --git a/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java b/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java index 72cd497b4f..d18c793a5e 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/tcp/server/TCPService.java @@ -9,30 +9,51 @@ import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.mock.MockApiUtils; import io.metersphere.service.MockConfigService; import org.apache.commons.lang3.StringUtils; +import org.apache.jmeter.protocol.tcp.sampler.ReadException; import org.json.JSONObject; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.net.Socket; +import java.nio.charset.StandardCharsets; public class TCPService { - private Socket s; - private InputStream is; - private OutputStream os; - private int port; + private final Socket socket; + private final int port; - public TCPService(Socket s, int port) { - this.s = s; + public TCPService(Socket socket, int port) { + this.socket = socket; this.port = port; } - public void run() { - byte[] b = new byte[1024]; - try { - is = s.getInputStream(); - os = s.getOutputStream(); - int len = is.read(b); - String message = new String(b, 0, len); + public String read(InputStream is) throws ReadException { + try (ByteArrayOutputStream w = new ByteArrayOutputStream()) { + final int size = 1024; + byte[] buffer = new byte[size]; + while (true) { + try { + int x = is.read(buffer); + if (x < size) { + w.write(buffer, 0, x); + break; + } + w.write(buffer, 0, x); + } catch (IOException e) { + break; + } + } + return w.toString(StandardCharsets.UTF_8); + } catch (UnsupportedEncodingException e) { + throw new ReadException("Error decoding bytes from server with " + StandardCharsets.UTF_8 + ", bytes read: ", + e, ""); + } catch (IOException e) { + throw new ReadException("Error reading from server, bytes read: ", e, StringUtils.EMPTY); + } + } + + public void run() throws IOException { + try (InputStream is = socket.getInputStream(); + OutputStream os = socket.getOutputStream()) { + String message = this.read(is); TCPMockReturnDTO returnDTO = this.getReturnMsg(message); if (StringUtils.isNotEmpty(returnDTO.getEncode())) { @@ -106,25 +127,9 @@ public class TCPService { return returnDTO; } - public void close() { - //关闭资源 - try { - is.close(); - } catch (Exception e) { - LogUtil.error(e); - } finally { - try { - os.close(); - } catch (Exception e) { - LogUtil.error(e); - } finally { - try { - s.close(); - } catch (Exception e) { - LogUtil.error(e); - } - } + public void close() throws IOException { + if (socket != null) { + socket.close(); } } - } diff --git a/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java b/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java index 778985153e..5b9fb48b40 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java @@ -1084,7 +1084,7 @@ public class MockConfigService { builder.parse(new InputSource(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)))); isXml = true; } catch (Exception e) { - e.printStackTrace(); + LogUtil.error(e); } return isXml; }