Decorator function for register method for RPC.
    Must be used a RemoteControllerDeamon (-derived) class method!

def rpc(method):
    """
    Decorator function for register method for RPC.
    Must be used a RemoteControllerDeamon (-derived) class method!
    """
    @wraps(method)
    def rpc_wrapper(*args, **kwargs):
        controller_instance = args[0]

        if not isinstance(controller_instance, RemoteControllerDeamon):
            raise Exception("'rpc' decorator must use in a RemoteControllerDeamon (-derived) class method!")

        # Decide the call origin: if the current pid is the daemon pid, this is server side,
        # so call registered method. If not, start a jsonrpc request.
        call_from_server = os.getpid() == controller_instance.get_pid()

        logger.debug("Initiate RPC method '%s' in '%s'", method.__name__, 'daemon' if call_from_server else 'cli')

        if call_from_server:
            return method(*args, **kwargs)
        else:
            url = "http://{}:{}/jsonrpc".format(controller_instance.host, controller_instance.port)
            headers = {'content-type': 'application/json'}

            payload = {
                "method": method.__name__,
                "params": kwargs,
                "jsonrpc": "2.0",
                "id": 0,
            }
            try:
                response = requests.post(url, data=json.dumps(payload), headers=headers).json()
                if 'result' in response:
                    return response['result']
                else:
                    logger.error("There is an error during rpc call. Response:\n%s", json.dumps(response, indent=4))
                    return "Error: {}. (see logs for details)".format(response['error']['message'])
            except requests.exceptions.ConnectionError as e:
                logger.error("Command server not runnning (%s), args: %s, kwargs: %s", e, args, kwargs)
                return None

    rpc_wrapper.registered_for_rpc = method
    return rpc_wrapper