__init__.py
Go to the documentation of this file.
1 import rospy
2 
3 
4 __all__ = ('LazyTransport',)
5 
6 
7 # define a new metaclass which overrides the '__call__' function
8 # See: http://martyalchin.com/2008/jan/10/simple-plugin-framework/
9 class MetaLazyTransport(type):
10 
11  def __call__(cls, *args, **kwargs):
12  """Called when you call LazyTransport()"""
13  obj = type.__call__(cls, *args, **kwargs)
14  obj._post_init()
15  return obj
16 
17 
18 class LazyTransport(rospy.SubscribeListener):
19  __metaclass__ = MetaLazyTransport
20 
21  def __init__(self):
22  super(LazyTransport, self).__init__()
23  self._publishers = []
24  # self._connection_status has 3 meanings
25  # - None: never been subscribed
26  # - False: currently not subscribed but has been subscribed before
27  # - True: currently subscribed
28  self._connection_status = None
29  rospy.Timer(rospy.Duration(5),
30  self._warn_never_subscribed_cb, oneshot=True)
31 
32  def _post_init(self):
33  if not rospy.get_param('~lazy', True):
34  self.subscribe()
35  self._connection_status = True
36 
37  def _warn_never_subscribed_cb(self, timer_event):
38  if self._connection_status is None:
39  rospy.logwarn(
40  '[{name}] subscribes topics only with'
41  " child subscribers. Set '~lazy' as False"
42  ' to have it always transport message.'
43  .format(name=rospy.get_name()))
44 
45  def subscribe(self):
46  raise NotImplementedError('Please overwrite this method')
47 
48  def unsubscribe(self):
49  raise NotImplementedError('Please overwrite this method')
50 
51  def peer_subscribe(self, *args, **kwargs):
52  rospy.logdebug('[{topic}] is subscribed'.format(topic=args[0]))
53  if self._connection_status is not True:
54  self.subscribe()
55  self._connection_status = True
56 
57  def peer_unsubscribe(self, *args, **kwargs):
58  rospy.logdebug('[{topic}] is unsubscribed'.format(topic=args[0]))
59  if not rospy.get_param('~lazy', True):
60  return # do not unsubscribe
61  if self._connection_status in [None, False]:
62  return # no need to unsubscribe
63  for pub in self._publishers:
64  if pub.get_num_connections() > 0:
65  break
66  else:
67  self.unsubscribe()
68  self._connection_status = False
69 
70  def advertise(self, *args, **kwargs):
71  # subscriber_listener should be 'self'
72  # to detect connection and disconnection of the publishing topics
73  assert len(args) < 3 or args[2] is None
74  assert kwargs.get('subscriber_listener') is None
75  kwargs['subscriber_listener'] = self
76 
77  pub = rospy.Publisher(*args, **kwargs)
78  self._publishers.append(pub)
79  return pub
def peer_unsubscribe(self, args, kwargs)
Definition: __init__.py:57
def peer_subscribe(self, args, kwargs)
Definition: __init__.py:51
def advertise(self, args, kwargs)
Definition: __init__.py:70
def __call__(cls, args, kwargs)
Definition: __init__.py:11
def _warn_never_subscribed_cb(self, timer_event)
Definition: __init__.py:37


topic_tools
Author(s): Morgan Quigley, Brian Gerkey
autogenerated on Sun Feb 3 2019 03:30:24