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

Switch to protocol buffers for communication.

This commit is contained in:
Simo Kinnunen 2014-01-22 16:34:07 +09:00
parent f60cf2008b
commit 90e405a341
8 changed files with 354 additions and 36 deletions

View file

@ -4,13 +4,24 @@ var Promise = require('bluebird')
var zmq = require('zmq')
var adbkit = require('adbkit')
var logger = require('../util/logger')
var wire = require('../wire')
var wireutil = require('../util/wireutil')(wire)
var devutil = require('../util/devutil')
var ChannelManager = require('../wire/channelmanager')
module.exports = function(options) {
var logger = require('../util/logger')
var log = logger.createLogger('device')
var identity = Object.create(null)
var solo = wireutil.makePrivateChannel()
var channels = new ChannelManager()
// Show serial number in logs
logger.setGlobalIdentifier(options.serial)
// Adb
var adb = Promise.promisifyAll(adbkit.createClient())
// Input
var sub = zmq.socket('sub')
options.endpoints.sub.forEach(function(endpoint) {
@ -18,39 +29,47 @@ module.exports = function(options) {
sub.connect(endpoint)
})
sub.on('message', function(channel, id, cmd) {
push.send([id, options.serial, 'ACK'])
switch (cmd.toString()) {
case 'ls':
log.info('Responding to "ls"')
push.send([id, options.serial, 'OKY'])
// Establish always-on channels
;[wireutil.global, solo].forEach(function(channel) {
log.info('Subscribing to permanent channel "%s"', channel)
sub.subscribe(channel)
channels.register(channel, Infinity)
})
// Unsubscribe from temporary channels when they timeout
channels.on('timeout', function(channel) {
log.info('Channel "%s" timed out', channel)
sub.unsubscribe(channel)
push.send([channel, wireutil.makeLeaveGroupMessage(options.serial)])
})
sub.on('message', function(channel, data) {
var wrapper = wire.Envelope.decode(data)
channels.keepalive(channel)
switch (wrapper.type) {
case wire.MessageType.GROUP:
var message = wire.GroupMessage.decode(wrapper.message)
, groupChannel = message.group
if (wireutil.matchesRequirements(identity, message.requirements)) {
channels.register(groupChannel, 600000)
log.info('Subscribing to group channel "%s"', groupChannel)
sub.subscribe(groupChannel)
push.send([groupChannel,
wireutil.makeJoinGroupMessage(options.serial)])
}
break
case 'shell':
var line = arguments[3]
log.info('Running shell command "%s"', line)
adb.shellAsync(options.serial, line)
.then(function(out) {
var chunks = []
out.on('data', function(chunk) {
chunks.push(chunk)
})
out.on('end', function(chunk) {
push.send([id, options.serial, 'OKY', Buffer.concat(chunks)])
})
case wire.MessageType.PROBE:
var message = wire.ProbeMessage.decode(wrapper.message)
adb.getPropertiesAsync(options.serial)
.then(function(properties) {
identity = devutil.makeIdentity(options.serial, properties)
push.send([channel,
wireutil.makeDevicePropertiesMessage(options.serial, properties)])
})
.catch(function(err) {
push.send([id, options.serial, 'ERR', err.message])
})
break
default:
log.warn('Unknown command "%s"', cmd)
break
}
})
// Respond to messages directed to everyone
sub.subscribe('ALL')
// Output
var push = zmq.socket('push')
options.endpoints.push.forEach(function(endpoint) {
@ -58,11 +77,10 @@ module.exports = function(options) {
push.connect(endpoint)
})
// Introduce worker
// push.send(['HELO', options.serial])
// Adb
var adb = Promise.promisifyAll(adbkit.createClient())
function poke() {
push.send([wireutil.global,
wireutil.makeDevicePokeMessage(options.serial, solo)])
}
function gracefullyExit() {
log.info('Bye')
@ -76,4 +94,6 @@ module.exports = function(options) {
process.on('SIGTERM', function() {
gracefullyExit()
})
poke()
}