Package rospy :: Package impl :: Module paramserver

Source Code for Module rospy.impl.paramserver

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2009, 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  """Parameter Server Cache""" 
 34   
 35  from __future__ import with_statement 
 36  import threading 
 37   
 38  #TODO: get rid of this routine entirely. It doesn't work with the current PS representation. 
39 -class ParamServerCache(object):
40 """ 41 Cache of values on the parameter server. Implementation 42 is just a thread-safe dictionary. 43 """ 44
45 - def __init__(self):
46 self.lock = threading.Lock() 47 self.d = {} 48 self.notifier = None
49 50 ## Delete parameter from cache
51 - def delete(self, key):
52 with self.lock: 53 del self.d[key]
54
55 - def set_notifier(self, notifier):
56 """ 57 Notifier implements any parameter subscription logic. The 58 notifier should be a function that takes in a key and value 59 that represents a parameter update. Notifier is called under 60 lock and thus must not implement any lengthy computation. 61 """ 62 self.notifier = notifier
63
64 - def update(self, key, value):
65 """ 66 Update the value of the parameter in the cache 67 @param key: parameter key 68 @type key: str 69 @param value: parameter value 70 @type value: str 71 @raise: KeyError if key is not already in the cache. 72 """ 73 # TODOXXX: remove this code, as it is wrong. It is remaining 74 # in place to maintain a unit test until the correct logic is 75 # implemented. update() needs to check all subscribed keys and 76 # do a prefix match. The best way to do this is probably to 77 # pull the paramserver implementation from rosmaster and use 78 # it here. 79 if not key in self.d: 80 raise KeyError(key) 81 with self.lock: 82 self.d[key] = value 83 if self.notifier is not None: 84 self.notifier(key, value)
85
86 - def set(self, key, value):
87 """ 88 Set the value of the parameter in the cache. This is a 89 prerequisite of calling update(). 90 @param key: parameter key 91 @type key: str 92 @param value: parameter value 93 @type value: str 94 """ 95 with self.lock: 96 self.d[key] = value
97
98 - def get(self, key):
99 """ 100 @param key: parameter key 101 @type key: str 102 @return: Current value for parameter 103 @raise: KeyError 104 """ 105 with self.lock: 106 return self.d[key]
107 108 _param_server_cache = None
109 -def get_param_server_cache():
110 """ 111 Get a handle on the client-wide parameter server cache 112 """ 113 global _param_server_cache 114 if _param_server_cache is None: 115 _param_server_cache = ParamServerCache() 116 return _param_server_cache
117