proxy_service_caller.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import roslib; roslib.load_manifest('flexbe_core')
4 import rospy
5 from threading import Timer
6 import time
7 
8 from flexbe_core.logger import Logger
9 
10 
11 class ProxyServiceCaller(object):
12  """
13  A proxy for calling services.
14  """
15  _services = {}
16 
17  def __init__(self, topics = {}):
18  """
19  Initializes the proxy with optionally a given set of clients.
20 
21  @type topics: dictionary string - message class
22  @param topics: A dictionay containing a collection of topic - message type pairs.
23  """
24 
25  for topic, msg_type in topics.iteritems():
26  self.setupService(topic, msg_type)
27 
28 
29  def setupService(self, topic, msg_type, persistent=False, wait_duration=10):
30  """
31  Tries to set up a service caller for calling it later.
32 
33  @type topic: string
34  @param topic: The topic of the service to call.
35 
36  @type msg_type: service class
37  @param msg_type: The type of messages of this service.
38 
39  @type persistent: bool
40  @param persistent: Defines if this service caller is persistent.
41 
42  @type wait_duration: int
43  @param wait_duration: Defines how long to wait for the given service if it is not available right now.
44  """
45  if topic not in ProxyServiceCaller._services:
46  warning_sent = False
47  available = False
48  try:
49  t = Timer(1, self._print_wait_warning, [topic])
50  t.start()
51  rospy.wait_for_service(topic, wait_duration)
52  available = True
53  except rospy.exceptions.ROSException, e:
54  available = False
55 
56  try:
57  t.cancel()
58  except Exception as ve:
59  # already printed the warning
60  warning_sent = True
61 
62  if not available:
63  Logger.logerr("Service client %s timed out!" % topic)
64  else:
65  ProxyServiceCaller._services[topic] = rospy.ServiceProxy(topic, msg_type, persistent)
66  if warning_sent:
67  Logger.loginfo("Finally found action client %s..." % (topic))
68 
69 
70  def is_available(self, topic):
71  """
72  Checks if the service on the given topic is available.
73 
74  @type topic: string
75  @param topic: The topic of interest.
76  """
77  return topic in ProxyServiceCaller._services
78 
79 
80  def call(self, topic, request):
81  """
82  Performs a service call on the given topic.
83 
84  @type topic: string
85  @param topic: The topic to call.
86 
87  @type request: service
88  @param request: The request to send to this service.
89  """
90  if topic not in ProxyServiceCaller._services:
91  rospy.logwarn('ProxyServiceCaller: topic not yet registered!')
92  return
93 
94  try:
95  return ProxyServiceCaller._services[topic].call(request)
96  except Exception, e:
97  print 'error: ' + str(e)
98  raise
99 
100 
101  def _print_wait_warning(self, topic):
102  Logger.logwarn("Waiting for service client %s..." % (topic))
def setupService(self, topic, msg_type, persistent=False, wait_duration=10)


flexbe_core
Author(s): Philipp Schillinger
autogenerated on Wed Jun 5 2019 21:51:59