OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Defines the host controller base library. |
| 6 |
| 7 This module is the basis on which host controllers are built and executed. |
| 8 """ |
| 9 |
| 10 import logging |
| 11 import sys |
| 12 |
| 13 #pylint: disable=relative-import |
| 14 import client_lib |
| 15 import common_lib |
| 16 import discovery_server |
| 17 |
| 18 |
| 19 class HostController(object): |
| 20 """The base host controller class.""" |
| 21 |
| 22 def __init__(self): |
| 23 self._discovery_server = discovery_server.DiscoveryServer() |
| 24 |
| 25 def SetUp(self): |
| 26 """Setup method used by the subclass.""" |
| 27 pass |
| 28 |
| 29 def Task(self): |
| 30 """Main task method used by the subclass.""" |
| 31 pass |
| 32 |
| 33 def TearDown(self): |
| 34 """Teardown method used by the subclass.""" |
| 35 pass |
| 36 |
| 37 def NewClient(self, *args, **kwargs): |
| 38 controller = client_lib.ClientController(*args, **kwargs) |
| 39 self._discovery_server.RegisterClientCallback( |
| 40 controller.otp, controller.OnConnect) |
| 41 return controller |
| 42 |
| 43 def RunController(self): |
| 44 """Main entry point for the controller.""" |
| 45 print ' '.join(sys.argv) |
| 46 common_lib.InitLogging() |
| 47 self._discovery_server.Start() |
| 48 |
| 49 error = None |
| 50 tb = None |
| 51 try: |
| 52 self.SetUp() |
| 53 self.Task() |
| 54 except Exception as e: |
| 55 # Defer raising exceptions until after TearDown and _TearDown are called. |
| 56 error = e |
| 57 tb = sys.exc_info()[-1] |
| 58 try: |
| 59 self.TearDown() |
| 60 except Exception as e: |
| 61 # Defer raising exceptions until after _TearDown is called. |
| 62 # Note that an error raised here will obscure any errors raised |
| 63 # previously. |
| 64 error = e |
| 65 tb = sys.exc_info()[-1] |
| 66 |
| 67 self._discovery_server.Shutdown() |
| 68 client_lib.ClientController.ReleaseAllControllers() |
| 69 if error: |
| 70 raise error, None, tb #pylint: disable=raising-bad-type |
OLD | NEW |