Package rospy :: Module service

Source Code for Module rospy.service

  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  """Base-classes and management of ROS services. 
 36  See L{rospy.tcpros_service} for actual implementation.""" 
 37   
 38   
 39   
 40  import logging 
 41  import traceback 
 42   
 43  from rospy.core import * 
 44   
 45  from rospy.impl.registration import set_service_manager, Registration, get_registration_listeners 
 46  from rospy.impl.transport import * 
 47   
 48  logger = logging.getLogger('rospy.service') 
 49   
50 -class ServiceException(Exception):
51 """Exception class for service-related errors""" 52 pass
53
54 -class _Service(object):
55 """Internal-use superclass for storing service information"""
56 - def __init__(self, name, service_class):
57 self.resolved_name = resolve_name(name) #services remap as well 58 self.service_class = service_class 59 self.request_class = service_class._request_class 60 self.response_class = service_class._response_class 61 self.uri = None #initialize attr
62
63 -class ServiceManager(object):
64 """Keeps track of currently registered services in the ROS system""" 65
66 - def __init__(self, registration_listeners=None):
67 """ 68 ctor 69 @param registration_listeners: override default registration listener. 70 @type registration_listeners: RegistrationListeners 71 """ 72 self.map = {} # {name : Service} 73 self.lock = threading.RLock() 74 if registration_listeners is None: 75 self.registration_listeners = get_registration_listeners() 76 else: 77 self.registration_listeners = registration_listeners
78
79 - def get_services(self):
80 """ 81 @return: List of (service_name, service_uri) for all registered services. 82 @rtype: [(str, str)] 83 """ 84 with self.lock: 85 ret_val = [] 86 for name, service in self.map.items(): 87 ret_val.append((name, service.uri)) 88 services = list(self.map.values()) 89 return ret_val
90
91 - def unregister_all(self):
92 """ 93 Unregister all registered services 94 """ 95 self.map.clear()
96
97 - def register(self, resolved_service_name, service):
98 """ 99 Register service with ServiceManager and ROS master 100 @param resolved_service_name: name of service (resolved) 101 @type resolved_service_name: str 102 @param service: Service to register 103 @type service: L{_Service} 104 """ 105 err = None 106 with self.lock: 107 if resolved_service_name in self.map: 108 err = "service [%s] already registered"%resolved_service_name 109 else: 110 self.map[resolved_service_name] = service 111 112 # NOTE: this call can potentially take a long time under lock and thus needs to be reimplmented 113 self.registration_listeners.notify_added(resolved_service_name, service.uri, Registration.SRV) 114 115 if err: 116 raise ServiceException(err)
117
118 - def unregister(self, resolved_service_name, service):
119 """ 120 Unregister service with L{ServiceManager} and ROS Master 121 @param resolved_service_name: name of service 122 @type resolved_service_name: str 123 @param service: service implementation 124 @type service: L{_Service} 125 """ 126 with self.lock: 127 curr = self.map.get(resolved_service_name, None) 128 if curr == service: 129 del self.map[resolved_service_name] 130 131 # NOTE: this call can potentially take a long time under lock 132 self.registration_listeners.notify_removed(resolved_service_name, service.uri, Registration.SRV)
133
134 - def get_service(self, resolved_service_name):
135 """ 136 @param resolved_service_name: name of service 137 @type resolved_service_name: str 138 @return: service implementation 139 @rtype: _Service 140 """ 141 return self.map.get(resolved_service_name, None)
142 143 set_service_manager(ServiceManager()) 144