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 |
(...skipping 27 matching lines...) Expand all Loading... |
38 """Forwards TCP ports on the device back to the host. | 38 """Forwards TCP ports on the device back to the host. |
39 | 39 |
40 Works like adb forward, but in reverse. | 40 Works like adb forward, but in reverse. |
41 | 41 |
42 Args: | 42 Args: |
43 adb: Instance of AndroidCommands for talking to the device. | 43 adb: Instance of AndroidCommands for talking to the device. |
44 build_type: 'Release' or 'Debug'. | 44 build_type: 'Release' or 'Debug'. |
45 """ | 45 """ |
46 assert build_type in ('Release', 'Debug') | 46 assert build_type in ('Release', 'Debug') |
47 self._adb = adb | 47 self._adb = adb |
| 48 self._device_to_host_port_map = dict() |
48 self._host_to_device_port_map = dict() | 49 self._host_to_device_port_map = dict() |
49 self._device_initialized = False | 50 self._device_initialized = False |
50 self._host_adb_control_port = 0 | 51 self._host_adb_control_port = 0 |
51 self._lock = threading.Lock() | 52 self._lock = threading.Lock() |
52 self._host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') | 53 self._host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') |
53 self._device_forwarder_path_on_host = os.path.join( | 54 self._device_forwarder_path_on_host = os.path.join( |
54 cmd_helper.OutDirectory.get(), build_type, 'forwarder_dist') | 55 cmd_helper.OutDirectory.get(), build_type, 'forwarder_dist') |
55 | 56 |
56 def Run(self, port_pairs, tool): | 57 def Run(self, port_pairs, tool): |
57 """Runs the forwarder. | 58 """Runs the forwarder. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 else: raise | 91 else: raise |
91 if exit_code != 0: | 92 if exit_code != 0: |
92 raise Exception('%s exited with %d:\n%s' % ( | 93 raise Exception('%s exited with %d:\n%s' % ( |
93 self._host_forwarder_path, exit_code, '\n'.join(output))) | 94 self._host_forwarder_path, exit_code, '\n'.join(output))) |
94 tokens = output.split(':') | 95 tokens = output.split(':') |
95 if len(tokens) != 2: | 96 if len(tokens) != 2: |
96 raise Exception('Unexpected host forwarder output "%s", ' + | 97 raise Exception('Unexpected host forwarder output "%s", ' + |
97 'expected "device_port:host_port"' % output) | 98 'expected "device_port:host_port"' % output) |
98 device_port = int(tokens[0]) | 99 device_port = int(tokens[0]) |
99 host_port = int(tokens[1]) | 100 host_port = int(tokens[1]) |
| 101 self._device_to_host_port_map[device_port] = host_port |
100 self._host_to_device_port_map[host_port] = device_port | 102 self._host_to_device_port_map[host_port] = device_port |
101 logging.info('Forwarding device port: %d to host port: %d.', | 103 logging.info('Forwarding device port: %d to host port: %d.', |
102 device_port, host_port) | 104 device_port, host_port) |
103 | 105 |
104 def _InitHostLocked(self): | 106 def _InitHostLocked(self): |
105 """Initializes the host forwarder process (only once).""" | 107 """Initializes the host forwarder process (only once).""" |
106 if self._host_adb_control_port: | 108 if self._host_adb_control_port: |
107 return | 109 return |
108 self._host_adb_control_port = ports.AllocateTestServerPort() | 110 self._host_adb_control_port = ports.AllocateTestServerPort() |
109 if not self._host_adb_control_port: | 111 if not self._host_adb_control_port: |
(...skipping 20 matching lines...) Expand all Loading... |
130 'Failed to start device forwarder:\n%s' % '\n'.join(output)) | 132 'Failed to start device forwarder:\n%s' % '\n'.join(output)) |
131 self._device_initialized = True | 133 self._device_initialized = True |
132 | 134 |
133 def UnmapDevicePort(self, device_port): | 135 def UnmapDevicePort(self, device_port): |
134 """Unmaps a previously forwarded device port. | 136 """Unmaps a previously forwarded device port. |
135 | 137 |
136 Args: | 138 Args: |
137 device_port: A previously forwarded port (through Run()). | 139 device_port: A previously forwarded port (through Run()). |
138 """ | 140 """ |
139 with self._lock: | 141 with self._lock: |
140 # Please note the minus sign below. | 142 self._UnmapDevicePortInternalLocked(device_port) |
141 redirection_command = '%d:-%d' % ( | 143 |
142 self._host_adb_control_port, device_port) | 144 def _UnmapDevicePortInternalLocked(self, device_port): |
143 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 145 if not device_port in self._device_to_host_port_map: |
144 [self._host_forwarder_path, redirection_command]) | 146 return |
145 if exit_code != 0: | 147 # Please note the minus sign below. |
146 raise Exception('%s exited with %d:\n%s' % ( | 148 redirection_command = '%d:-%d' % ( |
147 self._host_forwarder_path, exit_code, '\n'.join(output))) | 149 self._host_adb_control_port, device_port) |
| 150 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
| 151 [self._host_forwarder_path, redirection_command]) |
| 152 if exit_code != 0: |
| 153 raise Exception('%s exited with %d:\n%s' % ( |
| 154 self._host_forwarder_path, exit_code, '\n'.join(output))) |
| 155 host_port = self._device_to_host_port_map[device_port] |
| 156 del self._device_to_host_port_map[device_port] |
| 157 del self._host_to_device_port_map[host_port] |
148 | 158 |
149 @staticmethod | 159 @staticmethod |
150 def KillHost(build_type): | 160 def KillHost(build_type='Debug'): |
151 """Kills the forwarder process running on the host. | 161 """Kills the forwarder process running on the host. |
152 | 162 |
153 Args: | 163 Args: |
154 build_type: 'Release' or 'Debug' | 164 build_type: 'Release' or 'Debug' (default='Debug') |
155 """ | 165 """ |
156 logging.info('Killing host_forwarder.') | 166 logging.info('Killing host_forwarder.') |
157 host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') | 167 host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') |
| 168 if not os.path.exists(host_forwarder_path): |
| 169 host_forwarder_path = _MakeBinaryPath( |
| 170 'Release' if build_type == 'Debug' else 'Debug', 'host_forwarder') |
158 assert os.path.exists(host_forwarder_path), 'Please build forwarder2' | 171 assert os.path.exists(host_forwarder_path), 'Please build forwarder2' |
159 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 172 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
160 [host_forwarder_path, 'kill-server']) | 173 [host_forwarder_path, 'kill-server']) |
161 if exit_code != 0: | 174 if exit_code != 0: |
162 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 175 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
163 ['pkill', 'host_forwarder']) | 176 ['pkill', 'host_forwarder']) |
164 if exit_code != 0: | 177 if exit_code != 0: |
165 raise Exception('%s exited with %d:\n%s' % ( | 178 raise Exception('%s exited with %d:\n%s' % ( |
166 host_forwarder_path, exit_code, '\n'.join(output))) | 179 host_forwarder_path, exit_code, '\n'.join(output))) |
167 | 180 |
(...skipping 20 matching lines...) Expand all Loading... |
188 if not processes_killed: | 201 if not processes_killed: |
189 pids = adb.ExtractPid('device_forwarder') | 202 pids = adb.ExtractPid('device_forwarder') |
190 if pids: | 203 if pids: |
191 raise Exception('Timed out while killing device_forwarder') | 204 raise Exception('Timed out while killing device_forwarder') |
192 | 205 |
193 def DevicePortForHostPort(self, host_port): | 206 def DevicePortForHostPort(self, host_port): |
194 """Returns the device port that corresponds to a given host port.""" | 207 """Returns the device port that corresponds to a given host port.""" |
195 with self._lock: | 208 with self._lock: |
196 return self._host_to_device_port_map.get(host_port) | 209 return self._host_to_device_port_map.get(host_port) |
197 | 210 |
198 # Deprecated. | |
199 def Close(self): | 211 def Close(self): |
200 """Terminates the forwarder process.""" | 212 """Releases the previously forwarded ports.""" |
201 # TODO(pliard): Remove references in client code. | 213 with self._lock: |
202 pass | 214 for device_port in self._device_to_host_port_map.copy(): |
| 215 self._UnmapDevicePortInternalLocked(device_port) |
OLD | NEW |