resolvers.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # License: BSD
4 # https://raw.github.com/robotics-in-concert/rocon_multimaster/license/LICENSE
5 #
6 ##############################################################################
7 # Imports
8 ##############################################################################
9 
10 import re
11 import time
12 import rospy
13 import gateway_msgs.msg as gateway_msgs
14 import rocon_python_comms
15 import rosservice
16 import rostopic
17 
18 ##########################################################################
19 # Gateway Existence
20 ##########################################################################
21 
22 
23 def resolve_local_gateway(timeout=None):
24  """
25  @param timeout : timeout on checking for the gateway, if None, it just makes a single attempt.
26  @type rospy.rostime.Duration
27 
28  @raise rocon_python_comms.NotFoundException: if no remote gateways or no matching gateways available.
29  """
30  gateway_namespace = None
31  service_names = []
32  timeout_time = time.time() + timeout.to_sec() if timeout is not None else None
33  while not rospy.is_shutdown():
34  if timeout_time is not None and time.time() > timeout_time:
35  break
36  service_names = rosservice.rosservice_find("gateway_msgs/RemoteGatewayInfo")
37  if not service_names or len(service_names) > 1:
38  gateway_namespace = None
39  elif service_names[0] == '/remote_gateway_info':
40  gateway_namespace = "/"
41  else:
42  gateway_namespace = re.sub(r'/remote_gateway_info', '', service_names[0])
43  if gateway_namespace is not None or timeout is None:
44  break
45  else:
46  rospy.rostime.wallsleep(0.1)
47  if not gateway_namespace:
48  if not service_names:
49  raise rocon_python_comms.NotFoundException("no gateway found attached to this local master - did you start it?")
50  else:
51  raise rocon_python_comms.NotFoundException("found more than one gateway connected to this master, this is an invalid configuration.")
52  #console.debug("Found a local gateway at %s"%gateway_namespace)
53  return gateway_namespace
54 
55 
56 def resolve_connection_cache(timeout=None):
57  """
58  @param timeout : timeout on checking for the connection_cache, if None, it just makes a single attempt.
59  @type rospy.rostime.Duration
60 
61  @raise rocon_python_comms.NotFoundException: if no connection_cache available.
62  """
63  connection_cache_namespace = None
64  topic_names = []
65  timeout_time = time.time() + timeout.to_sec() if timeout is not None else None
66  while not rospy.is_shutdown():
67  if timeout_time is not None and time.time() > timeout_time:
68  break
69  # CAREFUL : This will still return a topic name if it was there once and has disappeared since.
70  topic_names = rostopic.find_by_type("rocon_std_msgs/ConnectionsList")
71  if not topic_names or len(topic_names) > 1:
72  connection_cache_namespace = None
73  elif topic_names[0] == '/connection_cache/list':
74  connection_cache_namespace = "/"
75  else:
76  connection_cache_namespace = re.sub(r'/connection_cache/list', '', topic_names[0])
77  if connection_cache_namespace is not None or timeout is None:
78  break
79  else:
80  rospy.rostime.wallsleep(0.1)
81  if not connection_cache_namespace:
82  if not topic_names:
83  raise rocon_python_comms.NotFoundException("no connection_cache found attached to this local master - did you start it?")
84  else:
85  raise rocon_python_comms.NotFoundException("found more than one connection_cache connected to this master, this is an invalid configuration.")
86  #console.debug("Found a connection_cache at %s"%connection_cache_namespace)
87  return connection_cache_namespace
88 
89 
90 def resolve_gateway_info(gateway_namespace=None):
91  '''
92  @param the local topic namespace to prepend to the 'gateway_info' identifier. Uses
93  resolve_local_gateway if none is specified.
94  @type str
95 
96  @return the local gateway info in all its gory detail.
97  @rtype gateway_msgs.GatewayInfo
98 
99  @raise rocon_gateway.GatewayError: if no remote gateways or no matching gateways available.
100  '''
101  if gateway_namespace == None:
102  gateway_namespace = resolve_local_gateway()
103  gateway_info = rocon_python_comms.SubscriberProxy(gateway_namespace + '/gateway_info', gateway_msgs.GatewayInfo)()
104  return gateway_info
def resolve_gateway_info(gateway_namespace=None)
Definition: resolvers.py:90
def resolve_connection_cache(timeout=None)
Definition: resolvers.py:56
def resolve_local_gateway(timeout=None)
Gateway Existence.
Definition: resolvers.py:23


rocon_gateway_utils
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 14:40:06