| 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 # pylint: disable=W0212 | 5 # pylint: disable=W0212 |
| 6 | 6 |
| 7 import fcntl | 7 import fcntl |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import psutil | 10 import psutil |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 | 45 |
| 46 class Forwarder(object): | 46 class Forwarder(object): |
| 47 """Thread-safe class to manage port forwards from the device to the host.""" | 47 """Thread-safe class to manage port forwards from the device to the host.""" |
| 48 | 48 |
| 49 _DEVICE_FORWARDER_FOLDER = (constants.TEST_EXECUTABLE_DIR + | 49 _DEVICE_FORWARDER_FOLDER = (constants.TEST_EXECUTABLE_DIR + |
| 50 '/forwarder/') | 50 '/forwarder/') |
| 51 _DEVICE_FORWARDER_PATH = (constants.TEST_EXECUTABLE_DIR + | 51 _DEVICE_FORWARDER_PATH = (constants.TEST_EXECUTABLE_DIR + |
| 52 '/forwarder/device_forwarder') | 52 '/forwarder/device_forwarder') |
| 53 _LOCK_PATH = '/tmp/chrome.forwarder.lock' | 53 _LOCK_PATH = '/tmp/chrome.forwarder.lock' |
| 54 _MULTIPROCESSING_ENV_VAR = 'CHROME_FORWARDER_USE_MULTIPROCESSING' | |
| 55 # Defined in host_forwarder_main.cc | 54 # Defined in host_forwarder_main.cc |
| 56 _HOST_FORWARDER_LOG = '/tmp/host_forwarder_log' | 55 _HOST_FORWARDER_LOG = '/tmp/host_forwarder_log' |
| 57 | 56 |
| 58 _instance = None | 57 _instance = None |
| 59 | 58 |
| 60 @staticmethod | 59 @staticmethod |
| 61 def UseMultiprocessing(): | |
| 62 """Tells the forwarder that multiprocessing is used.""" | |
| 63 os.environ[Forwarder._MULTIPROCESSING_ENV_VAR] = '1' | |
| 64 | |
| 65 @staticmethod | |
| 66 def Map(port_pairs, device, tool=None): | 60 def Map(port_pairs, device, tool=None): |
| 67 """Runs the forwarder. | 61 """Runs the forwarder. |
| 68 | 62 |
| 69 Args: | 63 Args: |
| 70 port_pairs: A list of tuples (device_port, host_port) to forward. Note | 64 port_pairs: A list of tuples (device_port, host_port) to forward. Note |
| 71 that you can specify 0 as a device_port, in which case a | 65 that you can specify 0 as a device_port, in which case a |
| 72 port will by dynamically assigned on the device. You can | 66 port will by dynamically assigned on the device. You can |
| 73 get the number of the assigned port using the | 67 get the number of the assigned port using the |
| 74 DevicePortForHostPort method. | 68 DevicePortForHostPort method. |
| 75 device: A DeviceUtils instance. | 69 device: A DeviceUtils instance. |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 logging.error('%s exited with %d:\n%s' % ( | 232 logging.error('%s exited with %d:\n%s' % ( |
| 239 instance._host_forwarder_path, exit_code, '\n'.join(output))) | 233 instance._host_forwarder_path, exit_code, '\n'.join(output))) |
| 240 host_port = instance._device_to_host_port_map[serial_with_port] | 234 host_port = instance._device_to_host_port_map[serial_with_port] |
| 241 del instance._device_to_host_port_map[serial_with_port] | 235 del instance._device_to_host_port_map[serial_with_port] |
| 242 del instance._host_to_device_port_map[host_port] | 236 del instance._host_to_device_port_map[host_port] |
| 243 | 237 |
| 244 @staticmethod | 238 @staticmethod |
| 245 def _GetPidForLock(): | 239 def _GetPidForLock(): |
| 246 """Returns the PID used for host_forwarder initialization. | 240 """Returns the PID used for host_forwarder initialization. |
| 247 | 241 |
| 248 In case multi-process sharding is used, the PID of the "sharder" is used. | 242 The PID of the "sharder" is used to handle multiprocessing. The "sharder" |
| 249 The "sharder" is the initial process that forks that is the parent process. | 243 is the initial process that forks that is the parent process. |
| 250 By default, multi-processing is not used. In that case the PID of the | |
| 251 current process is returned. | |
| 252 """ | 244 """ |
| 253 use_multiprocessing = Forwarder._MULTIPROCESSING_ENV_VAR in os.environ | 245 return os.getpgrp() |
| 254 return os.getpgrp() if use_multiprocessing else os.getpid() | |
| 255 | 246 |
| 256 def _InitHostLocked(self): | 247 def _InitHostLocked(self): |
| 257 """Initializes the host forwarder daemon. | 248 """Initializes the host forwarder daemon. |
| 258 | 249 |
| 259 Note that the global lock must be acquired before calling this method. This | 250 Note that the global lock must be acquired before calling this method. This |
| 260 method kills any existing host_forwarder process that could be stale. | 251 method kills any existing host_forwarder process that could be stale. |
| 261 """ | 252 """ |
| 262 # See if the host_forwarder daemon was already initialized by a concurrent | 253 # See if the host_forwarder daemon was already initialized by a concurrent |
| 263 # process or thread (in case multi-process sharding is not used). | 254 # process or thread (in case multi-process sharding is not used). |
| 264 pid_for_lock = Forwarder._GetPidForLock() | 255 pid_for_lock = Forwarder._GetPidForLock() |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 """ | 323 """ |
| 333 logging.info('Killing device_forwarder.') | 324 logging.info('Killing device_forwarder.') |
| 334 Forwarder._instance._initialized_devices.discard(str(device)) | 325 Forwarder._instance._initialized_devices.discard(str(device)) |
| 335 if not device.FileExists(Forwarder._DEVICE_FORWARDER_PATH): | 326 if not device.FileExists(Forwarder._DEVICE_FORWARDER_PATH): |
| 336 return | 327 return |
| 337 | 328 |
| 338 cmd = '%s %s --kill-server' % (tool.GetUtilWrapper(), | 329 cmd = '%s %s --kill-server' % (tool.GetUtilWrapper(), |
| 339 Forwarder._DEVICE_FORWARDER_PATH) | 330 Forwarder._DEVICE_FORWARDER_PATH) |
| 340 device.old_interface.GetAndroidToolStatusAndOutput( | 331 device.old_interface.GetAndroidToolStatusAndOutput( |
| 341 cmd, lib_path=Forwarder._DEVICE_FORWARDER_FOLDER) | 332 cmd, lib_path=Forwarder._DEVICE_FORWARDER_FOLDER) |
| OLD | NEW |