Ejemplo n.º 1
0
 def test_XmlRpcNode(self):
     from roslib.xmlrpc import XmlRpcNode
     # 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)
         self.assertEquals(true_port, n.port)
         self.assertEquals(handler, n.handler)
         self.assertEquals(None, n.uri)
         self.assertEquals(None, n.server)
         n.set_uri('http://fake:1234')
         self.assertEquals('http://fake:1234', n.uri)
Ejemplo n.º 2
0
 def test_XmlRpcNode(self):
   from roslib.xmlrpc import XmlRpcNode
   # 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)
     self.assertEquals(true_port, n.port)
     self.assertEquals(handler, n.handler)
     self.assertEquals(None, n.uri)
     self.assertEquals(None, n.server)
     n.set_uri('http://fake:1234')
     self.assertEquals('http://fake:1234', n.uri) 
Ejemplo n.º 3
0
    def __init__(self, master_uri, new_topics_callback):
        self.master_uri          = master_uri
        self.new_topics_callback = new_topics_callback

        name = roslib.names.ns_join(roslib.names.get_ros_namespace(), roslib.names.anonymous_name('master_sync'))
        self.master = rosgraph.masterapi.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)
Ejemplo n.º 4
0
class _RemoteManager(object):
    def __init__(self, master_uri, new_topics_callback):
        self.master_uri          = master_uri
        self.new_topics_callback = new_topics_callback

        name = roslib.names.ns_join(roslib.names.get_ros_namespace(), roslib.names.anonymous_name('master_sync'))
        self.master = rosgraph.masterapi.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 = roslib.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 = roslib.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 roslib.names.ns_join(roslib.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])