Exemplo n.º 1
0
    def receive_messages(self):
        response_to_function = {
            "ls": self.print_response,
            "cd": self.print_response,
            "pwd": self.print_response,
        }

        waiting = True
        while (waiting):
            self.incoming_stream.settimeout(1)

            try:
                data = self.incoming_stream.recv(4096)
                packet = packets.deserialize_packet(data, self.enc_obj)

                if packet._type != 'r':
                    print(
                        "[ERROR] Received following instead of a Response packet:\n\ttype = {}, data = {}"
                        .format(packet._type, packet.data))
                    sys.exit(1)

                response = packet.data.split(' ')
                response_to_function[response[0]](response[1:])
                break

            except socket.timeout:
                pass
Exemplo n.º 2
0
    def receive_metadata(self):
        while (True):
            try:
                self.incoming_stream.settimeout(1)
                data = self.incoming_stream.recv(4096)
                packet = packets.deserialize_packet(data, self.enc_obj)

                if packet._type != 'm':
                    print(
                        "[ERROR] Received following instead of metadata packet:\n\ttype = {}, data = {}"
                        .format(packet._type, packet.data))
                    sys.exit(1)

                return packet.file_uid, packet.file_name, packet.file_type, packet.client_id

            except socket.timeout:
                pass
Exemplo n.º 3
0
    def receive_download(self):
        # Set file metadata
        file_uid, file_name, file_type, client_id = self.receive_metadata()

        if self.client_id != client_id:
            print("[ERROR] Incorrect client_id. Exiting...")
            sys.exit(1)

        self.file_uid = file_uid

        new_file_path = self.directory.get_current_directory(
        ) + '/' + file_name + '.' + file_type
        new_file = files.Files(new_file_path, 'ab')

        seq_num = 0
        while (True):
            try:
                self.incoming_stream.settimeout(1)
                data = self.incoming_stream.recv(4096)
                packet = packets.deserialize_packet(data, self.enc_obj)
                seq_num += 1

                if packet.file_uid != self.file_uid:
                    print(
                        "[ERROR] Packet dumped, incorrect file uid. Exiting..."
                    )
                    sys.exit(1)

                if packet._type == 'e':
                    new_file.close()
                    return

                if packet._type != 'd':
                    print(
                        "[ERROR] Received following instead of data/end packet:\n\ttype = {}, data = {}"
                        .format(packet._type, packet.data))
                    sys.exit(1)
                elif seq_num != packet.seq_num:
                    print("[ERROR] Sequence Number does not match (Client at:",
                          seq_num, ")\nPacket:", packet)
                    sys.exit(1)

                new_file.write_file_by_append(packet.data)

            except socket.timeout:
                pass
Exemplo n.º 4
0
    def receive_ack(self, func):
        while (True):
            self.incoming_stream.settimeout(1)

            try:
                data = self.incoming_stream.recv(4096)
                packet = packets.deserialize_packet(data, self.enc_obj)

                if packet._type != 'r' or packet.data != "{} ACK".format(func):
                    print(
                        "[ERROR] Received following instead of a correct ACK:\n\ttype = {}, data = {}"
                        .format(packet._type, packet.data))
                    return False

                return True

            except socket.timeout:
                pass
    def receive_messages(self):
        print("Receiving Messages.")

        in_session = True
        while (in_session):
            self.incoming_stream.settimeout(1)

            try:
                data = self.incoming_stream.recv(4096)
                enc_obj = self.connection_handlers[0].enc_obj
                packet = deserialize_packet(data, enc_obj)

                # Send packet to connection handler
                self.connection_handlers[0].consume_packet(packet)

            except socket.timeout:
                pass
            except closeServer:
                print("Closing server . . .")
                return