cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alsoro...@apache.org
Subject cordova-paramedic git commit: Don't retry connection, just take a random port from increased range
Date Thu, 09 Feb 2017 13:10:53 GMT
Repository: cordova-paramedic
Updated Branches:
  refs/heads/master 1ed075d57 -> 053b3576b


Don't retry connection, just take a random port from increased range


Project: http://git-wip-us.apache.org/repos/asf/cordova-paramedic/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-paramedic/commit/053b3576
Tree: http://git-wip-us.apache.org/repos/asf/cordova-paramedic/tree/053b3576
Diff: http://git-wip-us.apache.org/repos/asf/cordova-paramedic/diff/053b3576

Branch: refs/heads/master
Commit: 053b3576b6d15ebcd4bf9f661ce8b09cb5206c8f
Parents: 1ed075d
Author: Alexander Sorokin <alexander.sorokin@akvelon.com>
Authored: Thu Feb 9 16:10:44 2017 +0300
Committer: Alexander Sorokin <alexander.sorokin@akvelon.com>
Committed: Thu Feb 9 16:10:44 2017 +0300

----------------------------------------------------------------------
 lib/LocalServer.js     | 42 +++++++++++++++---------------------------
 lib/ParamedicConfig.js |  4 ++--
 2 files changed, 17 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-paramedic/blob/053b3576/lib/LocalServer.js
----------------------------------------------------------------------
diff --git a/lib/LocalServer.js b/lib/LocalServer.js
index 79602d6..4c5fbc1 100644
--- a/lib/LocalServer.js
+++ b/lib/LocalServer.js
@@ -42,40 +42,27 @@ function LocalServer(port, externalServerUrl) {
 
 util.inherits(LocalServer, EventEmitter);
 
+function getRandomInt(min, max) {
+    return Math.floor(Math.random() * (max - min)) + min;
+}
+
 LocalServer.startServer = function (ports, externalServerUrl, useTunnel) {
     logger.normal("local-server: scanning ports from " + ports.start + " to " + ports.end);
-    var startServerOnPort = function (port) {
-        var d = Q.defer();
 
-        logger.normal("local-server: port " + port + " is available");
-        logger.info("local-server: starting local medic server");
+    return LocalServer.getAvailablePort(ports.start, ports.end)
+        .then(function (port) {
+            logger.normal("local-server: port " + port + " is available");
+            logger.info("local-server: starting local medic server");
 
-        try {
             var localServer = new LocalServer(port, externalServerUrl);
             localServer.createSocketListener();
 
             if (useTunnel) {
                 return localServer.createTunnel();
             }
-        } catch (e) {
-            d.reject();
-        }
-
-        d.resolve(localServer);
 
-        return d.promise;
-    };
-
-    return LocalServer.getFirstAvailablePort(ports.start, ports.end)
-    .then(startServerOnPort)
-    .fail(function () {
-        if (ports.end <= ports.start) {
-            throw new Error('LocalServer couldn\'t find a free port');
-        }
-        // sometimes the port gets busy real quick, let's retry
-        return LocalServer.getFirstAvailablePort(ports.start + 1, ports.end)
-        .then(startServerOnPort);
-    });
+            return localServer;
+        });
 };
 
 LocalServer.prototype.cleanUp = function () {
@@ -122,13 +109,14 @@ LocalServer.prototype.startFileTransferServer = function (tempPath)
{
     });
 };
 
-LocalServer.getFirstAvailablePort = function (startPort, endPort) {
-    return portChecker.check(startPort).then(function (isInUse) {
+LocalServer.getAvailablePort = function (startPort, endPort) {
+    var port = getRandomInt(startPort, endPort);
+    return portChecker.check(port).then(function (isInUse) {
         if (!isInUse) {
-            return startPort;
+            return port;
         }
         if (startPort < endPort) {
-            return LocalServer.getFirstAvailablePort(startPort + 1, endPort);
+            return LocalServer.getAvailablePort(startPort, endPort);
         }
         throw new Error('Unable to find available port');
     });

http://git-wip-us.apache.org/repos/asf/cordova-paramedic/blob/053b3576/lib/ParamedicConfig.js
----------------------------------------------------------------------
diff --git a/lib/ParamedicConfig.js b/lib/ParamedicConfig.js
index 98546a7..65b186d 100644
--- a/lib/ParamedicConfig.js
+++ b/lib/ParamedicConfig.js
@@ -17,8 +17,8 @@
     under the License.
 */
 
-var DEFAULT_START_PORT = 8008;
-var DEFAULT_END_PORT   = 8018;
+var DEFAULT_START_PORT = 7008;
+var DEFAULT_END_PORT   = 7208;
 var DEFAULT_TIMEOUT    = 60 * 60 * 1000; // 60 minutes in msec - this will become a param
 var DEFAULT_SAUCE_DEVICE_NAME_ANDROID      = 'Android Emulator';
 var DEFAULT_SAUCE_PLATFORM_VERSION_ANDROID = '4.4';


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message