jmeter-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [jmeter] DDDHuang commented on a change in pull request #637: add: tcp record proxy
Date Wed, 16 Dec 2020 07:36:42 GMT

DDDHuang commented on a change in pull request #637:
URL: https://github.com/apache/jmeter/pull/637#discussion_r544075044



##########
File path: src/protocol/tcp/src/main/java/org/apache/jmeter/protocol/tcp/proxy/TCPSamplerManager.java
##########
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jmeter.protocol.tcp.proxy;
+
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+import org.apache.jmeter.exceptions.IllegalUserActionException;
+import org.apache.jmeter.gui.GuiPackage;
+import org.apache.jmeter.gui.tree.JMeterTreeNode;
+import org.apache.jmeter.protocol.tcp.control.gui.TCPSamplerGui;
+import org.apache.jmeter.protocol.tcp.proxy.gui.TCPProxyDef;
+import org.apache.jmeter.protocol.tcp.sampler.BinaryTCPClientImpl;
+import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.testelement.TestElement;
+import org.apache.jorphan.util.JOrphanUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TCPSamplerManager extends Thread {
+    private static final Logger log = LoggerFactory.getLogger(TCPSamplerManager.class);
+
+    private final JMeterTreeNode targetNode;
+    private final TestElement tcpSamplerElement;
+    private boolean workStatus = false;
+
+    private final Queue<TCPSampler> tcpSamplerInfoQueue = new ArrayDeque<>();
+
+    public TCPSamplerManager(TestElement tcpSamplerElement, JMeterTreeNode targetNode) {
+        this.tcpSamplerElement = tcpSamplerElement;
+        this.targetNode = targetNode;
+    }
+
+    /**
+     * create a new TCPSampler and put into TCPSampler queue.
+     * manager will store all TCPSampler to target JmeterTreeNode.
+     *
+     * @param data tcp payload
+     * @return target server response payload
+     */
+    public byte[] newTCPSampler(String data) {
+        log.debug("new sampler , payload " + data);
+        TCPSampler tcpSampler = new TCPSampler();
+        TestElement element = (TestElement) tcpSamplerElement.clone();
+        element.removeProperty(TCPSampler.PROXY_SERVER_PORT);
+        if (useHexPayload(element)) {
+            data = JOrphanUtils.baToHexString(data.getBytes());
+        }
+        element.setProperty(TCPSampler.REQUEST, data);
+        element.setProperty(TestElement.GUI_CLASS, TCPSamplerGui.class.getName());
+        element.setName("TCP Sampler");
+        tcpSampler.addTestElement(element);
+        tcpSamplerInfoQueue.add(tcpSampler);
+        SampleResult tcpSampleResult = tcpSampler.sample();
+        if (useHexPayload(element)) {
+            return BinaryTCPClientImpl.hexStringToByteArray(new String(tcpSampleResult.getResponseData()));
+        }
+        return tcpSampleResult.getResponseData();
+    }
+
+    private boolean useHexPayload(TestElement element) {
+        String tcpSamplerClassPath = element.getPropertyAsString(TCPSampler.CLASSNAME);
+        TCPProxyDef samplerClass = TCPProxyDef.findByClassPath(tcpSamplerClassPath);
+        if (samplerClass != null) {
+            switch (samplerClass) {
+                case BinaryTCPClientImpl_class:
+                case LengthPrefixedBinaryTCPClientImpl_class:
+                    return true;
+                case TCPClientImpl_class:
+                default:
+                    return false;
+            }
+        }
+        return false;
+    }
+
+    public synchronized void managerStart() {
+        log.debug("start");
+        workStatus = true;
+        start();
+    }
+
+    /**
+     * manager will stop after samplerQueue is empty
+     */
+    public void managerStop() {
+        workStatus = false;
+    }
+
+    @Override
+    public void run() {
+        while (workStatus) {
+            try {
+                Thread.sleep(500);
+            } catch (InterruptedException e) {
+                e.printStackTrace();

Review comment:
       I have added the " Exception Log" in new commit. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message