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

Separate ForwardManager into its own file and attempt to make it a bit more resilient.

This commit is contained in:
Simo Kinnunen 2014-10-14 20:00:49 +09:00
parent 0a6cefcf59
commit 4bbcaa45db
3 changed files with 201 additions and 92 deletions

View file

@ -9,8 +9,7 @@ var lifecycle = require('../../../../util/lifecycle')
var streamutil = require('../../../../util/streamutil')
var wireutil = require('../../../../wire/util')
var ForwardReader = require('./util/reader')
var ForwardWriter = require('./util/writer')
var ForwardManager = require('./util/manager')
module.exports = syrup.serial()
.dependency(require('../../support/adb'))
@ -21,96 +20,6 @@ module.exports = syrup.serial()
.define(function(options, adb, router, push, minirev, group) {
var log = logger.createLogger('device:plugins:forward')
var plugin = Object.create(null)
function ForwardManager() {
var forwards = Object.create(null)
function Forward(conn, to) {
var proxies = Object.create(null)
function Proxy(fd) {
function maybeSend() {
var chunk
while ((chunk = this.read())) {
if (!conn.write(chunk)) {
break
}
}
}
function killListeners() {
src.removeListener('readable', maybeSend)
conn.removeListener('drain', maybeSend)
conn.removeListener('end', killListeners)
}
var src = new ForwardWriter(fd)
.on('readable', maybeSend)
.on('error', function(err) {
log.error('Proxy writer %d had an error', fd, to, err.stack)
})
conn.on('drain', maybeSend)
conn.on('end', killListeners)
this.dest = net.connect(to)
.once('end', function() {
delete proxies[fd]
killListeners()
})
.on('error', function(err) {
log.error('Proxy reader %d had an error', fd, to, err.stack)
})
this.dest.pipe(src)
this.stop = function() {
this.dest.end()
}
}
conn.pipe(new ForwardReader())
.on('packet', function(fd, packet) {
var proxy = proxies[fd]
if (packet) {
if (!proxy) {
// New connection
proxy = proxies[fd] = new Proxy(fd)
}
proxy.dest.write(packet)
}
else {
// The connection ended
if (proxy) {
proxy.stop()
}
}
})
this.end = function() {
conn.end()
}
}
this.add = function(port, conn, to) {
forwards[port] = new Forward(conn, to)
}
this.remove = function(port) {
if (forwards[port]) {
forwards[port].end()
}
}
this.removeAll = function() {
Object.keys(forwards).forEach(function(port) {
forwards[port].end()
})
}
}
var manager = new ForwardManager()
function startService() {
@ -203,9 +112,37 @@ module.exports = syrup.serial()
group.on('leave', plugin.reset)
manager.on('add', function(port, to) {
push.send([
wireutil.global
, wireutil.envelope(new wire.DeviceForwardAddEvent(
options.serial
, port
, to.host
, to.port
))
])
})
manager.on('remove', function(port) {
push.send([
wireutil.global
, wireutil.envelope(new wire.DeviceForwardRemoveEvent(
options.serial
, port
))
])
})
return startService()
.then(awaitServer)
.then(function() {
plugin.createForward(3000, {
host: '127.0.0.1'
, port: 3000
})
router
.on(wire.ForwardTestMessage, function(channel, message) {
var reply = wireutil.reply(options.serial)