コード例 #1
0
def send_file(connection, address):
    while (True):
        # print(1)
        for filename in glob.glob(r'cache/temp/*.mp4'):
            flag = True
            for fileindex, info in core.FILE_LIST.items():
                if (info['filename'] == filename):
                    flag = False
            if (flag and "finish" in filename):
                value = str({
                    "port": address[1],
                    "host": address[0],
                    "state": -1,
                    'fileindex': core.INDEX
                })
                r.set(name=filename[11:], value=value)
                r1.set(name=filename[11:], value='None')
                infos = {
                    'state': -1,
                    'epoch': 0,
                    'blocks_n': 0,
                    'filename': filename,
                    'times': 0,
                    'drops': my_encode(filename)
                }
                core.FILE_LIST[core.INDEX] = infos
                # core.INDEX += 1
        for fileindex in list(core.FILE_LIST.keys()):
            if (core.FILE_LIST[fileindex]['state'] == -1):  # file info packet
                file_blocks_n, drops_quantity, filesize = get_info(
                    core.FILE_LIST[fileindex]['filename'])
                core.FILE_LIST[fileindex]['blocks_n'] = file_blocks_n
                # send the info of file
                packet = core.Packet(packet_type=1,
                                     fileindex=fileindex,
                                     blocks=file_blocks_n,
                                     drops=drops_quantity,
                                     filesize=filesize)
                packet = core.parseArray(packet)
                connection.sendto(packet.tobytes(), address)
                # time.sleep(1)
                # print(packet)
            elif (core.FILE_LIST[fileindex]['state'] == 0):  # data packet
                symbols = random.sample(
                    core.FILE_LIST[fileindex]['drops'],
                    int(
                        len(core.FILE_LIST[fileindex]['drops']) / 2 *
                        core.REDUNDANCY))
                # for symbol in core.FILE_LIST[fileindex]['drops']:
                for symbol in symbols:
                    # print(symbol.index)
                    symbol = core.parseArray(symbol)
                    symbol[1] = fileindex
                    symbol[7] = core.FILE_LIST[fileindex]['epoch']
                    # print(symbol)
                    # print(sys.getsizeof(symbol.tobytes()))
                    connection.sendto(symbol.tobytes(), address)
                    time.sleep(1 / 2000)
コード例 #2
0
def send_file(connection):
    index = 1
    while(True):
        # address = ('10.28.216.46', 8000)
        address = ('127.0.0.1', 8000)
        for filename in glob.glob(r'cache/temp/*.mp4'):
            flag = True
            for fileindex, info in core.FILE_LIST.items():
                if(info['filename'] == filename):
                    flag = False
            if(flag and "finish" in filename):
                value = str({"port":address[1], "host":address[0], "state": -1, 'fileindex':index})
                r.set(name=filename[11:], value= value)
                r1.set(name=filename[11:], value= 'None')
                infos = {'state':-1, 'epoch':0, 'blocks_n':0, 'filename':filename, 'times':0, 'drops': my_encode(filename)}
                core.FILE_LIST[index] = infos
                index += 1
        for fileindex, info in core.FILE_LIST.items():
            if(info['state'] == -1):    # file info packet
                file_blocks_n,drops_quantity,filesize = get_info(core.FILE_LIST[fileindex]['filename'])
                core.FILE_LIST[fileindex]['blocks_n'] = file_blocks_n
                # send the info of file
                packet = core.Packet(packet_type= 1, fileindex= fileindex, blocks= file_blocks_n, drops= drops_quantity, filesize= filesize)
                packet = core.parseArray(packet)
                connection.sendto(packet.tobytes(),address)
                time.sleep(1)
                # print(packet)
            elif(info['state'] == 0):   # data packet
                if(True):
                # if(core.FILE_LIST[filename]['epoch'] == 0):
                    symbols = random.sample(info['drops'], int(len(info['drops'])))
                    for symbol in symbols:
                        symbol = core.parseArray(symbol)
                        symbol[1] = fileindex
                        symbol[7] = core.FILE_LIST[fileindex]['epoch']
                        # print(symbol)
                        # print(sys.getsizeof(symbol.tobytes()))
                        connection.sendto(symbol.tobytes(),address)
                        time.sleep(1/2000.0)
                        # sys.exit()
                    core.FILE_LIST[fileindex]['epoch'] += 1
                # else:
                #     for symbol in info['drops'][info['blocks_n']:]:
                #         # send the data
                #         connection.sendto(symbol.encode('utf-8'), address)
                #         time.sleep(1/1000.0)
            elif(info['state'] == 1 and info['times'] < 3):   # finish ack
                packet = core.Packet(packet_type= 2, fileindex= fileindex)
                packet = core.parseArray(packet)
                connection.sendto(packet.tobytes(),address)
                core.FILE_LIST[fileindex]['times'] += 1
コード例 #3
0
def send_ack(udp_socket):
    address = ("127.0.0.1", 8001)
    packet = core.Packet(packet_type=1)
    packet = core.parseArray(packet)
    udp_socket.sendto(packet.tobytes(), address)
    while (True):
        for fileindex in list(core.RECEIVE_LIST.keys()):
            if (core.RECEIVE_LIST[fileindex]['state'] == 0):
                packet = core.Packet(packet_type=2, fileindex=fileindex)
                packet = core.parseArray(packet)
                udp_socket.sendto(packet.tobytes(), address)
            elif (core.RECEIVE_LIST[fileindex]['state'] == 1):
                packet = core.Packet(
                    packet_type=3,
                    fileindex=fileindex)  # success recover the file
                packet = core.parseArray(packet)
                udp_socket.sendto(packet.tobytes(), address)
                print(packet)
        time.sleep(2)