diff --git a/lib/roles/device.js b/lib/roles/device.js index a1a8fd04..4e470b04 100644 --- a/lib/roles/device.js +++ b/lib/roles/device.js @@ -72,6 +72,40 @@ module.exports = function(options) { }) })) }) + .then(function() { + log.info('Checking if any processes from a previous run are still up') + return devutil.killProcsByBin(adb, options.serial, vendor.bin.dest) + }) + .then(function() { + var port = 2870 + return adb.openTcpAsync(options.serial, port) + .then(function(conn) { + conn.end() + throw new Error(util.format('Old connection still lives on port %d', port)) + }) + .catch(function(err) { + if (err.message === 'closed') { + return adb.shellAsync(options.serial, [ + vendor.bin.dest + , '--lib', vendor.lib.dest + , '--listen-http', port + ]) + .then(function(out) { + out.pipe(require('split')()) + .on('data', function(chunk) { + log.info('remote: "%s"', chunk) + }) + .on('end', function() { + log.fatal('remote: Connection closed') + selfDestruct() + }) + }) + } + else { + throw err + } + }) + }) .done(function() { log.info('Ready for instructions') poke() @@ -163,6 +197,10 @@ module.exports = function(options) { wireutil.makeDevicePokeMessage(options.serial, solo)]) } + function selfDestruct() { + process.exit(1) + } + function gracefullyExit() { log.info('Bye') process.exit(0) diff --git a/lib/util/devutil.js b/lib/util/devutil.js index fbaf6de1..fe6297a2 100644 --- a/lib/util/devutil.js +++ b/lib/util/devutil.js @@ -1,4 +1,8 @@ var util = require('util') +var path = require('path') + +var split = require('split') +var Promise = require('bluebird') var wire = require('../wire') var pathutil = require('./pathutil') @@ -22,6 +26,68 @@ devutil.vendorFiles = function(identity) { } } +devutil.killProcsByBin = function(adb, serial, bin) { + var basename = path.basename(bin) + return adb.shellAsync(serial, ['ps', basename]) + .then(function(out) { + return new Promise(function(resolve, reject) { + var header = false + , killables = [] + out.pipe(split()) + .on('data', function(chunk) { + if (header) { + header = false + } + else { + var cols = chunk.toString().split(/\s+/) + if (cols.pop() === bin) { + killables.push(cols[1]) + } + } + }) + .on('end', function() { + resolve(killables) + }) + }) + }) + .then(function(killables) { + if (killables.length) { + return adb.shellAsync(serial, ['kill'].concat(killables)) + .then(function(out) { + return new Promise(function(resolve, reject) { + out.on('end', resolve) + }) + }) + } + }) +} + +devutil.startProp = function(adb, serial, cmd) { + return adb.openTcpAsync(options.serial, port) + .then(function(conn) { + conn.end() + }) + .catch(function(err) { + if (err.message === 'closed') { + var cmd = [ + vendor.bin.dest + , '--lib', vendor.lib.dest + , '--listen-http', port + ] + return adb.shellAsync(options.serial, cmd) + .then(function(out) { + + out.on('end', function() { + log.fatal('') + }) + }) + } + else { + throw err + } + }) +} + devutil.platform = function(platform) { switch (platform) { case 'android':