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: msproxy.py 11729 2010-10-23 21:21:54Z kwc $ 
 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  import rospy.core 
 43  import rospy.exceptions 
 44  import rospy.names 
 45   
 46  import rospy.impl.paramserver 
 47  import rospy.impl.masterslave 
 48   
 49  _master_arg_remap = {  
 50      'deleteParam': [0], # remap key 
 51      'setParam': [0], # remap key 
 52      'getParam': [0], # remap key 
 53      'searchParam': [0], # remap key 
 54      'subscribeParam': [0], # remap key 
 55      'unsubscribeParam': [0], # remap key 
 56      'hasParam': [0], # remap key 
 57      'registerService': [0], # remap service 
 58      'lookupService': [0], # remap service 
 59      'unregisterService': [0], # remap service 
 60      'registerSubscriber': [0], # remap topic 
 61      'unregisterSubscriber': [0], # remap topic     
 62      'registerPublisher': [0], # remap topic    
 63      'unregisterPublisher': [0], # remap topic    
 64      'lookupNode': [0], # remap node 
 65      'getPublishedTopics': [0], # remap subgraph 
 66      } 
 67       
68 -class MasterProxy(object):
69 """ 70 Convenience wrapper for ROS master API and XML-RPC 71 implementation. The Master API methods can be invoked on this 72 object and will be forwarded appropriately. Names in arguments 73 will be remapped according to current node settings. Provides 74 dictionary-like access to parameter server, e.g.:: 75 76 master[key] = value 77 78 """ 79
80 - def __init__(self, uri):
81 """ 82 Constructor for wrapping a remote master instance. 83 @param uri: XML-RPC URI of master 84 @type uri: str 85 """ 86 self.target = rospy.core.xmlrpcapi(uri)
87
88 - def __getattr__(self, key): #forward api calls to target
89 f = getattr(self.target, key) 90 if key in _master_arg_remap: 91 remappings = _master_arg_remap[key] 92 else: 93 remappings = rospy.impl.masterslave.ROSHandler.remappings(key) 94 def wrappedF(*args, **kwds): 95 args = [rospy.names.get_caller_id(),]+list(args) 96 #print "Remap indicies", remappings 97 for i in remappings: 98 i = i + 1 #callerId does not count 99 #print "Remap %s => %s"%(args[i], rospy.names.resolve_name(args[i])) 100 args[i] = rospy.names.resolve_name(args[i]) 101 return f(*args, **kwds)
102 return wrappedF 103
104 - def __getitem__(self, key):
105 """ 106 Fetch item from parameter server and subscribe to future updates so that 107 values can be cached. 108 @param key: parameter key 109 @type key: str 110 @raise KeyError: if key is not set 111 """ 112 #NOTE: remapping occurs here! 113 resolved_key = rospy.names.resolve_name(key) 114 if 1: # disable param cache 115 code, msg, value = self.target.getParam(rospy.names.get_caller_id(), resolved_key) 116 if code != 1: #unwrap value with Python semantics 117 raise KeyError(key) 118 return value 119 120 try: 121 # check for value in the parameter server cache 122 return rospy.impl.paramserver.get_param_server_cache().get(resolved_key) 123 except KeyError: 124 # first access, make call to parameter server 125 code, msg, value = self.target.subscribeParam(rospy.names.get_caller_id(), rospy.core.get_node_uri(), resolved_key) 126 if code != 1: #unwrap value with Python semantics 127 raise KeyError(key) 128 # set the value in the cache so that it's marked as subscribed 129 rospy.impl.paramserver.get_param_server_cache().set(resolved_key, value) 130 return value
131
132 - def __setitem__(self, key, val):
133 """ 134 Set parameter value on Parameter Server 135 @param key: parameter key 136 @type key: str 137 @param val: parameter value 138 @type val: XMLRPC legal value 139 """ 140 self.target.setParam(rospy.names.get_caller_id(), rospy.names.resolve_name(key), val)
141
142 - def search_param(self, key):
143 """ 144 Search for a parameter matching key on the parameter server 145 @return: found key or None if search did not succeed 146 @rtype: str 147 @raise ROSException: if parameter server reports an error 148 """ 149 # #1810 searchParam has to use unresolved form of mappings 150 mappings = rospy.names.get_mappings() 151 if key in mappings: 152 key = mappings[key] 153 code, msg, val = self.target.searchParam(rospy.names.get_caller_id(), key) 154 if code == 1: 155 return val 156 elif code == -1: 157 return None 158 else: 159 raise rospy.exceptions.ROSException("cannot search for parameter: %s"%msg)
160
161 - def __delitem__(self, key):
162 """ 163 Delete parameter key from the parameter server. 164 @raise KeyError: if key is not set 165 @raise ROSException: if parameter server reports an error 166 """ 167 resolved_key = rospy.names.resolve_name(key) 168 code, msg, _ = self.target.deleteParam(rospy.names.get_caller_id(), resolved_key) 169 if code == -1: 170 raise KeyError(key) 171 elif code != 1: 172 raise rospy.exceptions.ROSException("cannot delete parameter: %s"%msg) 173 elif 0: #disable parameter cache 174 # set the value in the cache so that it's marked as subscribed 175 rospy.impl.paramserver.get_param_server_cache().delete(resolved_key)
176
177 - def __contains__(self, key):
178 """ 179 Check if parameter is set on Parameter Server 180 @param key: parameter key 181 @type key: str 182 @raise ROSException: if parameter server reports an error 183 """ 184 code, msg, value = self.target.hasParam(rospy.names.get_caller_id(), rospy.names.resolve_name(key)) 185 if code != 1: 186 raise rospy.exceptions.ROSException("cannot check parameter on server: %s"%msg) 187 return value
188
189 - def __iter__(self):
190 """ 191 @raise ROSException: if parameter server reports an error 192 """ 193 code, msg, value = self.target.getParamNames(rospy.names.get_caller_id()) 194 if code == 1: 195 return value.__iter__() 196 else: 197 raise rospy.exceptions.ROSException("cannot retrieve parameter names: %s"%msg)
198