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 """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
51 """Exception class for service-related errors"""
52 pass
53
55 """Internal-use superclass for storing service information"""
56 - def __init__(self, name, service_class):
57 self.resolved_name = resolve_name(name)
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
62
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 = {}
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
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
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
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
132 self.registration_listeners.notify_removed(resolved_service_name, service.uri, Registration.SRV)
133
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