示例#1
0
    def test_caller_id(self):
        from rospy.names import get_caller_id, get_name, _set_caller_id, get_namespace
        # test get_name, get_caller_id, and _set_caller_id
        try:
            self.assertEquals('/unnamed', get_name())
            self.assertEquals('/', get_namespace())
            _set_caller_id('/foo')
            self.assertEquals('/foo', get_name())
            self.assertEquals('/', get_namespace())
            _set_caller_id('/foo/bar')
            self.assertEquals('/foo/bar', get_name())
            self.assertEquals('/foo/', get_namespace())
        finally:
            _set_caller_id('/unnamed')

        # older get_caller_id usage
        try:
            self.assertEquals('/unnamed', get_caller_id())
            self.assertEquals('/', get_namespace())
            _set_caller_id('/foo')
            self.assertEquals('/foo', get_caller_id())
            self.assertEquals('/', get_namespace())
            _set_caller_id('/foo/bar')
            self.assertEquals('/foo/bar', get_caller_id())
            self.assertEquals('/foo/', get_namespace())
        finally:
            _set_caller_id('/unnamed')
示例#2
0
    def test_caller_id(self):
        from rospy.names import get_caller_id, get_name, _set_caller_id, get_namespace
        # test get_name, get_caller_id, and _set_caller_id
        try:
            self.assertEquals('/unnamed', get_name())
            self.assertEquals('/', get_namespace())
            _set_caller_id('/foo')
            self.assertEquals('/foo', get_name())
            self.assertEquals('/', get_namespace())
            _set_caller_id('/foo/bar')
            self.assertEquals('/foo/bar', get_name())
            self.assertEquals('/foo/', get_namespace())
        finally:
            _set_caller_id('/unnamed')

        # older get_caller_id usage
        try:
            self.assertEquals('/unnamed', get_caller_id())
            self.assertEquals('/', get_namespace())
            _set_caller_id('/foo')
            self.assertEquals('/foo', get_caller_id())
            self.assertEquals('/', get_namespace())
            _set_caller_id('/foo/bar')
            self.assertEquals('/foo/bar', get_caller_id())
            self.assertEquals('/foo/', get_namespace())
        finally:
            _set_caller_id('/unnamed')
示例#3
0
    def test_caller_id(self):
        from rospy.names import get_caller_id, get_name, _set_caller_id, get_namespace

        # test get_name, get_caller_id, and _set_caller_id
        try:
            self.assertEquals("/unnamed", get_name())
            self.assertEquals("/", get_namespace())
            _set_caller_id("/foo")
            self.assertEquals("/foo", get_name())
            self.assertEquals("/", get_namespace())
            _set_caller_id("/foo/bar")
            self.assertEquals("/foo/bar", get_name())
            self.assertEquals("/foo/", get_namespace())
        finally:
            _set_caller_id("/unnamed")

        # older get_caller_id usage
        try:
            self.assertEquals("/unnamed", get_caller_id())
            self.assertEquals("/", get_namespace())
            _set_caller_id("/foo")
            self.assertEquals("/foo", get_caller_id())
            self.assertEquals("/", get_namespace())
            _set_caller_id("/foo/bar")
            self.assertEquals("/foo/bar", get_caller_id())
            self.assertEquals("/foo/", get_namespace())
        finally:
            _set_caller_id("/unnamed")
示例#4
0
    def __init__(self, name, port=0, rpc_handler=None):
        """
        Node constructor
        @param name: name of this node
        @type  name: str
        @param port: port to use for starting XML-RPC API. Set to 0 or omit to bind to any available port.
        @type port: int
        @param rpc_handler: XML-RPC API handler for node. 
        @type  rpc_handler: roslib.xmlrpc.XmlRpcHandler
        """
        super(ROSNode, self).__init__(port, rpc_handler)
        self.name = scoped_name(get_namespace(), name) #use the de-contexted name
        #TODO: move out of ROSNode
        _set_caller_id(name) #update global name context

        logging.getLogger('rospy.msnode').info("Node initialized: callerId[%s] local name[%s] port[%s]", get_caller_id(), self.name, self.port)
    def start(self, uri, master_uri):
        """
        Start the RegManager. This should be passed in as an argument to a thread
        starter as the RegManager is designed to spin in its own thread
        @param uri: URI of local node
        @type  uri: str
        @param master_uri: Master URI
        @type  master_uri: str
        """
        self.registered = False 
        self.master_uri = master_uri
        self.uri = uri
        first = True
        tm = get_topic_manager()
        sm = get_service_manager()
        ns = get_namespace()
        caller_id = get_caller_id()
        if not master_uri or master_uri == uri:
            registered = True
            master = None
        else:
            registered = False
            master = xmlrpcapi(master_uri)
            self.logger.info("Registering with master node %s", master_uri)

        while not registered and not is_shutdown():
            try:
                try:
                    # prevent TopicManager and ServiceManager from accepting registrations until we are done
                    tm.lock.acquire()
                    sm.lock.acquire()                    

                    pub, sub, srv = tm.get_publications(), tm.get_subscriptions(), sm.get_services()
                    for resolved_name, data_type in pub:
                        self.logger.info("Registering publisher topic [%s] type [%s] with master", resolved_name, data_type)
                        code, msg, val = master.registerPublisher(caller_id, resolved_name, data_type, uri)
                        if code != 1:
                            logfatal("cannot register publication topic [%s] with master: %s"%(resolved_name, msg))
                            signal_shutdown("master/node incompatibility with register publisher")
                    for resolved_name, data_type in sub:
                        self.logger.info("registering subscriber topic [%s] type [%s] with master", resolved_name, data_type)
                        code, msg, val = master.registerSubscriber(caller_id, resolved_name, data_type, uri)
                        if code != 1:
                            logfatal("cannot register subscription topic [%s] with master: %s"%(resolved_name, msg))
                            signal_shutdown("master/node incompatibility with register subscriber")                        
                        else:
                            self.publisher_update(resolved_name, val)
                    for resolved_name, service_uri in srv:
                        self.logger.info("registering service [%s] uri [%s] with master", resolved_name, service_uri)
                        code, msg, val = master.registerService(caller_id, resolved_name, service_uri, uri)
                        if code != 1:
                            logfatal("cannot register service [%s] with master: %s"%(resolved_name, msg))
                            signal_shutdown("master/node incompatibility with register service")                        
 
                    registered = True
                    
                    # Subscribe to updates to our state
                    get_registration_listeners().add_listener(self)
                finally:
                    sm.lock.release()                    
                    tm.lock.release()
                
                if pub or sub:
                    logdebug("Registered [%s] with master node %s", caller_id, master_uri)
                else:
                    logdebug("No topics to register with master node %s", master_uri)
                    
            except Exception as e:
                if first:
                    # this use to print to console always, arguable whether or not this should be subjected to same configuration options as logging
                    logerr("Unable to immediately register with master node [%s]: master may not be running yet. Will keep trying."%master_uri)
                    first = False
                time.sleep(0.2)
        self.registered = True
        self.run()