Tom Wai-Hong Tam | 2b1ef4f | 2013-07-20 00:54:48 +0800 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS 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 | import httplib |
| 6 | import socket |
| 7 | import xmlrpclib |
| 8 | |
| 9 | from autotest_lib.client.cros.faft.config import Config as ClientConfig |
| 10 | |
| 11 | |
| 12 | class _Method: |
| 13 | """Class to save the name of the RPC method instead of the real object. |
| 14 | |
| 15 | It keeps the name of the RPC method locally first such that the RPC method |
| 16 | can be evalulated to a real object while it is called. Its purpose is to |
| 17 | refer to the latest RPC proxy as the original previous-saved RPC proxy may |
| 18 | be lost due to reboot. |
| 19 | |
| 20 | The call_method is the method which does refer to the latest RPC proxy. |
| 21 | """ |
| 22 | def __init__(self, call_method, name): |
| 23 | self.__call_method = call_method |
| 24 | self.__name = name |
| 25 | |
| 26 | def __getattr__(self, name): |
| 27 | # Support a nested method. |
| 28 | return _Method(self.__call_method, "%s.%s" % (self.__name, name)) |
| 29 | |
| 30 | def __call__(self, *args, **dargs): |
| 31 | return self.__call_method(self.__name, *args, **dargs) |
| 32 | |
| 33 | |
| 34 | class RPCProxy(object): |
| 35 | """Proxy to the FAFTClient RPC server on DUT. |
| 36 | |
| 37 | It acts as a proxy to the FAFTClient on DUT. It is smart enough to: |
| 38 | - postpone the RPC connection to the first class method call; |
| 39 | - reconnect to the RPC server in case connection lost, e.g. reboot; |
| 40 | - always call the latest RPC proxy object. |
| 41 | """ |
| 42 | _client_config = ClientConfig() |
| 43 | |
| 44 | def __init__(self, host): |
| 45 | """Constructor. |
| 46 | |
| 47 | @param host: The host object, passed via the test control file. |
| 48 | """ |
| 49 | self._client = host |
| 50 | self._faft_client = None |
| 51 | |
| 52 | def __del__(self): |
| 53 | self.disconnect() |
| 54 | |
| 55 | def __getattr__(self, name): |
| 56 | """Return a _Method object only, not its real object.""" |
| 57 | return _Method(self.__call_faft_client, name) |
| 58 | |
| 59 | def __call_faft_client(self, name, *args, **dargs): |
| 60 | """Make the call on the latest RPC proxy object. |
| 61 | |
| 62 | This method gets the internal method of the RPC proxy and calls it. |
| 63 | |
| 64 | @param name: Name of the RPC method, a nested method supported. |
| 65 | @param args: The rest of arguments. |
| 66 | @param dargs: The rest of dict-type arguments. |
| 67 | @return: The return value of the FAFTClient RPC method. |
| 68 | """ |
| 69 | try: |
| 70 | return getattr(self._faft_client, name)(*args, **dargs) |
| 71 | except (AttributeError, # _faft_client not initialized, still None |
| 72 | socket.error, |
| 73 | httplib.BadStatusLine, |
| 74 | xmlrpclib.ProtocolError): |
| 75 | # Reconnect the RPC server in case connection lost, e.g. reboot. |
| 76 | self.connect() |
| 77 | # Try again. |
| 78 | return getattr(self._faft_client, name)(*args, **dargs) |
| 79 | |
| 80 | def connect(self): |
| 81 | """Connect the RPC server.""" |
| 82 | self._faft_client = self._client.xmlrpc_connect( |
| 83 | self._client_config.rpc_command, |
| 84 | self._client_config.rpc_port, |
| 85 | command_name=self._client_config.rpc_command_short, |
| 86 | ready_test_name=self._client_config.rpc_ready_call, |
Yusuf Mohsinally | fff89d6 | 2013-11-18 16:34:07 -0800 | [diff] [blame^] | 87 | timeout_seconds=self._client_config.rpc_timeout, |
| 88 | logfile=self._client_config.rpc_logfile) |
Tom Wai-Hong Tam | 2b1ef4f | 2013-07-20 00:54:48 +0800 | [diff] [blame] | 89 | |
| 90 | def disconnect(self): |
| 91 | """Disconnect the RPC server.""" |
Tom Wai-Hong Tam | 161e1c1 | 2013-09-18 12:55:55 +0800 | [diff] [blame] | 92 | self._client.rpc_disconnect(self._client_config.rpc_port) |
Tom Wai-Hong Tam | 2b1ef4f | 2013-07-20 00:54:48 +0800 | [diff] [blame] | 93 | self._faft_client = None |