Exemplo n.º 1
0
def main():
    """
    Sliding window example.

    This example shows how to use sliding window encoder and decoder
    stacks. The sliding window is special in that it does not require
    that all symbols are available at the encoder before encoding can
    start. In addition it uses feedback between the decoder and encoder
    such that symbols that have already been received at the decoder
    are not included in the encoding again (saving computations).
    """
    # Set the number of symbols (i.e. the generation size in RLNC
    # terminology) and the size of a symbol in bytes
    symbols = 42
    symbol_size = 160
    # In the following we will make an encoder/decoder factory.
    # The factories are used to build actual encoders/decoders
    encoder_factory = kodo.SlidingWindowEncoderFactoryBinary(
        max_symbols=symbols, max_symbol_size=symbol_size)

    encoder = encoder_factory.build()

    decoder_factory = kodo.SlidingWindowDecoderFactoryBinary(
        max_symbols=symbols, max_symbol_size=symbol_size)

    decoder = decoder_factory.build()

    # Create some data to encode. In this case we make a buffer
    # with the same size as the encoder's block size (the max.
    # amount a single encoder can encode)
    # Just for fun - fill the input data with random data
    data_in = os.urandom(encoder.block_size())

    # Lets split the data into symbols and feed the encoder one symbol at a
    # time
    symbol_storage = [
        bytes(data_in[i:i + symbol_size])
        for i in range(0, len(data_in), symbol_size)
    ]

    while not decoder.is_complete():

        # Encode a packet into the payload buffer
        packet = encoder.encode()

        # Send the data to the decoders, here we just for fun
        # simulate that we are loosing 50% of the packets
        if random.choice([True, False]):
            continue

        # Packet got through - pass that packet to the decoder
        decoder.decode(packet)

        # Randomly choose to insert a symbol
        if random.choice([True, False]) and encoder.rank() < symbols:
            # For an encoder the rank specifies the number of symbols
            # it has available for encoding
            rank = encoder.rank()
            encoder.set_symbol(rank, symbol_storage[rank])

        # Transmit the feedback
        feedback = decoder.write_feedback()

        # Simulate loss of feedback
        if random.choice([True, False]):
            continue

        encoder.read_feedback(feedback)

    # The decoder is complete, now copy the symbols from the decoder
    data_out = decoder.copy_symbols()

    # Check we properly decoded the data
    if data_out == data_in:
        print("Data decoded correctly")
    else:
        print("Unexpected failure to decode please file a bug report :)")
        sys.exit(1)
Exemplo n.º 2
0
    if len(sys.argv) > 2:
        encoder_decoder = sys.argv[1]
        SYMBOL_SIZE = int(sys.argv[2])

    if coding_mode == "encode":
        if encoder_decoder == "FullVector":
            fw_fac = kodo.FullVectorEncoderFactoryBinary(GEN_SIZE, SYMBOL_SIZE)
        elif encoder_decoder == "SlidingWindow":
            fw_fac = kodo.SlidingWindowEncoderFactoryBinary(
                GEN_SIZE, SYMBOL_SIZE)
        redundancy = 10
    elif coding_mode in ("decode", "recode"):
        if encoder_decoder == "FullVector":
            fw_fac = kodo.FullVectorDecoderFactoryBinary(GEN_SIZE, SYMBOL_SIZE)
        elif encoder_decoder == "SlidingWindow":
            fw_fac = kodo.SlidingWindowDecoderFactoryBinary(
                GEN_SIZE, SYMBOL_SIZE)
    else:
        fw_fac = None

    encoder_info = convert_encoder(fw_fac)

    JSONL_FILE_PATH = "pd_{}_{}_{}.jsonl".format(encoder_decoder, SYMBOL_SIZE,
                                                 GEN_SIZE)

    # Bind sockets and start forwards and backwards processes
    recv_sock, send_sock = bind_raw_sock_pair(ingress_iface, egress_iface)
    fw_proc = multiprocessing.Process(target=forwards_forward,
                                      args=(recv_sock, send_sock, fw_fac))

    fw_proc.start()