示例#1
0
    def update_remote_file_data(self, file_id):
        my_tracker = UsersManager.get_tracker()
        try:
            sock = connect_socket(my_tracker.ip, my_tracker.port)
            local_ip = get_local_ip(sock.getsockname()[0])
            sock.send("FCHU" + UsersManager.get_my_session_id())
            sock.send(file_id)
            command = read_from_socket(sock, 4)
            if command == "AFCH":
                hitpeer = read_from_socket(sock, 3)
                for i in range(0, int(hitpeer)):
                    peer_ip = read_from_socket(sock, 15)
                    peer_port = read_from_socket(sock, 5)
                    f = FilesManager.find_file_by_id(file_id)

                    if not f:
                        raise Exception("File %s not found in request emitter" % file_id)

                    mask_length = int(math.ceil(f.parts_count / 8))
                    if f.parts_count % 8 != 0:
                        mask_length += 1
                    klog("MASK_LENGTH = %s" % mask_length)
                    partlist = read_from_socket(sock, mask_length)
                    partlist_array = []
                    for b in partlist:
                        byte = bin(int(binascii.b2a_hex(b),16))
                        byte = byte[2:]
                        byte = format_byte(byte)
                        for i in range(7,-1, -1):
                            partlist_array.append(byte[i])
                    for j in range(len(partlist_array)):
                        #klog("%s PARTE %s: %s" %(file_id,j,partlist_array[j]))
                        FilesManager.update_remote_file_part(file_id, Peer(peer_ip, peer_port), j, bool(int(partlist_array[j])))
        except Exception, ex:
            klog("Exception in updating file data to tracker: %s" % str(ex))
示例#2
0
 def download_part(self, peer_ip, peer_port, file_id, file_part):
     FilesManager.set_status_part_for_file(file_id, file_part, "downloading")
     downloadSocket = connect_socket(peer_ip, peer_port)
     downloadSocket.send("RETP"+format_fileid(file_id)+format_partnum(file_part))
     #downloadSocket.send(format_fileid(file_id))
     #downloadSocket.send(format_partnum(file_part))
     # Star a thread that will take care of the download and of the socket management
     f = FilesManager.find_file_by_id(file_id)
     dlThread = DownloadThread(downloadSocket, f.filename, f.id, file_part, peer_ip, self, self.ui_handler)
     dlThread.start()
     self.download_threads.append(dlThread)
示例#3
0
    def run(self):

        command = str(read_from_socket(self._socket, 4))

        if command == "AREP":

            klog("Received AREP")

            chunk_number = int(read_from_socket(self._socket, 6))
            #try:
            klog("Download started")

            klog("chunk number: " + str(chunk_number))
            newFile = open(FilesManager.get_filepart_path_from_file(self._file_id, self._file_part), "wb") # a = append, b = binary mode

            for i in range(0, chunk_number):
                chunk_length = read_from_socket(self._socket, 5)
                chunk_length = int(chunk_length)

                chunk_data = read_from_socket(self._socket, chunk_length)
                newFile.write(chunk_data)

                percent = i* 100/chunk_number
                self._ui_handler.download_file_changed(self._filename, self._file_id, self._file_part, self._peer_ip, percent)

            newFile.close()
            self._ui_handler.download_file_changed(self._filename, self._file_id, self._file_part, self._peer_ip, 100)
            klog("Download completed")

            f = FilesManager.find_file_by_id(self._file_id)
            self._request_emitter.part_download_finished(self._file_id, self._file_part)

            self._request_emitter.register_part_to_tracker(f, self._file_part)

#            except Exception, ex:
 #               klog("An exception has occurred: "+str(ex))

        else:
            FilesManager.set_status_part_for_file(self._file_id, self._file_part, "empty")

        self._socket.close()
示例#4
0
 def download_file(self, file_id):
     f = FilesManager.find_file_by_id(file_id)
     queue = DownloadQueue(f, self, self.ui_handler)
     self.download_queues[str(file_id)] = queue
示例#5
0
    def run(self):

        try:
            self._socket.setblocking(1) # <-------- ??

            command = str(self._socket.recv(4))

            #
            # PEERS
            #

            # Received package asking for a file
            if command == "RETP":
                klog("RETP received")
                CHUNK_DIM = 128

                file_id = self._socket.recv(16)
                part_num = int(self._socket.recv(8))

                self._socket.send("AREP")   #sending the ack command
                remote_ip = self._socket.getpeername()[0]
                my_session_id = UsersManager.get_my_session_id()

                # Get the file matching the file_id
                klog("finding file with id: %s, session_id %s, part %s" %(file_id, my_session_id, part_num))

                file = FilesManager.find_file_by_id(file_id)

                if file:
                    klog("i have found the file: %s stored in %s" % (file.filename, file.filepath))

                    # Chunks
                    size = file.get_part_size(part_num)#file_size(file.filepath)
                    bytes_sent = 0
                    chunks_num = int(size // CHUNK_DIM)
                    leftover = size % CHUNK_DIM
                    if leftover != 0.0:
                        chunks_num += 1

                    self._socket.send(format_chunks_number(chunks_num)) #sending the chunks number

                    part = file.get_part(part_num)
                    part_size = file.get_part_size(part_num)

                    index = 0
                    chunk = part[0:CHUNK_DIM]

                    while True:
                        self._socket.send(format_chunk_length(len(chunk)))  #sending the chunk length
                        bytes_sent += self._socket.send(chunk)    #sending the chunk

                        percent = bytes_sent*100/size
                        self.ui_handler.upload_file_changed(file.filename, file.id, part_num, remote_ip, percent)

                        index += 1
                        if ((index * CHUNK_DIM) <= part_size) and ((index + 1)* CHUNK_DIM <= part_size):
                            chunk = part[index * CHUNK_DIM : (index + 1)* CHUNK_DIM]
                        elif ((index * CHUNK_DIM) <= part_size) and ((index + 1)* CHUNK_DIM > part_size):
                            chunk = part[index * CHUNK_DIM : ]
                        else:
                            break

                    '''
                    #open the file
                    file2send = file.get_part(part_num)#open(file.filepath, 'rb')
                    chunk = file2send.read(CHUNK_DIM)

                    while chunk != '':
                        self._socket.send(format_chunk_length(len(chunk)))  #sending the chunk length
                        bytes_sent += self._socket.send(chunk)    #sending the chunk

                        percent = bytes_sent*100/size
                        self.ui_handler.upload_file_changed(file.filename, file.id, part_num, remote_ip, percent)

                        chunk = file2send.read(CHUNK_DIM)
                    file2send.close()
                    '''
                    klog("upload completed: %s" %file.filename)
                    self.ui_handler.upload_file_changed(file.filename, file.id, part_num, remote_ip, 100)

                else:
                    klog("I do not have this file!")

            else:
                klog("ERROR: received a %s command that service_thread does not have to handle:" %command)
            # Close the socket
            self._socket.close()

        except Exception, ex:
            print ex