def _run_thread_monitor(self): """ The monitor thread receive all messages on the connection and check them to see if the target is the application. If it is, call all listeners This method is not called by childs, so no need to protect it. """ while not self.p.should_stop(): try: readable, writeable, errored = select.select([self._UDPSock], [], [], READ_NETWORK_TIMEOUT) except: self.p.log.info("Error during the read of the socket : %s" % traceback.format_exc()) else: if len(readable) == 1: try: data, addr = self._UDPSock.recvfrom(self._buff) except: self.p.log.debug("bad data received") else: try: mess = XplMessage(data) if ( (not self._foundhub.is_set()) and (mess.source == self._source) and (mess.schema == "hbeat.app") ): self.foundhub() elif (mess.target == "*" or (mess.target == self._source)) and ( self._source != mess.source ): update = False if mess.schema == "fragment.basic": key = (mess.source, mess.data["partid"].split(":")[1]) if not key in self._received_fragments_buffer: self._received_fragments_buffer[key] = {} self._received_fragments_buffer[key][mess.data["partid"].split("/")[0]] = mess if len(self._received_fragments_buffer[key]) == int( mess.data["partid"].split("/")[1].split(":")[0] ): nf = FragmentedXplMessage() for f in self._received_fragments_buffer[key].keys(): nf.add_fragment(self._received_fragments_buffer[key][f]) mess = nf.build_message() update = True del self._received_fragments_buffer[key] else: update = True if update: for l in self._listeners: l.new_message(mess) # Enabling this debug will really polute your logs # self.p.log.debug("New message received : %s" % \ # mess.type) except XPLException: self.p.log.warning("XPL Exception occured in : %s" % sys.exc_info()[2]) except XplMessageError as exc: self.p.log.warning("Malformated message received, ignoring it.") self.p.log.warning("Error was : %s" % exc) self.p.log.warning("Message was : %s" % mess) self.p.log.info("self._should_stop set, leave.")
def send(self, message): """ This function allows you to send an xPL message on the Bus Be carreful, there is no check on message correctness This method is protected by semaphore because in some plugin (REST for example) many threads can call it. """ self._lock_send.acquire() try: if not message.hop_count: message.set_hop_count("5") if not message.source: message.set_source(self._source) if not message.target: message.set_target("*") try: if len(message.to_packet()) > 1472: fragments = FragmentedXplMessage.fragment_message( message, self._fragment_uid) self._sent_fragments_buffer[self._fragment_uid] = fragments for fragment in fragments.keys(): self.p.log.debug("fragment send") self._UDPSock.sendto(fragments[fragment].__str__(), (self._broadcast, 3865)) else: self.p.log.debug("normal send") self._UDPSock.sendto(message.__str__(), (self._broadcast, 3865)) except: if self.p.get_stop().is_set(): pass else: raise else: self._fragment_uid = self._fragment_uid + 1 # TODO : reactivate # commented by fritz in 0.4 for dev purpose #self.p.log.debug("xPL Message sent by thread %s : %s" % (threading.currentThread().getName(), message)) except: self.p.log.warning("Error during send of message") self.p.log.debug(traceback.format_exc()) self._lock_send.release()
def send(self, message): """ This function allows you to send an xPL message on the Bus Be carreful, there is no check on message correctness This method is protected by semaphore because in some plugin (REST for example) many threads can call it. """ self._lock_send.acquire() try: if not message.hop_count: message.set_hop_count("5") if not message.source: message.set_source(self._source) if not message.target: message.set_target("*") try: if len(message.to_packet()) > 1472: fragments = FragmentedXplMessage.fragment_message(message, self._fragment_uid) self._sent_fragments_buffer[self._fragment_uid] = fragments for fragment in fragments.keys(): self.p.log.debug("fragment send") self._UDPSock.sendto(fragments[fragment].__str__(), (self._broadcast, 3865)) else: self.p.log.debug("normal send") self._UDPSock.sendto(message.__str__(), (self._broadcast, 3865)) except: if self.p.get_stop().is_set(): pass else: raise else: self._fragment_uid = self._fragment_uid + 1 # TODO : reactivate # commented by fritz in 0.4 for dev purpose #self.p.log.debug("xPL Message sent by thread %s : %s" % (threading.currentThread().getName(), message)) except: self.p.log.warning("Error during send of message") self.p.log.debug(traceback.format_exc()) self._lock_send.release()
def _run_thread_monitor(self): """ The monitor thread receive all messages on the connection and check them to see if the target is the application. If it is, call all listeners This method is not called by childs, so no need to protect it. """ while not self.p.should_stop(): try: readable, writeable, errored = select.select( [self._UDPSock], [], [], READ_NETWORK_TIMEOUT) except: self.p.log.info("Error during the read of the socket : %s" % traceback.format_exc()) else: if len(readable) == 1: try: data, addr = self._UDPSock.recvfrom(self._buff) except: self.p.log.debug("bad data received") else: try: mess = XplMessage(data) if (not self._foundhub.is_set()) and (mess.source == self._source)\ and (mess.schema == "hbeat.app"): self.foundhub() elif (mess.target == "*" or (mess.target == self._source)) and\ (self._source != mess.source): update = False if mess.schema == "fragment.basic": key = (mess.source, mess.data["partid"].split(':')[1]) if not key in self._received_fragments_buffer: self._received_fragments_buffer[ key] = {} self._received_fragments_buffer[key][ mess.data["partid"].split( '/')[0]] = mess if len(self._received_fragments_buffer[key] ) == int(mess.data["partid"].split( '/')[1].split(':')[0]): nf = FragmentedXplMessage() for f in self._received_fragments_buffer[ key].keys(): nf.add_fragment( self. _received_fragments_buffer[key] [f]) mess = nf.build_message() update = True del self._received_fragments_buffer[ key] else: update = True if update: for l in self._listeners: l.new_message(mess) #Enabling this debug will really polute your logs #self.p.log.debug("New message received : %s" % \ # mess.type) except XPLException: self.p.log.warning( "XPL Exception occured in : %s" % sys.exc_info()[2]) except XplMessageError as exc: self.p.log.warning( "Malformated message received, ignoring it.") self.p.log.warning("Error was : %s" % exc) self.p.log.warning("Message was : %s" % mess) self.p.log.info("self._should_stop set, leave.")