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 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 get the number of the assigned port using the | 60 get the number of the assigned port using the |
61 DevicePortForHostPort method. | 61 DevicePortForHostPort method. |
62 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 |
63 forwarder (see valgrind_tools.py). | 63 forwarder (see valgrind_tools.py). |
64 host_name: Address to forward to, must be addressable from the | 64 host_name: Address to forward to, must be addressable from the |
65 host machine. Usually use loopback '127.0.0.1'. | 65 host machine. Usually use loopback '127.0.0.1'. |
66 | 66 |
67 Raises: | 67 Raises: |
68 Exception on failure to forward the port. | 68 Exception on failure to forward the port. |
69 """ | 69 """ |
70 host_adb_control_port = ports.AllocateTestServerPort() | 70 self._host_adb_control_port = ports.AllocateTestServerPort() |
71 if not host_adb_control_port: | 71 if not self._host_adb_control_port: |
72 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.') |
73 self._adb.PushIfNeeded( | 73 self._adb.PushIfNeeded( |
74 self._device_forwarder_path_on_host, Forwarder._DEVICE_FORWARDER_FOLDER) | 74 self._device_forwarder_path_on_host, Forwarder._DEVICE_FORWARDER_FOLDER) |
75 redirection_commands = [ | 75 redirection_commands = [ |
76 '%d:%d:%d:%s' % (host_adb_control_port, device, host, | 76 '%d:%d:%d:%s' % (self._host_adb_control_port, device, host, |
77 host_name) for device, host in port_pairs] | 77 host_name) for device, host in port_pairs] |
78 logging.info('Command format: <ADB port>:<Device port>' + | 78 logging.info('Command format: <ADB port>:<Device port>' + |
79 '[:<Forward to port>:<Forward to address>]') | 79 '[:<Forward to port>:<Forward to address>]') |
80 logging.info('Forwarding using commands: %s', redirection_commands) | 80 logging.info('Forwarding using commands: %s', redirection_commands) |
81 if cmd_helper.RunCmd( | 81 if cmd_helper.RunCmd( |
82 ['adb', '-s', self._adb._adb.GetSerialNumber(), 'forward', | 82 ['adb', '-s', self._adb._adb.GetSerialNumber(), 'forward', |
83 'tcp:%s' % host_adb_control_port, | 83 'tcp:%s' % self._host_adb_control_port, |
84 'localabstract:%s' % Forwarder._DEVICE_ADB_CONTROL_PORT]) != 0: | 84 'localabstract:%s' % Forwarder._DEVICE_ADB_CONTROL_PORT]) != 0: |
85 raise Exception('Error while running adb forward.') | 85 raise Exception('Error while running adb forward.') |
86 | 86 |
87 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( | 87 (exit_code, output) = self._adb.GetShellCommandStatusAndOutput( |
88 '%s %s %s %s' % (Forwarder._LD_LIBRARY_PATH, tool.GetUtilWrapper(), | 88 '%s %s %s %s' % (Forwarder._LD_LIBRARY_PATH, tool.GetUtilWrapper(), |
89 Forwarder._DEVICE_FORWARDER_PATH, | 89 Forwarder._DEVICE_FORWARDER_PATH, |
90 Forwarder._DEVICE_ADB_CONTROL_PORT)) | 90 Forwarder._DEVICE_ADB_CONTROL_PORT)) |
91 if exit_code != 0: | 91 if exit_code != 0: |
92 raise Exception( | 92 raise Exception( |
93 'Failed to start device forwarder:\n%s' % '\n'.join(output)) | 93 'Failed to start device forwarder:\n%s' % '\n'.join(output)) |
(...skipping 13 matching lines...) Expand all Loading... |
107 tokens = output.split(':') | 107 tokens = output.split(':') |
108 if len(tokens) != 2: | 108 if len(tokens) != 2: |
109 raise Exception('Unexpected host forwarder output "%s", ' + | 109 raise Exception('Unexpected host forwarder output "%s", ' + |
110 'expected "device_port:host_port"' % output) | 110 'expected "device_port:host_port"' % output) |
111 device_port = int(tokens[0]) | 111 device_port = int(tokens[0]) |
112 host_port = int(tokens[1]) | 112 host_port = int(tokens[1]) |
113 self._host_to_device_port_map[host_port] = device_port | 113 self._host_to_device_port_map[host_port] = device_port |
114 logging.info('Forwarding device port: %d to host port: %d.', device_port, | 114 logging.info('Forwarding device port: %d to host port: %d.', device_port, |
115 host_port) | 115 host_port) |
116 | 116 |
| 117 def UnmapDevicePort(self, device_port): |
| 118 # Please note the minus sign below. |
| 119 redirection_command = '%d:-%d' % (self._host_adb_control_port, device_port) |
| 120 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
| 121 [self._host_forwarder_path, redirection_command]) |
| 122 if exit_code != 0: |
| 123 raise Exception('%s exited with %d:\n%s' % ( |
| 124 self._host_forwarder_path, exit_code, '\n'.join(output))) |
| 125 |
117 @staticmethod | 126 @staticmethod |
118 def KillHost(build_type): | 127 def KillHost(build_type): |
119 logging.info('Killing host_forwarder.') | 128 logging.info('Killing host_forwarder.') |
120 host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') | 129 host_forwarder_path = _MakeBinaryPath(build_type, 'host_forwarder') |
121 assert os.path.exists(host_forwarder_path), 'Please build forwarder2' | 130 assert os.path.exists(host_forwarder_path), 'Please build forwarder2' |
122 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 131 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
123 [host_forwarder_path, 'kill-server']) | 132 [host_forwarder_path, 'kill-server']) |
124 if exit_code != 0: | 133 if exit_code != 0: |
125 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( | 134 (exit_code, output) = cmd_helper.GetCmdStatusAndOutput( |
126 ['pkill', 'host_forwarder']) | 135 ['pkill', 'host_forwarder']) |
(...skipping 21 matching lines...) Expand all Loading... |
148 | 157 |
149 def DevicePortForHostPort(self, host_port): | 158 def DevicePortForHostPort(self, host_port): |
150 """Get the device port that corresponds to a given host port.""" | 159 """Get the device port that corresponds to a given host port.""" |
151 return self._host_to_device_port_map.get(host_port) | 160 return self._host_to_device_port_map.get(host_port) |
152 | 161 |
153 def Close(self): | 162 def Close(self): |
154 """Terminate the forwarder process.""" | 163 """Terminate the forwarder process.""" |
155 if self._device_process: | 164 if self._device_process: |
156 self._device_process.close() | 165 self._device_process.close() |
157 self._device_process = None | 166 self._device_process = None |
OLD | NEW |