Esempio n. 1
0
def main():

    # Get directory of this file
    directory = os.path.dirname(os.path.realpath(__file__))

    # The name of the file to use for the test
    filename = 'lena.jpg'

    # Open the image convert it to RGB and get the height and width
    image = Image.open(os.path.join(directory, filename)).convert("RGB")
    image_width = image.size[0]
    image_height = image.size[1]

    # The canvas should be able to contain both the image and the decoding
    # state. Note the decoding state is the same width as the image height.
    canvas_width = image_width + image_height

    # Create the canvas
    canvas = kodo_helpers.CanvasScreenEngine(width=canvas_width,
                                             height=image_height)

    # Create the image viewer
    image_viewer = kodo_helpers.ImageViewer(width=image_width,
                                            height=image_height,
                                            canvas=canvas)

    # Create the decoding coefficient viewer
    state_viewer = kodo_helpers.DecodeStateViewer(size=image_height,
                                                  canvas=canvas,
                                                  canvas_position=(image_width,
                                                                   0))

    # Pick a symbol size (image_width * 3 will create a packet for each
    # horizontal line of the image)
    symbol_size = image_width * 3

    # Based on the size of the image and the symbol size, calculate the number
    # of symbols needed for containing the image in a single generation.
    symbols = int(math.ceil(image_width * image_height * 3.0 / symbol_size))

    # Create encoder factory and encoder
    encoder_factory = kodo.FullVectorEncoderFactoryBinary8(
        max_symbols=symbols, max_symbol_size=symbol_size)
    encoder = encoder_factory.build()

    # Create decoder factory and decoder
    decoder_factory = kodo.FullVectorDecoderFactoryBinary8Trace(
        max_symbols=symbols, max_symbol_size=symbol_size)
    decoder = decoder_factory.build()

    # Connect the tracing callback to the decode state viewer
    def callback(zone, msg):
        state_viewer.trace_callback(zone, msg)

    decoder.trace(callback)

    # Create a byte array from the image to use in the encoding
    data_in = image.tobytes()

    # Set the converted image data
    encoder.set_symbols(data_in)

    # Create an image viwer and run the following code in a try catch;
    # this prevents the program from locking up, as the finally clause will
    # close down the image viewer.
    canvas.start()
    try:
        packets = 0
        while not decoder.is_complete():
            packet = encoder.encode()
            packets += 1

            # Drop some packets
            if random.choice([True, False]):
                decoder.decode(packet)

            image_viewer.set_image(decoder.copy_symbols())

        # Let the user see the photo before closing the application
        time.sleep(1)
    finally:
        canvas.stop()

    # 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[:len(data_in)] == data_in:
        print("Data decoded correctly")
    else:
        print("Unexpected failure to decode please file a bug report :)")
        sys.exit(1)
def main():
    """
    Encode recode decode example.

    In Network Coding applications one of the key features is the
    ability of intermediate nodes in the network to recode packets
    as they traverse them. In Kodo it is possible to recode packets
    in decoders which provide the recode() function.

    This example shows how to use one encoder and two decoders to
    simulate a simple relay network as shown below (for simplicity
    we have error free links, i.e. no data packets are lost when being
    sent from encoder to decoder1 and decoder1 to decoder2):

            +-----------+     +-----------+     +-----------+
            |  encoder  |+---.| decoder1  |+---.|  decoder2 |
            +-----------+     | (recoder) |     +-----------+
                              +-----------+
    In a practical application recoding can be using in several different
    ways and one must consider several different factors e.g. such as
    reducing linear dependency by coordinating several recoding nodes
    in the network.
    Suggestions for dealing with such issues can be found in current
    research literature (e.g. MORE: A Network Coding Approach to
    Opportunistic Routing).
    """
    # 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.FullVectorEncoderFactoryBinary8(
        max_symbols=symbols,
        max_symbol_size=symbol_size)

    encoder = encoder_factory.build()

    decoder_factory = kodo.FullVectorDecoderFactoryBinary8(
        max_symbols=symbols,
        max_symbol_size=symbol_size)
    
    mean = 0.0
    for x in range(0,1000):
        decoder1 = decoder_factory.build()
        decoder2 = 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())

        # Assign the data buffer to the encoder so that we may start
        # to produce encoded symbols from it
        encoder.set_const_symbols(data_in)
        packet_count=0
        while not decoder2.is_complete():

            # Encode a packet into the payload buffer
            packet = encoder.write_payload()
            packet_count = packet_count +1
        
            if(random.uniform(0.0,1.0)>0.1):
                # Pass that packet to decoder1
                decoder1.read_payload(packet)

                # Now produce a new recoded packet from the current
                # decoding buffer, and place it into the payload buffer
        
                packet = decoder1.write_payload()

            if(random.uniform(0.0,1.0)>0.2):
                # Pass the recoded packet to decoder2
                decoder2.read_payload(packet)

        mean = mean + packet_count

    # Both decoder1 and decoder2 should now be complete,
    # copy the symbols from the decoders

    data_out1 = decoder1.copy_from_symbols()
    data_out2 = decoder2.copy_from_symbols()

    # Check we properly decoded the data
    if data_out1 == data_in and data_out2 == data_in:
        print("Data decoded correctly")
        print(packet_count)
        mean = mean/1000.0
        print("mean is :")
        print(mean)
        
    else:
        print("Unexpected failure to decode please file a bug report :)")
        sys.exit(1)
