示例#1
0
def recv_ack(udp_socket):
    while (True):
        recv, address = udp_socket.recvfrom(4096)
        recv = np.frombuffer(recv, dtype=core.NUMPY_TYPE)
        packet = core.parsePacket(recv)
        print(packet.packet_type)
        # print(time.time())
        # print(packet.packet_type)
        if (packet.packet_type == 1):  # hello packet
            core.CLIENT.append(address)
            print(address)
        elif (packet.packet_type == 2):  # ack of file info
            core.FILE_LIST[packet.fileindex]['state'] = 0
            value = str({
                "port": address[1],
                "host": address[0],
                "state": 0,
                'fileindex': packet.fileindex
            })
            r.set(name=core.FILE_LIST[packet.fileindex]['filename'][11:],
                  value=value)
        elif (packet.packet_type == 3):  # ack of file recovered
            core.FILE_LIST[packet.fileindex]['state'] = 1
            value = str({
                "port": address[1],
                "host": address[0],
                "state": 1,
                'fileindex': packet.fileindex
            })
            r.set(name=core.FILE_LIST[packet.fileindex]['filename'][11:],
                  value=value)
示例#2
0
def recv_file(connection):
    epoch = 0
    while (True):
        # receive the packet info
        recv, addr = connection.recvfrom(8500)
        # print("{}-{}".format(type(recv),sys.getsizeof(recv)))
        recv = np.frombuffer(recv, dtype=core.NUMPY_TYPE)
        # print(recv)
        packet = core.parsePacket(recv)
        # if(packet.index == 1):
        #     print(packet.data)
        if (packet.packet_type == 1
                and packet.fileindex not in core.RECEIVE_LIST.keys()):
            filename, file_blocks_n, drops, filesize = packet.fileindex, packet.blocks, packet.drops, packet.filesize
            info = {
                'state': 0,
                'blocks': [],
                'need': file_blocks_n,
                'recovered_blocks': [],
                'recovered_n': 0,
                'filesize': filesize,
                'drops': drops,
                'indexs': set(),
                'last_n': file_blocks_n,
                'start_time': time.time(),
                'end_time': '',
                'decode_times': 0
            }
            core.RECEIVE_LIST[filename] = info
        elif (packet.packet_type == 0):
            if core.RECEIVE_LIST[packet.fileindex]['state'] == 0:
                if packet.epoch == 0:  # recv first epoch
                    symbol = core.Symbol(index=packet.index,
                                         degree=packet.degree,
                                         data=packet.data,
                                         filename=packet.fileindex)
                    core.RECEIVE_LIST[packet.fileindex]['blocks'].append(
                        symbol)
                    core.RECEIVE_LIST[packet.fileindex]['indexs'].add(
                        packet.index)
                elif packet.epoch != 0 and packet.index not in core.RECEIVE_LIST[
                        packet.fileindex]['indexs']:  # recv other epoch
                    symbol = core.Symbol(index=packet.index,
                                         degree=packet.degree,
                                         data=packet.data,
                                         filename=packet.fileindex)
                    core.RECEIVE_LIST[packet.fileindex]['blocks'].append(
                        symbol)
                    core.RECEIVE_LIST[packet.fileindex]['indexs'].add(
                        packet.index)
        elif (packet.packet_type == 2
              and packet.fileindex in core.RECEIVE_LIST.keys()):
            value = core.RECEIVE_LIST.pop(packet.fileindex)