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 threading | 9 import threading |
10 import time | 10 import time |
11 | 11 |
12 import android_commands | 12 import android_commands |
13 import cmd_helper | 13 import cmd_helper |
14 import constants | 14 import constants |
15 import ports | 15 import ports |
16 | 16 |
17 from pylib import pexpect | 17 from pylib import pexpect |
18 | 18 |
19 | 19 |
20 def _MakeBinaryPath(build_type, binary_name): | 20 def _MakeBinaryPath(build_type, binary_name): |
21 return os.path.join(cmd_helper.OutDirectory.get(), build_type, binary_name) | 21 return os.path.join(cmd_helper.OutDirectory.get(), build_type, binary_name) |
22 | 22 |
23 | 23 |
24 class Forwarder(object): | 24 class Forwarder(object): |
25 """Thread-safe class to manage port forwards from the device to the host.""" | 25 """Thread-safe class to manage port forwards from the device to the host.""" |
26 | 26 |
27 # Unix Abstract socket path: | |
28 _DEVICE_ADB_CONTROL_PORT = 'chrome_device_forwarder' | 27 _DEVICE_ADB_CONTROL_PORT = 'chrome_device_forwarder' |
29 _TIMEOUT_SECS = 30 | |
30 | 28 |
31 _DEVICE_FORWARDER_FOLDER = (constants.TEST_EXECUTABLE_DIR + | 29 _DEVICE_FORWARDER_FOLDER = (constants.TEST_EXECUTABLE_DIR + |
32 '/forwarder/') | 30 '/forwarder/') |
33 _DEVICE_FORWARDER_PATH = (constants.TEST_EXECUTABLE_DIR + | 31 _DEVICE_FORWARDER_PATH = (constants.TEST_EXECUTABLE_DIR + |
34 '/forwarder/device_forwarder') | 32 '/forwarder/device_forwarder') |
35 _LD_LIBRARY_PATH = 'LD_LIBRARY_PATH=%s' % _DEVICE_FORWARDER_FOLDER | 33 _LD_LIBRARY_PATH = 'LD_LIBRARY_PATH=%s' % _DEVICE_FORWARDER_FOLDER |
36 | 34 |
37 def __init__(self, adb, build_type): | 35 def __init__(self, adb, build_type): |
38 """Forwards TCP ports on the device back to the host. | 36 """Forwards TCP ports on the device back to the host. |
39 | 37 |
(...skipping 23 matching lines...) Expand all Loading... | |
63 get the number of the assigned port using the | 61 get the number of the assigned port using the |
64 DevicePortForHostPort method. | 62 DevicePortForHostPort method. |
65 tool: Tool class to use to get wrapper, if necessary, for executing the | 63 tool: Tool class to use to get wrapper, if necessary, for executing the |
66 forwarder (see valgrind_tools.py). | 64 forwarder (see valgrind_tools.py). |
67 | 65 |
68 Raises: | 66 Raises: |
69 Exception on failure to forward the port. | 67 Exception on failure to forward the port. |
70 """ | 68 """ |
71 with self._lock: | 69 with self._lock: |
72 self._InitDeviceLocked(tool) | 70 self._InitDeviceLocked(tool) |
73 self._InitHostLocked() | |
74 host_name = '127.0.0.1' | 71 host_name = '127.0.0.1' |
75 redirection_commands = [ | 72 redirection_commands = [ |
76 '%d:%d:%d:%s' % (self._host_adb_control_port, device, host, | 73 ['--serial-id=' + self._adb.Adb().GetSerialNumber(), str(device), |
77 host_name) for device, host in port_pairs] | 74 str(host)] for device, host in port_pairs] |
78 logging.info('Command format: <ADB port>:<Device port>' + | |
79 '[:<Forward to port>:<Forward to address>]') | |
80 logging.info('Forwarding using commands: %s', redirection_commands) | 75 logging.info('Forwarding using commands: %s', redirection_commands) |
81 | 76 |
82 for redirection_command in redirection_commands: | 77 for redirection_command in redirection_commands: |
83 try: | 78 try: |
84 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 79 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
85 [self._host_forwarder_path, redirection_command]) | 80 [self._host_forwarder_path] + redirection_command) |
86 except OSError as e: | 81 except OSError as e: |
87 if e.errno == 2: | 82 if e.errno == 2: |
88 raise Exception('Unable to start host forwarder. Make sure you have' | 83 raise Exception('Unable to start host forwarder. Make sure you have' |
89 ' built host_forwarder.') | 84 ' built host_forwarder.') |
90 else: raise | 85 else: raise |
91 if exit_code != 0: | 86 if exit_code != 0: |
92 raise Exception('%s exited with %d:\n%s' % ( | 87 raise Exception('%s exited with %d:\n%s' % ( |
93 self._host_forwarder_path, exit_code, '\n'.join(output))) | 88 self._host_forwarder_path, exit_code, '\n'.join(output))) |
94 tokens = output.split(':') | 89 tokens = output.split(':') |
95 if len(tokens) != 2: | 90 if len(tokens) != 2: |
96 raise Exception('Unexpected host forwarder output "%s", ' + | 91 raise Exception('Unexpected host forwarder output "%s", ' + |
97 'expected "device_port:host_port"' % output) | 92 'expected "device_port:host_port"' % output) |
98 device_port = int(tokens[0]) | 93 device_port = int(tokens[0]) |
99 host_port = int(tokens[1]) | 94 host_port = int(tokens[1]) |
100 self._host_to_device_port_map[host_port] = device_port | 95 self._host_to_device_port_map[host_port] = device_port |
101 logging.info('Forwarding device port: %d to host port: %d.', | 96 logging.info('Forwarding device port: %d to host port: %d.', |
102 device_port, host_port) | 97 device_port, host_port) |
103 | 98 |
104 def _InitHostLocked(self): | |
105 """Initializes the host forwarder process (only once).""" | |
106 if self._host_adb_control_port: | |
107 return | |
108 self._host_adb_control_port = ports.AllocateTestServerPort() | |
109 if not self._host_adb_control_port: | |
110 raise Exception('Failed to allocate a TCP port in the host machine.') | |
111 if cmd_helper.RunCmd( | |
112 ['adb', '-s', self._adb._adb.GetSerialNumber(), 'forward', | |
113 'tcp:%s' % self._host_adb_control_port, | |
114 'localabstract:%s' % Forwarder._DEVICE_ADB_CONTROL_PORT]) != 0: | |
115 raise Exception('Error while running adb forward.') | |
116 | |
117 def _InitDeviceLocked(self, tool): | 99 def _InitDeviceLocked(self, tool): |
118 """Initializes the device forwarder process (only once).""" | 100 """Initializes the device forwarder process (only once).""" |
119 if self._device_initialized: | 101 if self._device_initialized: |
120 return | 102 return |
121 self._adb.PushIfNeeded( | 103 self._adb.PushIfNeeded( |
122 self._device_forwarder_path_on_host, | 104 self._device_forwarder_path_on_host, |
123 Forwarder._DEVICE_FORWARDER_FOLDER) | 105 Forwarder._DEVICE_FORWARDER_FOLDER) |
124 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( | 106 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( |
125 '%s %s %s %s' % (Forwarder._LD_LIBRARY_PATH, tool.GetUtilWrapper(), | 107 '%s %s %s %s' % (Forwarder._LD_LIBRARY_PATH, tool.GetUtilWrapper(), |
126 Forwarder._DEVICE_FORWARDER_PATH, | 108 Forwarder._DEVICE_FORWARDER_PATH, |
127 Forwarder._DEVICE_ADB_CONTROL_PORT)) | 109 Forwarder._DEVICE_ADB_CONTROL_PORT)) |
128 if exit_code != 0: | 110 if exit_code != 0: |
129 raise Exception( | 111 raise Exception( |
130 'Failed to start device forwarder:\n%s' % '\n'.join(output)) | 112 'Failed to start device forwarder:\n%s' % '\n'.join(output)) |
131 self._device_initialized = True | 113 self._device_initialized = True |
132 | 114 |
133 def UnmapDevicePort(self, device_port): | 115 def UnmapDevicePort(self, device_port): |
134 """Unmaps a previously forwarded device port. | 116 """Unmaps a previously forwarded device port. |
135 | 117 |
136 Args: | 118 Args: |
137 device_port: A previously forwarded port (through Run()). | 119 device_port: A previously forwarded port (through Run()). |
138 """ | 120 """ |
139 with self._lock: | 121 with self._lock: |
140 # Please note the minus sign below. | 122 # Please note the minus sign below. |
bulach
2013/07/02 13:13:06
how about changing this too? :)
could remove the c
Philippe
2013/07/02 15:58:25
Yeah, good point :) The '-' sign was introduced in
| |
141 redirection_command = '%d:-%d' % ( | 123 redirection_command = [ |
142 self._host_adb_control_port, device_port) | 124 '--serial-id=' + self._adb.Adb().GetSerialNumber(), str(-device_port)] |
143 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 125 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
144 [self._host_forwarder_path, redirection_command]) | 126 [self._host_forwarder_path] + redirection_command) |
145 if exit_code != 0: | 127 if exit_code != 0: |
146 raise Exception('%s exited with %d:\n%s' % ( | 128 raise Exception('%s exited with %d:\n%s' % ( |
147 self._host_forwarder_path, exit_code, '\n'.join(output))) | 129 self._host_forwarder_path, exit_code, '\n'.join(output))) |
148 | 130 |
149 @staticmethod | 131 @staticmethod |
150 def KillHost(build_type): | 132 def KillHost(build_type): |
151 """Kills the forwarder process running on the host. | 133 """Kills the forwarder process running on the host. |
152 | 134 |
153 Args: | 135 Args: |
154 build_type: 'Release' or 'Debug' | 136 build_type: 'Release' or 'Debug' |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 def DevicePortForHostPort(self, host_port): | 175 def DevicePortForHostPort(self, host_port): |
194 """Returns the device port that corresponds to a given host port.""" | 176 """Returns the device port that corresponds to a given host port.""" |
195 with self._lock: | 177 with self._lock: |
196 return self._host_to_device_port_map.get(host_port) | 178 return self._host_to_device_port_map.get(host_port) |
197 | 179 |
198 # Deprecated. | 180 # Deprecated. |
199 def Close(self): | 181 def Close(self): |
200 """Terminates the forwarder process.""" | 182 """Terminates the forwarder process.""" |
201 # TODO(pliard): Remove references in client code. | 183 # TODO(pliard): Remove references in client code. |
202 pass | 184 pass |
OLD | NEW |