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