Package rospy :: Module msproxy

Source Code for Module rospy.msproxy

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2008, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # Revision $Id$ 
 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], # remap key 
 53      'setParam': [0], # remap key 
 54      'getParam': [0], # remap key 
 55      'searchParam': [0], # remap key 
 56      'subscribeParam': [0], # remap key 
 57      'unsubscribeParam': [0], # remap key 
 58      'hasParam': [0], # remap key 
 59      'registerService': [0], # remap service 
 60      'lookupService': [0], # remap service 
 61      'unregisterService': [0], # remap service 
 62      'registerSubscriber': [0], # remap topic 
 63      'unregisterSubscriber': [0], # remap topic     
 64      'registerPublisher': [0], # remap topic    
 65      'unregisterPublisher': [0], # remap topic    
 66      'lookupNode': [0], # remap node 
 67      'getPublishedTopics': [0], # remap subgraph 
 68      } 
 69       
70 -class MasterProxy(object):
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
83 - def __init__(self, uri):
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
92 - def __getattr__(self, key): #forward api calls to target
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 #print "Remap indicies", remappings 100 for i in remappings: 101 i = i + 1 #callerId does not count 102 #print "Remap %s => %s"%(args[i], rospy.names.resolve_name(args[i])) 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
109 - def __getitem__(self, key):
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 #NOTE: remapping occurs here! 118 resolved_key = rospy.names.resolve_name(key) 119 if 1: # disable param cache 120 with self._lock: 121 code, msg, value = self.target.getParam(rospy.names.get_caller_id(), resolved_key) 122 if code != 1: #unwrap value with Python semantics 123 raise KeyError(key) 124 return value 125 126 try: 127 # check for value in the parameter server cache 128 return rospy.impl.paramserver.get_param_server_cache().get(resolved_key) 129 except KeyError: 130 # first access, make call to parameter server 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: #unwrap value with Python semantics 134 raise KeyError(key) 135 # set the value in the cache so that it's marked as subscribed 136 rospy.impl.paramserver.get_param_server_cache().set(resolved_key, value) 137 return value
138
139 - def __setitem__(self, key, val):
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 resolved_key = rospy.names.resolve_name(key) 148 149 with self._lock: 150 self.target.setParam(rospy.names.get_caller_id(), resolved_key, val) 151 try: 152 rospy.impl.paramserver.get_param_server_cache().update(resolved_key, val) 153 except KeyError: 154 pass
155
156 - def search_param(self, key):
157 """ 158 Search for a parameter matching key on the parameter server 159 @return: found key or None if search did not succeed 160 @rtype: str 161 @raise ROSException: if parameter server reports an error 162 """ 163 # #1810 searchParam has to use unresolved form of mappings 164 mappings = rospy.names.get_mappings() 165 if key in mappings: 166 key = mappings[key] 167 with self._lock: 168 code, msg, val = self.target.searchParam(rospy.names.get_caller_id(), key) 169 if code == 1: 170 return val 171 elif code == -1: 172 return None 173 else: 174 raise rospy.exceptions.ROSException("cannot search for parameter: %s"%msg)
175
176 - def __delitem__(self, key):
177 """ 178 Delete parameter key from the parameter server. 179 @raise KeyError: if key is not set 180 @raise ROSException: if parameter server reports an error 181 """ 182 resolved_key = rospy.names.resolve_name(key) 183 with self._lock: 184 code, msg, _ = self.target.deleteParam(rospy.names.get_caller_id(), resolved_key) 185 if code == -1: 186 raise KeyError(key) 187 elif code != 1: 188 raise rospy.exceptions.ROSException("cannot delete parameter: %s"%msg) 189 elif 0: #disable parameter cache 190 # set the value in the cache so that it's marked as subscribed 191 rospy.impl.paramserver.get_param_server_cache().delete(resolved_key)
192
193 - def __contains__(self, key):
194 """ 195 Check if parameter is set on Parameter Server 196 @param key: parameter key 197 @type key: str 198 @raise ROSException: if parameter server reports an error 199 """ 200 with self._lock: 201 code, msg, value = self.target.hasParam(rospy.names.get_caller_id(), rospy.names.resolve_name(key)) 202 if code != 1: 203 raise rospy.exceptions.ROSException("cannot check parameter on server: %s"%msg) 204 return value
205
206 - def __iter__(self):
207 """ 208 @raise ROSException: if parameter server reports an error 209 """ 210 with self._lock: 211 code, msg, value = self.target.getParamNames(rospy.names.get_caller_id()) 212 if code == 1: 213 return value.__iter__() 214 else: 215 raise rospy.exceptions.ROSException("cannot retrieve parameter names: %s"%msg)
216