Package rosh :: Module future
[frames] | no frames]

Source Code for Module rosh.future

 1  #TODO: disabling until rospy can talk to multiple masters correctly 
2 -def __set_master(name='localhost'):
3 """ 4 Switch the ROS master currently in use, e.g. master('prh'). If 5 master() is called with no args, defaults to localhost. 6 7 @param name: name of master (if running on default port), or full URI 8 @type name: str 9 """ 10 11 # TODO: rospy needs to have built-in multimaster support for this 12 # to actually work, or we need to get rid of the node singleton 13 14 if name.startswith('http://'): 15 ctx.master._reinit(name) 16 else: 17 # assume its a hostname 18 ctx.master._reinit('http://%s:11311'%name) 19 20 # update the system-wide environment 21 os.environ[roslib.rosenv.ROS_MASTER_URI] = ctx.master.master_uri 22 return ctx.master.is_online()
23
24 -class _Relay(object):
25
26 - def __init__(self, from_topic, to_topic):
27 self.from_topic = from_topic 28 self.to_topic = to_topic
29
30 - def __call__(self, msg):
31 # check for race condition 32 to_topic = self.to_topic 33 if to_topic is None: 34 return 35 36 # lazy-init new topic 37 if to_topic._type_name is None: 38 try: 39 to_topic._init(msg._type) 40 except: 41 #TODO: log properly 42 print >> sys.stderr, "FAILED to initialize, destroying relay [%s] | [%s]"%(self.from_topic, self.to_topic) 43 self._unrelay() 44 45 if msg._type == to_topic._type_name: 46 to_topic(msg) 47 else: 48 #TODO: WARN user once 49 pass
50
51 - def _unrelay(self):
52 if self.from_topic is not None: 53 self.from_topic._remove_subscriber_callback(self) 54 self.from_topic = self.to_topic = None
55
56 - def __del__(self):
57 self._unrelay()
58
59 -class _FilteredRelay(_Relay):
60
61 - def __init__(self, from_topic, to_topic, filter_fn):
62 super(_FilteredRelay, self).__init__(from_topic, to_topic) 63 self.filter_fn = filter_fn
64
65 - def __call__(self, msg):
66 msg = self.filter_fn(msg) 67 super(_FilteredRelay, self).__call__(msg)
68
69 -def relay(from_topic, to_topic, filter_fn=None):
70 if filter_fn: 71 relay_obj = _FilteredRelay(from_topic, to_topic, filter_fn) 72 else: 73 #TODO: replace with topic_tools launch 74 relay_obj = _Relay(from_topic, to_topic) 75 from_topic._add_subscriber_callback(relay_obj) 76 return relay_obj
77
78 -def unrelay(relay_obj):
79 relay_obj._unrelay()
80