blob: f7d8353ba4f5cad02925d26227563c8b30b8158c [file] [log] [blame]
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import httplib
import socket
import xmlrpclib
from autotest_lib.client.cros.faft.config import Config as ClientConfig
class _Method:
"""Class to save the name of the RPC method instead of the real object.
It keeps the name of the RPC method locally first such that the RPC method
can be evalulated to a real object while it is called. Its purpose is to
refer to the latest RPC proxy as the original previous-saved RPC proxy may
be lost due to reboot.
The call_method is the method which does refer to the latest RPC proxy.
def __init__(self, call_method, name):
self.__call_method = call_method
self.__name = name
def __getattr__(self, name):
# Support a nested method.
return _Method(self.__call_method, "%s.%s" % (self.__name, name))
def __call__(self, *args, **dargs):
return self.__call_method(self.__name, *args, **dargs)
class RPCProxy(object):
"""Proxy to the FAFTClient RPC server on DUT.
It acts as a proxy to the FAFTClient on DUT. It is smart enough to:
- postpone the RPC connection to the first class method call;
- reconnect to the RPC server in case connection lost, e.g. reboot;
- always call the latest RPC proxy object.
_client_config = ClientConfig()
def __init__(self, host):
@param host: The host object, passed via the test control file.
self._client = host
self._faft_client = None
def __del__(self):
def __getattr__(self, name):
"""Return a _Method object only, not its real object."""
return _Method(self.__call_faft_client, name)
def __call_faft_client(self, name, *args, **dargs):
"""Make the call on the latest RPC proxy object.
This method gets the internal method of the RPC proxy and calls it.
@param name: Name of the RPC method, a nested method supported.
@param args: The rest of arguments.
@param dargs: The rest of dict-type arguments.
@return: The return value of the FAFTClient RPC method.
return getattr(self._faft_client, name)(*args, **dargs)
except (AttributeError, # _faft_client not initialized, still None
# Reconnect the RPC server in case connection lost, e.g. reboot.
# Try again.
return getattr(self._faft_client, name)(*args, **dargs)
def connect(self):
"""Connect the RPC server."""
self._faft_client = self._client.xmlrpc_connect(
def disconnect(self):
"""Disconnect the RPC server."""
self._faft_client = None