Revision bdf7d8c0 lib/rpc.py
b/lib/rpc.py | ||
---|---|---|
49 | 49 |
individual call. |
50 | 50 |
|
51 | 51 |
""" |
52 |
def __init__(self, parent, node): |
|
52 |
def __init__(self, parent, node, address=None): |
|
53 |
"""Constructor for the node controller. |
|
54 |
|
|
55 |
@type parent: L{Client} |
|
56 |
@param parent: the C{Client} instance which holds global parameters for |
|
57 |
the call |
|
58 |
@type node: str |
|
59 |
@param node: the name of the node we connect to; it is used for error |
|
60 |
messages and in cases we the address paramater is not passed |
|
61 |
@type address: str |
|
62 |
@keyword address: the node's address, in case we know it, so that we |
|
63 |
don't need to resolve it; testing shows that httplib has high |
|
64 |
overhead in resolving addresses (even when speficied in /etc/hosts) |
|
65 |
|
|
66 |
""" |
|
53 | 67 |
self.parent = parent |
54 | 68 |
self.node = node |
69 |
if address is None: |
|
70 |
address = node |
|
55 | 71 |
self.failed = False |
56 | 72 |
|
57 |
self.http_conn = hc = httplib.HTTPConnection(node, self.parent.port)
|
|
73 |
self.http_conn = hc = httplib.HTTPConnection(address, parent.port)
|
|
58 | 74 |
try: |
59 | 75 |
hc.connect() |
60 |
hc.putrequest('PUT', "/%s" % self.parent.procedure,
|
|
76 |
hc.putrequest('PUT', "/%s" % parent.procedure, |
|
61 | 77 |
skip_accept_encoding=True) |
62 |
hc.putheader('Content-Length', str(len(parent.body)))
|
|
78 |
hc.putheader('Content-Length', parent.body_length)
|
|
63 | 79 |
hc.endheaders() |
64 | 80 |
hc.send(parent.body) |
65 |
except socket.error, err:
|
|
81 |
except socket.error: |
|
66 | 82 |
logging.exception("Error connecting to node %s", node) |
67 | 83 |
self.failed = True |
68 | 84 |
|
... | ... | |
99 | 115 |
'False' result, which is not good. This overloading of values can |
100 | 116 |
cause bugs. |
101 | 117 |
|
118 |
@var body_length: cached string value of the length of the body (so that |
|
119 |
individual C{NodeController} instances don't have to recompute it) |
|
120 |
|
|
102 | 121 |
""" |
103 | 122 |
result_set = False |
104 | 123 |
result = False |
... | ... | |
112 | 131 |
self.procedure = procedure |
113 | 132 |
self.args = args |
114 | 133 |
self.body = simplejson.dumps(args) |
134 |
self.body_length = str(len(self.body)) |
|
115 | 135 |
|
116 | 136 |
#--- generic connector ------------- |
117 | 137 |
|
118 |
def ConnectList(self, node_list): |
|
138 |
def ConnectList(self, node_list, address_list=None):
|
|
119 | 139 |
"""Add a list of nodes to the target nodes. |
120 | 140 |
|
121 | 141 |
@type node_list: list |
122 | 142 |
@param node_list: the list of node names to connect |
143 |
@type address_list: list or None |
|
144 |
@keyword address_list: either None or a list with node addresses, |
|
145 |
which must have the same length as the node list |
|
123 | 146 |
|
124 | 147 |
""" |
125 |
for node in node_list: |
|
126 |
self.ConnectNode(node) |
|
148 |
if address_list is None: |
|
149 |
address_list = [None for _ in node_list] |
|
150 |
else: |
|
151 |
assert len(node_list) == len(address_list), \ |
|
152 |
"Name and address lists should have the same length" |
|
153 |
for node, address in zip(node_list, address_list): |
|
154 |
self.ConnectNode(node, address) |
|
127 | 155 |
|
128 |
def ConnectNode(self, connect_node):
|
|
156 |
def ConnectNode(self, name, address=None):
|
|
129 | 157 |
"""Add a node to the target list. |
130 | 158 |
|
159 |
@type name: str |
|
160 |
@param name: the node name |
|
161 |
@type address: str |
|
162 |
@keyword address: the node address, if known |
|
163 |
|
|
131 | 164 |
""" |
132 |
self.nc[connect_node] = nc = NodeController(self, connect_node)
|
|
165 |
self.nc[name] = NodeController(self, name, address)
|
|
133 | 166 |
|
134 | 167 |
def GetResults(self): |
135 | 168 |
"""Return the results of the call. |
Also available in: Unified diff