Esempio n. 1
0
    def _registerSubscriberCallback(self, data):
        name = data[NODE_NAME]
        topic = data[TOPIC_NAME]
        datatype = data[TOPIC_TYPE]
        uri = data[XMLRPC_URI]

        url_data = [uri]

        tmp = [topic, [name]]
        if tmp not in self._stat_info[1]:
            self._stat_info[1].append(tmp) 

        tm = get_topic_manager()
        try:
            tm.lock.acquire()
            if tm.has_publication(topic):
                self._logger.debug("I has pub topic :%s" % topic)
                self.registerPublisher(
                    get_node_handler().name, topic, datatype, get_node_handler().uri)
        except Exception as e:
            self._logger.error(
                "_registerSubscriberCallback error is  %s" % e)
            self._logger.error(traceback.format_exc())
        finally:
            tm.lock.release()
Esempio n. 2
0
    def _registerPublisherCallback(self, data):
        name = data[NODE_NAME]
        topic = data[TOPIC_NAME]
        topic_type = data[TOPIC_TYPE]
        topic_uri = data[XMLRPC_URI]
        url_data = [topic_uri]

        tmp = [topic, [name]]
        if tmp not in self._stat_info[0]:
            self._stat_info[0].append(tmp)

        topic_info = [x for x in self._topic_info if x[0] == topic]
        if topic_info:
            topic_info[0][1] = topic_type
        else:
            self._topic_info.append([topic, topic_type])

        tm = get_topic_manager()
        try:
            tm.lock.acquire()
            if tm.has_subscription(topic):
                self._logger.debug("I has sub topic : %s" % topic)
                get_node_handler().publisherUpdate(name, topic, url_data)
        except Exception as e:
            self._logger.error(
                "registerPublisherCallback error is  %s" % e)
            self._logger.error(traceback.format_exc())
        finally:
            tm.lock.release()
Esempio n. 3
0
    def _unregisterPublisherCallback(self, data):
        name = data[NODE_NAME]
        topic = data[TOPIC_NAME]
        uri = data[XMLRPC_URI]
        uri_list = [uri]

        tm = get_topic_manager()
        try:
            tm.lock.acquire()
            if tm.has_subscription(topic):
                self._logger.debug("I has sub topic, recv unregisSub: %s" % topic)
                get_node_handler().reg_man.reg_removed(topic, uri_list, Registration.SUB)
        except Exception as e:
            self._logger.error(
                "unregisterPublisherCallback error is  %s" % e)
            self._logger.error(traceback.format_exc())
        finally:
            tm.lock.release()

        try:
            self._pubs[topic].remove(uri)
        except KeyError:
            pass

        try:
            self._pub_history[topic].remove(self._encode(data))
        except KeyError:
            pass
Esempio n. 4
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
Esempio n. 5
0
    def __init__(self, name=None, callback=None):
        """
        brief info for: __init__
        """
        super(BroadcastManager, self).__init__()

        self._logger = logging.getLogger(__name__)
        self._logger.setLevel(logging.INFO)

        if callback is None:
            self.callback = ["registerPublisher",
                             "unregisterPublisher",
                             "registerSubscriber",
                             "unregisterSubscriber",
                             "registerService",
                             "unregisterService",
                             "lookupService",
                             "getTopicTypes",
                             "lookupNode",
                             "registerNode",
                             ]
        else:
            self.callback = callback

        self._pubs = {}
        self._subs = {}
        self._service_cache = {}
        self._node_info = []
        self._topic_info = []
        self._pub_history = {}

        self._node_time = str(int(round(time.time()*1000)))
        if get_node_handler() is not None:
            self._name = get_node_handler().name
            while get_node_handler().uri is None:
                time.sleep(0.01) 
            self._uri = get_node_handler().uri
        else:
            if name is None:
                self._name = "_null_name"
            else:
                self._name = name
            self._uri = "_null_uri"
      
        self._participant = participant.Participant(self._name)
        self._participant.init_py()

        self._broardcast_manager_thread = threading.Thread(
            target=self.run, args=())
        self._broardcast_manager_thread.setDaemon(True)
        self._broardcast_manager_thread.start()
Esempio n. 6
0
 def updateHandler(self):
     self._name = get_node_handler().name
     while get_node_handler().uri is None:
         time.sleep(0.01) 
     self._uri = get_node_handler().uri