def __init__(self, master_uri, new_topics_callback): self.master_uri = master_uri self.new_topics_callback = new_topics_callback name = rosgraph.names.ns_join(rosgraph.names.get_ros_namespace(), rosgraph.names.anonymous_name('master_sync')) self.master = rosgraph.Master(name, master_uri=self.master_uri) self._lock = threading.RLock() self._type_cache = {} self._subs = {} self._pubs = {} self._srvs = {} self._external_node = XmlRpcNode(rpc_handler=_TopicPubListenerHandler(self._new_topics)) self._external_node.start() timeout_t = time.time() + 5.0 while time.time() < timeout_t and self._external_node.uri is None: time.sleep(0.01)
def __init__(self, master_uri, cb): self.master_uri = master_uri ns = rosgraph.names.get_ros_namespace() anon_name = rosgraph.names.anonymous_name('master_sync') self.master = rosgraph.Master(rosgraph.names.ns_join(ns, anon_name), master_uri=self.master_uri) self.cb = cb self.type_cache = {} self.subs = {} self.pubs = {} self.srvs = {} rpc_handler = TopicPubListenerHandler(self.new_topics) self.external_node = XmlRpcNode(rpc_handler=rpc_handler) self.external_node.start() timeout_t = time.time() + 5. while time.time() < timeout_t and self.external_node.uri is None: time.sleep(0.01)
def configure_external(self): # Start another TCPROSServer to handle requests on the external port # - TODO: allow handlers these to be passed into TCPROSServer constructor self.external_tcpros = external_tcpros = TCPROSServer() tcpros_handler = rosproxy.tcpros.ProxyTCPROSHandler( self.topic_manager, external_tcpros) self._configure_proxy_services(external_tcpros) self._configure_proxy_topics(external_tcpros, tcpros_handler) tcpros_port = rospy.get_param('~tcpros_port', 11312) xmlrpc_port = rospy.get_param('~xmlrpc_port', 11313) rospy.loginfo("reading port configuration: TCPROS[%s] XMLRPC[%s]" % (tcpros_port, xmlrpc_port)) external_tcpros.start_server(port=tcpros_port) # TODO: this may report the address of the wrong interface rospy.loginfo("ROSRPC URI is rosrpc://%s:%s" % (rosgraph.network.get_local_address(), tcpros_port)) # Startup XMLRPC interface so we can do pub/sub master_uri = rosgraph.get_master_uri() name = 'proxy-proxy' protocol_handlers = [tcpros_handler] rpc_handler = rosproxy.handler.ProxyHandler(name, master_uri, self.topic_manager, protocol_handlers) self.external_node = external_node = XmlRpcNode( xmlrpc_port, rpc_handler) # - start the node and wait for init external_node.start() import time timeout_t = time.time() + 10. while time.time() < timeout_t and external_node.uri is None: time.sleep(0.01) rospy.loginfo("XMLRPC interface is up %s" % self.external_node.uri)
class RemoteManager(object): def __init__(self, master_uri, cb): self.master_uri = master_uri ns = rosgraph.names.get_ros_namespace() anon_name = rosgraph.names.anonymous_name('master_sync') self.master = rosgraph.Master(rosgraph.names.ns_join(ns, anon_name), master_uri=self.master_uri) self.cb = cb self.type_cache = {} self.subs = {} self.pubs = {} self.srvs = {} rpc_handler = TopicPubListenerHandler(self.new_topics) self.external_node = XmlRpcNode(rpc_handler=rpc_handler) self.external_node.start() timeout_t = time.time() + 5. while time.time() < timeout_t and self.external_node.uri is None: time.sleep(0.01) def get_topic_type(self, query_topic): query_topic = self.resolve(query_topic) if query_topic in self.type_cache: return self.type_cache[query_topic] else: for topic, topic_type in self.master.getTopicTypes(): self.type_cache[topic] = topic_type if query_topic in self.type_cache: return self.type_cache[query_topic] else: return "*" def subscribe(self, topic): topic = self.resolve(topic) publishers = self.master.registerSubscriber(topic, '*', self.external_node.uri) self.subs[(topic, self.external_node.uri)] = self.master self.new_topics(topic, publishers) def advertise(self, topic, topic_type, uri): topic = self.resolve(topic) # Prevent double-advertisements if (topic, uri) in self.pubs: return # These registrations need to be anonymous so the master doesn't kill us if there is a duplicate name anon_name = rosgraph.names.anonymous_name('master_sync') master = rosgraph.Master(anon_name, master_uri=self.master_uri) rospy.loginfo("Registering (%s,%s) on master %s"%(topic,uri,master.master_uri)) master.registerPublisher(topic, topic_type, uri) self.pubs[(topic, uri)] = master def unadvertise(self, topic, uri): if (topic, uri) in self.pubs: m = self.pubs[(topic,uri)] rospy.loginfo("Unregistering (%s,%s) from master %s"%(topic,uri,m.master_uri)) m.unregisterPublisher(topic,uri) del self.pubs[(topic,uri)] def advertise_list(self, topic, topic_type, uris): topic = self.resolve(topic) unadv = set((t,u) for (t,u) in self.pubs.iterkeys() if t == topic) - set([(topic, u) for u in uris]) for (t,u) in self.pubs.keys(): if (t,u) in unadv: self.unadvertise(t,u) for u in uris: self.advertise(topic, topic_type, u) def lookup_service(self, service_name): service_name = self.resolve(service_name) try: return self.master.lookupService(service_name) except rosgraph.MasterError: return None def advertise_service(self, service_name, uri): # These registrations need to be anonymous so the master doesn't kill us if there is a duplicate name anon_name = rosgraph.names.anonymous_name('master_sync') master = rosgraph.Master(anon_name, master_uri=self.master_uri) if (service_name) in self.srvs: if self.srvs[service_name][0] == uri: return else: self.unadvertise_service(service_name) fake_api = 'http://%s:0'%rosgraph.network.get_host_name() rospy.loginfo("Registering service (%s,%s) on master %s"%(service_name, uri, master.master_uri)) master.registerService(service_name, uri, fake_api) self.srvs[service_name] = (uri, master) def unadvertise_service(self, service_name): if service_name in self.srvs: uri,m = self.srvs[service_name] rospy.loginfo("Unregistering service (%s,%s) from master %s"%(service_name, uri, m.master_uri)) m.unregisterService(service_name, uri) del self.srvs[service_name] def resolve(self, topic): ns = rosgraph.names.namespace(self.master.caller_id) return rosgraph.names.ns_join(ns, topic) def unsubscribe_all(self): for (t,u),m in self.subs.iteritems(): m.unregisterSubscriber(t,u) for t,u in self.pubs.keys(): self.unadvertise(t,u) for s in self.srvs.keys(): self.unadvertise_service(s) def new_topics(self, topic, publishers): self.cb(topic, [p for p in publishers if (topic,p) not in self.pubs])
class _RemoteManager(object): def __init__(self, master_uri, new_topics_callback): self.master_uri = master_uri self.new_topics_callback = new_topics_callback name = rosgraph.names.ns_join(rosgraph.names.get_ros_namespace(), rosgraph.names.anonymous_name('master_sync')) self.master = rosgraph.Master(name, master_uri=self.master_uri) self._lock = threading.RLock() self._type_cache = {} self._subs = {} self._pubs = {} self._srvs = {} self._external_node = XmlRpcNode(rpc_handler=_TopicPubListenerHandler(self._new_topics)) self._external_node.start() timeout_t = time.time() + 5.0 while time.time() < timeout_t and self._external_node.uri is None: time.sleep(0.01) def get_topic_type(self, query_topic): query_topic = self.resolve(query_topic) query_topic_type = self._type_cache.get(query_topic) if query_topic_type: return query_topic_type for topic, topic_type in self.master.getTopicTypes(): self._type_cache[topic] = topic_type return self._type_cache.get(query_topic, '*') def subscribe(self, topic): topic = self.resolve(topic) publishers = self.master.registerSubscriber(topic, '*', self._external_node.uri) self._subs[(topic, self._external_node.uri)] = self.master self._new_topics(topic, publishers) def publishers_updated(self, topic, topic_type, uris): resolved = self.resolve(topic) with self._lock: # Unregister any publishers that no longer exist uris_set = set(uris) for t, uri in self._pubs.keys(): if t == resolved and uri not in uris_set: self.unadvertise(t, uri) # Register new publishers for uri in uris: if (resolved, uri) not in self._pubs: # Registrations need to be anonymous so master doesn't kill us if there's a duplicate name rospy.loginfo('Registering (%s, %s) on master %s' % (resolved, uri, self.master_uri)) anon_name = rosgraph.names.anonymous_name('master_sync') master = rosgraph.masterapi.Master(anon_name, master_uri=self.master_uri) master.registerPublisher(resolved, topic_type, uri) self._pubs[(resolved, uri)] = master def unadvertise(self, topic, uri): with self._lock: if (topic, uri) in self._pubs: master = self._pubs[(topic, uri)] rospy.loginfo('Unregistering (%s, %s) from master %s' % (topic, uri, master.master_uri)) master.unregisterPublisher(topic, uri) del self._pubs[(topic, uri)] def lookup_service(self, service_name): service_name = self.resolve(service_name) try: return self.master.lookupService(service_name) except: return None def advertise_service(self, service_name, uri): # These registrations need to be anonymous so the master doesn't kill us if there is a duplicate name anon_name = rosgraph.names.anonymous_name('master_sync') master = rosgraph.masterapi.Master(anon_name, master_uri=self.master_uri) if service_name in self._srvs: if self._srvs[service_name][0] == uri: return self.unadvertise_service(service_name) rospy.loginfo('Registering service (%s, %s) on master %s' % (service_name, uri, master.master_uri)) fake_api = 'http://%s:0' % roslib.network.get_host_name() master.registerService(service_name, uri, fake_api) self._srvs[service_name] = (uri, master) def unadvertise_service(self, service_name): if service_name in self._srvs: uri, master = self._srvs[service_name] rospy.loginfo('Unregistering service (%s, %s) from master %s' % (service_name, uri, master.master_uri)) master.unregisterService(service_name, uri) del self._srvs[service_name] def resolve(self, topic): return rosgraph.names.ns_join(rosgraph.names.namespace(self.master.caller_id), topic) def unsubscribe_all(self): for (topic, uri), master in self._subs.iteritems(): master.unregisterSubscriber(topic, uri) for topic, uri in self._pubs.keys(): self.unadvertise(topic, uri) for service in self._srvs.keys(): self.unadvertise_service(service) def _new_topics(self, topic, publishers): self.new_topics_callback(topic, [p for p in publishers if (topic, p) not in self._pubs])
def test_XmlRpcNode(): from rosgraph.xmlrpc import XmlRpcNode, XmlRpcHandler # not a very comprehensive test (yet) #port, handler tests = [ (None, None, None), (8080, None, 8080), ('8080', None, 8080), (u'8080', None, 8080), ] for port, handler, true_port in tests: n = XmlRpcNode(port, handler) assert true_port == n.port assert handler == n.handler assert None == n.uri assert None == n.server n.set_uri('http://fake:1234') assert 'http://fake:1234' == n.uri n.start() start = time.time() while not n.uri and time.time() - start < 5.: time.sleep(0.00001) #poll for XMLRPC init assert n.uri n.shutdown('test case') # get coverage on run init n = XmlRpcNode(0, XmlRpcHandler()) n._run_init() n.shutdown('test case') # mock in server in order to play with _run() n.server = mock.Mock() n.is_shutdown = False n._run_init = mock.Mock() n.server.serve_forever.side_effect = IOError(1, 'boom') n._run()
def test_XmlRpcNode(): from rosgraph.xmlrpc import XmlRpcNode, XmlRpcHandler # not a very comprehensive test (yet) #port, handler tests = [ (None, None, None), (8080, None, 8080), ('8080', None, 8080), (u'8080', None, 8080), ] for port, handler,true_port in tests: n = XmlRpcNode(port, handler) assert true_port == n.port assert handler == n.handler assert None == n.uri assert None == n.server n.set_uri('http://fake:1234') assert 'http://fake:1234' == n.uri n.start() start = time.time() while not n.uri and time.time() - start < 5.: time.sleep(0.00001) #poll for XMLRPC init assert n.uri n.shutdown('test case') # get coverage on run init n = XmlRpcNode(0, XmlRpcHandler()) n._run_init() n.shutdown('test case') # mock in server in order to play with _run() n.server = mock.Mock() n.is_shutdown = False n._run_init = mock.Mock() n.server.serve_forever.side_effect = IOError(1, 'boom') n._run()