def __init__(self, test_num=0, randomize=False):
     self.__test_num = test_num
     self.__randomize = randomize
     PresenceService.__init__(self)
Beispiel #2
0
    def run(self):
        """
        Main thread function.
        In this thread the socket connection to the daemon is monitored. If service
        announcements arrive they are forwarded to their respective callback functions, 
        and if ACKs of ERRORs are received they are placed in the command queue.
        """
        logger = logging.getLogger("presence")
        logger.info("Monitoring thread started.")

        with self.__shutdown_signal:
            try:
                while not self.__shutdown:
                    (rd, _, _) = select.select([self.__sock], [], [self.__sock], Presence.TIMEOUT)
                    if rd != []:
                        # Data has arrived from the daemon.
                        message = self.__sock.recv(4096)

                        length = UInt16()
                        idx = 0
                        packets = []
                        while idx < len(message):
                            try:
                                idx += length.unpack(message[idx:])
                                if len(message) < idx + length.value:
                                    raise ValidationError("Insufficient amount of data received")
                            except (UnpackingError, ValidationError), e:
                                self.__handle_monitor_error(False, e)
                                break
                            packets.append(message[idx : idx + length.value])
                            idx += length.value

                        for packet in packets:
                            # Find out which kind of packet we are dealing with here.
                            idx = 0
                            (packet_type,) = struct.unpack("!B", packet[0])
                            idx += 1

                            if packet_type == PresenceDefines.ANNOUNCEMENT:
                                # print "announcement received"
                                peer = PresencePeer()
                                service = PresenceService()
                                try:
                                    idx += peer.unpack(packet[idx:])
                                    idx += service.unpack(packet[idx:])
                                except (UnpackingError, ValidationError), e:
                                    self.__handle_monitor_error(False, e)
                                    continue

                                with self.__lock:
                                    try:
                                        # Check for "promiscuous mode" listeners.
                                        if self.__callbacks.has_key("*"):
                                            callback_functions = self.__callbacks["*"]
                                            for callback_function in callback_functions:
                                                callback_function(
                                                    peer._name.name, socket.inet_ntoa(peer.address_str), service
                                                )

                                        # Check for named listeners.
                                        if self.__callbacks.has_key(service.name):
                                            callback_functions = self.__callbacks[service.name]
                                            for callback_function in callback_functions:
                                                callback_function(
                                                    peer._name.name, socket.inet_ntoa(peer.address_str), service
                                                )
                                    except Exception, e:
                                        self.__handle_monitor_error(False, e)

                            elif packet_type == PresenceDefines.ACK:
                                self.__command_queue.put((packet_type, ""))

                            elif packet_type == PresenceDefines.ERROR:
                                try:
                                    (error_number,) = struct.unpack("!B", packet[idx : idx + 1])
                                    idx += 1
                                    error_message = MaxLengthCString(PresenceDefines.MAX_COMMAND_SIZE - 4)
                                    idx += error_message.unpack(packet[idx:])
                                    self.__command_queue.put((packet_type, (error_number, error_message)))
                                except Exception, e:
                                    self.__handle_monitor_error(False, e)
                                    break