def main():
    """An example of how to use the trace functionality."""
    # Set the number of symbols (i.e. the generation size in RLNC
    # terminology) and the size of a symbol in bytes
    symbols = 8
    symbol_size = 16

    # In the following we will make an encoder/decoder factory.
    # The factories are used to build actual encoders/decoders
    encoder_factory = kodo.FullVectorEncoderFactoryBinary8(
        max_symbols=symbols,
        max_symbol_size=symbol_size)
    encoder = encoder_factory.build()

    decoder_factory = kodo.FullVectorDecoderFactoryBinary8(
        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())

    # Setup tracing

    # Enable the stdout trace function of the encoder
    encoder.set_trace_stdout()

    # Define a custom trace function for the decoder which filters the
    # trace message based on their zones
    def callback_function(zone, message):
        if zone in ["decoder_state", "input_symbol_coefficients"]:
            print("{}:".format(zone))
            print(message)

    decoder.set_trace_callback(callback_function)

    # Assign the data buffer to the encoder so that we may start
    # to produce encoded symbols from it
    encoder.set_const_symbols(data_in)
    encoder.set_systematic_off()
    while not decoder.is_complete():

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

        # Here we "simulate" a packet loss of approximately 50%
        # by dropping half of the encoded packets.
        # When running this example you will notice that the initial
        # symbols are received systematically (i.e. uncoded). After
        # sending all symbols once uncoded, the encoder will switch
        # to full coding, in which case you will see the full encoding
        # vectors being sent and received.
        if random.choice([True, False]):
            continue

        # Pass that packet to the decoder
        decoder.read_payload(packet)

    # The decoder is complete, now copy the symbols from the decoder
    data_out = decoder.copy_from_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)
Esempio n. 4
0
def main():

    # Setup canvas and viewer
    size = 512
    canvas = kodo_helpers.CanvasScreenEngine(size * 2, size)

    encoder_viewer = kodo_helpers.EncodeStateViewer(size=size, canvas=canvas)

    decoder_viewer = kodo_helpers.DecodeStateViewer(size=size,
                                                    canvas=canvas,
                                                    canvas_position=(size, 0))

    canvas.start()
    try:
        # Set the number of symbols (i.e. the generation size in RLNC
        # terminology) and the size of a symbol in bytes
        symbols = 64
        symbol_size = 16

        # In the following we will make an encoder/decoder factory.
        # The factories are used to build actual encoders/decoders
        encoder_factory = kodo.FullVectorEncoderFactoryBinary8(
            max_symbols=symbols, max_symbol_size=symbol_size)
        encoder = encoder_factory.build()

        decoder_factory = kodo.FullVectorDecoderFactoryBinary8(
            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())

        def decoder_callback(zone, msg):
            decoder_viewer.trace_callback(zone, msg)

        decoder.set_trace_callback(decoder_callback)

        def encoder_callback(zone, msg):
            encoder_viewer.trace_callback(zone, msg)

        encoder.set_trace_callback(encoder_callback)

        # Assign the data buffer to the encoder so that we may start
        # to produce encoded symbols from it
        encoder.set_const_symbols(data_in)
        while not decoder.is_complete():
            # Encode a packet into the payload buffer
            packet = encoder.write_payload()

            # Here we "simulate" a packet loss of approximately 50%
            # by dropping half of the encoded packets.
            # When running this example you will notice that the initial
            # symbols are received systematically (i.e. uncoded). After
            # sending all symbols once uncoded, the encoder will switch
            # to full coding, in which case you will see the full encoding
            # vectors being sent and received.
            if random.choice([True, False]):
                continue

            # Pass that packet to the decoder
            decoder.read_payload(packet)

        time.sleep(1)
    finally:
        # What ever happens, make sure we stop the viewer.
        canvas.stop()

    # The decoder is complete, now copy the symbols from the decoder
    data_out = decoder.copy_from_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)