Exemplo n.º 1
0
    def run(self):
        """
        brief info for: thread run method
        """
        #print "starting broadcast_manager!"
        self._logger.debug("starting broadcast_manager!")

        if get_node_handler() is not None:
            self._name = get_node_handler().name
            
            while get_node_handler().uri is None:
                time.sleep(0.0001) 
            self._uri = get_node_handler().uri

        self._logger.debug("self.name:%s" % self._name)
        self._logger.debug("self.uri:%s" % self._uri)

        self._register_node()
        #self.requestParam()  

        while True:
            try:
                msg = self._participant.read_msg()
                if msg is None:
                    continue
                if(len(msg) > 0):
                    data = self._unpack_msg(msg.strip())
                        
                    if self._name == data[NODE_NAME] \
                            and data[REQUEST_TYPE] == "registerNode" \
                            and self._uri != data[XMLRPC_URI] \
                            and int(data[HEADER_OFFSET]) > int(self._node_time):
                        signal_shutdown("it has already same name node, exit it now.")
                        continue

                    self._logger.debug("recv data: %s " % data)
                    #print("recv data: %s " % data)

                    if data[REQUEST_TYPE] in self.callback:
                        self._registerNodeCallback(data)
                        cb = '_' + data[REQUEST_TYPE] + "Callback"
                        func = getattr(self, cb)
                        func(data)
                        pass
                    else:
                        self._logger.error("[broadcast_manager] invalid request type: %s" % data[REQUEST_TYPE])
                else:
                    time.sleep(0.005)

            except Exception as e:
                self._logger.error("broadcast_manager thread error is  %s" % e)
            finally:
                pass
Exemplo n.º 2
0
    def run(self):
        """
        brief info for: thread run method
        """
        self._logger.debug("starting broadcast_manager!")
        self._register_node()

        while True:
            try:
                msg = self._participant.read_msg()
                if msg is None:
                    continue
                if(len(msg) > 0):
                    data = self._unpack_msg(msg.strip())
                        
                    if self._name == data[NODE_NAME]:
                        if data[REQUEST_TYPE] == "registerNode" \
                                and self._uri != data[XMLRPC_URI] \
                                and int(data[NODE_TIME]) > int(self._node_time):
                            signal_shutdown("it has already same name node, exit it now.")
                        continue

                    self._logger.debug("recv data: %s " % data)

                    cb = '_' + data[REQUEST_TYPE] + "Callback"
                    try:
                        func = getattr(self, cb)
                        func(data)
                    except AttributeError:
                        pass
                else:
                    time.sleep(0.005)

            except Exception as e:
                self._logger.error("[broadcast_manager] Unexpected error:  %s" % e)
            finally:
                pass
    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()