rishabhdeepsingh
6/29/2018 - 11:41 AM

randomPort

diff --git a/OpenRobertaParent/OpenRobertaDesktopApp/src/main/java/de/fhg/iais/DesktopApp.java b/OpenRobertaParent/OpenRobertaDesktopApp/src/main/java/de/fhg/iais/DesktopApp.java
index 3a18834..08a8660 100644
--- a/OpenRobertaParent/OpenRobertaDesktopApp/src/main/java/de/fhg/iais/DesktopApp.java
+++ b/OpenRobertaParent/OpenRobertaDesktopApp/src/main/java/de/fhg/iais/DesktopApp.java
@@ -3,7 +3,11 @@ package de.fhg.iais;
 import java.awt.*;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
 import java.util.List;
+import java.util.Random;
 
 import javax.swing.*;
 
@@ -20,6 +24,14 @@ import joptsimple.OptionSpec;
 
 public class DesktopApp {
     public static void main(String[] args) throws Exception {
+        ServerSocket serverSocket = new ServerSocket(0);
+        serverSocket.setReuseAddress(true);
+        Random random = new Random();
+        int po = random.nextInt(10000);
+        while ( !available(po) ) {
+            po = random.nextInt(10000);
+        }
+        String port = String.valueOf(po);
         String[] strings =
             new String[] {
                 "-d",
@@ -27,7 +39,9 @@ public class DesktopApp {
                 "-d",
                 "database.parentdir=../OpenRobertaServer",
                 "-d",
-                "server.staticresources.dir=../OpenRobertaServer/staticResources"
+                "server.staticresources.dir=../OpenRobertaServer/staticResources",
+                "-d",
+                "server.port=" + port,
             };
         OptionParser parser = new OptionParser();
         OptionSpec<String> defineOpt = parser.accepts("d").withRequiredArg().ofType(String.class);
@@ -42,7 +56,7 @@ public class DesktopApp {
         pandomium.initialize();
 
         PandomiumClient client = pandomium.createClient();
-        PandomiumBrowser browser = client.loadURL("localhost:1999");
+        PandomiumBrowser browser = client.loadURL("http://0.0.0.0:" + port);
 
         JFrame frame = new JFrame();
         frame.getContentPane().add(browser.toAWTComponent(), BorderLayout.CENTER);
@@ -63,4 +77,27 @@ public class DesktopApp {
         server.join();
         System.exit(0);
     }
+
+    private static boolean available(int port) {
+        System.out.println("--------------Testing port " + port);
+        Socket s = null;
+        try {
+            s = new Socket("localhost", port);
+            // If the code makes it this far without an exception it means
+            // something is using the port and has responded.
+            System.out.println("--------------Port " + port + " is not available");
+            return false;
+        } catch ( IOException e ) {
+            System.out.println("--------------Port " + port + " is available");
+            return true;
+        } finally {
+            if ( s != null ) {
+                try {
+                    s.close();
+                } catch ( IOException e ) {
+                    throw new RuntimeException("You should handle this error.", e);
+                }
+            }
+        }
+    }
 }
\ No newline at end of file