Skip to content

Commit

Permalink
Addressing CR notes
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrebnov committed Apr 8, 2016
1 parent 7fa161c commit b46d3d8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
58 changes: 34 additions & 24 deletions lib/LocalServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ var Q = require('q'),
EventEmitter = require('events').EventEmitter,
localtunnel = require('localtunnel');


// how many ms without a pong packet to consider the connection closed
var CONNECTION_HEARBEAT_PING_TIMEOUT = 60000,
// how many ms before sending a new ping packet
CONNECTION_HEARBEAT_PING_INTERVAL = 25000;

function LocalServer(port, externalServerUrl) {
this.port = port;
this.externalServerUrl = externalServerUrl;
Expand All @@ -46,18 +52,15 @@ LocalServer.startServer = function(ports, externalServerUrl, useTunnel) {
localServer.createSocketListener();

if (useTunnel) {
return Q.promise(function(resolve) {
localServer.createTunnel().then(function(){
resolve(localServer);
});
});
return localServer.createTunnel();
}

return localServer;
});
};

LocalServer.getFirstAvailablePort = function(startPort, endPort) {
// returns array range [startPort..endPort]
var ports = Array.apply(null, Array(endPort - startPort + 1)).map(function(element, index) {
return startPort + index;
});
Expand Down Expand Up @@ -91,7 +94,7 @@ LocalServer.prototype.createTunnel = function() {
self.tunneledUrl = tunnel.url;
logger.info('cordova-paramedic: using tunneled url ' + self.tunneledUrl);

resolve();
resolve(self);
});

// this trace is useful to debug test run timeout issue
Expand All @@ -103,8 +106,8 @@ LocalServer.prototype.createTunnel = function() {

LocalServer.prototype.createSocketListener = function() {
var listener = io.listen(this.port, {
pingTimeout: 60000, // how many ms without a pong packet to consider the connection closed
pingInterval: 25000 // how many ms before sending a new ping packet
pingTimeout: CONNECTION_HEARBEAT_PING_TIMEOUT,
pingInterval: CONNECTION_HEARBEAT_PING_INTERVAL
});

var self = this;
Expand All @@ -124,25 +127,32 @@ LocalServer.prototype.createSocketListener = function() {
});
};

LocalServer.prototype.getConnectionUrl = function() {
return this.tunneledUrl ||
(this.externalServerUrl ? this.externalServerUrl + ":" + this.port : undefined);
};

LocalServer.prototype.getStandartUrlForPlatform = function(platformId) {
// Connection url could be platform specific so we pass platform as param here
LocalServer.prototype.getConnectionUrl = function(platformId) {
// --useTunnel option
if (this.tunneledUrl) {
return this.tunneledUrl;
}
// --externalServerUrl option / we know ip or dns name
if (this.externalServerUrl) {
return this.externalServerUrl + ":" + this.port;
}
// build connection uri for localhost based on platform
var connectionUrl;

switch(platformId) {
case "android" : connectionUrl = "http://10.0.2.2:";
break;
case "ios" :
case "browser" :
case "windows" :
/* falls through */
default: connectionUrl = "http://127.0.0.1:";
}

return connectionUrl + this.port;
case "android" :
connectionUrl = "http://10.0.2.2";
break;
case "ios" :
case "browser" :
case "windows" :
/* falls through */
default:
connectionUrl = "http://127.0.0.1";
}

return connectionUrl + ":" + this.port;
};

LocalServer.prototype.isDeviceConnected = function() {
Expand Down
3 changes: 1 addition & 2 deletions lib/paramedic.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ ParamedicRunner.prototype.run = function() {
self.injectReporters();
self.subcribeForEvents();

var connectionUrl = server.getConnectionUrl() ||
server.getStandartUrlForPlatform(self.config.getPlatformId());
var connectionUrl = server.getConnectionUrl(self.config.getPlatformId());
self.writeMedicConnectionUrl(connectionUrl);

return self.runTests();
Expand Down
6 changes: 4 additions & 2 deletions paramedic-plugin/paramedic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Paramedic.prototype.initialize = function() {
this.socket = io.connect(connectionUri);

this.socket.on('connect', function () {
console.log("Paramedic has been susccessfully connected to server");
console.log('Paramedic has been susccessfully connected to server');
if (typeof device != 'undefined') me.socket.emit('deviceInfo', device);
});

Expand Down Expand Up @@ -85,7 +85,9 @@ function loadParamedicServerUrl() {

return cfg.logurl || PARAMEDIC_SERVER_DEFAULT_URL;

} catch (ex) {}
} catch (ex) {
console.log('Unable to load paramedic server url: ' + ex);
}

return PARAMEDIC_SERVER_DEFAULT_URL;
}
Expand Down

0 comments on commit b46d3d8

Please sign in to comment.