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 22 matching lines...) Expand all Loading... |
33 """Forwards TCP ports on the device back to the host. | 33 """Forwards TCP ports on the device back to the host. |
34 | 34 |
35 Works like adb forward, but in reverse. | 35 Works like adb forward, but in reverse. |
36 | 36 |
37 Args: | 37 Args: |
38 adb: Instance of AndroidCommands for talking to the device. | 38 adb: Instance of AndroidCommands for talking to the device. |
39 build_type: 'Release' or 'Debug'. | 39 build_type: 'Release' or 'Debug'. |
40 """ | 40 """ |
41 assert build_type in ('Release', 'Debug') | 41 assert build_type in ('Release', 'Debug') |
42 self._adb = adb | 42 self._adb = adb |
43 self._device_to_host_port_map = dict() | |
44 self._host_to_device_port_map = dict() | 43 self._host_to_device_port_map = dict() |
45 self._device_initialized = False | 44 self._device_initialized = False |
46 self._host_adb_control_port = 0 | 45 self._host_adb_control_port = 0 |
47 self._lock = threading.Lock() | 46 self._lock = threading.Lock() |
48 self._host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') | 47 self._host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') |
49 self._device_forwarder_path_on_host = os.path.join( | 48 self._device_forwarder_path_on_host = os.path.join( |
50 cmd_helper.OutDirectory.get(), build_type, 'forwarder_dist') | 49 cmd_helper.OutDirectory.get(), build_type, 'forwarder_dist') |
51 | 50 |
52 def Run(self, port_pairs, tool): | 51 def Run(self, port_pairs, tool): |
53 """Runs the forwarder. | 52 """Runs the forwarder. |
(...skipping 29 matching lines...) Expand all Loading... |
83 else: raise | 82 else: raise |
84 if exit_code != 0: | 83 if exit_code != 0: |
85 raise Exception('%s exited with %d:\n%s' % ( | 84 raise Exception('%s exited with %d:\n%s' % ( |
86 self._host_forwarder_path, exit_code, '\n'.join(output))) | 85 self._host_forwarder_path, exit_code, '\n'.join(output))) |
87 tokens = output.split(':') | 86 tokens = output.split(':') |
88 if len(tokens) != 2: | 87 if len(tokens) != 2: |
89 raise Exception(('Unexpected host forwarder output "%s", ' + | 88 raise Exception(('Unexpected host forwarder output "%s", ' + |
90 'expected "device_port:host_port"') % output) | 89 'expected "device_port:host_port"') % output) |
91 device_port = int(tokens[0]) | 90 device_port = int(tokens[0]) |
92 host_port = int(tokens[1]) | 91 host_port = int(tokens[1]) |
93 self._device_to_host_port_map[device_port] = host_port | |
94 self._host_to_device_port_map[host_port] = device_port | 92 self._host_to_device_port_map[host_port] = device_port |
95 logging.info('Forwarding device port: %d to host port: %d.', | 93 logging.info('Forwarding device port: %d to host port: %d.', |
96 device_port, host_port) | 94 device_port, host_port) |
97 | 95 |
98 def _InitDeviceLocked(self, tool): | 96 def _InitDeviceLocked(self, tool): |
99 """Initializes the device forwarder process (only once).""" | 97 """Initializes the device forwarder process (only once).""" |
100 if self._device_initialized: | 98 if self._device_initialized: |
101 return | 99 return |
102 self._adb.PushIfNeeded( | 100 self._adb.PushIfNeeded( |
103 self._device_forwarder_path_on_host, | 101 self._device_forwarder_path_on_host, |
104 Forwarder._DEVICE_FORWARDER_FOLDER) | 102 Forwarder._DEVICE_FORWARDER_FOLDER) |
105 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( | 103 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( |
106 '%s %s %s' % (Forwarder._LD_LIBRARY_PATH, tool.GetUtilWrapper(), | 104 '%s %s %s' % (Forwarder._LD_LIBRARY_PATH, tool.GetUtilWrapper(), |
107 Forwarder._DEVICE_FORWARDER_PATH)) | 105 Forwarder._DEVICE_FORWARDER_PATH)) |
108 if exit_code != 0: | 106 if exit_code != 0: |
109 raise Exception( | 107 raise Exception( |
110 'Failed to start device forwarder:\n%s' % '\n'.join(output)) | 108 'Failed to start device forwarder:\n%s' % '\n'.join(output)) |
111 self._device_initialized = True | 109 self._device_initialized = True |
112 | 110 |
113 def UnmapDevicePort(self, device_port): | 111 def UnmapDevicePort(self, device_port): |
114 """Unmaps a previously forwarded device port. | 112 """Unmaps a previously forwarded device port. |
115 | 113 |
116 Args: | 114 Args: |
117 device_port: A previously forwarded port (through Run()). | 115 device_port: A previously forwarded port (through Run()). |
118 """ | 116 """ |
119 with self._lock: | 117 with self._lock: |
120 self._UnmapDevicePortInternalLocked(device_port) | 118 redirection_command = [ |
121 | 119 '--serial-id=' + self._adb.Adb().GetSerialNumber(), '--unmap', |
122 def _UnmapDevicePortInternalLocked(self, device_port): | 120 str(device_port)] |
123 if not device_port in self._device_to_host_port_map: | 121 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
124 return | 122 [self._host_forwarder_path] + redirection_command) |
125 redirection_command = [ | 123 if exit_code != 0: |
126 '--serial-id=' + self._adb.Adb().GetSerialNumber(), '--unmap', | 124 logging.error('%s exited with %d:\n%s' % ( |
127 str(device_port)] | 125 self._host_forwarder_path, exit_code, '\n'.join(output))) |
128 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | |
129 [self._host_forwarder_path] + redirection_command) | |
130 if exit_code != 0: | |
131 logging.error('%s exited with %d:\n%s' % ( | |
132 self._host_forwarder_path, exit_code, '\n'.join(output))) | |
133 host_port = self._device_to_host_port_map[device_port] | |
134 del self._device_to_host_port_map[device_port] | |
135 del self._host_to_device_port_map[host_port] | |
136 | 126 |
137 @staticmethod | 127 @staticmethod |
138 def KillHost(build_type='Debug'): | 128 def KillHost(build_type): |
139 """Kills the forwarder process running on the host. | 129 """Kills the forwarder process running on the host. |
140 | 130 |
141 Args: | 131 Args: |
142 build_type: 'Release' or 'Debug' (default='Debug') | 132 build_type: 'Release' or 'Debug' |
143 """ | 133 """ |
144 logging.info('Killing host_forwarder.') | 134 logging.info('Killing host_forwarder.') |
145 host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') | 135 host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') |
146 if not os.path.exists(host_forwarder_path): | |
147 host_forwarder_path = _MakeBinaryPath( | |
148 'Release' if build_type == 'Debug' else 'Debug', 'host_forwarder') | |
149 assert os.path.exists(host_forwarder_path), 'Please build forwarder2' | 136 assert os.path.exists(host_forwarder_path), 'Please build forwarder2' |
150 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 137 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
151 [host_forwarder_path, '--kill-server']) | 138 [host_forwarder_path, '--kill-server']) |
152 if exit_code != 0: | 139 if exit_code != 0: |
153 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 140 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
154 ['pkill', 'host_forwarder']) | 141 ['pkill', 'host_forwarder']) |
155 if exit_code != 0: | 142 if exit_code != 0: |
156 raise Exception('%s exited with %d:\n%s' % ( | 143 raise Exception('%s exited with %d:\n%s' % ( |
157 host_forwarder_path, exit_code, '\n'.join(output))) | 144 host_forwarder_path, exit_code, '\n'.join(output))) |
158 | 145 |
(...skipping 20 matching lines...) Expand all Loading... |
179 if not processes_killed: | 166 if not processes_killed: |
180 pids = adb.ExtractPid('device_forwarder') | 167 pids = adb.ExtractPid('device_forwarder') |
181 if pids: | 168 if pids: |
182 raise Exception('Timed out while killing device_forwarder') | 169 raise Exception('Timed out while killing device_forwarder') |
183 | 170 |
184 def DevicePortForHostPort(self, host_port): | 171 def DevicePortForHostPort(self, host_port): |
185 """Returns the device port that corresponds to a given host port.""" | 172 """Returns the device port that corresponds to a given host port.""" |
186 with self._lock: | 173 with self._lock: |
187 return self._host_to_device_port_map.get(host_port) | 174 return self._host_to_device_port_map.get(host_port) |
188 | 175 |
| 176 # Deprecated. |
189 def Close(self): | 177 def Close(self): |
190 """Releases the previously forwarded ports.""" | 178 """Terminates the forwarder process.""" |
191 with self._lock: | 179 # TODO(pliard): Remove references in client code. |
192 for device_port in self._device_to_host_port_map.copy(): | 180 pass |
193 self._UnmapDevicePortInternalLocked(device_port) | |
OLD | NEW |