1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 02:29:26 +02:00

Initial version of transactions. Shell command implemented as an example. Still needs channel cleanup on app side, handling a device dying in the middle of a transaction, and getting device list back to normal.

This commit is contained in:
Simo Kinnunen 2014-02-21 11:43:51 +09:00
parent 84207e1f36
commit 8769b8040c
19 changed files with 433 additions and 181 deletions

View file

@ -334,24 +334,24 @@ module.exports = function(options) {
})
sub.on('message', wirerouter()
.on('message', function(channel) {
channels.keepalive(channel)
})
.on(wire.ProbeMessage, function(channel, message) {
push.send([wireutil.global,
wireutil.makeDeviceIdentityMessage(options.serial, identity)])
channels.keepalive(channel)
})
.on(wire.GroupMessage, function(channel, message) {
if (!isGrouped() &&
devutil.matchesRequirements(identity, message.requirements)) {
joinGroup(message.owner, message.timeout)
}
channels.keepalive(channel)
})
.on(wire.UngroupMessage, function(channel, message) {
if (isGrouped() &&
devutil.matchesRequirements(identity, message.requirements)) {
leaveGroup()
}
channels.keepalive(channel)
})
.on(wire.TouchDownMessage, function(channel, message) {
services.input.touchDownAsync(message.x, message.y)
@ -409,55 +409,85 @@ module.exports = function(options) {
})
})
.on(wire.ShellCommandMessage, function(channel, message) {
log.info('Running shell command "%s"', message.command.join(' '))
var router = this
, seq = 0
log.info('Running shell command "%s"', message.command)
adb.shellAsync(options.serial, message.command)
.then(function(stream) {
var resolver = Promise.defer()
, seq = 0
, timer
function dataListener(chunk) {
push.send([message.channel,
wireutil.makeShellCommandDataMessage(
options.serial
, seq++
, chunk
)])
function keepAliveListener(channel, message) {
clearTimeout(timer)
timer = setTimeout(forceStop, message.timeout)
}
function readableListener() {
var chunk
while (chunk = stream.read()) {
push.send([
channel
, wireutil.envelope(new wire.TransactionProgressMessage(
options.serial
, seq++
, chunk
))
])
}
}
function endListener() {
push.send([message.channel,
wireutil.makeShellCommandDoneMessage(options.serial)])
push.send([
channel
, wireutil.envelope(new wire.TransactionDoneMessage(
options.serial
, seq++
, true
))
])
resolver.resolve()
}
function errorListener(err) {
log.error('Shell command "%s" failed due to "%s"'
, message.command.join(' '), err.message)
resolver.reject(err)
push.send([message.channel,
wireutil.makeShellCommandFailMessage(
options.serial
, err.message
)])
}
stream.on('data', dataListener)
function forceStop() {
stream.end()
}
stream.on('readable', readableListener)
stream.on('end', endListener)
stream.on('error', errorListener)
sub.subscribe(channel)
router.on(wire.ShellKeepAliveMessage, keepAliveListener)
timer = setTimeout(forceStop, message.timeout)
return resolver.promise.finally(function() {
stream.removeListener('data', dataListener)
stream.removeListener('readable', readableListener)
stream.removeListener('end', endListener)
stream.removeListener('error', errorListener)
sub.unsubscribe(channel)
router.removeListener(wire.ShellKeepAliveMessage, keepAliveListener)
clearTimeout(timer)
})
})
.error(function(err) {
log.error('Shell command "%s" failed due to "%s"'
, message.command.join(' '), err.message)
push.send([message.channel,
wire.makeShellCommandFailMessage(options.serial, err.message)])
, message.command, err.message)
push.send([
channel
, wireutil.envelope(new wire.TransactionDoneMessage(
options.serial
, seq++
, false
, err.message
))
])
})
channels.keepalive(channel)
})
.handler())