1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-04 10:19:30 +02:00

Start HTTP API on device.

This commit is contained in:
Simo Kinnunen 2014-01-29 11:44:28 +09:00
parent 2f2e4b6f31
commit 403c29c60a
2 changed files with 104 additions and 0 deletions

View file

@ -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)

View file

@ -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':