OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging | 5 import logging |
6 import os | 6 import os |
7 import re | 7 import re |
8 import sys | 8 import sys |
9 import time | 9 import time |
10 | 10 |
11 import android_commands | 11 import android_commands |
12 import cmd_helper | 12 import cmd_helper |
13 import constants | 13 import constants |
14 import ports | 14 import ports |
15 | 15 |
16 from pylib import pexpect | 16 from pylib import pexpect |
17 | 17 |
18 | 18 |
19 def _MakeBinaryPath(build_type, binary_name): | 19 def _MakeBinaryPath(build_type, binary_name): |
20 return os.path.join(cmd_helper.OutDirectory.get(), build_type, binary_name) | 20 return os.path.join(cmd_helper.OutDirectory.get(), build_type, binary_name) |
21 | 21 |
22 | 22 |
23 class Forwarder(object): | 23 class Forwarder(object): |
24 """Class to manage port forwards from the device to the host.""" | 24 """Class to manage port forwards from the device to the host.""" |
25 | 25 |
26 # Unix Abstract socket path: | 26 # Unix Abstract socket path: |
27 _DEVICE_ADB_CONTROL_PORT = 'chrome_device_forwarder' | 27 _DEVICE_ADB_CONTROL_PORT = 'chrome_device_forwarder' |
28 _TIMEOUT_SECS = 30 | 28 _TIMEOUT_SECS = 30 |
29 | 29 |
30 _DEVICE_FORWARDER_PATH = constants.TEST_EXECUTABLE_DIR + '/device_forwarder' | 30 _DEVICE_FORWARDER_FOLDER = (constants.TEST_EXECUTABLE_DIR + |
| 31 '/forwarder/') |
| 32 _DEVICE_FORWARDER_PATH = (constants.TEST_EXECUTABLE_DIR + |
| 33 '/forwarder/device_forwarder') |
| 34 _LD_LIBRARY_PATH = 'LD_LIBRARY_PATH=%s' % _DEVICE_FORWARDER_FOLDER |
31 | 35 |
32 def __init__(self, adb, build_type): | 36 def __init__(self, adb, build_type): |
33 """Forwards TCP ports on the device back to the host. | 37 """Forwards TCP ports on the device back to the host. |
34 | 38 |
35 Works like adb forward, but in reverse. | 39 Works like adb forward, but in reverse. |
36 | 40 |
37 Args: | 41 Args: |
38 adb: Instance of AndroidCommands for talking to the device. | 42 adb: Instance of AndroidCommands for talking to the device. |
39 build_type: 'Release' or 'Debug'. | 43 build_type: 'Release' or 'Debug'. |
40 """ | 44 """ |
41 assert build_type in ('Release', 'Debug') | 45 assert build_type in ('Release', 'Debug') |
42 self._adb = adb | 46 self._adb = adb |
43 self._host_to_device_port_map = dict() | 47 self._host_to_device_port_map = dict() |
44 self._device_process = None | 48 self._device_process = None |
45 self._host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') | 49 self._host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') |
46 self._device_forwarder_path = _MakeBinaryPath( | 50 self._device_forwarder_path_on_host = os.path.join( |
47 build_type, 'device_forwarder') | 51 cmd_helper.OutDirectory.get(), build_type, 'forwarder_dist') |
48 | 52 |
49 def Run(self, port_pairs, tool, host_name): | 53 def Run(self, port_pairs, tool, host_name): |
50 """Runs the forwarder. | 54 """Runs the forwarder. |
51 | 55 |
52 Args: | 56 Args: |
53 port_pairs: A list of tuples (device_port, host_port) to forward. Note | 57 port_pairs: A list of tuples (device_port, host_port) to forward. Note |
54 that you can specify 0 as a device_port, in which case a | 58 that you can specify 0 as a device_port, in which case a |
55 port will by dynamically assigned on the device. You can | 59 port will by dynamically assigned on the device. You can |
56 get the number of the assigned port using the | 60 get the number of the assigned port using the |
57 DevicePortForHostPort method. | 61 DevicePortForHostPort method. |
58 tool: Tool class to use to get wrapper, if necessary, for executing the | 62 tool: Tool class to use to get wrapper, if necessary, for executing the |
59 forwarder (see valgrind_tools.py). | 63 forwarder (see valgrind_tools.py). |
60 host_name: Address to forward to, must be addressable from the | 64 host_name: Address to forward to, must be addressable from the |
61 host machine. Usually use loopback '127.0.0.1'. | 65 host machine. Usually use loopback '127.0.0.1'. |
62 | 66 |
63 Raises: | 67 Raises: |
64 Exception on failure to forward the port. | 68 Exception on failure to forward the port. |
65 """ | 69 """ |
66 host_adb_control_port = ports.AllocateTestServerPort() | 70 host_adb_control_port = ports.AllocateTestServerPort() |
67 if not host_adb_control_port: | 71 if not host_adb_control_port: |
68 raise Exception('Failed to allocate a TCP port in the host machine.') | 72 raise Exception('Failed to allocate a TCP port in the host machine.') |
69 self._adb.PushIfNeeded( | 73 self._adb.PushIfNeeded( |
70 self._device_forwarder_path, Forwarder._DEVICE_FORWARDER_PATH) | 74 self._device_forwarder_path_on_host, Forwarder._DEVICE_FORWARDER_FOLDER) |
71 redirection_commands = [ | 75 redirection_commands = [ |
72 '%d:%d:%d:%s' % (host_adb_control_port, device, host, | 76 '%d:%d:%d:%s' % (host_adb_control_port, device, host, |
73 host_name) for device, host in port_pairs] | 77 host_name) for device, host in port_pairs] |
74 logging.info('Command format: <ADB port>:<Device port>' + | 78 logging.info('Command format: <ADB port>:<Device port>' + |
75 '[:<Forward to port>:<Forward to address>]') | 79 '[:<Forward to port>:<Forward to address>]') |
76 logging.info('Forwarding using commands: %s', redirection_commands) | 80 logging.info('Forwarding using commands: %s', redirection_commands) |
77 if cmd_helper.RunCmd( | 81 if cmd_helper.RunCmd( |
78 ['adb', '-s', self._adb._adb.GetSerialNumber(), 'forward', | 82 ['adb', '-s', self._adb._adb.GetSerialNumber(), 'forward', |
79 'tcp:%s' % host_adb_control_port, | 83 'tcp:%s' % host_adb_control_port, |
80 'localabstract:%s' % Forwarder._DEVICE_ADB_CONTROL_PORT]) != 0: | 84 'localabstract:%s' % Forwarder._DEVICE_ADB_CONTROL_PORT]) != 0: |
81 raise Exception('Error while running adb forward.') | 85 raise Exception('Error while running adb forward.') |
82 | 86 |
83 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( | 87 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( |
84 '%s %s %s' % (tool.GetUtilWrapper(), Forwarder._DEVICE_FORWARDER_PATH, | 88 '%s %s %s %s' % (tool.GetUtilWrapper(), Forwarder._LD_LIBRARY_PATH, |
85 Forwarder._DEVICE_ADB_CONTROL_PORT)) | 89 Forwarder._DEVICE_FORWARDER_PATH, |
| 90 Forwarder._DEVICE_ADB_CONTROL_PORT)) |
86 if exit_code != 0: | 91 if exit_code != 0: |
87 raise Exception( | 92 raise Exception( |
88 'Failed to start device forwarder:\n%s' % '\n'.join(output)) | 93 'Failed to start device forwarder:\n%s' % '\n'.join(output)) |
89 | 94 |
90 for redirection_command in redirection_commands: | 95 for redirection_command in redirection_commands: |
91 try: | 96 try: |
92 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 97 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
93 [self._host_forwarder_path, redirection_command]) | 98 [self._host_forwarder_path, redirection_command]) |
94 except OSError as e: | 99 except OSError as e: |
95 if e.errno == 2: | 100 if e.errno == 2: |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 148 |
144 def DevicePortForHostPort(self, host_port): | 149 def DevicePortForHostPort(self, host_port): |
145 """Get the device port that corresponds to a given host port.""" | 150 """Get the device port that corresponds to a given host port.""" |
146 return self._host_to_device_port_map.get(host_port) | 151 return self._host_to_device_port_map.get(host_port) |
147 | 152 |
148 def Close(self): | 153 def Close(self): |
149 """Terminate the forwarder process.""" | 154 """Terminate the forwarder process.""" |
150 if self._device_process: | 155 if self._device_process: |
151 self._device_process.close() | 156 self._device_process.close() |
152 self._device_process = None | 157 self._device_process = None |
OLD | NEW |