jakarta-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1067155 - in /jakarta/jmeter/trunk: bin/jmeter.properties src/core/org/apache/jmeter/JMeter.java
Date Fri, 04 Feb 2011 11:33:03 GMT
Author: sebb
Date: Fri Feb  4 11:33:03 2011
New Revision: 1067155

URL: http://svn.apache.org/viewvc?rev=1067155&view=rev
Log:
Allow for multiple local UDP ports

Modified:
    jakarta/jmeter/trunk/bin/jmeter.properties
    jakarta/jmeter/trunk/src/core/org/apache/jmeter/JMeter.java

Modified: jakarta/jmeter/trunk/bin/jmeter.properties
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/bin/jmeter.properties?rev=1067155&r1=1067154&r2=1067155&view=diff
==============================================================================
--- jakarta/jmeter/trunk/bin/jmeter.properties (original)
+++ jakarta/jmeter/trunk/bin/jmeter.properties Fri Feb  4 11:33:03 2011
@@ -729,6 +729,10 @@ beanshell.server.file=../extras/startup.
 # If running non-GUI, then JMeter listens on the following port for a shutdown message.
 # To disable, set the port to 0.
 #jmeterengine.nongui.port=4445
+#
+# If the initial port is busy, keep trying until this port is reached
+# (to disable searching, set the value less than or equal to the .port property)
+#jmeterengine.nongui.portmax=4455
 
 #Should JMeter expand the tree when loading a test plan?
 #onload.expandtree=true

Modified: jakarta/jmeter/trunk/src/core/org/apache/jmeter/JMeter.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/core/org/apache/jmeter/JMeter.java?rev=1067155&r1=1067154&r2=1067155&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/core/org/apache/jmeter/JMeter.java (original)
+++ jakarta/jmeter/trunk/src/core/org/apache/jmeter/JMeter.java Fri Feb  4 11:33:03 2011
@@ -28,6 +28,7 @@ import java.net.DatagramPacket;
 import java.net.DatagramSocket;
 import java.net.InetAddress;
 import java.net.MalformedURLException;
+import java.net.SocketException;
 import java.text.SimpleDateFormat;
 import java.util.Collection;
 import java.util.Date;
@@ -132,7 +133,10 @@ public class JMeter implements JMeterPlu
 
 
     /** UDP port used in non-GUI runs. Disabled if <=1000. */
-    private static final int UDP_PORT = JMeterUtils.getPropDefault("jmeterengine.nongui.port",
4445);
+    private static final int UDP_PORT = JMeterUtils.getPropDefault("jmeterengine.nongui.port",
4445); // $NON-NLS-1$
+
+    /** Maximum UDP port used in non-GUI runs. Disabled if <= UDP_PORT */
+    private static final int UDP_PORT_MAX = JMeterUtils.getPropDefault("jmeterengine.nongui.maxport",
4455); // $NON-NLS-1$
 
 
 
@@ -1030,26 +1034,29 @@ public class JMeter implements JMeterPlu
         log.info(prop+separator+System.getProperty(prop));//$NON-NLS-1$
     }
 
-    private void startUdpDdaemon(final JMeterEngine engine) {
+    private static void startUdpDdaemon(final JMeterEngine engine) {
         if (UDP_PORT > 1000){
-            Thread waiter = new Thread(){
-                @Override
-                public void run() {
-                    waitForSignals(engine);
-                }
-            };
-            waiter.setDaemon(true);
-            waiter.start();
+            final DatagramSocket socket = getSocket(UDP_PORT, UDP_PORT_MAX);
+            if (socket != null) {
+                Thread waiter = new Thread(){
+                    @Override
+                    public void run() {
+                        waitForSignals(engine, socket);
+                    }
+                };
+                waiter.setDaemon(true);
+                waiter.start();
+            } else {
+                System.out.println("Failed to create UDP port");
+            }
         }
     }
 
-    private void waitForSignals(final JMeterEngine engine) {
+    private static void waitForSignals(final JMeterEngine engine, DatagramSocket socket)
{
         byte[] buf = new byte[80];
-        DatagramSocket socket = null;
-        System.out.println("Waiting for possible shutdown message on port "+UDP_PORT);
+        System.out.println("Waiting for possible shutdown message on port "+socket.getLocalPort());
+        DatagramPacket request = new DatagramPacket(buf, buf.length);
         try {
-            socket = new DatagramSocket(UDP_PORT);
-            DatagramPacket request = new DatagramPacket(buf, buf.length);
             while(true) {
                 socket.receive(request);
                 InetAddress address = request.getAddress();
@@ -1067,12 +1074,24 @@ public class JMeter implements JMeterPlu
                 }
             }
         } catch (Exception e) {
-            e.printStackTrace();
+            System.out.println(e);
         } finally {
-            if (socket != null) {
-                socket.close();
-            }
+            socket.close();
         }
+    }
+
+    private static DatagramSocket getSocket(int udpPort, int udpPortMax) {
+        DatagramSocket socket = null;
+        int i = udpPort;
+        do {
+            try {
+                socket = new DatagramSocket(i);
+                break;
+            } catch (SocketException e) {
+                // ignored
+            }            
+        } while (++i <= udpPortMax);
 
+        return socket;
     }
 }
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@jakarta.apache.org
For additional commands, e-mail: notifications-help@jakarta.apache.org


Mime
View raw message