| 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 """Functions that deal with local and device ports.""" | 5 """Functions that deal with local and device ports.""" |
| 6 | 6 |
| 7 import contextlib | 7 import contextlib |
| 8 import fcntl | 8 import fcntl |
| 9 import httplib | 9 import httplib |
| 10 import logging | 10 import logging |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 port_info = '(\*)|(127\.0\.0\.1)|(localhost):%d' % host_port | 94 port_info = '(\*)|(127\.0\.0\.1)|(localhost):%d' % host_port |
| 95 # TODO(jnd): Find a better way to filter the port. Note that connecting to the | 95 # TODO(jnd): Find a better way to filter the port. Note that connecting to the |
| 96 # socket and closing it would leave it in the TIME_WAIT state. Setting | 96 # socket and closing it would leave it in the TIME_WAIT state. Setting |
| 97 # SO_LINGER on it and then closing it makes the Python HTTP server crash. | 97 # SO_LINGER on it and then closing it makes the Python HTTP server crash. |
| 98 re_port = re.compile(port_info, re.MULTILINE) | 98 re_port = re.compile(port_info, re.MULTILINE) |
| 99 if re_port.search(cmd_helper.GetCmdOutput(['lsof', '-nPi:%d' % host_port])): | 99 if re_port.search(cmd_helper.GetCmdOutput(['lsof', '-nPi:%d' % host_port])): |
| 100 return True | 100 return True |
| 101 return False | 101 return False |
| 102 | 102 |
| 103 | 103 |
| 104 def IsDevicePortUsed(adb, device_port, state=''): | 104 def IsDevicePortUsed(device, device_port, state=''): |
| 105 """Checks whether the specified device port is used or not. | 105 """Checks whether the specified device port is used or not. |
| 106 | 106 |
| 107 Args: | 107 Args: |
| 108 adb: Instance of AndroidCommands for talking to the device. | 108 device: A DeviceUtils instance. |
| 109 device_port: Port on device we want to check. | 109 device_port: Port on device we want to check. |
| 110 state: String of the specified state. Default is empty string, which | 110 state: String of the specified state. Default is empty string, which |
| 111 means any state. | 111 means any state. |
| 112 | 112 |
| 113 Returns: | 113 Returns: |
| 114 True if the port on device is already used, otherwise returns False. | 114 True if the port on device is already used, otherwise returns False. |
| 115 """ | 115 """ |
| 116 base_url = '127.0.0.1:%d' % device_port | 116 base_url = '127.0.0.1:%d' % device_port |
| 117 netstat_results = adb.RunShellCommand('netstat', log_result=False) | 117 netstat_results = device.old_interface.RunShellCommand( |
| 118 'netstat', log_result=False) |
| 118 for single_connect in netstat_results: | 119 for single_connect in netstat_results: |
| 119 # Column 3 is the local address which we want to check with. | 120 # Column 3 is the local address which we want to check with. |
| 120 connect_results = single_connect.split() | 121 connect_results = single_connect.split() |
| 121 if connect_results[0] != 'tcp': | 122 if connect_results[0] != 'tcp': |
| 122 continue | 123 continue |
| 123 if len(connect_results) < 6: | 124 if len(connect_results) < 6: |
| 124 raise Exception('Unexpected format while parsing netstat line: ' + | 125 raise Exception('Unexpected format while parsing netstat line: ' + |
| 125 single_connect) | 126 single_connect) |
| 126 is_state_match = connect_results[5] == state if state else True | 127 is_state_match = connect_results[5] == state if state else True |
| 127 if connect_results[3] == base_url and is_state_match: | 128 if connect_results[3] == base_url and is_state_match: |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 client_error = ('Bad response: %s %s version %s\n ' % | 168 client_error = ('Bad response: %s %s version %s\n ' % |
| 168 (r.status, r.reason, r.version) + | 169 (r.status, r.reason, r.version) + |
| 169 '\n '.join([': '.join(h) for h in r.getheaders()])) | 170 '\n '.join([': '.join(h) for h in r.getheaders()])) |
| 170 except (httplib.HTTPException, socket.error) as e: | 171 except (httplib.HTTPException, socket.error) as e: |
| 171 # Probably too quick connecting: try again. | 172 # Probably too quick connecting: try again. |
| 172 exception_error_msgs = traceback.format_exception_only(type(e), e) | 173 exception_error_msgs = traceback.format_exception_only(type(e), e) |
| 173 if exception_error_msgs: | 174 if exception_error_msgs: |
| 174 client_error = ''.join(exception_error_msgs) | 175 client_error = ''.join(exception_error_msgs) |
| 175 # Only returns last client_error. | 176 # Only returns last client_error. |
| 176 return (False, client_error or 'Timeout') | 177 return (False, client_error or 'Timeout') |
| OLD | NEW |