OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Provides an interface to start and stop Android emulator. | 6 """Provides an interface to start and stop Android emulator. |
7 | 7 |
8 Assumes system environment ANDROID_NDK_ROOT has been set. | 8 Assumes system environment ANDROID_NDK_ROOT has been set. |
9 | 9 |
10 Emulator: The class provides the methods to launch/shutdown the emulator with | 10 Emulator: The class provides the methods to launch/shutdown the emulator with |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 if not emulators: | 43 if not emulators: |
44 return | 44 return |
45 for emu_name in emulators: | 45 for emu_name in emulators: |
46 cmd_helper.GetCmdOutput(['adb', '-s', emu_name, 'emu', 'kill']) | 46 cmd_helper.GetCmdOutput(['adb', '-s', emu_name, 'emu', 'kill']) |
47 logging.info('Emulator killing is async; give a few seconds for all to die.') | 47 logging.info('Emulator killing is async; give a few seconds for all to die.') |
48 for i in range(5): | 48 for i in range(5): |
49 if not android_commands.GetEmulators(): | 49 if not android_commands.GetEmulators(): |
50 return | 50 return |
51 time.sleep(1) | 51 time.sleep(1) |
52 | 52 |
| 53 |
| 54 class PortPool(object): |
| 55 """Pool for emulator port starting position that changes over time.""" |
| 56 _port_min = 5554 |
| 57 _port_max = 5585 |
| 58 _port_current_index = 0 |
| 59 |
| 60 @classmethod |
| 61 def port_range(cls): |
| 62 """Return a range of valid ports for emulator use. |
| 63 |
| 64 The port must be an even number between 5554 and 5584. Sometimes |
| 65 a killed emulator "hangs on" to a port long enough to prevent |
| 66 relaunch. This is especially true on slow machines (like a bot). |
| 67 Cycling through a port start position helps make us resilient.""" |
| 68 ports = range(cls._port_min, cls._port_max, 2) |
| 69 n = cls._port_current_index |
| 70 cls._port_current_index = (n + 1) % len(ports) |
| 71 return ports[n:] + ports[:n] |
| 72 |
| 73 |
53 def _GetAvailablePort(): | 74 def _GetAvailablePort(): |
54 """Returns an available TCP port for the console.""" | 75 """Returns an available TCP port for the console.""" |
55 used_ports = [] | 76 used_ports = [] |
56 emulators = android_commands.GetEmulators() | 77 emulators = android_commands.GetEmulators() |
57 for emulator in emulators: | 78 for emulator in emulators: |
58 used_ports.append(emulator.split('-')[1]) | 79 used_ports.append(emulator.split('-')[1]) |
59 # The port must be an even number between 5554 and 5584. | 80 for port in PortPool.port_range(): |
60 for port in range(5554, 5585, 2): | |
61 if str(port) not in used_ports: | 81 if str(port) not in used_ports: |
62 return port | 82 return port |
63 | 83 |
64 | 84 |
65 class Emulator(object): | 85 class Emulator(object): |
66 """Provides the methods to lanuch/shutdown the emulator. | 86 """Provides the methods to lanuch/shutdown the emulator. |
67 | 87 |
68 The emulator has the android virtual device named 'buildbot'. | 88 The emulator has the android virtual device named 'buildbot'. |
69 | 89 |
70 The emulator could use any even TCP port between 5554 and 5584 for the | 90 The emulator could use any even TCP port between 5554 and 5584 for the |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 """Install a handler to kill the emulator when we exit unexpectedly.""" | 244 """Install a handler to kill the emulator when we exit unexpectedly.""" |
225 for sig in self._SIGNALS: | 245 for sig in self._SIGNALS: |
226 signal.signal(sig, self._ShutdownOnSignal) | 246 signal.signal(sig, self._ShutdownOnSignal) |
227 | 247 |
228 def main(argv): | 248 def main(argv): |
229 Emulator().launch() | 249 Emulator().launch() |
230 | 250 |
231 | 251 |
232 if __name__ == '__main__': | 252 if __name__ == '__main__': |
233 main(sys.argv) | 253 main(sys.argv) |
OLD | NEW |