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 json |
| 11 import logging |
| 12 import sys |
| 13 |
| 14 #pylint: disable=relative-import |
| 15 import client_lib |
| 16 import common_lib |
| 17 import discovery_server |
| 18 |
| 19 |
| 20 class HostController(object): |
| 21 """The base host controller class.""" |
| 22 |
| 23 def __init__(self): |
| 24 self._discovery_server = discovery_server.DiscoveryServer() |
| 25 |
| 26 def SetUp(self): |
| 27 """Setup method used by the subclass.""" |
| 28 pass |
| 29 |
| 30 def Task(self): |
| 31 """Main task method used by the subclass.""" |
| 32 pass |
| 33 |
| 34 def TearDown(self): |
| 35 """Teardown method used by the subclass.""" |
| 36 pass |
| 37 |
| 38 def NewClient(self, *args, **kwargs): |
| 39 controller = client_lib.ClientController(*args, **kwargs) |
| 40 self._discovery_server.RegisterClientCallback( |
| 41 controller.otp, controller.OnConnect) |
| 42 return controller |
| 43 |
| 44 def RunController(self): |
| 45 """Main entry point for the controller.""" |
| 46 common_lib.PrintCommandLine() |
| 47 common_lib.InitLogging() |
| 48 self._discovery_server.Start() |
| 49 |
| 50 error = None |
| 51 tb = None |
| 52 try: |
| 53 self.SetUp() |
| 54 self.Task() |
| 55 except Exception, e: |
| 56 # Defer raising exceptions until after TearDown and _TearDown are called. |
| 57 error = e |
| 58 tb = sys.exc_info()[-1] |
| 59 try: |
| 60 self.TearDown() |
| 61 except Exception, e: |
| 62 # Defer raising exceptions until after _TearDown is called. |
| 63 # Note that an error raised here will obscure any errors raised |
| 64 # previously. |
| 65 error = e |
| 66 tb = sys.exc_info()[-1] |
| 67 |
| 68 self._discovery_server.Shutdown() |
| 69 client_lib.ClientController.ReleaseAllControllers() |
| 70 if error: |
| 71 raise error, None, tb #pylint: disable=raising-bad-type |
OLD | NEW |