1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 """
35 Master/Slave XML-RPC Wrappers.
36
37 The L{MasterProxy} simplifies usage of master/slave
38 APIs by automatically inserting the caller ID and also adding python
39 dictionary accessors on the parameter server.
40 """
41
42 from threading import Lock
43
44 import rospy.core
45 import rospy.exceptions
46 import rospy.names
47
48 import rospy.impl.paramserver
49 import rospy.impl.masterslave
50
51 _master_arg_remap = {
52 'deleteParam': [0],
53 'setParam': [0],
54 'getParam': [0],
55 'searchParam': [0],
56 'subscribeParam': [0],
57 'unsubscribeParam': [0],
58 'hasParam': [0],
59 'registerService': [0],
60 'lookupService': [0],
61 'unregisterService': [0],
62 'registerSubscriber': [0],
63 'unregisterSubscriber': [0],
64 'registerPublisher': [0],
65 'unregisterPublisher': [0],
66 'lookupNode': [0],
67 'getPublishedTopics': [0],
68 }
69
71 """
72 Convenience wrapper for ROS master API and XML-RPC
73 implementation. The Master API methods can be invoked on this
74 object and will be forwarded appropriately. Names in arguments
75 will be remapped according to current node settings. Provides
76 dictionary-like access to parameter server, e.g.::
77
78 master[key] = value
79
80 All methods are thread-safe.
81 """
82
84 """
85 Constructor for wrapping a remote master instance.
86 @param uri: XML-RPC URI of master
87 @type uri: str
88 """
89 self.target = rospy.core.xmlrpcapi(uri)
90 self._lock = Lock()
91
93 if key in _master_arg_remap:
94 remappings = _master_arg_remap[key]
95 else:
96 remappings = rospy.impl.masterslave.ROSHandler.remappings(key)
97 def wrappedF(*args, **kwds):
98 args = [rospy.names.get_caller_id(),]+list(args)
99
100 for i in remappings:
101 i = i + 1
102
103 args[i] = rospy.names.resolve_name(args[i])
104 with self._lock:
105 f = getattr(self.target, key)
106 return f(*args, **kwds)
107 return wrappedF
108
110 """
111 Fetch item from parameter server and subscribe to future updates so that
112 values can be cached.
113 @param key: parameter key
114 @type key: str
115 @raise KeyError: if key is not set
116 """
117
118 resolved_key = rospy.names.resolve_name(key)
119 if 1:
120 with self._lock:
121 code, msg, value = self.target.getParam(rospy.names.get_caller_id(), resolved_key)
122 if code != 1:
123 raise KeyError(key)
124 return value
125
126 try:
127
128 return rospy.impl.paramserver.get_param_server_cache().get(resolved_key)
129 except KeyError:
130
131 with self._lock:
132 code, msg, value = self.target.subscribeParam(rospy.names.get_caller_id(), rospy.core.get_node_uri(), resolved_key)
133 if code != 1:
134 raise KeyError(key)
135
136 rospy.impl.paramserver.get_param_server_cache().set(resolved_key, value)
137 return value
138
140 """
141 Set parameter value on Parameter Server
142 @param key: parameter key
143 @type key: str
144 @param val: parameter value
145 @type val: XMLRPC legal value
146 """
147 with self._lock:
148 self.target.setParam(rospy.names.get_caller_id(), rospy.names.resolve_name(key), val)
149
151 """
152 Search for a parameter matching key on the parameter server
153 @return: found key or None if search did not succeed
154 @rtype: str
155 @raise ROSException: if parameter server reports an error
156 """
157
158 mappings = rospy.names.get_mappings()
159 if key in mappings:
160 key = mappings[key]
161 with self._lock:
162 code, msg, val = self.target.searchParam(rospy.names.get_caller_id(), key)
163 if code == 1:
164 return val
165 elif code == -1:
166 return None
167 else:
168 raise rospy.exceptions.ROSException("cannot search for parameter: %s"%msg)
169
171 """
172 Delete parameter key from the parameter server.
173 @raise KeyError: if key is not set
174 @raise ROSException: if parameter server reports an error
175 """
176 resolved_key = rospy.names.resolve_name(key)
177 with self._lock:
178 code, msg, _ = self.target.deleteParam(rospy.names.get_caller_id(), resolved_key)
179 if code == -1:
180 raise KeyError(key)
181 elif code != 1:
182 raise rospy.exceptions.ROSException("cannot delete parameter: %s"%msg)
183 elif 0:
184
185 rospy.impl.paramserver.get_param_server_cache().delete(resolved_key)
186
188 """
189 Check if parameter is set on Parameter Server
190 @param key: parameter key
191 @type key: str
192 @raise ROSException: if parameter server reports an error
193 """
194 with self._lock:
195 code, msg, value = self.target.hasParam(rospy.names.get_caller_id(), rospy.names.resolve_name(key))
196 if code != 1:
197 raise rospy.exceptions.ROSException("cannot check parameter on server: %s"%msg)
198 return value
199
201 """
202 @raise ROSException: if parameter server reports an error
203 """
204 with self._lock:
205 code, msg, value = self.target.getParamNames(rospy.names.get_caller_id())
206 if code == 1:
207 return value.__iter__()
208 else:
209 raise rospy.exceptions.ROSException("cannot retrieve parameter names: %s"%msg)
210