1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 Red Hat, Inc.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
18
A helper class for proxy objects to remote APIs.
20
For more information about rpc API version numbers, see:
28
class RpcProxy(object):
29
"""A helper class for rpc clients.
31
This class is a wrapper around the RPC client API. It allows you to
32
specify the topic and API version in a single place. This is intended to
33
be used as a base class for a class that implements the client side of an
37
def __init__(self, topic, default_version):
38
"""Initialize an RpcProxy.
40
:param topic: The topic to use for all messages.
41
:param default_version: The default API version to request in all
42
outgoing messages. This can be overridden on a per-message
46
self.default_version = default_version
47
super(RpcProxy, self).__init__()
49
def _set_version(self, msg, vers):
50
"""Helper method to set the version in a message.
52
:param msg: The message having a version added to it.
53
:param vers: The version number to add to the message.
55
msg['version'] = vers if vers else self.default_version
57
def _get_topic(self, topic):
58
"""Return the topic to use for a message."""
59
return topic if topic else self.topic
62
def make_msg(method, **kwargs):
63
return {'method': method, 'args': kwargs}
65
def call(self, context, msg, topic=None, version=None, timeout=None):
66
"""rpc.call() a remote method.
68
:param context: The request context
69
:param msg: The message to send, including the method and args.
70
:param topic: Override the topic for this message.
71
:param timeout: (Optional) A timeout to use when waiting for the
72
response. If no timeout is specified, a default timeout will be
73
used that is usually sufficient.
74
:param version: (Optional) Override the requested API version in this
77
:returns: The return value from the remote method.
79
self._set_version(msg, version)
80
return rpc.call(context, self._get_topic(topic), msg, timeout)
82
def multicall(self, context, msg, topic=None, version=None, timeout=None):
83
"""rpc.multicall() a remote method.
85
:param context: The request context
86
:param msg: The message to send, including the method and args.
87
:param topic: Override the topic for this message.
88
:param timeout: (Optional) A timeout to use when waiting for the
89
response. If no timeout is specified, a default timeout will be
90
used that is usually sufficient.
91
:param version: (Optional) Override the requested API version in this
94
:returns: An iterator that lets you process each of the returned values
95
from the remote method as they arrive.
97
self._set_version(msg, version)
98
return rpc.multicall(context, self._get_topic(topic), msg, timeout)
100
def cast(self, context, msg, topic=None, version=None):
101
"""rpc.cast() a remote method.
103
:param context: The request context
104
:param msg: The message to send, including the method and args.
105
:param topic: Override the topic for this message.
106
:param version: (Optional) Override the requested API version in this
109
:returns: None. rpc.cast() does not wait on any return value from the
112
self._set_version(msg, version)
113
rpc.cast(context, self._get_topic(topic), msg)
115
def fanout_cast(self, context, msg, version=None):
116
"""rpc.fanout_cast() a remote method.
118
:param context: The request context
119
:param msg: The message to send, including the method and args.
120
:param version: (Optional) Override the requested API version in this
123
:returns: None. rpc.fanout_cast() does not wait on any return value
124
from the remote method.
126
self._set_version(msg, version)
127
rpc.fanout_cast(context, self.topic, msg)
129
def cast_to_server(self, context, server_params, msg, topic=None,
131
"""rpc.cast_to_server() a remote method.
133
:param context: The request context
134
:param server_params: Server parameters. See rpc.cast_to_server() for
136
:param msg: The message to send, including the method and args.
137
:param topic: Override the topic for this message.
138
:param version: (Optional) Override the requested API version in this
141
:returns: None. rpc.cast_to_server() does not wait on any
144
self._set_version(msg, version)
145
rpc.cast_to_server(context, server_params, self._get_topic(topic), msg)
147
def fanout_cast_to_server(self, context, server_params, msg, version=None):
148
"""rpc.fanout_cast_to_server() a remote method.
150
:param context: The request context
151
:param server_params: Server parameters. See rpc.cast_to_server() for
153
:param msg: The message to send, including the method and args.
154
:param version: (Optional) Override the requested API version in this
157
:returns: None. rpc.fanout_cast_to_server() does not wait on any
160
self._set_version(msg, version)
161
rpc.fanout_cast_to_server(context, server_params, self.topic, msg)